import {z} from 'zod' export type GetApiApiV2EntitiesArgs = {reference_id?: (string | null) | undefined, page?: number | undefined, page_size?: number | undefined, limit?: number | undefined, order?: ('asc' | 'desc') | undefined, next?: (string | null) | undefined, previous?: (string | null) | undefined}; export type GetApiApiV2EntitiesOptions = {baseUrl?: string | undefined, ApiKeyId?: string | undefined, ApiSecretKey?: string | undefined, fetch?: typeof fetch | undefined}; export const entityType1 = z.enum(["INDIVIDUAL", "ORGANIZATION"]); export const entity = z.object({id: z.string(), is_kyc_complete: z.boolean(), entity_type: entityType1, name: z.string().nullable().optional(), nationality: z.string().nullable().optional(), reference_id: z.string().min(1).max(100).nullable().optional()}); export const baseOpenApiPaginationMetadata = z.object({previous: z.string().optional(), next: z.string().optional()}); export const paginatedEntityResponse = z.object({_sv: z.literal("PaginatedEntityResponse:v1").optional(), data: z.array(entity), pagination_metadata: baseOpenApiPaginationMetadata}); export const getApiApiV2EntitiesResponse = z.union([z.array(entity), paginatedEntityResponse]); 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 Entities * * GET /api/v2/entities/ */ export const getApiApiV2Entities = async (args: GetApiApiV2EntitiesArgs, options?: GetApiApiV2EntitiesOptions) => { const {baseUrl = "https://api-enterprise.sbt.dinari.com", fetch: fetchFn = fetch, ApiKeyId, ApiSecretKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.reference_id != null) query.append("reference_id", String(args.reference_id)) if (args.page != null) query.append("page", String(args.page)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.order != null) query.append("order", String(args.order)) if (args.next != null) query.append("next", String(args.next)) if (args.previous != null) query.append("previous", String(args.previous)) if (ApiKeyId !== undefined) headers.set("X-API-Key-Id", ApiKeyId) if (ApiSecretKey !== undefined) headers.set("X-API-Secret-Key", ApiSecretKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/entities/${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(getApiApiV2EntitiesResponse, await res.json()) };