import {z} from 'zod' export type GetApiNotificationsArgs = {company?: string | undefined, topic?: string | undefined, start_date?: string | undefined, 'end date'?: string | undefined, recipient_type?: string | undefined, recipient?: string | undefined, id?: Array | undefined}; export type GetApiNotificationsOptions = {baseUrl?: string | undefined, sec0?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiNotificationsResponse = z.object({next: z.unknown(), previous: z.unknown(), results: z.array(z.object({id: z.string().optional(), topic: z.string().optional(), company: z.string().optional(), recipient_type: z.string().optional(), recipient: z.string().optional(), email_communication: z.string().optional(), created_at: z.string().optional()})).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 notifications * * GET /notifications */ export const getApiNotifications = async (args: GetApiNotificationsArgs, options?: GetApiNotificationsOptions) => { const {baseUrl = "https://sandbox.checkhq.com", fetch: fetchFn = fetch, sec0} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.company != null) query.append("company", String(args.company)) if (args.topic != null) query.append("topic", String(args.topic)) if (args.start_date != null) query.append("start_date", String(args.start_date)) if (args['end date'] != null) query.append("end date", String(args['end date'])) if (args.recipient_type != null) query.append("recipient_type", String(args.recipient_type)) if (args.recipient != null) query.append("recipient", String(args.recipient)) for (const item of args.id ?? []) query.append("id", String(item)) if (sec0 !== undefined) headers.set("Authorization", sec0) const url = `${baseUrl.replace(/\/+$/, '')}/notifications${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(getApiNotificationsResponse, await res.json()) };