import {z} from 'zod' export type GetApiCommentsArgs = {'filter[agencyId]'?: string | undefined, 'filter[searchTerm]'?: string | undefined, 'filter[postedDate]'?: string | undefined, 'filter[lastModifiedDate]'?: string | undefined, 'filter[commentOnId]'?: string | undefined, sort?: string | undefined, 'page[number]'?: number | undefined, 'page[size]'?: number | undefined}; export type GetApiCommentsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const documentType = z.enum(["Notice", "Rule", "Proposed Rule", "Supporting & Related Material", "Other"]); export const comment = z.object({agencyId: z.string().optional(), documentType: documentType.optional(), highlightedContent: z.string().nullable().optional(), lastModifiedDate: z.string().optional(), objectId: z.string().optional(), postedDate: z.string().optional(), title: z.string().optional(), withdrawn: z.boolean().optional()}); export const link = z.string(); export const selfLink = z.object({self: link.optional()}); export const commentFindAllItem = z.object({id: z.string().optional(), type: z.string().optional(), attributes: comment.optional(), links: z.array(selfLink).optional()}); export const findAllResponseMetadata = z.object({hasNextPage: z.boolean().optional(), hasPreviousPage: z.boolean().optional(), numberOfElements: z.number().int().optional(), pageNumber: z.number().int().optional(), pageSize: z.number().int().optional(), totalElements: z.number().int().optional(), totalPages: z.number().int().optional(), firstPage: z.boolean().optional(), lastPage: z.boolean().optional()}); export const commentFindAllResponse = z.object({data: z.array(commentFindAllItem).optional(), meta: findAllResponseMetadata.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 }; /** * List of comments * * GET /comments */ export const getApiComments = async (args: GetApiCommentsArgs, options?: GetApiCommentsOptions) => { const {baseUrl = "https://api.regulations.gov/v4", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/vnd.api+json") if (args['filter[agencyId]'] != null) query.append("filter[agencyId]", String(args['filter[agencyId]'])) if (args['filter[searchTerm]'] != null) query.append("filter[searchTerm]", String(args['filter[searchTerm]'])) if (args['filter[postedDate]'] != null) query.append("filter[postedDate]", String(args['filter[postedDate]'])) if (args['filter[lastModifiedDate]'] != null) query.append("filter[lastModifiedDate]", String(args['filter[lastModifiedDate]'])) if (args['filter[commentOnId]'] != null) query.append("filter[commentOnId]", String(args['filter[commentOnId]'])) if (args.sort != null) query.append("sort", String(args.sort)) if (args['page[number]'] != null) query.append("page[number]", String(args['page[number]'])) if (args['page[size]'] != null) query.append("page[size]", String(args['page[size]'])) if (ApiKeyAuth !== undefined) headers.set("X-Api-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/comments${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(commentFindAllResponse, await res.json()) };