import {z} from 'zod' export type PlaylistLinkScope = 'public' | 'private' | 'password' | 'embedonly'; export type PlaylistVisibility = 'org' | 'personal'; export type CreatePlaylistRequest = {description?: string | undefined, emoji?: string | undefined, linkScope?: PlaylistLinkScope | undefined, name: string, password?: string | undefined, searchEngineIndexingEnabled?: boolean | undefined, visibility?: PlaylistVisibility | undefined}; export type CreateApiV1PlaylistsArgs = {body: CreatePlaylistRequest}; export type CreateApiV1PlaylistsOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const playlistLinkScope = z.enum(["public", "private", "password", "embedonly"]); export const playlistLinks = z.object({viewPage: z.string()}); export const playlistVisibility = z.enum(["org", "personal"]); export const playlistDetail = z.object({createdAt: z.string(), description: z.string().max(5000), emoji: z.string().max(10), groupId: z.string().nullable(), id: z.string(), linkScope: playlistLinkScope, links: playlistLinks, name: z.string().min(1).max(255), searchEngineIndexingEnabled: z.boolean(), updatedAt: z.string(), videos: z.number().int().gte(0).lte(9007199254740991), visibility: playlistVisibility}); export const createApiV1PlaylistsResponse = z.object({playlist: playlistDetail}); 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 new playlist * * POST /v1/playlists */ export const createApiV1Playlists = async (args: CreateApiV1PlaylistsArgs, options?: CreateApiV1PlaylistsOptions) => { const {baseUrl = "https://api.tella.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/playlists${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(createApiV1PlaylistsResponse, await res.json()) };