import {z} from 'zod' export type Bank = {id?: string | undefined, bank: number, name?: (string | null) | undefined, number?: (string | null) | undefined, holder_id?: (string | null) | undefined, holder_name?: (string | null) | undefined, account_type?: ('checking_account' | 'savings_account' | 'sight_account' | 'rut_account' | 'line_of_credit' | null) | undefined, auto_match?: boolean | undefined, extra_data?: Record | undefined, last_sync?: (string | null) | undefined, status?: ('active' | 'authentication_failed' | 'unknown_error' | 'archived') | undefined, archived?: (string | null) | undefined, created?: string | undefined, updated?: string | undefined}; export type UpdateApiBanksIdArgs = {body: Bank}; export type UpdateApiBanksIdOptions = {baseUrl?: string | undefined, TokenAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const bank = z.object({id: z.string().optional(), bank: z.number().int(), name: z.string().max(64).nullable().optional(), number: z.string().max(24).nullable().optional(), holder_id: z.string().max(32).nullable().optional(), holder_name: z.string().max(64).nullable().optional(), account_type: z.enum(["checking_account", "savings_account", "sight_account", "rut_account", "line_of_credit"]).nullable().optional(), auto_match: z.boolean().optional(), extra_data: z.object({}).optional(), last_sync: z.string().nullable().optional(), status: z.enum(["active", "authentication_failed", "unknown_error", "archived"]).optional(), archived: z.string().nullable().optional(), created: z.string().optional(), updated: 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 }; /** * Replace bank * * PUT /banks/{id}/ */ export const updateApiBanksId = async (args: UpdateApiBanksIdArgs, options?: UpdateApiBanksIdOptions) => { const {baseUrl = "https://api.piriod.com", fetch: fetchFn = fetch, TokenAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (TokenAuth !== undefined) headers.set("Authorization", TokenAuth) const url = `${baseUrl.replace(/\/+$/, '')}/banks/${encodeURIComponent(String(args.id))}/${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(bank, await res.json()) };