type Cloudflare = {
token: string;
email: string;
key: string;
};
/**
* Get Dataset csv Stream
* Get the csv content of a given dataset by alias or id. When getting the content by alias the latest dataset is returned, optionally filtered by the latest available at a given date.
*/
export async function main(
auth: Cloudflare,
alias: string,
date: string | undefined
) {
const url = new URL(
`https://api.cloudflare.com/client/v4/radar/datasets/${alias}`
);
for (const [k, v] of [["date", date]]) {
if (v !== undefined && v !== "") {
url.searchParams.append(k, v);
}
}
const response = await fetch(url, {
method: "GET",
headers: {
"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.text();
}
Submitted by hugo697 411 days ago