Edits history of script submission #13720 for ' Create a custom environment for the current project. (vercel)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Vercel = {
      token: string;
    };
    /**
     * Create a custom environment for the current project.
     * Creates a custom environment for the current project. Cannot be named 'Production' or 'Preview'.
     */
    export async function main(
      auth: Vercel,
      idOrName: string,
      teamId: string | undefined,
      slug: string | undefined,
      body: {
        slug?: string;
        description?: string;
        branchMatcher?: {
          type: "equals" | "startsWith" | "endsWith";
          pattern: string;
        };
        copyEnvVarsFrom?: string;
      },
    ) {
      const url = new URL(
        `https://api.vercel.com/v9/projects/${idOrName}/custom-environments`,
      );
      for (const [k, v] of [
        ["teamId", teamId],
        ["slug", slug],
      ]) {
        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 428 days ago