Edits history of script submission #21264 for ' Creating a package (zoho)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Zoho = {
      token: string;
    };
    /**
     * Creating a package
     * A new package can be created. To create package, URL parameter salesorder_id also needed.
     */
    export async function main(
      auth: Zoho,
      organization_id: string | undefined,
      salesorder_id: string | undefined,
      body: {
        package_number?: string;
        date: string;
        custom_fields?: { customfield_id?: number; value?: string }[];
        line_items: { so_line_item_id?: number; quantity?: number }[];
        notes?: string;
      },
    ) {
      const url = new URL(`https://www.zohoapis.com/inventory/v1/packages`);
      for (const [k, v] of [
        ["organization_id", organization_id],
        ["salesorder_id", salesorder_id],
      ]) {
        if (v !== undefined && v !== "" && k !== undefined) {
          url.searchParams.append(k, v);
        }
      }
      const response = await fetch(url, {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
          Authorization: "Zoho-oauthtoken " + auth.token,
        },
        body: JSON.stringify(body),
      });
      if (!response.ok) {
        const text = await response.text();
        throw new Error(`${response.status} ${text}`);
      }
      return await response.json();
    }
    

    Submitted by hugo697 235 days ago