import {z} from 'zod' export type GetApiV1VideosArgs = {cursor?: string | undefined, limit?: number | undefined, playlistId?: string | undefined, tagIds?: string | undefined}; export type GetApiV1VideosOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const pagination = z.object({hasMore: z.boolean(), nextCursor: z.string().nullable()}); export const videoLinks = z.object({embedPage: z.string(), viewPage: z.string()}); export const videoListItem = z.object({aspectRatio: z.string(), autoRatio: z.boolean(), createdAt: z.string(), description: z.string().max(5000), dimensions: z.object({height: z.number(), width: z.number()}), id: z.string(), links: videoLinks, name: z.string().min(1).max(255), updatedAt: z.string(), views: z.number().int().gte(0).lte(9007199254740991)}); export const getApiV1VideosResponse = z.object({pagination: pagination, videos: z.array(videoListItem)}); 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 all videos * * GET /v1/videos */ export const getApiV1Videos = async (args: GetApiV1VideosArgs, options?: GetApiV1VideosOptions) => { 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.cursor != null) query.append("cursor", String(args.cursor)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.playlistId != null) query.append("playlistId", String(args.playlistId)) if (args.tagIds != null) query.append("tagIds", String(args.tagIds)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/videos${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(getApiV1VideosResponse, await res.json()) };