import {z} from 'zod' export type CustomerStatus = 'ONBOARDING' | 'ACTIVE' | 'INACTIVE' | 'ONBOARDING_FAILED'; export type SortDirection = 'ASC' | 'DESC'; export type GetApiV1CustomersArgs = {customerStatus?: CustomerStatus | undefined, createDateStart?: string | undefined, createDateEnd?: string | undefined, referenceId?: string | undefined, after?: string | undefined, limit?: unknown | undefined, sortField?: ('customerStatus' | 'createdDate' | 'referenceId') | undefined, sortDirection?: SortDirection | undefined}; export type GetApiV1CustomersOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const customerStatus = z.enum(["ONBOARDING", "ACTIVE", "INACTIVE", "ONBOARDING_FAILED"]); export const onboardingStatus = z.enum(["PENDING_DOCUMENTATION_RECEIPT", "PROCESSING", "FAILED", "UNDER_REVIEW", "ONBOARDED", "OFFBOARDED"]); export const customerPreTradeRiskGroupNullable = z.enum(["LOW", "HIGH"]); export const accountStatus = z.enum(["ACTIVE", "INACTIVE", "CREATING"]); export const accountType = z.enum(["CREDIT", "CASH"]); export const accounts = z.array(z.object({accountNumber: z.string().optional(), accountName: z.string().nullable().optional(), accountId: z.string().optional(), accountStatus: accountStatus.optional(), accountType: accountType.optional()})); export const getApiV1CustomersResponse = z.object({after: z.string().nullable().optional(), totalCount: z.string().optional(), items: z.array(z.object({customerId: z.string().optional(), referenceId: z.string().optional(), customerStatus: customerStatus.optional(), customerType: z.enum(["RETAIL", "ENTERPRISE", "WEALTH"]).optional(), onboardingStatus: onboardingStatus.optional(), createDate: z.string().optional(), customerPreTradeRiskGroup: customerPreTradeRiskGroupNullable.optional(), accounts: accounts.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 }; /** * Get Customers * * GET /v1/customers */ export const getApiV1Customers = async (args: GetApiV1CustomersArgs, options?: GetApiV1CustomersOptions) => { const {baseUrl = "https://cams.aquanow.com/api", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.customerStatus != null) query.append("customerStatus", String(args.customerStatus)) if (args.createDateStart != null) query.append("createDateStart", String(args.createDateStart)) if (args.createDateEnd != null) query.append("createDateEnd", String(args.createDateEnd)) if (args.referenceId != null) query.append("referenceId", String(args.referenceId)) if (args.after != null) query.append("after", String(args.after)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.sortField != null) query.append("sortField", String(args.sortField)) if (args.sortDirection != null) query.append("sortDirection", String(args.sortDirection)) if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) 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()) };