import {z} from 'zod' export type CreateApiV1RegistrationsVerifyEmailHashArgs = {email_hash: string, app_name?: string | undefined}; export type CreateApiV1RegistrationsVerifyEmailHashOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const systemDomainFromTemplateSchema = z.object({admin_email: z.string().optional(), admin_first_name: z.string().optional(), admin_id: z.string().optional(), admin_last_name: z.string().optional(), admin_password: z.string().optional(), base_url: z.string().optional(), billing_tier: z.enum(["PAYGO", "PRO", "ENTERPRISE", "ENTERPRISE_LEGACY_2025", "PRO_LEGACY_2025"]).optional(), custom_terms: z.boolean().optional(), date_created: z.string().optional(), date_modified: z.string().optional(), description: z.string().optional(), disable_ai_features: z.boolean().optional(), id: z.string().optional(), name: z.string(), status: z.enum(["ACTIVE", "WARNING", "FROZEN", "DEACTIVATED"]).optional(), type: z.enum(["TRIAL", "CUSTOMER", "PARTNER", "INTERNAL"]).optional()}); export const autoLoginSchema = z.object({app_id: z.string(), token: z.string()}); export const verificationResponseSchema = z.object({auto_login: z.boolean().optional(), domain_data: systemDomainFromTemplateSchema.optional(), login_data: autoLoginSchema.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 email address, create system domain from template, and authenticate user * * POST /v1/registrations/verify/{email_hash}/ */ export const createApiV1RegistrationsVerifyEmailHash = async (args: CreateApiV1RegistrationsVerifyEmailHashArgs, options: CreateApiV1RegistrationsVerifyEmailHashOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.app_name != null) query.append("app_name", String(args.app_name)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/registrations/verify/${encodeURIComponent(String(args.email_hash))}/${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(verificationResponseSchema, await res.json()) };