import {z} from 'zod' export type GetApiListsDateListJsonArgs = {isbn?: number | undefined, 'list-name'?: string | undefined, 'published-date'?: string | undefined, 'bestsellers-date'?: string | undefined, 'weeks-on-list'?: number | undefined, rank?: string | undefined, 'rank-last-week'?: number | undefined, offset?: number | undefined, 'sort-order'?: ('ASC' | 'DESC') | undefined}; export type GetApiListsDateListJsonOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiListsDateListJsonResponse = z.object({status: z.string().optional(), copyright: z.string().optional(), num_results: z.number().int().optional(), last_modified: z.string().optional(), results: z.object({list_name: z.string().optional(), bestsellers_date: z.string().optional(), published_date: z.string().optional(), display_name: z.string().optional(), normal_list_ends_at: z.number().int().optional(), updated: z.string().optional(), books: z.array(z.object({rank: z.number().int().optional(), rank_last_week: z.number().int().optional(), weeks_on_list: z.number().int().optional(), asterisk: z.number().int().optional(), dagger: z.number().int().optional(), primary_isbn10: z.string().optional(), primary_isbn13: z.string().optional(), publisher: z.string().optional(), description: z.string().optional(), price: z.number().int().optional(), title: z.string().optional(), author: z.string().optional(), contributor: z.string().optional(), contributor_note: z.string().optional(), book_image: z.string().optional(), amazon_product_url: z.string().optional(), age_group: z.string().optional(), book_review_link: z.string().optional(), first_chapter_link: z.string().optional(), sunday_review_link: z.string().optional(), article_chapter_link: z.string().optional(), isbns: z.array(z.object({isbn10: z.string().optional(), isbn13: z.string().optional()})).optional()})).optional(), corrections: z.array(z.object({})).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 List by Date * * GET /lists/{date}/{list}.json */ export const getApiListsDateListJson = async (args: GetApiListsDateListJsonArgs, options?: GetApiListsDateListJsonOptions) => { 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.isbn != null) query.append("isbn", String(args.isbn)) if (args['list-name'] != null) query.append("list-name", String(args['list-name'])) if (args['published-date'] != null) query.append("published-date", String(args['published-date'])) if (args['bestsellers-date'] != null) query.append("bestsellers-date", String(args['bestsellers-date'])) if (args['weeks-on-list'] != null) query.append("weeks-on-list", String(args['weeks-on-list'])) if (args.rank != null) query.append("rank", String(args.rank)) if (args['rank-last-week'] != null) query.append("rank-last-week", String(args['rank-last-week'])) if (args.offset != null) query.append("offset", String(args.offset)) if (args['sort-order'] != null) query.append("sort-order", String(args['sort-order'])) if (apiKey !== undefined) query.set("api-key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/lists/${encodeURIComponent(String(args.date))}/${encodeURIComponent(String(args.list))}.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(getApiListsDateListJsonResponse, await res.json()) };