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 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 FieldVisibility = {mood: boolean, tags: boolean, genre: boolean, share: boolean, play_count: boolean, remixes: boolean}; export type StemParent = {category: string, parent_track_id: number}; export type TrackElementWrite = {parent_track_id: string}; export type RemixParentWrite = {tracks: Array}; export type DdexResourceContributor = {name: string, roles: Array, sequence_number?: number | undefined}; export type DdexRightsController = {name: string, roles: Array, rights_share_unknown?: string | undefined}; export type DdexCopyright = {year: string, text: string}; export type CreateTrackRequestBody = {track_id?: string | undefined, title: string, genre: Genre, description?: (string | null) | undefined, mood?: (Mood | null) | undefined, bpm?: (number | null) | undefined, musical_key?: (string | null) | undefined, tags?: (string | null) | undefined, license?: (string | null) | undefined, isrc?: (string | null) | undefined, iswc?: (string | null) | undefined, release_date?: string | undefined, is_scheduled_release?: boolean | undefined, track_cid: string, orig_file_cid?: string | undefined, orig_filename?: string | undefined, cover_art_sizes?: string | undefined, preview_cid?: string | undefined, preview_start_seconds?: number | undefined, duration?: number | undefined, is_downloadable?: boolean | undefined, is_unlisted?: boolean | undefined, is_stream_gated?: (boolean | null) | undefined, access_authorities?: (Array | null) | undefined, feedback_requested?: (boolean | null) | undefined, allowed_api_keys?: (Array | null) | undefined, stream_conditions?: (AccessGate | null) | undefined, download_conditions?: (AccessGate | null) | undefined, field_visibility?: FieldVisibility | undefined, placement_hosts?: string | undefined, stem_of?: StemParent | undefined, remix_of?: RemixParentWrite | undefined, ddex_app?: (string | null) | undefined, ddex_release_ids?: (Record | null) | undefined, artists?: (Array> | null) | undefined, resource_contributors?: (Array | null) | undefined, indirect_resource_contributors?: (Array | null) | undefined, rights_controller?: DdexRightsController | undefined, copyright_line?: (DdexCopyright | null) | undefined, producer_copyright_line?: (DdexCopyright | null) | undefined, parental_warning_type?: (string | null) | undefined, cover_original_song_title?: (string | null) | undefined, cover_original_artist?: (string | null) | undefined, is_owned_by_user?: boolean | undefined, territory_codes?: (Array | null) | undefined, no_ai_use?: boolean | undefined}; export type CreateApiTracksArgs = {user_id: string, body: CreateTrackRequestBody}; export type CreateApiTracksOptions = {baseUrl: string, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createTrackResponse = z.object({transaction_hash: z.string().optional(), block_hash: z.string().optional(), block_number: z.number().int().optional(), track_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 track * * POST /tracks * * Also accepts auth: BasicAuth, OAuth2 */ export const createApiTracks = async (args: CreateApiTracksArgs, options: CreateApiTracksOptions) => { 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(/\/+$/, '')}/tracks${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(createTrackResponse, await res.json()) };