import {z} from 'zod' export type CreateApiCustomersArgs = {body: {name: string, email?: string | undefined, code?: string | undefined, document?: string | undefined, document_type?: string | undefined, type: string, gender?: string | undefined, address?: Record | undefined, phones?: ({home_phone?: Record | undefined, mobile_phone?: Record | undefined}) | undefined, birthdate?: string | undefined, metadata?: string | undefined}}; export type CreateApiCustomersOptions = {baseUrl?: string | undefined, sec0?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const createApiCustomersResponse = z.object({id: z.string().optional(), name: z.string().optional(), email: z.string().optional(), code: z.string().optional(), document: z.string().optional(), document_type: z.string().optional(), type: z.string().optional(), gender: z.string().optional(), delinquent: z.boolean().optional(), address: z.object({id: z.string().optional(), line_1: z.string().optional(), line_2: z.string().optional(), zip_code: z.string().optional(), city: z.string().optional(), state: z.string().optional(), country: z.string().optional(), status: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional()}).optional(), created_at: z.string().optional(), updated_at: z.string().optional(), birthdate: z.string().optional(), phones: z.object({home_phone: z.object({country_code: z.string().optional(), number: z.string().optional(), area_code: z.string().optional()}).optional(), mobile_phone: z.object({country_code: z.string().optional(), number: z.string().optional(), area_code: z.string().optional()}).optional()}).optional(), metadata: z.object({id: z.string().optional(), company: 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 cliente * * POST /customers */ export const createApiCustomers = async (args: CreateApiCustomersArgs, options?: CreateApiCustomersOptions) => { const {baseUrl = "https://api.mundipagg.com/core/v1", fetch: fetchFn = fetch, sec0} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (sec0 !== undefined) headers.set('authorization', `Basic ${btoa(`${sec0.username}:${sec0.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/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(createApiCustomersResponse, await res.json()) };