//native
type Netlify = {
token: string;
};
/**
* Create env vars
* Creates new environment variables. Granular scopes are available on Pro plans and above.
*/
export async function main(
auth: Netlify,
account_id: 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`,
);
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: "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