import {z} from 'zod' export type PlaylistLinkScope = 'public' | 'private' | 'password' | 'embedonly'; export type UpdatePlaylistRequest = {description?: string | undefined, groupId?: (string | null) | undefined, linkScope?: PlaylistLinkScope | undefined, name?: string | undefined, password?: string | undefined, position?: number | undefined, searchEngineIndexingEnabled?: boolean | undefined}; export type PatchApiV1PlaylistsIdArgs = {id: string, body: UpdatePlaylistRequest}; export type PatchApiV1PlaylistsIdOptions = {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 patchApiV1PlaylistsIdResponse = 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 }; /** * Update a playlist * * PATCH /v1/playlists/{id} */ export const patchApiV1PlaylistsId = async (args: PatchApiV1PlaylistsIdArgs, options?: PatchApiV1PlaylistsIdOptions) => { 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/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(patchApiV1PlaylistsIdResponse, await res.json()) };