import {z} from 'zod' export type GetApiKybFormsIdArgs = {id: string}; export type GetApiKybFormsIdOptions = {baseUrl?: string | undefined, kybTokenAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const countryEnum = z.enum(["AF", "AX", "AL", "DZ", "AS", "AD", "AO", "AI", "AQ", "AG", "AR", "AM", "AW", "AU", "AT", "AZ", "BS", "BH", "BD", "BB", "BY", "BE", "BZ", "BJ", "BM", "BT", "BO", "BQ", "BA", "BW", "BV", "BR", "IO", "BN", "BG", "BF", "BI", "CV", "KH", "CM", "CA", "KY", "CF", "TD", "CL", "CN", "CX", "CC", "CO", "KM", "CG", "CK", "CR", "CI", "HR", "CU", "CW", "CY", "CZ", "CD", "DK", "DJ", "DM", "DO", "EC", "EG", "SV", "GQ", "ER", "EE", "SZ", "ET", "FK", "FO", "FJ", "FI", "FR", "GF", "PF", "TF", "GA", "GM", "GE", "DE", "GH", "GI", "GR", "GL", "GD", "GP", "GU", "GT", "GG", "GN", "GW", "GY", "HT", "HM", "HN", "HK", "HU", "IS", "IN", "ID", "IR", "IQ", "IE", "IM", "IL", "IT", "JM", "JP", "JE", "JO", "KZ", "KE", "KI", "XK", "KW", "KG", "LA", "LV", "LB", "LS", "LR", "LY", "LI", "LT", "LU", "MO", "MG", "MW", "MY", "MV", "ML", "MT", "MH", "MQ", "MR", "MU", "YT", "MX", "FM", "MD", "MC", "MN", "ME", "MS", "MA", "MZ", "MM", "NA", "NR", "NP", "NL", "NC", "NZ", "NI", "NE", "NG", "NU", "NF", "KP", "MK", "MP", "NO", "OM", "PK", "PW", "PS", "PA", "PG", "PY", "PE", "PH", "PN", "PL", "PT", "PR", "QA", "RE", "RO", "RU", "RW", "BL", "SH", "KN", "LC", "MF", "PM", "VC", "WS", "SM", "ST", "SA", "SN", "RS", "SC", "SL", "SG", "SX", "SK", "SI", "SB", "SO", "ZA", "GS", "KR", "SS", "ES", "LK", "SD", "SR", "SJ", "SE", "CH", "SY", "TW", "TJ", "TZ", "TH", "TL", "TG", "TK", "TO", "TT", "TN", "TR", "TM", "TC", "TV", "UG", "UA", "AE", "GB", "UM", "US", "UY", "UZ", "VU", "VA", "VE", "VN", "VG", "VI", "WF", "EH", "YE", "ZM", "ZW"]); export const customFieldTypeEnum = z.enum(["CHECKBOX", "COUNTRY", "COUNTRY_MULTI", "DATE", "INTEGER", "SELECT", "SELECT_MULTI", "TEXT"]); export const additionalFieldValue = z.object({title: z.string(), type: customFieldTypeEnum, value: z.unknown()}); export const companyInfo = z.object({companyName: z.string().max(255), registrationNumber: z.string().max(100).nullable().optional(), country: countryEnum, region: z.string().max(2).nullable().optional(), type: z.string().max(100).nullable().optional(), phone: z.string().max(128).nullable().optional(), website: z.string().max(200).nullable().optional(), email: z.string().max(254).nullable().optional(), brandNames: z.array(z.string().max(100)).optional(), activityCode: z.string().max(8).nullable().optional(), tin: z.string().max(32).nullable().optional(), operatingAddress: z.string().max(255).nullable().optional(), postalAddress: z.string().max(255).nullable().optional(), postcode: z.string().max(20).nullable().optional(), street: z.string().max(100).nullable().optional(), city: z.string().max(100).nullable().optional(), additionalInfo: z.record(z.string(), additionalFieldValue).optional()}); export const kybDocumentTypeEnum = z.enum(["INCORPORATION_CERT", "DIRECTOR_CERT", "SHAREHOLDER_CERT", "UBO_CERT", "INCORPORATION_ARTICLES", "SHAREHOLDER_REGISTRY", "DIRECTORS_REGISTRY", "STATE_REGISTRY", "UBO_REGISTRY", "OWNERSHIP_STRUCTURE", "INCUMBENCY_CERT", "TRUST_AGREEMENT", "INFORMATION_STATEMENT", "GOOD_STANDING_CERT", "PROOF_OF_DOMAIN", "PROOF_OF_ADDRESS", "SOURCE_OF_FUNDS", "BANK_STATEMENT", "INVOICE", "ONLINE_PRESENCE", "DIRECTOR_ONLINE_PRESENCE", "SHAREHOLDER_ONLINE_PRESENCE", "WEBSITE_OVERVIEW", "E_SIGNATURE_CERT", "ANNUAL_REPORT", "NAME_CHANGE_PROOF", "TRADING_STATUS_PROOF", "ORG_CHART", "COMPANY_ADVERSE_MEDIA", "INDIVIDUAL_ADVERSE_MEDIA", "EMAIL_CHECK", "SOURCE_OF_WEALTH", "OTHER"]); export const kybFormDocument = z.object({id: z.string(), type: kybDocumentTypeEnum, filename: z.string().max(100).nullable().optional(), file: z.string()}); export const beneficiaryTypeEnum = z.enum(["CEO", "REPRESENTATIVE", "SHAREHOLDER", "UBO", "ABO"]); export const applicantTypeEnum = z.enum(["INDIVIDUAL", "COMPANY"]); export const checkboxFieldValue = z.object({title: z.string(), type: customFieldTypeEnum, value: z.boolean().nullable().optional()}); export const countryFieldValue = z.object({title: z.string(), type: customFieldTypeEnum, value: countryEnum.nullable().optional()}); export const countryMultiFieldValue = z.object({title: z.string(), type: customFieldTypeEnum, value: z.array(countryEnum).nullable().optional()}); export const dateFieldValue = z.object({title: z.string(), type: customFieldTypeEnum, value: z.string().nullable().optional()}); export const integerFieldValue = z.object({title: z.string(), type: customFieldTypeEnum, value: z.number().int().nullable().optional()}); export const selectFieldValue = z.object({title: z.string(), type: customFieldTypeEnum, value: z.string().max(32).nullable().optional()}); export const selectMultiFieldValue = z.object({title: z.string(), type: customFieldTypeEnum, value: z.array(z.string().max(32)).nullable().optional()}); export const textFieldValue = z.object({title: z.string(), type: customFieldTypeEnum, value: z.string().max(100).nullable().optional()}); export const customFieldValue = z.discriminatedUnion("type", [checkboxFieldValue, countryFieldValue, countryMultiFieldValue, dateFieldValue, integerFieldValue, selectFieldValue, selectMultiFieldValue, textFieldValue]); export const individualApplicant = z.object({infoType: applicantTypeEnum, name: z.string().max(100), surname: z.string().max(100), email: z.string().max(254).nullable().optional(), phone: z.string().max(128).nullable().optional(), dateOfBirth: z.string().nullable().optional(), country: countryEnum.nullable().optional(), nationality: countryEnum.nullable().optional(), documentNumber: z.string().max(50).nullable().optional(), personalNumber: z.string().max(50).nullable().optional(), citizenship: countryEnum.nullable().optional(), countryOfBirth: countryEnum.nullable().optional(), residentialAddress: z.string().max(255).nullable().optional(), countryOfResidence: countryEnum.nullable().optional(), postcode: z.string().max(20).nullable().optional(), address: z.string().max(255).nullable().optional(), street: z.string().max(100).nullable().optional(), city: z.string().max(100).nullable().optional(), tin: z.string().max(20).nullable().optional(), taxResidence: countryEnum.nullable().optional(), selfDeclaredPep: z.boolean().nullable().optional(), additionalInfo: z.record(z.string(), customFieldValue).optional()}); export const companyApplicant = z.object({infoType: applicantTypeEnum, companyName: z.string().max(255), registrationNumber: z.string().max(100).nullable().optional(), country: countryEnum, region: z.string().max(2).nullable().optional(), type: z.string().max(100).nullable().optional(), phone: z.string().max(128).nullable().optional(), website: z.string().max(200).nullable().optional(), email: z.string().max(254).nullable().optional(), brandNames: z.array(z.string().max(100)).optional(), activityCode: z.string().max(8).nullable().optional(), tin: z.string().max(32).nullable().optional(), operatingAddress: z.string().max(255).nullable().optional(), postalAddress: z.string().max(255).nullable().optional(), postcode: z.string().max(20).nullable().optional(), street: z.string().max(100).nullable().optional(), city: z.string().max(100).nullable().optional(), additionalInfo: z.record(z.string(), customFieldValue).optional()}); export const applicant = z.discriminatedUnion("infoType", [individualApplicant, companyApplicant]); export const beneficiary = z.object({id: z.string(), beneficiaryType: beneficiaryTypeEnum.optional(), beneficiaryTypes: z.array(beneficiaryTypeEnum).optional(), info: applicant, positions: z.array(z.string().max(50)).optional(), formFiller: z.boolean().optional(), ownershipPercentage: z.number().gte(0).lte(100).nullable().optional(), parent: z.string().nullable()}); export const clientSystem = z.object({ipAddress: z.string().nullable().optional(), timeZone: z.string().nullable().optional()}); export const kybStatusEnum = z.enum(["PENDING", "PROCESSING", "NEED_TO_PROCESS", "NEED_TO_REVIEW", "COMPLETED"]); export const approvalStatusEnum = z.enum(["APPROVED", "DENIED"]); export const kybFormMeta = z.object({status: kybStatusEnum.optional(), approvalStatus: approvalStatusEnum.nullable().optional(), submissionTime: z.string().nullable().optional(), soleProprietor: z.boolean().optional()}); export const kybForm = z.object({id: z.string(), company: companyInfo, documents: z.array(kybFormDocument).optional(), beneficiaries: z.array(beneficiary).optional(), clientSystem: clientSystem.optional(), meta: kybFormMeta}); 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 KYB form info * * GET /kyb/forms/{id}/ * * Also accepts auth: apiKeyPair */ export const getApiKybFormsId = async (args: GetApiKybFormsIdArgs, options?: GetApiKybFormsIdOptions) => { const {baseUrl = "https://ivs.idenfy.com", fetch: fetchFn = fetch, kybTokenAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (kybTokenAuth !== undefined) headers.set('authorization', `Bearer ${kybTokenAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/kyb/forms/${encodeURIComponent(String(args.id))}/${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(kybForm, await res.json()) };