import {z} from 'zod' export type GetApiApiV1AuthUserInfoOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const v1PermissionBase = z.object({actions: z.array(z.string()), alias: z.string(), createTime: z.string(), effect: z.string(), name: z.string(), resources: z.array(z.string()), updateTime: z.string()}); export const v1NameAlias = z.object({alias: z.string(), name: z.string()}); export const v1UserProjectBase = z.object({alias: z.string(), description: z.string(), joinTime: z.string(), name: z.string(), owner: v1NameAlias.optional(), roles: z.array(v1NameAlias)}); export const v1LoginUserInfoResponse = z.object({alias: z.string().optional(), createTime: z.string(), disabled: z.boolean(), email: z.string(), lastLoginTime: z.string(), name: z.string(), platformPermissions: z.array(v1PermissionBase), projectPermissions: z.record(z.string(), z.array(v1PermissionBase)), projects: z.array(v1UserProjectBase)}); 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 }; /** * get login user detail info * * GET /api/v1/auth/user_info */ export const getApiApiV1AuthUserInfo = async (options: GetApiApiV1AuthUserInfoOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/auth/user_info${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(v1LoginUserInfoResponse, await res.json()) };