//native
type Ably = {
accessToken: string;
};
/**
* Retrieve app statistics
* Retrieves application-level statistics for an app.
*/
export async function main(
auth: Ably,
id: string,
start: string | undefined,
end: string | undefined,
unit: "minute" | "hour" | "day" | "month" | undefined,
direction: "forwards" | "backwards" | undefined,
limit: string | undefined,
) {
const url = new URL(`https://control.ably.net/v1/apps/${id}/stats`);
for (const [k, v] of [
["start", start],
["end", end],
["unit", unit],
["direction", direction],
["limit", limit],
]) {
if (v !== undefined && v !== "" && k !== undefined) {
url.searchParams.append(k, v);
}
}
const response = await fetch(url, {
method: "GET",
headers: {
Authorization: "Bearer " + auth.accessToken,
},
body: undefined,
});
if (!response.ok) {
const text = await response.text();
throw new Error(`${response.status} ${text}`);
}
return await response.json();
}
Submitted by hugo697 138 days ago