import {z} from 'zod' export type GetApiApiAwardsArgs = {q?: string | undefined, recipient?: string | undefined, agency?: string | undefined, naics?: string | undefined, state?: string | undefined, min_amount?: number | undefined, after?: string | undefined, sort?: ('newest' | 'largest') | undefined, cursor?: string | undefined, limit?: number | undefined}; export type GetApiApiAwardsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const classification = z.object({code: z.string(), description: z.string().nullable()}); export const place = z.object({address: z.string().nullable(), city: z.string().nullable(), country: z.string().nullable(), latitude: z.number().nullable(), longitude: z.number().nullable(), state: z.string().nullable(), zip: z.string().nullable()}); export const award = z.object({agency: z.string().nullable(), amount: z.number().nullable(), award_id: z.string().nullable(), award_type: z.string().nullable(), date: z.string(), description: z.string().nullable(), end_date: z.string().nullable(), id: z.string(), naics: classification.nullable(), place: place, psc: classification.nullable(), recipient: z.string().nullable(), reference: z.string(), source: z.string(), start_date: z.string().nullable(), sub_agency: z.string().nullable(), url: z.string().nullable()}); export const awardsResponse = z.object({as_of: z.string().nullable(), awards: z.array(award), count: z.number().int(), next_cursor: z.string().nullable()}); 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 federal contract awards * * GET /api/awards */ export const getApiApiAwards = async (args: GetApiApiAwardsArgs, options?: GetApiApiAwardsOptions) => { const {baseUrl = "https://noozra.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.q != null) query.append("q", String(args.q)) if (args.recipient != null) query.append("recipient", String(args.recipient)) if (args.agency != null) query.append("agency", String(args.agency)) if (args.naics != null) query.append("naics", String(args.naics)) if (args.state != null) query.append("state", String(args.state)) if (args.min_amount != null) query.append("min_amount", String(args.min_amount)) if (args.after != null) query.append("after", String(args.after)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.limit != null) query.append("limit", String(args.limit)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/awards${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(awardsResponse, await res.json()) };