Edits history of script submission #17431 for ' Create site deploy (netlify)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Netlify = {
      token: string;
    };
    /**
     * Create site deploy
     *
     */
    export async function main(
      auth: Netlify,
      site_id: string,
      deploy_previews: string | undefined,
      production: string | undefined,
      state:
        | "new"
        | "pending_review"
        | "accepted"
        | "rejected"
        | "enqueued"
        | "building"
        | "uploading"
        | "uploaded"
        | "preparing"
        | "prepared"
        | "processing"
        | "processed"
        | "ready"
        | "error"
        | "retrying"
        | undefined,
      branch: string | undefined,
      latest_published: string | undefined,
      title: string | undefined,
      body: {
        files?: {};
        draft?: false | true;
        async?: false | true;
        functions?: {};
        function_schedules?: { name?: string; cron?: string }[];
        functions_config?: {};
        branch?: string;
        framework?: string;
        framework_version?: string;
      },
    ) {
      const url = new URL(
        `https://api.netlify.com/api/v1/sites/${site_id}/deploys`,
      );
      for (const [k, v] of [
        ["deploy-previews", deploy_previews],
        ["production", production],
        ["state", state],
        ["branch", branch],
        ["latest-published", latest_published],
        ["title", title],
      ]) {
        if (v !== undefined && v !== "" && k !== undefined) {
          url.searchParams.append(k, v);
        }
      }
      const response = await fetch(url, {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
          Authorization: "Bearer " + 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