import {z} from 'zod' export type TaxIdKind = 'cnpj' | 'cpf' | 'ie' | 'im' | 'pt_nif' | 'es_nif' | 'vat' | 'steuernummer' | 'partita_iva' | 'codice_fiscale' | 'sdi' | 'siret' | 'ein' | 'nip' | 'se_orgnr' | 'no_orgnr' | 'cvr' | 'ico' | 'adoszam' | 'ro_cui' | 'ch_uid' | 'inn' | 'kz_biniin' | 'edrpou' | 'cuit' | 'rfc' | 'cl_rut' | 'uy_rut' | 'co_nit' | 'gt_nit' | 'pe_ruc' | 'cr_cedula' | 'rnc' | 'ca_bn' | 'abn' | 'nzbn' | 'jp_corporate' | 'gstin' | 'pan' | 'npwp' | 'ph_tin' | 'vn_mst' | 'sg_uen' | 'uscc' | 'az_voen' | 'other'; export type TaxIdInput = {kind: TaxIdKind, value: string}; export type PayeeUpdate = {name?: (string | null) | undefined, type?: ('person' | 'company' | null) | undefined, is_favorite?: (boolean | null) | undefined, notes?: (string | null) | undefined, email?: (string | null) | undefined, phone?: (string | null) | undefined, address?: (string | null) | undefined, website?: (string | null) | undefined, tax_ids?: (Array | null) | undefined}; export type PatchApiApiPayeesPayeeIdArgs = {payee_id: string, 'X-Workspace-Id'?: (string | null) | undefined, body: PayeeUpdate}; export type PatchApiApiPayeesPayeeIdOptions = {baseUrl: string, OAuth2PasswordBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const taxIdRead = z.object({kind: z.string(), value: z.string()}); export const payeeRead = z.object({id: z.string(), user_id: z.string(), name: z.string(), type: z.enum(["person", "company"]).nullable().optional(), source: z.enum(["manual", "sync", "import"]).optional(), is_favorite: z.boolean(), notes: z.string().nullable().optional(), email: z.string().nullable().optional(), phone: z.string().nullable().optional(), address: z.string().nullable().optional(), website: z.string().nullable().optional(), tax_ids: z.array(taxIdRead).optional(), created_at: z.string(), transaction_count: z.number().int().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 }; /** * Update Payee * * PATCH /api/payees/{payee_id} */ export const patchApiApiPayeesPayeeId = async (args: PatchApiApiPayeesPayeeIdArgs, options: PatchApiApiPayeesPayeeIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth2PasswordBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['X-Workspace-Id'] != null) headers.set("X-Workspace-Id", String(args['X-Workspace-Id'])) if (OAuth2PasswordBearer !== undefined) headers.set('authorization', `Bearer ${OAuth2PasswordBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/payees/${encodeURIComponent(String(args.payee_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(payeeRead, await res.json()) };