import {z} from 'zod' export type GetApiApiV1SessionsArgs = {botId?: string | undefined, channelType?: string | undefined, status?: string | undefined, limit?: number | undefined, offset?: (number | null) | undefined}; export type GetApiApiV1SessionsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const getApiApiV1SessionsResponse = z.object({sessions: z.array(z.object({id: z.string(), botId: z.string(), sessionKey: z.string(), channelType: z.string().nullable(), channelId: z.string().nullable(), title: z.string(), status: z.string(), messageCount: z.number(), lastMessageAt: z.string().nullable(), metadata: z.record(z.string(), z.unknown()).nullable(), createdAt: z.string(), updatedAt: z.string()})), total: z.number(), limit: z.number(), offset: z.number()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * GET /api/v1/sessions */ export const getApiApiV1Sessions = async (args: GetApiApiV1SessionsArgs, options: GetApiApiV1SessionsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.botId != null) query.append("botId", String(args.botId)) if (args.channelType != null) query.append("channelType", String(args.channelType)) if (args.status != null) query.append("status", String(args.status)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/sessions${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiApiV1SessionsResponse, await res.json()) };