Receive a single Article
One script reply has been approved by the moderators Verified

Retrieves a single 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
 * Receive a single Article
7
 * Retrieves a single 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
  fields: string | undefined
15
) {
16
  const url = new URL(
17
    `https://${auth.store_name}.myshopify.com/admin/api/${api_version}/blogs/${blog_id}/articles/${article_id}.json`
18
  );
19
  for (const [k, v] of [["fields", fields]]) {
20
    if (v !== undefined && v !== "") {
21
      url.searchParams.append(k, v);
22
    }
23
  }
24
  const response = await fetch(url, {
25
    method: "GET",
26
    headers: {
27
      "X-Shopify-Access-Token": auth.token,
28
    },
29
    body: undefined,
30
  });
31
  if (!response.ok) {
32
    const text = await response.text();
33
    throw new Error(`${response.status} ${text}`);
34
  }
35
  return await response.json();
36
}
37