import {z} from 'zod' export type GetApiApiContractsArgs = {q?: string | undefined, naics?: string | undefined, psc?: string | undefined, agency?: string | undefined, set_aside?: string | undefined, state?: string | undefined, type?: string | undefined, after?: string | undefined, posted_after?: string | undefined, deadline_after?: string | undefined, active?: boolean | undefined, sort?: ('newest' | 'deadline') | undefined, cursor?: string | undefined, limit?: number | undefined}; export type GetApiApiContractsOptions = {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 contract = z.object({active: z.boolean(), agency: z.string().nullable(), amount: z.number().nullable(), archive_date: z.string().nullable(), award: z.object({amount: z.number().nullable().optional(), awardee: z.string().nullable().optional(), date: z.string().nullable().optional(), number: z.string().nullable().optional()}).nullable(), date: z.string(), description: z.string().nullable(), id: z.string(), naics: classification.nullable(), notice_type: z.string().nullable(), office: z.string().nullable(), place: place, psc: classification.nullable(), reference: z.string(), response_deadline: z.string().nullable(), set_aside: z.object({code: z.string().nullable().optional(), name: z.string().nullable().optional()}).nullable(), solicitation_number: z.string().nullable(), source: z.string(), sub_agency: z.string().nullable(), title: z.string(), url: z.string().nullable()}); export const contractsResponse = z.object({as_of: z.string().nullable(), contracts: z.array(contract), 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 opportunities * * GET /api/contracts */ export const getApiApiContracts = async (args: GetApiApiContractsArgs, options?: GetApiApiContractsOptions) => { 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.naics != null) query.append("naics", String(args.naics)) if (args.psc != null) query.append("psc", String(args.psc)) if (args.agency != null) query.append("agency", String(args.agency)) if (args.set_aside != null) query.append("set_aside", String(args.set_aside)) if (args.state != null) query.append("state", String(args.state)) if (args.type != null) query.append("type", String(args.type)) if (args.after != null) query.append("after", String(args.after)) if (args.posted_after != null) query.append("posted_after", String(args.posted_after)) if (args.deadline_after != null) query.append("deadline_after", String(args.deadline_after)) if (args.active != null) query.append("active", String(args.active)) 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/contracts${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(contractsResponse, await res.json()) };