import {z} from 'zod' export type GetApiNotificationsIdPlaylistUpdatesArgs = {id: string, user_id?: string | undefined}; export type GetApiNotificationsIdPlaylistUpdatesOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const versionMetadata = z.object({service: z.string(), version: z.string()}); export const playlistUpdate = z.object({playlist_id: z.string(), updated_at: z.number().int(), last_seen_at: z.number().int().optional()}); export const playlistUpdates = z.object({playlist_updates: z.array(playlistUpdate).optional()}); export const playlistUpdatesResponse = z.object({latest_chain_block: z.number().int(), latest_indexed_block: z.number().int(), latest_chain_slot_plays: z.number().int(), latest_indexed_slot_plays: z.number().int(), signature: z.string(), timestamp: z.string(), version: versionMetadata, data: playlistUpdates.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 playlists the user has saved that have been updated for user ID * * GET /notifications/{id}/playlist_updates */ export const getApiNotificationsIdPlaylistUpdates = async (args: GetApiNotificationsIdPlaylistUpdatesArgs, options: GetApiNotificationsIdPlaylistUpdatesOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.user_id != null) query.append("user_id", String(args.user_id)) const url = `${baseUrl.replace(/\/+$/, '')}/notifications/${encodeURIComponent(String(args.id))}/playlist_updates${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(playlistUpdatesResponse, await res.json()) };