import {z} from 'zod' export type MultiDomainLoginSchema = {app_name?: string | undefined, email: string, marketplace_signup_nonce?: string | undefined, otp?: (string | null) | undefined, otp_type?: (string | null) | undefined, system_domain_id: string}; export type CreateApiV1AuthMultidomainLoginArgs = {'Temp-Auth-Token': string, grant_type?: string | undefined, body: MultiDomainLoginSchema}; export type CreateApiV1AuthMultidomainLoginOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const multiDomainUserSystemSchema = z.object({logo_url: z.string().optional(), mfa_methods: z.array(z.enum(["TOTP", "MAIL_2SV"])).optional(), mfa_methods_configured: z.array(z.enum(["TOTP", "MAIL_2SV"])).optional(), mfa_required: z.boolean().optional(), mfa_required_configured: z.boolean().optional(), platform_url: z.string().optional(), system_domain_id: z.string(), system_domain_name: z.string().optional(), url: z.string().optional()}); export const tokenSchema = z.object({app_id: z.string().optional(), auth_system_domains: z.array(multiDomainUserSystemSchema).optional(), date_created: z.string().optional(), date_modified: z.string().optional(), expires: z.string().optional(), id: z.string().optional(), is_admin: z.boolean().optional(), is_mfa_authenticated: z.boolean().optional(), is_saml_authenticated: z.boolean().optional(), is_super_admin: z.boolean().optional(), is_super_admin_light: z.boolean().optional(), system_domain_id: z.string().optional(), system_domain_is_plg: z.boolean().optional(), system_domain_status: z.enum(["ACTIVE", "WARNING", "FROZEN", "DEACTIVATED"]).optional(), system_domain_type: z.string().optional(), system_domain_warning_message: z.string().optional(), system_domains: z.array(z.string()).optional(), token: z.string(), user_id: z.string().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 }; /** * Login by using temp token * * POST /v1/auth/multidomain/login/ */ export const createApiV1AuthMultidomainLogin = async (args: CreateApiV1AuthMultidomainLoginArgs, options: CreateApiV1AuthMultidomainLoginOptions) => { const {baseUrl, 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['Temp-Auth-Token'] != null) headers.set("Temp-Auth-Token", String(args['Temp-Auth-Token'])) if (args.grant_type != null) query.append("grant_type", String(args.grant_type)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/auth/multidomain/login/${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(tokenSchema, await res.json()) };