import {z} from 'zod' export type GetApiArtistsArtistIdBrowseArgs = {artist_id: string, catalog?: string | undefined, add?: string | undefined, update?: string | undefined, offset?: number | undefined, limit?: number | undefined, cond?: string | undefined, sort?: string | undefined}; export type GetApiArtistsArtistIdBrowseOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const browseObject = z.object({id: z.string(), name: z.string(), prefix: z.string().nullable(), basename: z.string()}).and(z.record(z.string(), z.unknown())); export const browseResponse = z.object({total_count: z.number().int(), md5: z.string(), catalog_id: z.string(), parent_id: z.string(), parent_type: z.string(), child_type: z.string(), browse: z.array(browseObject)}).and(z.record(z.string(), z.unknown())); 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 }; /** * Browse albums of an artist * * GET /artists/{artist_id}/browse * * Also accepts auth: ApiKeyAuthHeader, ApiKeyAuthQuery */ export const getApiArtistsArtistIdBrowse = async (args: GetApiArtistsArtistIdBrowseArgs, options?: GetApiArtistsArtistIdBrowseOptions) => { const {baseUrl = "https://develop.ampache.dev/rest/8/json", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.catalog != null) query.append("catalog", String(args.catalog)) if (args.add != null) query.append("add", String(args.add)) if (args.update != null) query.append("update", String(args.update)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.cond != null) query.append("cond", String(args.cond)) if (args.sort != null) query.append("sort", String(args.sort)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/artists/${encodeURIComponent(String(args.artist_id))}/browse${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(browseResponse, await res.json()) };