Updates an existing smart collection
One script reply has been approved by the moderators Verified

Updates an existing smart collection

Created by hugo697 883 days ago
Submitted by hugo697 Typescript (fetch-only)
Verified 337 days ago
1
type Shopify = {
2
  token: string;
3
  store_name: string;
4
};
5
/**
6
 * Updates an existing smart collection
7
 * Updates an existing smart collection
8
 */
9
export async function main(
10
  auth: Shopify,
11
  api_version: string = "2023-10",
12
  smart_collection_id: string,
13
  body: {
14
    smart_collection?: {
15
      id?: number;
16
      image?: { alt?: string; attachment?: string; [k: string]: unknown };
17
      [k: string]: unknown;
18
    };
19
    [k: string]: unknown;
20
  }
21
) {
22
  const url = new URL(
23
    `https://${auth.store_name}.myshopify.com/admin/api/${api_version}/smart_collections/${smart_collection_id}.json`
24
  );
25

26
  const response = await fetch(url, {
27
    method: "PUT",
28
    headers: {
29
      "Content-Type": "application/json",
30
      "X-Shopify-Access-Token": auth.token,
31
    },
32
    body: JSON.stringify(body),
33
  });
34
  if (!response.ok) {
35
    const text = await response.text();
36
    throw new Error(`${response.status} ${text}`);
37
  }
38
  return await response.json();
39
}
40