import {z} from 'zod' export type GetApiDigestsSchedulesScheduleIdInstancesArgs = {schedule_id: string, cursor?: string | undefined, limit?: number | undefined}; export type GetApiDigestsSchedulesScheduleIdInstancesOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const digestCategory = z.object({category_key: z.string(), retain: z.enum(["first", "last", "highest", "lowest", "none"]), sort_key: z.string().optional()}); export const digestInstance = z.object({digest_instance_id: z.string(), status: z.enum(["IN_PROGRESS", "COMPLETED"]), event_count: z.number().int(), user_id: z.string(), tenant_id: z.string().nullable().optional(), categories: z.array(digestCategory).optional(), category_key_counts: z.record(z.string(), z.number().int()).optional(), disabled: z.boolean().optional(), created_at: z.string().optional()}); export const digestInstanceListResponse = z.object({type: z.literal("list"), items: z.array(digestInstance), has_more: z.boolean(), cursor: z.string().nullable().optional(), url: z.string().optional(), next_url: z.string().nullable().optional()}); 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 }; /** * List digest instances * * GET /digests/schedules/{schedule_id}/instances */ export const getApiDigestsSchedulesScheduleIdInstances = async (args: GetApiDigestsSchedulesScheduleIdInstancesArgs, options?: GetApiDigestsSchedulesScheduleIdInstancesOptions) => { const {baseUrl = "https://api.courier.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.limit != null) query.append("limit", String(args.limit)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/digests/schedules/${encodeURIComponent(String(args.schedule_id))}/instances${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(digestInstanceListResponse, await res.json()) };