import {z} from 'zod' export type Genre = string; export type Mood = 'Peaceful' | 'Romantic' | 'Sentimental' | 'Tender' | 'Easygoing' | 'Yearning' | 'Sophisticated' | 'Sensual' | 'Cool' | 'Gritty' | 'Melancholy' | 'Serious' | 'Brooding' | 'Fiery' | 'Defiant' | 'Aggressive' | 'Rowdy' | 'Excited' | 'Energizing' | 'Empowering' | 'Stirring' | 'Upbeat' | 'Other'; export type PlaylistAddedTimestamp = {metadata_timestamp?: number | undefined, timestamp: number, track_id: string}; export type TipGate = {tip_user_id: number}; export type FollowGate = {follow_user_id: number}; export type PaymentSplit = {user_id: number, percentage: number}; export type UsdcGate = {price: number, splits: Array}; export type PurchaseGate = {usdc_purchase: UsdcGate}; export type ExtendedTokenGate = {token_mint: string, token_amount: number}; export type TokenGate = {token_gate: ExtendedTokenGate}; export type AccessGate = TipGate | FollowGate | PurchaseGate | TokenGate; export type DdexResourceContributor = {name: string, roles: Array, sequence_number?: number | undefined}; export type DdexCopyright = {year: string, text: string}; export type CreatePlaylistRequestBody = {playlist_id?: string | undefined, playlist_name: string, description?: string | undefined, is_private?: boolean | undefined, is_album?: boolean | undefined, genre?: Genre | undefined, mood?: Mood | undefined, tags?: string | undefined, license?: string | undefined, upc?: string | undefined, release_date?: string | undefined, playlist_image_sizes_multihash?: string | undefined, playlist_contents?: Array | undefined, is_stream_gated?: (boolean | null) | undefined, is_scheduled_release?: (boolean | null) | undefined, stream_conditions?: (AccessGate | null) | undefined, ddex_app?: string | undefined, ddex_release_ids?: ({[key: string]: string} | null) | undefined, artists?: (Array | null) | undefined, copyright_line?: (DdexCopyright | null) | undefined, producer_copyright_line?: (DdexCopyright | null) | undefined, parental_warning_type?: (string | null) | undefined, is_image_autogenerated?: (boolean | null) | undefined}; export type CreateApiPlaylistsArgs = {user_id: string, body: CreatePlaylistRequestBody}; export type CreateApiPlaylistsOptions = {baseUrl: string, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createPlaylistResponse = z.object({transaction_hash: z.string().optional(), block_hash: z.string().optional(), block_number: z.number().int().optional(), playlist_id: z.string().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 }; /** * Creates a new playlist or album * * POST /playlists * * Also accepts auth: BasicAuth, OAuth2 */ export const createApiPlaylists = async (args: CreateApiPlaylistsArgs, options: CreateApiPlaylistsOptions) => { const {baseUrl, 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 (args.user_id != null) query.append("user_id", String(args.user_id)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/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(createPlaylistResponse, await res.json()) };