import {z} from 'zod' export type GetApiKybFormsCompanyIdQuestionnairesArgs = {companyId: string}; export type GetApiKybFormsCompanyIdQuestionnairesOptions = {baseUrl?: string | undefined, kybTokenAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const questionnaireTypeEnum = z.enum(["KYC", "KYB"]); export const choice = z.object({key: z.string().max(32), title: z.string().max(512)}); export const typeB9cEnum = z.enum(["CHECKBOX", "COLOR", "COUNTRY", "COUNTRY_MULTI", "DATE", "DATETIME", "EMAIL", "FILE", "FILE_MULTI", "FLOAT", "LIST", "INTEGER", "PASSWORD", "RADIO", "SELECT", "SELECT_MULTI", "TEL", "TEXT", "TEXT_AREA", "TIME", "URL"]); export const question = z.object({key: z.string().max(32), title: z.string().max(512), description: z.string().max(512).nullable().optional(), choices: z.array(choice).optional(), type: typeB9cEnum, required: z.boolean().optional(), placeholder: z.string().max(100).nullable().optional(), condition: z.string().nullable().optional()}); export const section = z.object({key: z.string().max(32), title: z.string().max(512), description: z.string().max(512).nullable().optional(), condition: z.string().nullable().optional(), questions: z.array(question)}); export const questionnaire = z.object({id: z.string(), key: z.string().max(32), name: z.string().max(32), title: z.string().max(512), description: z.string().max(512).nullable().optional(), type: questionnaireTypeEnum.optional(), sections: z.array(section)}); export const getApiKybFormsCompanyIdQuestionnairesResponse = z.array(questionnaire); 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 questionnaires * * GET /kyb/forms/{companyId}/questionnaires/ * * Also accepts auth: apiKeyPair */ export const getApiKybFormsCompanyIdQuestionnaires = async (args: GetApiKybFormsCompanyIdQuestionnairesArgs, options?: GetApiKybFormsCompanyIdQuestionnairesOptions) => { 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.companyId))}/questionnaires/${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(getApiKybFormsCompanyIdQuestionnairesResponse, await res.json()) };