import {z} from 'zod' export type GetApiV1DataSetsArgs = {$filter?: string | undefined, $top?: number | undefined, $skip?: number | undefined, $count?: boolean | undefined, $filename?: string | undefined, $metadata?: boolean | undefined, $gzip?: boolean | undefined, $orderby?: (Array<'identifier' | 'identifier desc' | 'name' | 'name desc' | 'title' | 'title desc' | 'description' | 'description desc' | 'distribution' | 'distribution desc' | 'webService' | 'webService desc' | 'dataDictionary' | 'dataDictionary desc' | 'keyword' | 'keyword desc' | 'modified' | 'modified desc' | 'publisher' | 'publisher desc' | 'contactPoint' | 'contactPoint desc' | 'mbox' | 'mbox desc' | 'accessLevel' | 'accessLevel desc' | 'landingPage' | 'landingPage desc' | 'temporal' | 'temporal desc' | 'api' | 'api desc' | 'version' | 'version desc' | 'recordCount' | 'recordCount desc' | 'bureauCode' | 'bureauCode desc' | 'programCode' | 'programCode desc' | 'accessLevelComment' | 'accessLevelComment desc' | 'license' | 'license desc' | 'spatial' | 'spatial desc' | 'theme' | 'theme desc' | 'dataQuality' | 'dataQuality desc' | 'accrualPeriodicity' | 'accrualPeriodicity desc' | 'language' | 'language desc' | 'primaryITInvestmentUII' | 'primaryITInvestmentUII desc' | 'references' | 'references desc' | 'issued' | 'issued desc' | 'systemOfRecords' | 'systemOfRecords desc' | 'depDate' | 'depDate desc' | 'depApiMessage' | 'depApiMessage desc' | 'depWebMessage' | 'depWebMessage desc' | 'depNewURL' | 'depNewURL desc' | 'lastRefresh' | 'lastRefresh desc' | 'lastDataSetRefresh' | 'lastDataSetRefresh desc' | 'hash' | 'hash desc' | 'id' | 'id desc'>) | undefined, $select?: (Array<'identifier' | 'name' | 'title' | 'description' | 'distribution' | 'webService' | 'dataDictionary' | 'keyword' | 'modified' | 'publisher' | 'contactPoint' | 'mbox' | 'accessLevel' | 'landingPage' | 'temporal' | 'api' | 'version' | 'recordCount' | 'bureauCode' | 'programCode' | 'accessLevelComment' | 'license' | 'spatial' | 'theme' | 'dataQuality' | 'accrualPeriodicity' | 'language' | 'primaryITInvestmentUII' | 'references' | 'issued' | 'systemOfRecords' | 'depDate' | 'depApiMessage' | 'depWebMessage' | 'depNewURL' | 'lastRefresh' | 'lastDataSetRefresh' | 'hash' | 'id'>) | undefined, $format?: ('json' | 'jsona' | 'csv' | 'jsonl' | 'parquet') | undefined}; export type GetApiV1DataSetsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const metadata = z.object({skip: z.number().nullable().optional(), filter: z.string().nullable().optional(), orderBy: z.string().nullable().optional(), select: z.string().nullable().optional(), rundate: z.string().nullable().optional(), top: z.number().nullable().optional(), format: z.string().nullable().optional(), metadata: z.boolean().nullable().optional(), entityname: z.string().optional(), version: z.string().optional(), url: z.string().optional(), count: z.number().nullable().optional()}); export const v1DataSets = z.object({identifier: z.string().optional(), name: z.string().optional(), title: z.string().optional(), description: z.string().optional(), distribution: z.array(z.object({})).nullable().optional(), webService: z.string().optional(), dataDictionary: z.string().optional(), keyword: z.array(z.object({})).nullable().optional(), modified: z.string().nullable().optional(), publisher: z.string().optional(), contactPoint: z.string().optional(), mbox: z.string().optional(), accessLevel: z.string().optional(), landingPage: z.string().nullable().optional(), temporal: z.string().nullable().optional(), api: z.boolean().optional(), version: z.number().optional(), recordCount: z.number().nullable().optional(), bureauCode: z.array(z.object({})).optional(), programCode: z.array(z.object({})).optional(), accessLevelComment: z.string().nullable().optional(), license: z.string().nullable().optional(), spatial: z.string().nullable().optional(), theme: z.string().optional(), dataQuality: z.string().optional(), accrualPeriodicity: z.string().optional(), language: z.string().optional(), primaryITInvestmentUII: z.string().nullable().optional(), references: z.array(z.object({})).nullable().optional(), issued: z.string().optional(), systemOfRecords: z.string().nullable().optional(), depDate: z.string().nullable().optional(), depApiMessage: z.string().nullable().optional(), depWebMessage: z.string().nullable().optional(), depNewURL: z.string().nullable().optional(), lastRefresh: z.string().optional(), lastDataSetRefresh: z.string().optional(), hash: z.string().optional(), id: z.string().optional()}); export const getApiV1DataSetsResponse = z.object({metadata: metadata.optional(), DataSets: z.array(v1DataSets).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 }; /** * OpenFEMA Data Sets * * GET /v1/DataSets */ export const getApiV1DataSets = async (args: GetApiV1DataSetsArgs, options?: GetApiV1DataSetsOptions) => { const {baseUrl = "https://www.fema.gov/api/open", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.$filter != null) query.append("$filter", String(args.$filter)) if (args.$top != null) query.append("$top", String(args.$top)) if (args.$skip != null) query.append("$skip", String(args.$skip)) if (args.$count != null) query.append("$count", String(args.$count)) if (args.$filename != null) query.append("$filename", String(args.$filename)) if (args.$metadata != null) query.append("$metadata", String(args.$metadata)) if (args.$gzip != null) query.append("$gzip", String(args.$gzip)) for (const item of args.$orderby ?? []) query.append("$orderby", String(item)) for (const item of args.$select ?? []) query.append("$select", String(item)) if (args.$format != null) query.append("$format", String(args.$format)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/DataSets${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(getApiV1DataSetsResponse, await res.json()) };