import {z} from 'zod' export type ResourceId = string; export type CustomParamSchema = {[key: string]: string}; export type PlaylistMatch = 'any' | 'all'; export type PlaylistTagRules = {values?: Array | undefined, match?: PlaylistMatch | undefined}; export type PlaylistTagFilter = {include?: PlaylistTagRules | undefined, exclude?: PlaylistTagRules | undefined}; export type PlaylistParamRules = {values?: {[key: string]: string} | undefined, match?: PlaylistMatch | undefined}; export type PlaylistCustomParamFilter = {include?: PlaylistParamRules | undefined, exclude?: PlaylistParamRules | undefined}; export type PlaylistLimit = number; export type PlaylistId = string; export type PlaylistPinning = {slot1?: PlaylistId | undefined, slot2?: PlaylistId | undefined}; export type PlaylistRecency = string; export type RecommendationsPlaylistMetadata = {title?: string | undefined, description?: string | undefined, author?: string | undefined, link?: string | undefined, custom_params?: CustomParamSchema | undefined, tag_filter?: PlaylistTagFilter | undefined, custom_param_filter?: PlaylistCustomParamFilter | undefined, media_filter?: ({limit?: PlaylistLimit | undefined}) | undefined, pinning?: PlaylistPinning | undefined, recency?: PlaylistRecency | undefined}; export type CreateApiV2SitesSiteIdPlaylistsRecommendationsPlaylistArgs = {site_id: ResourceId, body: {metadata?: RecommendationsPlaylistMetadata | undefined}}; export type CreateApiV2SitesSiteIdPlaylistsRecommendationsPlaylistOptions = {baseUrl?: string | undefined, v2ApiSecret?: string | undefined, fetch?: typeof fetch | undefined}; export const resourceId = z.string().min(8).max(8); export const customParamSchema = z.record(z.string(), z.string()); export const playlistMatch = z.enum(["any", "all"]); export const playlistTagRules = z.object({values: z.array(z.string()).optional(), match: playlistMatch.optional()}); export const playlistTagFilter = z.object({include: playlistTagRules.optional(), exclude: playlistTagRules.optional()}); export const playlistParamRules = z.object({values: z.record(z.string(), z.string()).optional(), match: playlistMatch.optional()}); export const playlistCustomParamFilter = z.object({include: playlistParamRules.optional(), exclude: playlistParamRules.optional()}); export const playlistLimit = z.number().int(); export const playlistId = z.string().min(8).max(8); export const playlistPinning = z.object({slot1: playlistId.optional(), slot2: playlistId.optional()}); export const playlistRecency = z.string(); export const recommendationsPlaylistMetadata = z.object({title: z.string().optional(), description: z.string().optional(), author: z.string().optional(), link: z.string().optional(), custom_params: customParamSchema.optional(), tag_filter: playlistTagFilter.optional(), custom_param_filter: playlistCustomParamFilter.optional(), media_filter: z.object({limit: playlistLimit.optional()}).optional(), pinning: playlistPinning.optional(), recency: playlistRecency.optional()}); export const recommendationsPlaylistResourceRead = z.object({schema: z.string().optional(), id: resourceId.optional(), created: z.string().optional(), last_modified: z.string().optional(), type: z.string().optional(), relationships: z.object({}).optional(), metadata: recommendationsPlaylistMetadata.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 }; /** * Create a recommendations playlist * * POST /v2/sites/{site_id}/playlists/recommendations_playlist/ */ export const createApiV2SitesSiteIdPlaylistsRecommendationsPlaylist = async (args: CreateApiV2SitesSiteIdPlaylistsRecommendationsPlaylistArgs, options?: CreateApiV2SitesSiteIdPlaylistsRecommendationsPlaylistOptions) => { const {baseUrl = "https://api.jwplayer.com", fetch: fetchFn = fetch, v2ApiSecret} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (v2ApiSecret !== undefined) headers.set("Authorization", v2ApiSecret) const url = `${baseUrl.replace(/\/+$/, '')}/v2/sites/${encodeURIComponent(String(args.site_id))}/playlists/recommendations_playlist/${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(recommendationsPlaylistResourceRead, await res.json()) };