import {z} from 'zod' export type VeriffIngestionProcess = 'profile' | 'address' | 'identity' | 'proof-of-address'; export type VeriffIntegrationConfigForRequest = {name: string, apiKey?: string | undefined, sharedSecretKey?: string | undefined, processes: Array}; export type UpdateApiKycConnectorVeriffConfigArgs = {'X-On-Behalf-Of'?: string | undefined, body: {integrations: Array}}; export type UpdateApiKycConnectorVeriffConfigOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const veriffIngestionProcess = z.enum(["profile", "address", "identity", "proof-of-address"]); export const veriffIntegrationConfig = z.object({name: z.string(), apiKey: z.string().optional(), sharedSecretKey: z.string().optional(), processes: z.array(veriffIngestionProcess)}); export const veriffProviderConfig = z.object({integrations: z.array(veriffIntegrationConfig), createdAt: z.string(), updatedAt: z.string()}); export const updateApiKycConnectorVeriffConfigResponse = z.object({config: veriffProviderConfig}); 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 }; /** * Set Veriff config * * PUT /kyc-connector/veriff/config */ export const updateApiKycConnectorVeriffConfig = async (args: UpdateApiKycConnectorVeriffConfigArgs, options?: UpdateApiKycConnectorVeriffConfigOptions) => { const {baseUrl = "https://api.enterprise.sandbox.uphold.com", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['X-On-Behalf-Of'] != null) headers.set("X-On-Behalf-Of", String(args['X-On-Behalf-Of'])) if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/kyc-connector/veriff/config${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(updateApiKycConnectorVeriffConfigResponse, await res.json()) };