import {z} from 'zod' export type StorefrontUpdateRequest = {title: string, type: 'digital' | 'vinyl' | 'cd' | 'cassette' | 'apparel' | 'sample_pack' | 'subscription' | 'live_event' | 'live_stream' | 'other', link: string, link_title?: string | undefined, description?: string | undefined, price?: string | undefined}; export type UpdateApiTracksTrackUrnStorefrontArgs = {track_urn: string, body: StorefrontUpdateRequest}; export type UpdateApiTracksTrackUrnStorefrontOptions = {baseUrl?: string | undefined, AuthHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const storefront = z.object({track_urn: z.string().optional(), title: z.string().optional(), type: z.enum(["digital", "vinyl", "cd", "cassette", "apparel", "sample_pack", "subscription", "live_event", "live_stream", "other"]).optional(), link: z.string().optional(), link_title: z.string().nullable().optional(), description: z.string().nullable().optional(), image_url: z.string().nullable().optional(), price: z.string().nullable().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 or updates the storefront (Artist Storefront) of a track. * * PUT /tracks/{track_urn}/storefront * * Also accepts auth: oAuth2_1 */ export const updateApiTracksTrackUrnStorefront = async (args: UpdateApiTracksTrackUrnStorefrontArgs, options?: UpdateApiTracksTrackUrnStorefrontOptions) => { const {baseUrl = "https://api.soundcloud.com", fetch: fetchFn = fetch, AuthHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json; charset=utf-8") if (AuthHeader !== undefined) headers.set("Authorization", AuthHeader) const url = `${baseUrl.replace(/\/+$/, '')}/tracks/${encodeURIComponent(String(args.track_urn))}/storefront${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(storefront, await res.json()) };