import {z} from 'zod' export type CreateApiApiCustomersArgs = {body: {data?: ({attributes?: ({business_name?: string | undefined, contact_name?: string | undefined, email?: string | undefined, internal_observations?: string | undefined, mobile_number?: number | undefined, observations?: string | undefined, phone_number?: number | undefined, tax_registration_number?: string | undefined, website?: string | undefined}) | undefined, type?: string | undefined}) | undefined}}; export type CreateApiApiCustomersOptions = {baseUrl: string, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiApiCustomersResponse = z.object({data: z.object({attributes: z.object({business_name: z.string().optional(), cashed_vat: z.boolean().optional(), contact_name: z.string().optional(), country_iso_alpha_2: z.string().optional(), data: z.object({}).optional(), email: z.string().optional(), internal_observations: z.string().optional(), is_tax_exempt: z.boolean().optional(), mobile_number: z.string().optional(), not_final_customer: z.boolean().optional(), observations: z.string().optional(), phone_number: z.string().optional(), saft_import_id: 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(), company: z.object({data: z.object({id: z.string().optional(), type: z.string().optional()}).optional()}).optional(), defaults: z.object({data: z.unknown()}).optional(), email_addresses: z.object({data: z.array(z.unknown()).optional()}).optional(), main_address: z.object({data: z.unknown()}).optional(), main_email_address: 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 }; /** * Criar Clientes * * POST /api/customers */ export const createApiApiCustomers = async (args: CreateApiApiCustomersArgs, options: CreateApiApiCustomersOptions) => { 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/customers${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(createApiApiCustomersResponse, await res.json()) };