import {z} from 'zod' export type GetApiConsumersConsumerIdSyncsSyncidPreviewsPreviewIdObjectsArgs = {consumer_id: string, syncid: string, preview_id: string, kind?: (string | null) | undefined, capability?: (string | null) | undefined, page?: number | undefined, size?: number | undefined, language?: (string | null) | undefined}; export type GetApiConsumersConsumerIdSyncsSyncidPreviewsPreviewIdObjectsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const previewSummary = z.object({title: z.string().nullable().optional(), amount: z.number().nullable().optional(), currency: z.string().nullable().optional(), date: z.string().nullable().optional()}); export const previewLink = z.object({relation: z.string(), synthetic_id: z.string(), title: z.string().nullable().optional()}); export const previewObjectItem = z.object({synthetic_id: z.string(), kind: z.string(), kind_title: z.string(), capability: z.string(), capability_title: z.string(), source_id: z.string().nullable(), summary: previewSummary, links: z.array(previewLink), payload: z.record(z.string(), z.unknown())}); export const chiftPagePreviewObjectItem = z.object({items: z.array(previewObjectItem), total: z.number().int().gte(0), page: z.number().int().gte(1), size: z.number().int().gte(1)}); 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 }; /** * The objects a preview would create * * GET /consumers/{consumer_id}/syncs/{syncid}/previews/{preview_id}/objects */ export const getApiConsumersConsumerIdSyncsSyncidPreviewsPreviewIdObjects = async (args: GetApiConsumersConsumerIdSyncsSyncidPreviewsPreviewIdObjectsArgs, options?: GetApiConsumersConsumerIdSyncsSyncidPreviewsPreviewIdObjectsOptions) => { const {baseUrl = "https://api.chift.eu", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.kind != null) query.append("kind", String(args.kind)) if (args.capability != null) query.append("capability", String(args.capability)) if (args.page != null) query.append("page", String(args.page)) if (args.size != null) query.append("size", String(args.size)) if (args.language != null) query.append("language", String(args.language)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/consumers/${encodeURIComponent(String(args.consumer_id))}/syncs/${encodeURIComponent(String(args.syncid))}/previews/${encodeURIComponent(String(args.preview_id))}/objects${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(chiftPagePreviewObjectItem, await res.json()) };