import {z} from 'zod' export type GetApiListsOverviewFormatArgs = {published_date?: string | undefined, 'api-key'?: string | undefined}; export type GetApiListsOverviewFormatOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiListsOverviewFormatResponse = z.object({status: z.string().optional(), copyright: z.string().optional(), num_results: z.number().int().optional(), results: z.object({bestsellers_date: z.string().optional(), published_date: z.string().optional(), lists: z.array(z.object({list_id: z.number().int().optional(), list_name: z.string().optional(), display_name: z.string().optional(), updated: z.string().optional(), list_image: z.string().optional(), books: z.array(z.object({age_group: z.string().optional(), author: z.string().optional(), contributor: z.string().optional(), contributor_note: z.string().optional(), created_date: z.string().optional(), description: z.string().optional(), price: z.number().int().optional(), primary_isbn13: z.string().optional(), primary_isbn10: z.string().optional(), publisher: z.string().optional(), rank: z.number().int().optional(), title: z.string().optional(), updated_date: z.string().optional()})).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 List Overview * * GET /lists/overview.{format} */ export const getApiListsOverviewFormat = async (args: GetApiListsOverviewFormatArgs, options?: GetApiListsOverviewFormatOptions) => { 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.published_date != null) query.append("published_date", String(args.published_date)) if (args['api-key'] != null) query.append("api-key", String(args['api-key'])) if (apiKey !== undefined) query.set("api-key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/lists/overview.${encodeURIComponent(String(args.format))}${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(getApiListsOverviewFormatResponse, await res.json()) };