Retrieves a page count

Retrieves a page count.

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 page count
7
 * Retrieves a page count.
8
 */
9
export async function main(
10
  auth: Shopify,
11
  api_version: string = "2023-10",
12
  title: string | undefined,
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}/pages/count.json`
23
  );
24
  for (const [k, v] of [
25
    ["title", title],
26
    ["created_at_min", created_at_min],
27
    ["created_at_max", created_at_max],
28
    ["updated_at_min", updated_at_min],
29
    ["updated_at_max", updated_at_max],
30
    ["published_at_min", published_at_min],
31
    ["published_at_max", published_at_max],
32
    ["published_status", published_status],
33
  ]) {
34
    if (v !== undefined && v !== "") {
35
      url.searchParams.append(k, v);
36
    }
37
  }
38
  const response = await fetch(url, {
39
    method: "GET",
40
    headers: {
41
      "X-Shopify-Access-Token": auth.token,
42
    },
43
    body: undefined,
44
  });
45
  if (!response.ok) {
46
    const text = await response.text();
47
    throw new Error(`${response.status} ${text}`);
48
  }
49
  return await response.json();
50
}
51