Edits history of script submission #20746 for ' RetrieveCatalogObject (square)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Square = {
      token: string;
    };
    /**
     * RetrieveCatalogObject
     * Returns a single [CatalogItem]($m/CatalogItem) as a
    [CatalogObject]($m/CatalogObject) based on the provided ID. The returned
    object includes all of the relevant [CatalogItem]($m/CatalogItem)
    information including: [CatalogItemVariation]($m/CatalogItemVariation)
    children, references to its
    [CatalogModifierList]($m/CatalogModifierList) objects, and the ids of
    any [CatalogTax]($m/CatalogTax) objects that apply to it.
     */
    export async function main(
      auth: Square,
      object_id: string,
      include_related_objects: string | undefined,
      catalog_version: string | undefined,
      include_category_path_to_root: string | undefined,
    ) {
      const url = new URL(
        `https://connect.squareup.com/v2/catalog/object/${object_id}`,
      );
      for (const [k, v] of [
        ["include_related_objects", include_related_objects],
        ["catalog_version", catalog_version],
        ["include_category_path_to_root", include_category_path_to_root],
      ]) {
        if (v !== undefined && v !== "" && k !== undefined) {
          url.searchParams.append(k, v);
        }
      }
      const response = await fetch(url, {
        method: "GET",
        headers: {
          Authorization: "Bearer " + auth.token,
        },
        body: undefined,
      });
      if (!response.ok) {
        const text = await response.text();
        throw new Error(`${response.status} ${text}`);
      }
      return await response.json();
    }
    

    Submitted by hugo697 235 days ago