import {z} from 'zod' export type BackupCodeTwoFaAccountConfig = {useByDefault?: boolean | undefined, providerType: string, codes: Array, codesLeft?: number | undefined}; export type EmailTwoFaAccountConfig = {useByDefault?: boolean | undefined, providerType: string, email: string}; export type SmsTwoFaAccountConfig = {useByDefault?: boolean | undefined, providerType: string, phoneNumber: string}; export type TotpTwoFaAccountConfig = {useByDefault?: boolean | undefined, providerType: string, authUrl: string}; export type CreateApiApi2faAccountConfigArgs = {verificationCode?: string | undefined, body: BackupCodeTwoFaAccountConfig | EmailTwoFaAccountConfig | SmsTwoFaAccountConfig | TotpTwoFaAccountConfig}; export type CreateApiApi2faAccountConfigOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const backupCodeTwoFaAccountConfig = z.object({useByDefault: z.boolean().optional(), providerType: z.string(), codes: z.array(z.string()), codesLeft: z.number().int().optional()}); export const emailTwoFaAccountConfig = z.object({useByDefault: z.boolean().optional(), providerType: z.string(), email: z.string().min(1)}); export const smsTwoFaAccountConfig = z.object({useByDefault: z.boolean().optional(), providerType: z.string(), phoneNumber: z.string().min(1)}); export const totpTwoFaAccountConfig = z.object({useByDefault: z.boolean().optional(), providerType: z.string(), authUrl: z.string().min(1)}); export const accountTwoFaSettings = z.object({configs: z.record(z.string(), z.union([backupCodeTwoFaAccountConfig, emailTwoFaAccountConfig, smsTwoFaAccountConfig, totpTwoFaAccountConfig])).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 }; /** * Verify and save 2FA account config (verifyAndSaveTwoFaAccountConfig) * * POST /api/2fa/account/config */ export const createApiApi2faAccountConfig = async (args: CreateApiApi2faAccountConfigArgs, options?: CreateApiApi2faAccountConfigOptions) => { const {baseUrl = "https://demo.thingsboard.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.verificationCode != null) query.append("verificationCode", String(args.verificationCode)) const url = `${baseUrl.replace(/\/+$/, '')}/api/2fa/account/config${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(accountTwoFaSettings, await res.json()) };