Retrieves a list of provinces for a country
One script reply has been approved by the moderators Verified

Retrieves a list of provinces

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