import {z} from 'zod' export type DocumentType = 'Notice' | 'Rule' | 'Proposed Rule' | 'Supporting & Related Material' | 'Other'; export type GetApiDocumentsArgs = {'filter[agencyId]'?: string | undefined, 'filter[commentEndDate]'?: string | undefined, 'filter[docketId]'?: string | undefined, 'filter[documentType]'?: DocumentType | undefined, 'filter[frDocNum]'?: string | undefined, 'filter[searchTerm]'?: string | undefined, 'filter[postedDate]'?: string | undefined, 'filter[lastModifiedDate]'?: string | undefined, 'filter[subtype]'?: string | undefined, 'filter[withinCommentPeriod]'?: boolean | undefined, sort?: string | undefined, 'page[number]'?: number | undefined, 'page[size]'?: number | undefined}; export type GetApiDocumentsOptions = {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 document = z.object({agencyId: z.string().optional(), commentEndDate: z.string().nullable().optional(), commentStartDate: z.string().nullable().optional(), docketId: z.string().optional(), documentType: documentType.optional(), frDocNum: z.string().optional(), highlightedContent: z.string().nullable().optional(), lastModifiedDate: z.string().optional(), objectId: z.string().optional(), openForComment: z.boolean().optional(), postedDate: z.string().optional(), subtype: z.string().nullable().optional(), title: z.string().optional(), withdrawn: z.boolean().optional()}); export const link = z.string(); export const selfLink = z.object({self: link.optional()}); export const documentFindAllItem = z.object({id: z.string().optional(), type: z.string().optional(), attributes: document.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 documentFindAllResponse = z.object({data: z.array(documentFindAllItem).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 documents * * GET /documents */ export const getApiDocuments = async (args: GetApiDocumentsArgs, options?: GetApiDocumentsOptions) => { 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[commentEndDate]'] != null) query.append("filter[commentEndDate]", String(args['filter[commentEndDate]'])) if (args['filter[docketId]'] != null) query.append("filter[docketId]", String(args['filter[docketId]'])) if (args['filter[documentType]'] != null) query.append("filter[documentType]", String(args['filter[documentType]'])) if (args['filter[frDocNum]'] != null) query.append("filter[frDocNum]", String(args['filter[frDocNum]'])) 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[subtype]'] != null) query.append("filter[subtype]", String(args['filter[subtype]'])) if (args['filter[withinCommentPeriod]'] != null) query.append("filter[withinCommentPeriod]", String(args['filter[withinCommentPeriod]'])) 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(/\/+$/, '')}/documents${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(documentFindAllResponse, await res.json()) };