Retrieves a count of smart collections

Retrieves a count of smart collections

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 smart collections
7
 * Retrieves a count of smart collections
8
 */
9
export async function main(
10
  auth: Shopify,
11
  api_version: string = "2023-10",
12
  title: string | undefined,
13
  product_id: string | undefined,
14
  updated_at_min: string | undefined,
15
  updated_at_max: string | undefined,
16
  published_at_min: string | undefined,
17
  published_at_max: string | undefined,
18
  published_status: string | undefined
19
) {
20
  const url = new URL(
21
    `https://${auth.store_name}.myshopify.com/admin/api/${api_version}/smart_collections/count.json`
22
  );
23
  for (const [k, v] of [
24
    ["title", title],
25
    ["product_id", product_id],
26
    ["updated_at_min", updated_at_min],
27
    ["updated_at_max", updated_at_max],
28
    ["published_at_min", published_at_min],
29
    ["published_at_max", published_at_max],
30
    ["published_status", published_status],
31
  ]) {
32
    if (v !== undefined && v !== "") {
33
      url.searchParams.append(k, v);
34
    }
35
  }
36
  const response = await fetch(url, {
37
    method: "GET",
38
    headers: {
39
      "X-Shopify-Access-Token": auth.token,
40
    },
41
    body: undefined,
42
  });
43
  if (!response.ok) {
44
    const text = await response.text();
45
    throw new Error(`${response.status} ${text}`);
46
  }
47
  return await response.json();
48
}
49