import {z} from 'zod' export type PatchApiApiAddressesArgs = {body: {data?: ({attributes?: ({address_detail?: string | undefined, city?: string | undefined, country_id?: string | undefined, postcode?: string | undefined, region?: string | undefined}) | undefined, id?: string | undefined, type?: string | undefined}) | undefined}}; export type PatchApiApiAddressesOptions = {baseUrl: string, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const patchApiApiAddressesResponse = z.object({data: z.object({attributes: z.object({address_detail: z.string().optional(), city: z.string().optional(), code: z.unknown(), for_charge: z.boolean().optional(), for_discharge: z.boolean().optional(), is_national_holidays_workday: z.boolean().optional(), is_primary: z.boolean().optional(), is_saturday_workday: z.boolean().optional(), is_sunday_workday: z.boolean().optional(), name: z.string().optional(), payroll_enumerations_tax_office_id: z.unknown(), postcode: z.string().optional(), region: z.string().optional(), subtype: z.unknown()}).optional(), id: z.string().optional(), relationships: z.object({company: z.object({data: z.unknown()}).optional(), country: z.object({data: z.object({id: z.string().optional(), type: z.string().optional()}).optional()}).optional(), customer: z.object({data: z.unknown()}).optional(), supplier: z.object({data: z.unknown()}).optional(), user: 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 Morada * * PATCH /api/addresses */ export const patchApiApiAddresses = async (args: PatchApiApiAddressesArgs, options: PatchApiApiAddressesOptions) => { 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/addresses${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(patchApiApiAddressesResponse, await res.json()) };