Edits history of script submission #13728 for ' Create one or more environment variables (vercel)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Vercel = {
      token: string;
    };
    /**
     * Create one or more environment variables
     * Create one or more environment variables for a project by passing its `key`, `value`, `type` and `target` and by specifying the project by either passing the project `id` or `name` in the URL. If you include `upsert=true` as a query parameter, a new environment variable will not be created if it already exists but, the existing variable's value will be updated.
     */
    export async function main(
      auth: Vercel,
      idOrName: string,
      upsert: string | undefined,
      teamId: string | undefined,
      slug: string | undefined,
      body: {} | {} | {}[],
    ) {
      const url = new URL(`https://api.vercel.com/v10/projects/${idOrName}/env`);
      for (const [k, v] of [
        ["upsert", upsert],
        ["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