Updates an article
One script reply has been approved by the moderators Verified

Updates an article

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 article
7
 * Updates an article
8
 */
9
export async function main(
10
  auth: Shopify,
11
  api_version: string = "2023-10",
12
  blog_id: string,
13
  article_id: string,
14
  body: {
15
    article?: {
16
      id?: number;
17
      metafields?: {
18
        key?: string;
19
        namespace?: string;
20
        value?: string;
21
        value_type?: string;
22
        [k: string]: unknown;
23
      }[];
24
      [k: string]: unknown;
25
    };
26
    [k: string]: unknown;
27
  }
28
) {
29
  const url = new URL(
30
    `https://${auth.store_name}.myshopify.com/admin/api/${api_version}/blogs/${blog_id}/articles/${article_id}.json`
31
  );
32

33
  const response = await fetch(url, {
34
    method: "PUT",
35
    headers: {
36
      "Content-Type": "application/json",
37
      "X-Shopify-Access-Token": auth.token,
38
    },
39
    body: JSON.stringify(body),
40
  });
41
  if (!response.ok) {
42
    const text = await response.text();
43
    throw new Error(`${response.status} ${text}`);
44
  }
45
  return await response.json();
46
}
47