import {z} from 'zod' export type GetApiPublicYoutubeRelatedVideoIdArgs = {page?: string | undefined, limit?: string | undefined}; export type GetApiPublicYoutubeRelatedVideoIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiPublicYoutubeRelatedVideoIdResponse = z.object({data: z.object({currentPageItems: z.number().optional(), data: z.array(z.object({items: z.object({contentDetails: z.object({caption: z.string().optional(), contentRating: z.object({}).optional(), definition: z.string().optional(), dimension: z.string().optional(), duration: z.string().optional(), licensedContent: z.boolean().optional(), projection: z.string().optional()}).optional(), id: z.string().optional(), kind: z.string().optional(), snippet: z.object({categoryId: z.string().optional(), channelId: z.string().optional(), channelTitle: z.string().optional(), defaultAudioLanguage: z.string().optional(), description: z.string().optional(), liveBroadcastContent: z.string().optional(), localized: z.object({description: z.string().optional(), title: z.string().optional()}).optional(), publishedAt: z.string().optional(), tags: z.array(z.string()).optional(), thumbnails: z.object({default: z.object({height: z.number().optional(), url: z.string().optional(), width: z.number().optional()}).optional(), high: z.object({height: z.number().optional(), url: z.string().optional(), width: z.number().optional()}).optional(), maxres: z.object({height: z.number().optional(), url: z.string().optional(), width: z.number().optional()}).optional(), medium: z.object({height: z.number().optional(), url: z.string().optional(), width: z.number().optional()}).optional(), standard: z.object({height: z.number().optional(), url: z.string().optional(), width: z.number().optional()}).optional()}).optional(), title: z.string().optional()}).optional(), statistics: z.object({commentCount: z.string().optional(), favoriteCount: z.string().optional(), likeCount: z.string().optional(), viewCount: z.string().optional()}).optional()}).optional(), kind: z.string().optional()})).optional(), limit: z.number().optional(), nextPage: z.boolean().optional(), page: z.number().optional(), previousPage: z.boolean().optional(), totalItems: z.number().optional(), totalPages: z.number().optional()}).optional(), message: z.string().optional(), statusCode: z.number().optional(), success: z.boolean().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 related videos * * GET /public/youtube/related/{videoId} */ export const getApiPublicYoutubeRelatedVideoId = async (args: GetApiPublicYoutubeRelatedVideoIdArgs, options?: GetApiPublicYoutubeRelatedVideoIdOptions) => { const {baseUrl = "https://api.freeapi.app/api/v1", fetch: fetchFn = fetch} = 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.limit != null) query.append("limit", String(args.limit)) const url = `${baseUrl.replace(/\/+$/, '')}/public/youtube/related/${encodeURIComponent(String(args.videoId))}${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(getApiPublicYoutubeRelatedVideoIdResponse, await res.json()) };