import {z} from 'zod' export type BackgroundScope = 'personal' | 'workspace' | 'default'; export type GetApiV1BackgroundsArgs = {scope?: BackgroundScope | undefined, categoryId?: string | undefined}; export type GetApiV1BackgroundsOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const clipBackgroundOutput = z.object({color: z.string().nullable().optional(), gradientAngle: z.number().int().gte(-9007199254740991).lte(9007199254740991).nullable().optional(), gradientColor1: z.string().nullable().optional(), gradientColor2: z.string().nullable().optional(), imageUrl: z.string().nullable().optional(), shaderGradientPreset: z.string().nullable().optional(), sourceId: z.string().nullable().optional(), type: z.enum(["solid", "gradient", "image", "video", "shaderGradient", "ambient"]), videoDurationSeconds: z.number().gte(0).nullable().optional(), videoUrl: z.string().nullable().optional()}); export const backgroundItem = z.object({background: clipBackgroundOutput, name: z.string(), thumbnailUrl: z.string()}); export const backgroundScope = z.enum(["personal", "workspace", "default"]); export const backgroundCategory = z.object({backgrounds: z.array(backgroundItem), id: z.string(), name: z.string(), scope: backgroundScope}); export const listBackgroundsResponse = z.object({categories: z.array(backgroundCategory)}); 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 }; /** * List available backgrounds * * GET /v1/backgrounds */ export const getApiV1Backgrounds = async (args: GetApiV1BackgroundsArgs, options?: GetApiV1BackgroundsOptions) => { const {baseUrl = "https://api.tella.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.scope != null) query.append("scope", String(args.scope)) if (args.categoryId != null) query.append("categoryId", String(args.categoryId)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/backgrounds${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(listBackgroundsResponse, await res.json()) };