31 lines
1.1 KiB
TypeScript
31 lines
1.1 KiB
TypeScript
import type { KyInstance } from "ky";
|
|
import type { MessageQueryOptions } from "./types.js";
|
|
|
|
export async function fetchMessagePage(
|
|
client: KyInstance,
|
|
corpId: string,
|
|
options: MessageQueryOptions = {}
|
|
): Promise<{ count: number; items: Record<string, unknown>[] }> {
|
|
const start = options.start ?? 0;
|
|
const count = options.count ?? 20;
|
|
const read = options.read ?? false;
|
|
const response = await client.get("api/message/v1/messages", {
|
|
searchParams: {
|
|
read: String(read),
|
|
start: String(start),
|
|
count: String(count),
|
|
corpId,
|
|
},
|
|
});
|
|
const json = (await response.json()) as { count?: number; items?: Record<string, unknown>[] };
|
|
return { count: json.count ?? json.items?.length ?? 0, items: json.items ?? [] };
|
|
}
|
|
|
|
export async function markMessageRead(client: KyInstance, corpId: string, messageId: string): Promise<void> {
|
|
// The downstream route accepts the compound ID with its colon separators intact.
|
|
await client.put(`api/message/v1/messages/$${messageId}`, {
|
|
searchParams: { corpId },
|
|
json: { id: messageId },
|
|
});
|
|
}
|