import {z} from 'zod' export type GetApiV4UsersMeOptions = {baseUrl?: string | undefined, APIKeyCookie?: string | undefined, fetch?: typeof fetch | undefined}; export const userTeam = z.object({id: z.string(), name: z.string(), role: z.string()}); export const userGet = z.object({firstname: z.string().optional(), lastname: z.string().optional(), company: z.string().optional(), job_title: z.string().optional(), avatar_colour: z.string().optional(), show_tutorial: z.boolean().optional(), email: z.string(), username: z.string(), id: z.string(), has_password: z.boolean(), roles: z.array(z.string()).optional(), teams: z.array(userTeam).optional(), selected_team: 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 }; /** * Get Logged In User * * GET /v4/users/me */ export const getApiV4UsersMe = async (options?: GetApiV4UsersMeOptions) => { const {baseUrl = "https://www.mystic.ai", fetch: fetchFn = fetch, APIKeyCookie} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (APIKeyCookie !== undefined) cookies.push("access-token=" + encodeURIComponent(APIKeyCookie)) const url = `${baseUrl.replace(/\/+$/, '')}/v4/users/me${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(userGet, await res.json()) };