Edits history of script submission #17532 for ' Update env var (netlify)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Netlify = {
      token: string;
    };
    /**
     * Update env var
     * Updates an existing environment variable and all of its values. Existing values will be replaced by values provided.
     */
    export async function main(
      auth: Netlify,
      account_id: string,
      key: string,
      site_id: string | undefined,
      body: {
        key?: string;
        scopes?: "builds" | "functions" | "runtime" | "post-processing"[];
        values?: {
          id?: string;
          value?: string;
          context?:
            | "all"
            | "dev"
            | "branch-deploy"
            | "deploy-preview"
            | "production"
            | "branch";
          context_parameter?: string;
        }[];
        is_secret?: false | true;
      },
    ) {
      const url = new URL(
        `https://api.netlify.com/api/v1/accounts/${account_id}/env/${key}`,
      );
      for (const [k, v] of [["site_id", site_id]]) {
        if (v !== undefined && v !== "" && k !== undefined) {
          url.searchParams.append(k, v);
        }
      }
      const response = await fetch(url, {
        method: "PUT",
        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