import {z} from 'zod' export type GetApiCoaApiTransactionMatchesArgs = {limit?: number | undefined, offset?: number | undefined}; export type GetApiCoaApiTransactionMatchesOptions = {baseUrl?: string | undefined, knoxApiToken?: string | undefined, fetch?: typeof fetch | undefined}; export const transactionMatchStatusEnum = z.enum(["pending", "draft", "accepted", "rejected"]); export const transactionMatch = z.object({id: z.number().int(), matched_object: z.string(), app_label: z.string(), model: z.string(), drafts: z.string(), object_id: z.number().int().gte(0).lte(2147483647), created_at: z.string(), reviewed_at: z.string().nullable().optional(), status: transactionMatchStatusEnum.optional(), confidence_score: z.number().gte(0).lte(1).nullable().optional(), reason: z.string().max(64).nullable().optional(), snapshot: z.unknown(), customer: z.number().int(), transaction: z.number().int().nullable(), content_type: z.number().int(), reviewed_by: z.number().int().nullable().optional()}); export const paginatedTransactionMatchList = z.object({count: z.number().int(), next: z.string().nullable().optional(), previous: z.string().nullable().optional(), results: z.array(transactionMatch)}); 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 Transaction Matches * * GET /coa/api/transaction-matches */ export const getApiCoaApiTransactionMatches = async (args: GetApiCoaApiTransactionMatchesArgs, options?: GetApiCoaApiTransactionMatchesOptions) => { const {baseUrl = "https://api.meetcampfire.com", fetch: fetchFn = fetch, knoxApiToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (knoxApiToken !== undefined) headers.set("Authorization", knoxApiToken) const url = `${baseUrl.replace(/\/+$/, '')}/coa/api/transaction-matches${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(paginatedTransactionMatchList, await res.json()) };