import {z} from 'zod' export type GetApiV2AuthorizeUserinfoOptions = {baseUrl?: string | undefined, XAuthToken?: string | undefined, fetch?: typeof fetch | undefined}; export const loginTokenX = z.object({authToken: z.string().optional(), userId: z.string().optional(), userType: z.enum(["AccountUser", "EndUser", "ManagementUser", "AlianzaUser", "BusinessLines"]).optional(), endUserType: z.enum(["ADMIN", "ADVANCED_ADMIN", "BASIC_ADMIN", "STANDARD", "STANDARD_ADMIN", "SUPER_ADMIN"]).optional(), username: z.string().optional(), firstName: z.string().optional(), lastName: z.string().optional(), emailAddress: z.string().optional(), partitionId: z.string().optional(), partitionName: z.string().optional(), accountId: z.string().optional(), permissions: z.record(z.string(), z.enum(["NONE", "READ_MYUSER", "READ", "EDIT_MYUSER", "EDIT", "CREATE_MYUSER", "CREATE", "DELETE_MYUSER", "DELETE", "DENIED"])).optional(), featureToggles: z.record(z.string(), z.boolean()).optional(), mustChangePassword: z.boolean().optional(), subPartitionIds: z.array(z.string()).optional(), tokenSource: z.string().optional(), grantedAuthorities: z.array(z.string()).optional(), pwdHash: z.string().optional(), mustAddEmail: z.boolean().optional(), maxLifeInHours: z.number().int().optional(), groupId: z.string().optional(), groupName: 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 }; /** * User Information * * GET /v2/authorize/userinfo */ export const getApiV2AuthorizeUserinfo = async (options?: GetApiV2AuthorizeUserinfoOptions) => { const {baseUrl = "https://api.alianza.com", fetch: fetchFn = fetch, XAuthToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (XAuthToken !== undefined) headers.set("X-AUTH-TOKEN", XAuthToken) const url = `${baseUrl.replace(/\/+$/, '')}/v2/authorize/userinfo${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(loginTokenX, await res.json()) };