import {z} from 'zod' export type GetApiKybFormsOptions = {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 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 kybFormList = z.object({id: z.string(), company: companyInfo, meta: kybFormMeta}); export const getApiKybFormsResponse = z.array(kybFormList); 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 }; /** * List KYB forms * * GET /kyb/forms/ */ export const getApiKybForms = async (options?: GetApiKybFormsOptions) => { 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/${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(getApiKybFormsResponse, await res.json()) };