import {z} from 'zod' export type CustomerStatus = 'ACTIVE' | 'INACTIVE' | 'BLOCKED'; export type CustomerValidationStatus = 'PENDING' | 'VALIDATED' | 'REJECTED'; export type GetApiV1ClientsClientIdCustomersArgs = {clientId: string, customer_status?: CustomerStatus | undefined, customer_alias?: string | undefined, name?: string | undefined, customer_validation_status?: CustomerValidationStatus | undefined, page?: number | undefined, per_page?: number | undefined}; export type GetApiV1ClientsClientIdCustomersOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const customerStatus = z.enum(["ACTIVE", "INACTIVE", "BLOCKED"]); export const customerValidationStatus = z.enum(["PENDING", "VALIDATED", "REJECTED"]); export const audit = z.object({createdAt: z.string().optional(), updatedAt: z.string().optional(), deletedAt: z.string().nullable().optional(), blockedAt: z.string().nullable().optional(), activatedAt: z.string().nullable().optional(), suspendedAt: z.string().nullable().optional()}); export const customerResponse = z.object({id: z.string(), clientId: z.string(), name: z.string(), rfc: z.string(), legalRepresentativeName: z.string(), legalRepresentativeRfc: z.string(), legalRepresentativePhone: z.string(), legalRepresentativeEmail: z.string(), website: z.string().optional(), domain: z.string().optional(), customerAlias: z.string().optional(), customerStatus: customerStatus.optional(), customerValidationStatus: customerValidationStatus.optional(), audit: audit.optional()}); export const customersResponse = z.object({currentPage: z.number().int().optional(), perPage: z.number().int().optional(), totalItems: z.number().int().optional(), data: z.array(customerResponse)}); 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 }; /** * List Business Units * * GET /v1/clients/{clientId}/customers */ export const getApiV1ClientsClientIdCustomers = async (args: GetApiV1ClientsClientIdCustomersArgs, options?: GetApiV1ClientsClientIdCustomersOptions) => { const {baseUrl = "https://apicore.stg.finch.lat", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.customer_status != null) query.append("customer_status", String(args.customer_status)) if (args.customer_alias != null) query.append("customer_alias", String(args.customer_alias)) if (args.name != null) query.append("name", String(args.name)) if (args.customer_validation_status != null) query.append("customer_validation_status", String(args.customer_validation_status)) if (args.page != null) query.append("page", String(args.page)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/clients/${encodeURIComponent(String(args.clientId))}/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(customersResponse, await res.json()) };