import {z} from 'zod' export type DictProxyKeyCreationSchemeData = {reason: 'USER_REQUESTED' | 'RECONCILIATION', tax_identification_number: string}; export type DictProxyKeyCreationDetails = {owner_type: 'individual' | 'legal_entity', proxy_type: 'phone' | 'email' | 'tax_id' | 'organisation' | 'random_alias', proxy_type_code?: ('cpf' | 'cnpj' | 'abn' | 'teli' | 'emal' | 'aubn' | 'orgn' | 'evp') | undefined, proxy_identification: string, owner_name: string, account_identification: string, account_type?: ('CACC' | 'SVGS' | 'SLRY' | 'TRAN') | undefined, account_issuer?: string | undefined, servicer: string, connected_account_id: string, internal_account_id?: string | undefined, scheme_data: DictProxyKeyCreationSchemeData, metadata?: {[key: string]: unknown} | undefined}; export type CreateApiProxyServicesDictProxyKeysArgs = {body: DictProxyKeyCreationDetails}; export type CreateApiProxyServicesDictProxyKeysOptions = {baseUrl?: string | undefined, sec0?: string | undefined, fetch?: typeof fetch | undefined}; export const dictProxyKeySchemeData = z.object({reason: z.string(), tax_identification_number: z.string(), directory_entry_id: z.string()}); export const dictProxyKey = z.object({id: z.string(), object: z.literal("proxy_key"), scheme: z.string(), owner_type: z.enum(["individual", "legal_entity"]), owner_name: z.string(), proxy_type: z.enum(["phone", "email", "tax_id", "organisation", "random_alias"]), proxy_type_code: z.enum(["cpf", "cnpj", "abn", "teli", "emal", "aubn", "orgn", "evp", "null"]).nullable(), proxy_identification: z.string(), status: z.enum(["pending_registration", "registration_failed", "active", "disabled"]), status_details: z.string().nullable(), status_reason: z.enum(["released_via_claim", "null"]).nullable(), account_identification: z.string(), account_type: z.enum(["CACC", "SVGS", "SLRY", "TRAN", "null"]).nullable(), account_issuer: z.string().nullable(), connected_account_id: z.string(), internal_account_id: z.string().nullable(), servicer: z.string(), scheme_data: dictProxyKeySchemeData, metadata: z.record(z.string(), z.unknown()), created_at: z.string(), disabled_at: z.string().nullable(), enabled_at: z.string().nullable()}); 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 }; /** * Create DICT proxy key * * POST /proxy_services/dict/proxy_keys */ export const createApiProxyServicesDictProxyKeys = async (args: CreateApiProxyServicesDictProxyKeysArgs, options?: CreateApiProxyServicesDictProxyKeysOptions) => { const {baseUrl = "https://sandbox.numeral.io/v1", fetch: fetchFn = fetch, sec0} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (sec0 !== undefined) headers.set("x-api-key", sec0) const url = `${baseUrl.replace(/\/+$/, '')}/proxy_services/dict/proxy_keys${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(dictProxyKey, await res.json()) };