70bf21e064
Deploy (to testing) and Test Playground Preview Worker / Deploy Playground Preview Worker (testing) (push) Has been skipped
Deploy Workers Shared Staging / Deploy Workers Shared Staging (push) Failing after 0s
Prerelease / build (push) Has been skipped
Handle Changesets / Handle Changesets (push) Has been cancelled
Semgrep OSS scan / semgrep-oss (push) Has been cancelled
69 lines
1.3 KiB
TypeScript
69 lines
1.3 KiB
TypeScript
import { fetch } from "undici";
|
|
|
|
type ApiSuccessBody = {
|
|
result: unknown[];
|
|
};
|
|
|
|
export type Project = {
|
|
name: string;
|
|
created_on: string;
|
|
};
|
|
|
|
export type Worker = {
|
|
id: string;
|
|
created_on: string;
|
|
};
|
|
|
|
const apiFetch = async (
|
|
path: string,
|
|
init = { method: "GET" },
|
|
queryParams = {}
|
|
) => {
|
|
const baseUrl = `https://api.cloudflare.com/client/v4/accounts/${process.env.CLOUDFLARE_ACCOUNT_ID}`;
|
|
const queryString = queryParams
|
|
? `?${new URLSearchParams(queryParams).toString()}`
|
|
: "";
|
|
const url = `${baseUrl}${path}${queryString}`;
|
|
|
|
const response = await fetch(url, {
|
|
...init,
|
|
headers: {
|
|
Authorization: `Bearer ${process.env.CLOUDFLARE_API_TOKEN}`,
|
|
},
|
|
});
|
|
|
|
if (response.status >= 400) {
|
|
throw { url, init, response };
|
|
}
|
|
|
|
const json = (await response.json()) as ApiSuccessBody;
|
|
|
|
return json.result;
|
|
};
|
|
|
|
export const deleteProject = async (project: string) => {
|
|
if (!process.env.CLOUDFLARE_API_TOKEN || !process.env.CLOUDFLARE_ACCOUNT_ID) {
|
|
return;
|
|
}
|
|
try {
|
|
await apiFetch(`/pages/projects/${project}`, {
|
|
method: "DELETE",
|
|
});
|
|
} catch {
|
|
// Ignore errors
|
|
}
|
|
};
|
|
|
|
export const deleteWorker = async (id: string) => {
|
|
if (!process.env.CLOUDFLARE_API_TOKEN || !process.env.CLOUDFLARE_ACCOUNT_ID) {
|
|
return;
|
|
}
|
|
try {
|
|
await apiFetch(`/workers/scripts/${id}`, {
|
|
method: "DELETE",
|
|
});
|
|
} catch {
|
|
// Ignore errors
|
|
}
|
|
};
|