//native
type Ably = {
apiKey: string;
};
/**
* Get message history for a channel
* Get message history for a channel
*/
export async function main(
auth: Ably,
channel_id: string,
format: "json" | "jsonp" | "msgpack" | "html" | undefined,
start: string | undefined,
limit: string | undefined,
end: string | undefined,
direction: "forwards" | "backwards" | undefined,
X_Ably_Version: string,
) {
const url = new URL(`https://rest.ably.io/channels/${channel_id}/messages`);
for (const [k, v] of [
["format", format],
["start", start],
["limit", limit],
["end", end],
["direction", direction],
]) {
if (v !== undefined && v !== "" && k !== undefined) {
url.searchParams.append(k, v);
}
}
const response = await fetch(url, {
method: "GET",
headers: {
"X-Ably-Version": X_Ably_Version,
Authorization: "Bearer " + auth.apiKey,
},
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