Retrieves an order count

Retrieves an order 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 an order count
7
 * Retrieves an order count
8
 */
9
export async function main(
10
  auth: Shopify,
11
  api_version: string = "2023-10",
12
  created_at_min: string | undefined,
13
  created_at_max: string | undefined,
14
  updated_at_min: string | undefined,
15
  updated_at_max: string | undefined,
16
  status: string | undefined,
17
  financial_status: string | undefined,
18
  fulfillment_status: string | undefined
19
) {
20
  const url = new URL(
21
    `https://${auth.store_name}.myshopify.com/admin/api/${api_version}/orders/count.json`
22
  );
23
  for (const [k, v] of [
24
    ["created_at_min", created_at_min],
25
    ["created_at_max", created_at_max],
26
    ["updated_at_min", updated_at_min],
27
    ["updated_at_max", updated_at_max],
28
    ["status", status],
29
    ["financial_status", financial_status],
30
    ["fulfillment_status", fulfillment_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