0

Retrieves a count of comments

Published Nov 8, 2023

Retrieves a count of comments

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