type Cloudflare = {
token: string;
email: string;
key: string;
};
/**
* Put script content
* Put script content without touching config or metadata
*/
export async function main(
auth: Cloudflare,
account_identifier: string,
script_name: string,
CF_WORKER_BODY_PART: string,
CF_WORKER_MAIN_MODULE_PART: string,
body: {
"<any part name>"?: string[];
metadata?: {
body_part?: string;
main_module?: string;
[k: string]: unknown;
};
[k: string]: unknown;
}
) {
const url = new URL(
`https://api.cloudflare.com/client/v4/accounts/${account_identifier}/workers/scripts/${script_name}/content`
);
const formData = new FormData();
for (const [k, v] of Object.entries(body)) {
if (v !== undefined && v !== "") {
formData.append(k, String(v));
}
}
const response = await fetch(url, {
method: "PUT",
headers: {
"CF-WORKER-BODY-PART": CF_WORKER_BODY_PART,
"CF-WORKER-MAIN-MODULE-PART": CF_WORKER_MAIN_MODULE_PART,
"X-AUTH-EMAIL": auth.email,
"X-AUTH-KEY": auth.key,
Authorization: "Bearer " + auth.token,
},
body: formData,
});
if (!response.ok) {
const text = await response.text();
throw new Error(`${response.status} ${text}`);
}
return await response.json();
}
Submitted by hugo697 383 days ago
type Cloudflare = {
token: string;
email: string;
key: string;
};
/**
* Put script content
* Put script content without touching config or metadata
*/
export async function main(
auth: Cloudflare,
account_identifier: string,
script_name: string,
CF_WORKER_BODY_PART: string,
CF_WORKER_MAIN_MODULE_PART: string,
body: {
"<any part name>"?: string[];
metadata?: {
body_part?: string;
main_module?: string;
[k: string]: unknown;
};
[k: string]: unknown;
}
) {
const url = new URL(
`https://api.cloudflare.com/client/v4/accounts/${account_identifier}/workers/scripts/${script_name}/content`
);
const formData = new FormData();
for (const [k, v] of Object.entries(body)) {
if (v !== undefined && v !== "") {
formData.append(k, String(v));
}
}
const response = await fetch(url, {
method: "PUT",
headers: {
"CF-WORKER-BODY-PART": CF_WORKER_BODY_PART,
"CF-WORKER-MAIN-MODULE-PART": CF_WORKER_MAIN_MODULE_PART,
"X-AUTH-EMAIL": auth.email,
"X-AUTH-KEY": auth.key,
Authorization: "Bearer " + auth.token,
},
body: formData,
});
if (!response.ok) {
const text = await response.text();
throw new Error(`${response.status} ${text}`);
}
return await response.json();
}
Submitted by hugo697 810 days ago
type Cloudflare = {
token: string;
email: string;
key: string;
};
/**
* Put script content
* Put script content without touching config or metadata
*/
export async function main(
auth: Cloudflare,
account_identifier: string,
script_name: string,
CF_WORKER_BODY_PART: string,
CF_WORKER_MAIN_MODULE_PART: string
) {
const url = new URL(
`https://api.cloudflare.com/client/v4/accounts/${account_identifier}/workers/scripts/${script_name}/content`
);
const response = await fetch(url, {
method: "PUT",
headers: {
"CF-WORKER-BODY-PART": CF_WORKER_BODY_PART,
"CF-WORKER-MAIN-MODULE-PART": CF_WORKER_MAIN_MODULE_PART,
"X-AUTH-EMAIL": auth.email,
"X-AUTH-KEY": auth.key,
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 920 days ago