import {z} from 'zod' export type GetApiV1FormsArgs = {order?: string | undefined, order_asc_desc?: string | undefined, application_status?: ('new' | 'submitted') | undefined, entity_type?: ('individual' | 'corporate') | undefined, page?: number | undefined, per_page?: number | undefined}; export type GetApiV1FormsOptions = {baseUrl?: string | undefined, AuthToken?: string | undefined, fetch?: typeof fetch | undefined}; export const businessInformation = z.object({id: z.string().optional(), form_id: z.string().optional(), document_ids: z.array(z.string()).optional(), business_country: z.string(), company_name: z.string(), trading_name: z.string().nullable().optional(), legal_entity_type: z.string(), sector: z.string(), company_registration_number: z.string().nullable().optional(), vat_number: z.string().nullable().optional(), incorporation_date: z.string(), business_website: z.string().nullable().optional(), business_activity_description: z.string(), registered_address: z.object({city: z.string(), street: z.string(), country: z.string(), postal_code: z.string(), apartment_number: z.string().nullable().optional(), building_name: z.string().nullable().optional(), building_number: z.string().nullable().optional(), state_or_province: z.string().nullable().optional()}), trading_address: z.object({city: z.string(), street: z.string(), country: z.string(), postal_code: z.string(), apartment_number: z.string().nullable().optional(), building_name: z.string().nullable().optional(), building_number: z.string().nullable().optional(), state_or_province: z.string().nullable().optional()}).optional(), created_at: z.string().optional(), updated_at: z.string().optional()}).nullable(); export const accountUsage = z.object({id: z.string().optional(), form_id: z.string().optional(), document_ids: z.array(z.string()).optional(), collections_account: z.boolean(), collection_currencies: z.array(z.string().nullable()).optional(), payment_currencies: z.array(z.string().nullable()).optional(), transaction_countries: z.array(z.string().nullable()).optional(), one_off_transaction: z.boolean(), estimated_monthly_transaction_currency: z.string().nullable().optional(), estimated_monthly_transaction_value: z.number().int().nullable().optional(), estimated_monthly_transaction_volume: z.number().int().nullable().optional(), source_of_funds: z.string().nullable().optional(), primary_purpose: z.string().nullable().optional(), purpose_details: z.string().nullable().optional(), created_at: z.string().optional(), updated_at: z.string().optional()}).nullable(); export const person = z.object({id: z.string(), form_id: z.string().optional(), document_ids: z.array(z.string()).optional(), roles: z.array(z.string()), first_name: z.string(), middle_name: z.string().nullable().optional(), last_name: z.string(), dob: z.string().nullable().optional(), phone_number: z.string().nullable().optional(), email: z.string().nullable().optional(), ownership_percentage: z.number().gte(0).lte(100).optional(), id_number: z.string().nullable().optional(), home_country: z.string(), passport_number: z.string().nullable().optional(), passport_country_of_issue: z.string().nullable().optional(), personal_address: z.object({city: z.string().optional(), street: z.string().optional(), country: z.string().optional(), postal_code: z.string().optional(), apartment_number: z.string().nullable().optional(), building_name: z.string().nullable().optional(), building_number: z.string().nullable().optional(), state_or_province: z.string().nullable().optional()}).optional(), created_at: z.string().optional(), updated_at: z.string().optional()}); export const formWithAssociations = z.object({id: z.string(), detached_document_ids: z.array(z.string()).optional(), affiliate_code: z.string().nullable().optional(), agreed_to_marketing_emails: z.boolean(), agreed_to_terms_and_conditions: z.boolean(), agreed_to_corporate_authority: z.boolean().optional(), country: z.string(), email: z.string().optional(), entity_type: z.string(), brand_name: z.string(), brand_key: z.string().optional(), referred_by: z.string().nullable().optional(), products_required: z.array(z.string()), state: z.string().optional(), phone_number: z.string().optional(), contact_name: z.string().optional(), created_at: z.string(), updated_at: z.string(), business_information: businessInformation.optional(), account_usage: accountUsage.optional(), people: z.array(person).optional()}); export const getApiV1FormsResponse = z.object({forms: z.array(formWithAssociations).optional(), pagination: z.object({total_entries: z.number().int().gte(0), total_pages: z.number().int().gte(0), current_page: z.number().int().gte(1), per_page: z.number().int().gte(1), previous_page: z.number().int().gte(1).nullable(), next_page: z.number().int().gte(1).nullable(), order: z.string(), order_asc_desc: z.string()}).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 }; /** * Retrieve all forms for a house account * * GET /v1/forms */ export const getApiV1Forms = async (args: GetApiV1FormsArgs, options?: GetApiV1FormsOptions) => { const {baseUrl = "https://devapi.currencycloud.com/onboarding", fetch: fetchFn = fetch, AuthToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.order != null) query.append("order", String(args.order)) if (args.order_asc_desc != null) query.append("order_asc_desc", String(args.order_asc_desc)) if (args.application_status != null) query.append("application_status", String(args.application_status)) if (args.entity_type != null) query.append("entity_type", String(args.entity_type)) if (args.page != null) query.append("page", String(args.page)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (AuthToken !== undefined) headers.set("X-Auth-Token", AuthToken) const url = `${baseUrl.replace(/\/+$/, '')}/v1/forms${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(getApiV1FormsResponse, await res.json()) };