import {z} from 'zod' export type CreateApiTenantIdTransactionInitializeArgs = {apiKey: string, tenant_id: string, body: {user_id: string, transaction_id: string, transaction_data: Record}}; export type CreateApiTenantIdTransactionInitializeOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const credentialDescriptorEntity = z.object({type: z.literal("public-key"), id: z.string(), transports: z.array(z.enum(["ble", "hybrid", "internal", "nfc", "usb"])).optional()}); export const createApiTenantIdTransactionInitializeResponse = z.object({publicKey: z.object({challenge: z.string(), timeout: z.number().int().optional(), rpId: z.string().optional(), allowCredentials: z.array(credentialDescriptorEntity).optional(), userVerification: z.enum(["discouraged", "preferred", "required"]).optional(), extensions: z.object({appid: z.boolean().optional(), appidExclude: z.boolean().optional(), credProps: z.object({rk: z.boolean().optional()}).optional()}).optional()}).optional(), mediation: z.enum(["optional", "required", "silent"]).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 }; /** * Initialize a transaction * * POST /{tenant_id}/transaction/initialize */ export const createApiTenantIdTransactionInitialize = async (args: CreateApiTenantIdTransactionInitializeArgs, options?: CreateApiTenantIdTransactionInitializeOptions) => { const {baseUrl = "https://passkeys.hanko.io", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.apiKey != null) headers.set("apiKey", String(args.apiKey)) const url = `${baseUrl.replace(/\/+$/, '')}/${encodeURIComponent(String(args.tenant_id))}/transaction/initialize${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(createApiTenantIdTransactionInitializeResponse, await res.json()) };