import {z} from 'zod' export type GetApiV1CustomersArgs = {'User-Agent'?: string | undefined}; export type GetApiV1CustomersOptions = {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 getApiV1CustomersResponse = z.array(customerObject); 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 }; /** * Listar Clientes * * GET /v1/customers */ export const getApiV1Customers = async (args: GetApiV1CustomersArgs, options?: GetApiV1CustomersOptions) => { const {baseUrl = "https://api-sandbox.kobana.com.br", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['User-Agent'] != null) headers.set("User-Agent", String(args['User-Agent'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/customers${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiV1CustomersResponse, await res.json()) };