import {z} from 'zod' export type FiscalEntityRequestAddress = {street1: string, street2?: string | undefined, postal_code: string, city: string, state?: string | undefined, country?: string | undefined, external_number?: string | undefined}; export type FiscalEntityRequest = {address: FiscalEntityRequestAddress, tax_id?: string | undefined, email?: string | undefined, phone?: string | undefined, metadata?: {[key: string]: Record} | undefined, company_name?: string | undefined}; export type CreateApiCustomersIdFiscalEntitiesArgs = {id: string, 'Accept-Language'?: ('es' | 'en') | undefined, 'X-Child-Company-Id'?: string | undefined, body: FiscalEntityRequest}; export type CreateApiCustomersIdFiscalEntitiesOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const fiscalEntityRequestAddress = z.object({street1: z.string(), street2: z.string().optional(), postal_code: z.string(), city: z.string(), state: z.string().optional(), country: z.string().optional(), external_number: z.string().optional()}); export const createCustomerFiscalEntitiesResponse = z.object({address: fiscalEntityRequestAddress, tax_id: z.string().optional(), email: z.string().optional(), phone: z.string().optional(), metadata: z.record(z.string(), z.object({})).optional(), company_name: z.string().optional(), id: z.string(), object: z.string(), created_at: z.number().int(), parent_id: z.string().optional(), default: z.boolean().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 }; /** * Create Fiscal Entity * * POST /customers/{id}/fiscal_entities */ export const createApiCustomersIdFiscalEntities = async (args: CreateApiCustomersIdFiscalEntitiesArgs, options?: CreateApiCustomersIdFiscalEntitiesOptions) => { const {baseUrl = "https://api.conekta.io", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/vnd.conekta-v2.3.0+json") if (args['Accept-Language'] != null) headers.set("Accept-Language", String(args['Accept-Language'])) if (args['X-Child-Company-Id'] != null) headers.set("X-Child-Company-Id", String(args['X-Child-Company-Id'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/customers/${encodeURIComponent(String(args.id))}/fiscal_entities${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(createCustomerFiscalEntitiesResponse, await res.json()) };