import {z} from 'zod' export type ModelsClientData = {remoteAddress?: string | undefined, userAgent?: string | undefined}; export type ModelsAuthenticatorSelectionCriteria = {authenticatorAttachment?: ('platform' | 'cross-platform') | undefined, requireResidentKey?: boolean | undefined, userVerification?: ('required' | 'preferred' | 'discouraged') | undefined}; export type ModelsCreateRequest = {operation: 'AUTH' | 'DEREG' | 'REG', username?: string | undefined, userId?: string | undefined, displayName?: string | undefined, clientData?: ModelsClientData | undefined, transaction?: string | undefined, deviceIds?: Array | undefined, authenticatorSelectionCriteria?: ModelsAuthenticatorSelectionCriteria | undefined, attestationConveyancePreference?: ('direct' | 'indirect' | 'none') | undefined, isSecondFactorOnly?: boolean | undefined}; export type CreateApiV1UafRequestsArgs = {body: ModelsCreateRequest}; export type CreateApiV1UafRequestsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const modelsRelyingParty = z.object({id: z.string(), name: z.string(), appId: z.string(), icon: z.string()}); export const modelsClientData = z.object({remoteAddress: z.string().optional(), userAgent: z.string().optional()}); export const modelsLocation = z.object({country: z.string(), region: z.string(), city: z.string()}); export const modelsLink = z.object({rel: z.string(), method: z.string(), href: z.string()}); export const modelsRequestDevice = z.object({deviceId: z.string().optional(), keyName: z.string().optional()}); export const modelsRequestError = z.object({code: z.number().int(), reason: z.string().optional()}); export const modelsHankoRequest = z.object({id: z.string(), operation: z.enum(["AUTH", "DEREG", "REG"]), username: z.string(), userId: z.string(), status: z.enum(["PENDING", "OK", "FAILED", "INVALIDATED"]), createdAt: z.string(), validUntil: z.string(), relyingParty: modelsRelyingParty, clientData: modelsClientData.optional(), location: modelsLocation.optional(), links: z.array(modelsLink), transaction: z.string().optional(), request: z.string(), deviceID: z.string(), device: modelsRequestDevice.optional(), isSecondFactorOnly: z.boolean().optional(), error: modelsRequestError.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 }; /** * Create a request for the Hanko Authenticator * * POST /v1/uaf/requests */ export const createApiV1UafRequests = async (args: CreateApiV1UafRequestsArgs, options?: CreateApiV1UafRequestsOptions) => { const {baseUrl = "https://.hanko.io/admin", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v1/uaf/requests${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(modelsHankoRequest, await res.json()) };