import {z} from 'zod' export type GetApiCommunicationsArgs = {company?: string | undefined, start_date?: string | undefined, end_date?: string | undefined, type?: string | undefined, recipient?: string | undefined, recipient_type?: string | undefined, id?: Array | undefined}; export type GetApiCommunicationsOptions = {baseUrl?: string | undefined, sec0?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiCommunicationsResponse = z.object({next: z.unknown(), previous: z.unknown(), results: z.array(z.object({id: z.string().optional(), company: z.string().optional(), recipient_type: z.string().optional(), recipient: z.string().optional(), type: z.string().optional(), originator: z.string().optional(), email: z.object({to: z.array(z.string()).optional(), from: z.string().optional(), cc: z.array(z.string()).optional(), subject: z.string().optional(), noreply: z.boolean().optional()}).optional(), notification: z.object({id: z.unknown(), topic: z.unknown()}).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 communications * * GET /communications */ export const getApiCommunications = async (args: GetApiCommunicationsArgs, options?: GetApiCommunicationsOptions) => { 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.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.type != null) query.append("type", String(args.type)) if (args.recipient != null) query.append("recipient", String(args.recipient)) if (args.recipient_type != null) query.append("recipient_type", String(args.recipient_type)) for (const item of args.id ?? []) query.append("id", String(item)) if (sec0 !== undefined) headers.set("Authorization", sec0) const url = `${baseUrl.replace(/\/+$/, '')}/communications${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(getApiCommunicationsResponse, await res.json()) };