import {z} from 'zod' export type CustomerObject = {id?: number | undefined, external_code?: (string | null) | undefined, cnpj_cpf?: string | undefined, person_name: string, contact_person?: (string | null) | undefined, nickname?: (string | null) | undefined, birthday?: (string | null) | undefined, email?: string | undefined, email_cc?: string | undefined, address: string, address_complement?: (string | null) | undefined, address_number?: string | undefined, neighborhood: string, zipcode: string, state?: string | undefined, city_name: string, phone_number?: (string | null) | undefined, mobile_local_code?: (string | null) | undefined, mobile_number?: (string | null) | undefined, ignore_email?: (boolean | null) | undefined, ignore_sms?: (boolean | null) | undefined, ignore_whatsapp?: (boolean | null) | undefined, notes?: (string | null) | undefined, tags?: (Array | null) | undefined, tag_list?: (string | null) | undefined, custom_data?: (Record | null) | undefined, created_via_api?: (boolean | null) | undefined, created_at?: (string | null) | undefined, updated_at?: (string | null) | undefined}; export type CreateApiV1CustomersArgs = {'User-Agent'?: string | undefined, 'X-Idempotency-Key'?: string | undefined, body: CustomerObject}; export type CreateApiV1CustomersOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const customerObject = z.object({id: z.number().int().optional(), external_code: z.string().max(255).nullable().optional(), cnpj_cpf: z.string().max(20).optional(), person_name: z.string().max(120), contact_person: z.string().max(120).nullable().optional(), nickname: z.string().max(255).nullable().optional(), birthday: z.string().nullable().optional(), email: z.string().max(80).optional(), email_cc: z.string().max(80).optional(), address: z.string().max(255), address_complement: z.string().max(120).nullable().optional(), address_number: z.string().max(10).optional(), neighborhood: z.string().max(80), zipcode: z.string().max(8), state: z.string().max(2).optional(), city_name: z.string().max(60), phone_number: z.string().max(15).nullable().optional(), mobile_local_code: z.string().max(2).nullable().optional(), mobile_number: z.string().max(9).nullable().optional(), ignore_email: z.boolean().nullable().optional(), ignore_sms: z.boolean().nullable().optional(), ignore_whatsapp: z.boolean().nullable().optional(), notes: z.string().nullable().optional(), tags: z.array(z.string()).nullable().optional(), tag_list: z.string().nullable().optional(), custom_data: z.object({}).nullable().optional(), created_via_api: z.boolean().nullable().optional(), created_at: z.string().nullable().optional(), updated_at: z.string().nullable().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 um Cliente * * POST /v1/customers */ export const createApiV1Customers = async (args: CreateApiV1CustomersArgs, options?: CreateApiV1CustomersOptions) => { const {baseUrl = "https://api-sandbox.kobana.com.br", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['User-Agent'] != null) headers.set("User-Agent", String(args['User-Agent'])) if (args['X-Idempotency-Key'] != null) headers.set("X-Idempotency-Key", String(args['X-Idempotency-Key'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/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(customerObject, await res.json()) };