0

Retrieves a list of provinces for a country

Published Nov 8, 2023

Retrieves a list of provinces

Script shopify
  • Submitted by hugo697 Typescript (fetch-only)
    Created 565 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