import {z} from 'zod' export type CreateApiApiImportsIdColumnMapArgs = {id: string, body: {defaultAccountName?: string | undefined, defaultDate?: string | undefined, mapping: {account: {column: string} | null, amount: {column: string, kind: 'signed', parensNegative: boolean, signConvention: 'outflow_negative' | 'outflow_positive'} | {inflowColumn: string, kind: 'split', outflowColumn: string} | {debitValues: Array, kind: 'magnitude_type', magnitudeColumn: string, typeColumn: string}, category: {column: string} | null, confidence: number, date: {column: string, format: string, kind: 'column'} | {kind: 'missing'}, excludeRule: {column: string, trueValues: Array} | null, merchant: {column: string}, notes: {column: string} | null, originalDescription: {column: string} | null, tags: {column: string, delimiter: string} | null, transferRule: {kind: 'category_match', values: Array} | {column: string, kind: 'type_match', values: Array} | {kind: 'merchant_prefix', prefixes: Array} | null}, singleAccountChoice?: ({accountId: string, kind: 'existing'} | {institutionLogoDomain?: string | undefined, institutionName?: string | undefined, kind: 'create', name: string}) | undefined}}; export type CreateApiApiImportsIdColumnMapOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const createApiApiImportsIdColumnMapResponse = z.object({rejected: z.number().int().gte(0), staged: z.number().int().gte(0), success: z.boolean()}); 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 }; /** * Confirm column mapping * * POST /api/imports/{id}/column-map */ export const createApiApiImportsIdColumnMap = async (args: CreateApiApiImportsIdColumnMapArgs, options: CreateApiApiImportsIdColumnMapOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/imports/${encodeURIComponent(String(args.id))}/column-map${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(createApiApiImportsIdColumnMapResponse, await res.json()) };