import {z} from 'zod' export type GetApiListsBestSellersHistoryJsonArgs = {'age-group'?: string | undefined, author?: string | undefined, contributor?: string | undefined, isbn?: string | undefined, price?: string | undefined, publisher?: string | undefined, title?: string | undefined}; export type GetApiListsBestSellersHistoryJsonOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiListsBestSellersHistoryJsonResponse = z.object({status: z.string().optional(), copyright: z.string().optional(), num_results: z.number().int().optional(), results: z.array(z.object({title: z.string().optional(), description: z.string().optional(), contributor: z.string().optional(), author: z.string().optional(), contributor_note: z.string().optional(), price: z.number().int().optional(), age_group: z.string().optional(), publisher: z.string().optional(), isbns: z.array(z.object({isbn10: z.string().optional(), isbn13: z.string().optional()})).optional(), ranks_history: z.array(z.object({primary_isbn10: z.string().optional(), primary_isbn13: z.string().optional(), rank: z.number().int().optional(), list_name: z.string().optional(), display_name: z.string().optional(), published_date: z.string().optional(), bestsellers_date: z.string().optional(), weeks_on_list: z.number().int().optional(), ranks_last_week: z.unknown(), asterisk: z.number().int().optional(), dagger: z.number().int().optional()})).optional(), reviews: z.array(z.object({book_review_link: z.string().optional(), first_chapter_link: z.string().optional(), sunday_review_link: z.string().optional(), article_chapter_link: z.string().optional()})).optional()})).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 }; /** * Best Seller History List * * GET /lists/best-sellers/history.json */ export const getApiListsBestSellersHistoryJson = async (args: GetApiListsBestSellersHistoryJsonArgs, options?: GetApiListsBestSellersHistoryJsonOptions) => { const {baseUrl = "https://api.nytimes.com/svc/books/v3", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['age-group'] != null) query.append("age-group", String(args['age-group'])) if (args.author != null) query.append("author", String(args.author)) if (args.contributor != null) query.append("contributor", String(args.contributor)) if (args.isbn != null) query.append("isbn", String(args.isbn)) if (args.price != null) query.append("price", String(args.price)) if (args.publisher != null) query.append("publisher", String(args.publisher)) if (args.title != null) query.append("title", String(args.title)) if (apiKey !== undefined) query.set("api-key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/lists/best-sellers/history.json${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(getApiListsBestSellersHistoryJsonResponse, await res.json()) };