import {z} from 'zod' export type LibraryReadScope = 'private' | 'workspace' | 'default' | 'pinterest' | 'arena' | 'mymind' | 'vintage'; export type LibraryItemType = 'image' | 'video' | 'sound-effect' | 'music' | 'lut' | 'screenshot' | 'font'; export type GetApiV1LibraryArgs = {scope: LibraryReadScope, type?: LibraryItemType | undefined, query?: string | undefined, collection?: string | undefined, cursor?: string | undefined, limit?: number | undefined}; export type GetApiV1LibraryOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const libraryItemScope = z.enum(["private", "workspace", "default", "video", "pinterest", "arena", "mymind", "vintage"]); export const libraryItemType = z.enum(["image", "video", "sound-effect", "music", "lut", "screenshot", "font"]); export const libraryItem = z.object({category: z.string().optional(), createdAt: z.string().optional(), dimensions: z.object({height: z.number().int().gte(-9007199254740991).lte(9007199254740991), width: z.number().int().gte(-9007199254740991).lte(9007199254740991)}).optional(), durationMs: z.number().optional(), id: z.string(), link: z.string().optional(), name: z.string(), presetId: z.string().optional(), scope: libraryItemScope, sourceId: z.string().optional(), summary: z.string().optional(), tags: z.array(z.string()).optional(), type: libraryItemType, updatedAt: z.string().optional(), url: z.string().optional()}); export const listLibraryResponse = z.object({cursor: z.string().optional(), items: z.array(libraryItem)}); 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 library items * * GET /v1/library */ export const getApiV1Library = async (args: GetApiV1LibraryArgs, options?: GetApiV1LibraryOptions) => { 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.type != null) query.append("type", String(args.type)) if (args.query != null) query.append("query", String(args.query)) if (args.collection != null) query.append("collection", String(args.collection)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.limit != null) query.append("limit", String(args.limit)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/library${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(listLibraryResponse, await res.json()) };