import {z} from 'zod' export type GetApiPartnerCustomersArgs = {'api-key': string, 'auth-key': string, limit?: string | undefined, skip?: string | undefined, from?: string | undefined}; export type GetApiPartnerCustomersOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiPartnerCustomersResponse = z.object({status: z.number().int().optional(), doc: z.array(z.object({custId: z.string().optional(), firstName: z.string().optional(), lastName: z.string().optional(), phone: z.string().optional(), username: z.string().optional(), email: z.string().optional(), idNo: z.string().optional(), riskTolerance: z.string().optional(), networthTotal: z.string().optional(), networthLiquid: z.string().optional(), annualIncome: z.string().optional(), investmentExperience: z.string().optional(), investmentObjectives: z.string().optional(), politicallyExposedNames: z.string().optional(), directorOf: z.string().optional(), employmentStatus: z.string().optional(), employerIsBroker: z.string().optional(), dob: z.string().optional(), zipPostalCode: z.string().optional(), countryID: z.string().optional(), stateProvince: z.string().optional(), city: z.string().optional(), addressLine2: z.string().optional(), addressLine1: z.string().optional(), createdAt: z.string().optional(), accountApprovedDate: z.string().optional(), kycCreated: z.string().optional(), partnerToken: z.string().optional(), plan: z.string().optional(), referralCode: z.string().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 }; /** * List Customers * * GET /partner/customers */ export const getApiPartnerCustomers = async (args: GetApiPartnerCustomersArgs, options?: GetApiPartnerCustomersOptions) => { const {baseUrl = "https://uatclientapi-v2.stockal.com/client/v2", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['api-key'] != null) headers.set("api-key", String(args['api-key'])) if (args['auth-key'] != null) headers.set("auth-key", String(args['auth-key'])) if (args.limit != null) query.append("limit", String(args.limit)) if (args.skip != null) query.append("skip", String(args.skip)) if (args.from != null) query.append("from", String(args.from)) const url = `${baseUrl.replace(/\/+$/, '')}/partner/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(getApiPartnerCustomersResponse, await res.json()) };