import {z} from 'zod' export type CustomerType = 'COMPANY' | 'INDIVIDUAL'; export type CreateApiPlatformV1CustomersAgreementSessionsArgs = {body: {customerType: CustomerType, countryCode: string, useCase: 'STABLECOIN_PAYOUTS' | 'EMBEDDED_STABLECOIN_WALLETS' | 'EMBEDDED_FIAT_ACCOUNTS' | 'CARD_AGREEMENT', customerReference?: string | undefined}}; export type CreateApiPlatformV1CustomersAgreementSessionsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiPlatformV1CustomersAgreementSessionsResponse = z.object({reference: z.string().optional(), status: z.enum(["PENDING", "SIGNED", "DECLINED"]).optional(), customerType: z.string().optional(), useCase: z.string().optional(), countryCode: z.string().optional(), expiresOn: z.string().optional(), agreements: z.array(z.object({name: z.string().optional(), displayName: z.string().optional(), description: z.string().optional(), url: z.string().optional(), status: z.string().optional(), privacyPolicyName: z.string().optional(), privacyPolicyDescription: z.string().optional(), privacyPolicyUrl: 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 }; /** * Create agreement signing session * * POST /platform/v1/customers/agreement/sessions */ export const createApiPlatformV1CustomersAgreementSessions = async (args: CreateApiPlatformV1CustomersAgreementSessionsArgs, options?: CreateApiPlatformV1CustomersAgreementSessionsOptions) => { const {baseUrl = "https://api.sandbox.bvnk.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/platform/v1/customers/agreement/sessions${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(createApiPlatformV1CustomersAgreementSessionsResponse, await res.json()) };