import {z} from 'zod' export type CreateExport = {type: 'REDEMPTION' | 'CALCULATED_PRICE', filters?: (Array<{field: string, value: string | Array, operator: 'EQUAL' | 'GREATER_THAN_OR_EQUAL_TO' | 'LESS_THAN' | 'IN'}>) | undefined}; export type CreateApiApiOffersOffersExportsArgs = {'x-site-context': string, body: CreateExport}; export type CreateApiApiOffersOffersExportsOptions = {baseUrl?: string | undefined, AuthorizationToken?: string | undefined, fetch?: typeof fetch | undefined}; export const createExportResponse = z.object({exportId: z.string(), startedAt: z.string(), endedAt: z.string().nullable().optional(), totalDataExported: z.number().optional(), status: z.enum(["IN_PROGRESS", "COMPLETED", "ERROR"]), errors: z.array(z.object({type: z.string().optional(), message: z.string().optional()})).optional(), type: z.string(), filters: z.array(z.object({field: z.string(), value: z.union([z.string().min(1).max(120), z.array(z.union([z.string(), z.number()]))]), operator: z.string()})).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 }; /** * Initiate export request * * POST /api-offers/offers-exports */ export const createApiApiOffersOffersExports = async (args: CreateApiApiOffersOffersExportsArgs, options?: CreateApiApiOffersOffersExportsOptions) => { const {baseUrl = "https://live.copilot.fabric.inc", fetch: fetchFn = fetch, AuthorizationToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['x-site-context'] != null) headers.set("x-site-context", String(args['x-site-context'])) if (AuthorizationToken !== undefined) headers.set('authorization', `Bearer ${AuthorizationToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/api-offers/offers-exports${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createExportResponse, await res.json()) };