import {z} from 'zod' export type GetApiUserContentByDateJsonArgs = {date?: string | undefined}; export type GetApiUserContentByDateJsonOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiUserContentByDateJsonResponse = z.object({debug: z.object({version: z.number().optional()}).optional(), status: z.string().optional(), copyright: z.string().optional(), results: z.object({comments: z.array(z.object({commentID: z.number().int().optional(), parentID: z.unknown(), statusID: z.number().int().optional(), lft: z.number().int().optional(), rgt: z.number().int().optional(), commentSequence: z.number().int().optional(), commentBody: z.string().optional(), commentTitle: z.string().optional(), createDate: z.string().optional(), updateDate: z.string().optional(), approveDate: z.string().optional(), userID: z.number().int().optional(), userDisplayName: z.string().optional(), userTitle: z.string().optional(), userURL: z.number().int().optional(), userLocation: z.string().optional(), editorsSelection: z.number().int().optional(), recommendationCount: z.number().int().optional(), replyCount: z.number().int().optional(), assetID: z.number().int().optional(), assetURL: z.string().optional(), status: z.string().optional(), commentType: z.string().optional(), replies: z.array(z.object({})).optional()})).optional(), totalCommentsReturned: z.number().int().optional(), totalCommentsFound: z.number().int().optional(), api_timestamp: z.string().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 }; /** * Comments by Date * * GET /user-content/by-date.json */ export const getApiUserContentByDateJson = async (args: GetApiUserContentByDateJsonArgs, options?: GetApiUserContentByDateJsonOptions) => { const {baseUrl = "http://api.nytimes.com/svc/community/v3", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.date != null) query.append("date", String(args.date)) if (apiKey !== undefined) query.set("api-key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/user-content/by-date.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(getApiUserContentByDateJsonResponse, await res.json()) };