import {z} from 'zod' export type DocketType = 'Rulemaking' | 'Nonrulemaking'; export type GetApiDocketsArgs = {'filter[agencyId]'?: string | undefined, 'filter[searchTerm]'?: string | undefined, 'filter[lastModifiedDate]'?: string | undefined, 'filter[docketType]'?: DocketType | undefined, sort?: string | undefined, 'page[number]'?: number | undefined, 'page[size]'?: number | undefined}; export type GetApiDocketsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const docketType = z.enum(["Rulemaking", "Nonrulemaking"]); export const docket = z.object({agencyId: z.string().optional(), docketType: docketType.optional(), highlightedContent: z.string().nullable().optional(), lastModifiedDate: z.string().optional(), objectId: z.string().optional(), title: z.string().optional()}); export const link = z.string(); export const selfLink = z.object({self: link.optional()}); export const docketFindAllItem = z.object({id: z.string().optional(), type: z.string().optional(), attributes: docket.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 docketFindAllResponse = z.object({data: z.array(docketFindAllItem).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 dockets * * GET /dockets */ export const getApiDockets = async (args: GetApiDocketsArgs, options?: GetApiDocketsOptions) => { 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[lastModifiedDate]'] != null) query.append("filter[lastModifiedDate]", String(args['filter[lastModifiedDate]'])) if (args['filter[docketType]'] != null) query.append("filter[docketType]", String(args['filter[docketType]'])) 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(/\/+$/, '')}/dockets${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(docketFindAllResponse, await res.json()) };