import {z} from 'zod' export type GetApiGeneralUserInfoOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const quotaStatistics = z.object({currentUsage: z.number().int(), maximumLimit: z.number().int()}); export const runningProfilesStatistics = z.object({automated: quotaStatistics, manual: quotaStatistics, mobile: quotaStatistics, total: quotaStatistics}); export const runningProfiles = z.object({user: runningProfilesStatistics, tenant: runningProfilesStatistics}); export const profileMinutesQuota = z.object({currentUsage: z.string().optional(), maximumLimit: z.string().nullable().optional(), nextResetAt: z.string().optional()}); export const cloudTrafficQuota = z.object({currentUsage: z.number().int().optional(), maximumLimit: z.number().int().nullable().optional(), nextResetAt: z.string().optional()}); export const userInfoResponse = z.object({userId: z.string(), displayName: z.string(), email: z.string(), emailConfirmed: z.boolean(), subscriptionEnd: z.string(), plan: z.string(), capabilities: z.array(z.string()), gracePeriod: z.boolean(), lastAppLogin: z.string(), version: z.string(), workspaceFolder: z.string(), localStorage: quotaStatistics, cloudStorage: quotaStatistics, hasTeamSubscription: z.boolean(), teamId: z.string().nullable().optional(), teamRole: z.string().nullable().optional(), runningProfiles: runningProfiles, profileMinutes: profileMinutesQuota, cloudTraffic: cloudTrafficQuota}); 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 }; /** * Fetch detailed information about your user account. * * GET /general/user-info */ export const getApiGeneralUserInfo = async (options?: GetApiGeneralUserInfoOptions) => { const {baseUrl = "http://localhost:5050", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/general/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(userInfoResponse, await res.json()) };