import {z} from 'zod' export type GetApiV1AuthTokensArgs = {'App-ID': string, 'Auth-Token': string, per_page?: number | undefined, last_id?: string | undefined}; export type GetApiV1AuthTokensOptions = {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 tokenOutputSchema = 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(), user_id: z.string().optional()}); export const tokensSchema = z.object({first_url: z.string().optional(), last_url: z.string().optional(), next_url: z.string().optional(), objects: z.array(tokenOutputSchema).optional(), page: z.number().int().gte(-2147483648).lte(2147483647).optional(), pages: z.number().int().gte(-2147483648).lte(2147483647).optional(), per_page: z.number().int().gte(-2147483648).lte(2147483647).optional(), prev_url: z.string().optional(), total: z.number().int().gte(-9223372036854776000).lte(9223372036854776000).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 }; /** * List of tokens * * GET /v1/auth/tokens/ */ export const getApiV1AuthTokens = async (args: GetApiV1AuthTokensArgs, options: GetApiV1AuthTokensOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['App-ID'] != null) headers.set("App-ID", String(args['App-ID'])) if (args['Auth-Token'] != null) headers.set("Auth-Token", String(args['Auth-Token'])) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (args.last_id != null) query.append("last_id", String(args.last_id)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/auth/tokens/${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(tokensSchema, await res.json()) };