import {z} from 'zod' export type GetApiVideosOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const videoMetadataModel = z.object({name: z.string(), date_created: z.string().nullable(), width: z.number().int(), height: z.number().int(), duration: z.number().nullable().optional(), fps: z.number().nullable().optional(), file_location: z.string(), file_size: z.number().int(), item_type: z.string()}); export const videoData = z.object({id: z.string(), path: z.string(), folder_id: z.string(), thumbnailPath: z.string().nullable(), metadata: videoMetadataModel, isFavourite: z.boolean(), favouritedAt: z.string().nullable().optional(), tags: z.array(z.string()).nullable().optional()}); export const getAllVideosResponse = z.object({success: z.boolean(), message: z.string(), data: z.array(videoData)}); 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 All Videos * * GET /videos/ */ export const getApiVideos = async (options?: GetApiVideosOptions) => { const {baseUrl = "http://localhost:52123", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/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(getAllVideosResponse, await res.json()) };