Retrieves a count of all script tags

Retrieves a count of all script tags

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 script tags
7
 * Retrieves a count of all script tags
8
 */
9
export async function main(
10
  auth: Shopify,
11
  api_version: string = "2023-10",
12
  src: string | undefined
13
) {
14
  const url = new URL(
15
    `https://${auth.store_name}.myshopify.com/admin/api/${api_version}/script_tags/count.json`
16
  );
17
  for (const [k, v] of [["src", src]]) {
18
    if (v !== undefined && v !== "") {
19
      url.searchParams.append(k, v);
20
    }
21
  }
22
  const response = await fetch(url, {
23
    method: "GET",
24
    headers: {
25
      "X-Shopify-Access-Token": auth.token,
26
    },
27
    body: undefined,
28
  });
29
  if (!response.ok) {
30
    const text = await response.text();
31
    throw new Error(`${response.status} ${text}`);
32
  }
33
  return await response.json();
34
}
35