import {z} from 'zod' export type CreateDocumentRequest = {document_type: 'bank_statement' | 'biometric_state_id' | 'driving_licence' | 'mortgage' | 'passport' | 'proof_of_business_activity' | 'proof_of_business_identity' | 'proof_of_directors' | 'proof_of_owners' | 'proof_of_source_of_funds' | 'residence_id' | 'state_id' | 'supporting' | 'unknown' | 'utility_bill' | 'visa', category: 'proof_of_address' | 'proof_of_business_activity' | 'proof_of_business_identity' | 'proof_of_directors' | 'proof_of_identity' | 'proof_of_owners' | 'proof_of_source_of_funds' | 'supporting'}; export type CreateApiV1FormsFormIdDocumentsArgs = {form_id: string, body: CreateDocumentRequest}; export type CreateApiV1FormsFormIdDocumentsOptions = {baseUrl?: string | undefined, AuthToken?: string | undefined, fetch?: typeof fetch | undefined}; export const document = z.object({id: z.string().optional(), document_type: z.string(), category: z.string(), created_at: z.string().optional(), updated_at: z.string().optional()}); export const createApiV1FormsFormIdDocumentsResponse = z.object({data: document.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 document for a form * * POST /v1/forms/{form_id}/documents */ export const createApiV1FormsFormIdDocuments = async (args: CreateApiV1FormsFormIdDocumentsArgs, options?: CreateApiV1FormsFormIdDocumentsOptions) => { const {baseUrl = "https://devapi.currencycloud.com/onboarding", fetch: fetchFn = fetch, AuthToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (AuthToken !== undefined) headers.set("X-Auth-Token", AuthToken) const url = `${baseUrl.replace(/\/+$/, '')}/v1/forms/${encodeURIComponent(String(args.form_id))}/documents${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(createApiV1FormsFormIdDocumentsResponse, await res.json()) };