import {z} from 'zod' export type GetApiApiV3UserPreferencesOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const pinnedAccountView = z.enum(["full", "compact"]); export const pinnedAccountSchema = z.object({team: z.string(), view: pinnedAccountView}); export const theme = z.enum(["light", "dark", "system"]); export const pinnedAppsSchema = z.object({team_id: z.string(), app_ids: z.array(z.string())}); export const preferencesSchema = z.object({pinned_accounts: z.array(pinnedAccountSchema), theme: theme, selected_team: z.string().nullable(), expanded_nav: z.boolean().nullable(), eligible_for_onboarding_flow: z.boolean(), pinned_apps: z.array(pinnedAppsSchema)}); export const preferencesSchemaNoPagination = z.object({data: preferencesSchema}); 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 preferences for the authenticated user * * GET /api/v3/user/preferences */ export const getApiApiV3UserPreferences = async (options?: GetApiApiV3UserPreferencesOptions) => { const {baseUrl = "https://codemagic.io", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (apiKey !== undefined) headers.set("x-auth-token", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/v3/user/preferences${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(preferencesSchemaNoPagination, await res.json()) };