Edits history of script submission #17471 for ' Get env vars (netlify)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Netlify = {
      token: string;
    };
    /**
     * Get env vars
     * Returns all environment variables for an account or site. An account corresponds to a team in the Netlify UI.
     */
    export async function main(
      auth: Netlify,
      account_id: string,
      context_name:
        | "all"
        | "dev"
        | "branch-deploy"
        | "deploy-preview"
        | "production"
        | undefined,
      scope: "builds" | "functions" | "runtime" | "post-processing" | undefined,
      site_id: string | undefined,
    ) {
      const url = new URL(
        `https://api.netlify.com/api/v1/accounts/${account_id}/env`,
      );
      for (const [k, v] of [
        ["context_name", context_name],
        ["scope", scope],
        ["site_id", site_id],
      ]) {
        if (v !== undefined && v !== "" && k !== undefined) {
          url.searchParams.append(k, v);
        }
      }
      const response = await fetch(url, {
        method: "GET",
        headers: {
          Authorization: "Bearer " + auth.token,
        },
        body: undefined,
      });
      if (!response.ok) {
        const text = await response.text();
        throw new Error(`${response.status} ${text}`);
      }
      return await response.json();
    }
    

    Submitted by hugo697 235 days ago