Retrieves a count of all articles from a blog

Retrieves a count of all articles from a blog

Script shopify Verified

by hugo697 ยท 11/8/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 396 days ago
1
type Shopify = {
2
  token: string;
3
  store_name: string;
4
};
5
/**
6
 * Retrieves a count of all articles from a blog
7
 * Retrieves a count of all articles from a blog
8
 */
9
export async function main(
10
  auth: Shopify,
11
  api_version: string = "2023-10",
12
  blog_id: string,
13
  created_at_min: string | undefined,
14
  created_at_max: string | undefined,
15
  updated_at_min: string | undefined,
16
  updated_at_max: string | undefined,
17
  published_at_min: string | undefined,
18
  published_at_max: string | undefined,
19
  published_status: string | undefined
20
) {
21
  const url = new URL(
22
    `https://${auth.store_name}.myshopify.com/admin/api/${api_version}/blogs/${blog_id}/articles/count.json`
23
  );
24
  for (const [k, v] of [
25
    ["created_at_min", created_at_min],
26
    ["created_at_max", created_at_max],
27
    ["updated_at_min", updated_at_min],
28
    ["updated_at_max", updated_at_max],
29
    ["published_at_min", published_at_min],
30
    ["published_at_max", published_at_max],
31
    ["published_status", published_status],
32
  ]) {
33
    if (v !== undefined && v !== "") {
34
      url.searchParams.append(k, v);
35
    }
36
  }
37
  const response = await fetch(url, {
38
    method: "GET",
39
    headers: {
40
      "X-Shopify-Access-Token": auth.token,
41
    },
42
    body: undefined,
43
  });
44
  if (!response.ok) {
45
    const text = await response.text();
46
    throw new Error(`${response.status} ${text}`);
47
  }
48
  return await response.json();
49
}
50