import {z} from 'zod' export type GetApiV2EmergencyManagementPerformanceGrantsArgs = {$filter?: string | undefined, $top?: number | undefined, $skip?: number | undefined, $count?: boolean | undefined, $filename?: string | undefined, $metadata?: boolean | undefined, $gzip?: boolean | undefined, $orderby?: (Array<'id' | 'id desc' | 'reportingPeriod' | 'reportingPeriod desc' | 'state' | 'state desc' | 'legalAgencyName' | 'legalAgencyName desc' | 'projectType' | 'projectType desc' | 'projectStartDate' | 'projectStartDate desc' | 'projectEndDate' | 'projectEndDate desc' | 'nameOfProgram' | 'nameOfProgram desc' | 'fundingAmount' | 'fundingAmount desc'>) | undefined, $select?: (Array<'id' | 'reportingPeriod' | 'state' | 'legalAgencyName' | 'projectType' | 'projectStartDate' | 'projectEndDate' | 'nameOfProgram' | 'fundingAmount'>) | undefined, $format?: ('json' | 'jsona' | 'csv' | 'jsonl' | 'parquet') | undefined}; export type GetApiV2EmergencyManagementPerformanceGrantsOptions = {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 v2EmergencyManagementPerformanceGrants = z.object({id: z.string().optional(), reportingPeriod: z.string().optional(), state: z.string().optional(), legalAgencyName: z.string().optional(), projectType: z.string().optional(), projectStartDate: z.string().optional(), projectEndDate: z.string().optional(), nameOfProgram: z.string().optional(), fundingAmount: z.number().optional()}); export const getApiV2EmergencyManagementPerformanceGrantsResponse = z.object({metadata: metadata.optional(), EmergencyManagementPerformanceGrants: z.array(v2EmergencyManagementPerformanceGrants).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 }; /** * Emergency Management Performance Grants * * GET /v2/EmergencyManagementPerformanceGrants */ export const getApiV2EmergencyManagementPerformanceGrants = async (args: GetApiV2EmergencyManagementPerformanceGrantsArgs, options?: GetApiV2EmergencyManagementPerformanceGrantsOptions) => { 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(/\/+$/, '')}/v2/EmergencyManagementPerformanceGrants${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(getApiV2EmergencyManagementPerformanceGrantsResponse, await res.json()) };