import {z} from 'zod' export type GetApiLibraryLibraryIdCollectionsArgs = {libraryId: number, page?: number | undefined, itemsPerPage?: number | undefined, search?: (string | null) | undefined, orderBy?: (string | null) | undefined, includeThumbnails?: boolean | undefined}; export type GetApiLibraryLibraryIdCollectionsOptions = {baseUrl?: string | undefined, AccessKey?: string | undefined, fetch?: typeof fetch | undefined}; export const collectionModel = z.object({videoLibraryId: z.number().int().optional(), guid: z.string().nullable().optional(), name: z.string().nullable().optional(), videoCount: z.number().int().optional(), liveStreamCount: z.number().int().optional(), totalSize: z.number().int().optional(), previewVideoIds: z.string().nullable().optional(), previewImageUrls: z.array(z.string()).nullable().optional()}); export const paginationListOfCollectionModel = z.object({totalItems: z.number().int().optional(), currentPage: z.number().int().optional(), itemsPerPage: z.number().int().optional(), items: z.array(collectionModel).nullable().optional()}); 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 Collection List * * GET /library/{libraryId}/collections */ export const getApiLibraryLibraryIdCollections = async (args: GetApiLibraryLibraryIdCollectionsArgs, options?: GetApiLibraryLibraryIdCollectionsOptions) => { const {baseUrl = "https://video.bunnycdn.com", fetch: fetchFn = fetch, AccessKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.itemsPerPage != null) query.append("itemsPerPage", String(args.itemsPerPage)) if (args.search != null) query.append("search", String(args.search)) if (args.orderBy != null) query.append("orderBy", String(args.orderBy)) if (args.includeThumbnails != null) query.append("includeThumbnails", String(args.includeThumbnails)) if (AccessKey !== undefined) headers.set("AccessKey", AccessKey) const url = `${baseUrl.replace(/\/+$/, '')}/library/${encodeURIComponent(String(args.libraryId))}/collections${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(paginationListOfCollectionModel, await res.json()) };