import {z} from 'zod' export type Comment = {commentId?: string | undefined, text?: string | undefined}; export type IDVHostedUrlRequest = {applicationRef?: string | undefined, channelRef?: string | undefined, consent?: boolean | undefined, customerRef?: string | undefined, entityId?: string | undefined, errorRedirectURL?: string | undefined, oneSDKFlowId?: string | undefined, phoneCode?: string | undefined, phoneNumber?: string | undefined, recipientName?: string | undefined, sendSMS?: boolean | undefined, successRedirectURL?: string | undefined, comment?: Comment | undefined}; export type CreateApiV2IndividualsHostedurlArgs = {api_key: string, 'X-Frankie-CustomerID': string, 'X-Frankie-CustomerChildID'?: string | undefined, 'X-Frankie-Channel'?: string | undefined, body: IDVHostedUrlRequest}; export type CreateApiV2IndividualsHostedurlOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const requestID = z.string(); export const entityID = z.string(); export const iDVHostedUrlResponse = z.object({status: z.string().optional(), url: z.string().optional(), urlExpiry: z.string().optional(), requestId: requestID.optional(), entityId: entityID.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 }; /** * Provide hosted url for a hosted version of onesdk. * * POST /v2/individuals/hostedurl * * Also accepts auth: jwt */ export const createApiV2IndividualsHostedurl = async (args: CreateApiV2IndividualsHostedurlArgs, options?: CreateApiV2IndividualsHostedurlOptions) => { const {baseUrl = "https://api.uat.frankie.one", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.api_key != null) headers.set("api_key", String(args.api_key)) if (args['X-Frankie-CustomerID'] != null) headers.set("X-Frankie-CustomerID", String(args['X-Frankie-CustomerID'])) if (args['X-Frankie-CustomerChildID'] != null) headers.set("X-Frankie-CustomerChildID", String(args['X-Frankie-CustomerChildID'])) if (args['X-Frankie-Channel'] != null) headers.set("X-Frankie-Channel", String(args['X-Frankie-Channel'])) if (ApiKey !== undefined) headers.set("api_key", ApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v2/individuals/hostedurl${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(iDVHostedUrlResponse, await res.json()) };