import {z} from 'zod' export type PatchApiApiSuppliersArgs = {body: {data?: ({attributes?: ({accounting_number: unknown, business_name?: string | undefined, country_iso_alpha_2?: string | undefined, document_series_id: unknown, internal_observations: unknown, is_independent_worker?: boolean | undefined, is_tax_exempt?: boolean | undefined, is_taxable?: boolean | undefined, saft_import_id: unknown, self_billing: unknown, tax_country_region?: string | undefined, tax_exemption_reason_id: unknown, tax_registration_number?: string | undefined, trusted_email_source?: boolean | undefined, website?: string | undefined}) | undefined, id?: string | undefined, type?: string | undefined}) | undefined}}; export type PatchApiApiSuppliersOptions = {baseUrl: string, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const patchApiApiSuppliersResponse = z.object({data: z.object({attributes: z.object({business_name: z.string().optional(), country_iso_alpha_2: z.string().optional(), document_series_id: z.unknown(), internal_observations: z.unknown(), is_independent_worker: z.boolean().optional(), is_tax_exempt: z.boolean().optional(), is_taxable: z.boolean().optional(), saft_import_id: z.unknown(), self_billing: z.unknown(), tax_country_region: z.string().optional(), tax_registration_number: z.string().optional(), website: z.string().optional()}).optional(), id: z.string().optional(), relationships: z.object({addresses: z.object({data: z.array(z.unknown()).optional()}).optional(), bank_accounts: z.object({data: z.array(z.unknown()).optional()}).optional(), company: z.object({data: z.object({id: z.string().optional(), type: z.string().optional()}).optional()}).optional(), contacts: z.object({data: z.array(z.unknown()).optional()}).optional(), defaults: z.object({data: z.unknown()}).optional(), main_address: z.object({data: z.unknown()}).optional(), main_contact: z.object({data: z.unknown()}).optional(), tax_exemption_reason: z.object({data: z.unknown()}).optional()}).optional(), type: z.string().optional()}).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 }; /** * Atualizar Fornecedor * * PATCH /api/suppliers */ export const patchApiApiSuppliers = async (args: PatchApiApiSuppliersArgs, options: PatchApiApiSuppliersOptions) => { const {baseUrl, fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/suppliers${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(patchApiApiSuppliersResponse, await res.json()) };