type Cloudflare = {
token: string;
email: string;
key: string;
};
/**
* Get Dataset download url
* Get a url to download a single dataset.
*/
export async function main(
auth: Cloudflare,
format: "JSON" | "CSV" | undefined,
body: { datasetId: number; [k: string]: unknown }
) {
const url = new URL(
`https://api.cloudflare.com/client/v4/radar/datasets/download`
);
for (const [k, v] of [["format", format]]) {
if (v !== undefined && v !== "") {
url.searchParams.append(k, v);
}
}
const response = await fetch(url, {
method: "POST",
headers: {
"X-AUTH-EMAIL": auth.email,
"X-AUTH-KEY": auth.key,
"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 402 days ago