import {z} from 'zod' export type RFPStatus = 'active' | 'expired' | 'no_due_date'; export type RFPSearchFilters = {query?: (string | null) | undefined, status?: (RFPStatus | null) | undefined, due_after?: (string | null) | undefined, due_before?: (string | null) | undefined, institution_ids?: (Array | null) | undefined, states?: (Array | null) | undefined, source_tables?: (Array | null) | undefined, sort?: ('due_date_asc' | 'due_date_desc' | 'updated_desc' | 'relevance') | undefined, offset?: number | undefined, limit?: number | undefined}; export type CreateApiApiInternalRfpsSearchArgs = {body: RFPSearchFilters}; export type CreateApiApiInternalRfpsSearchOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const rFPStatus = z.enum(["active", "expired", "no_due_date"]); export const rFPResponse = z.object({id: z.number().int(), rfp_id: z.string().nullable().optional(), title: z.string().nullable().optional(), description: z.string().nullable().optional(), agency: z.string().nullable().optional(), due_date: z.string().nullable().optional(), rfp_url: z.string().nullable().optional(), source_url: z.string().nullable().optional(), created_at: z.string().nullable().optional(), updated_at: z.string().nullable().optional(), institution_id: z.string().nullable().optional(), institution_name: z.string().nullable().optional(), institution_state: z.string().nullable().optional(), institution_type: z.string().nullable().optional(), status: rFPStatus.nullable().optional(), days_until_due: z.number().int().nullable().optional(), relevance: z.number().nullable().optional()}); export const paginatedResponseRFPResponse = z.object({results: z.array(rFPResponse), total: z.number().int(), offset: z.number().int(), limit: z.number().int()}); 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 }; /** * Search Rfps * * POST /api/internal/rfps/search */ export const createApiApiInternalRfpsSearch = async (args: CreateApiApiInternalRfpsSearchArgs, options: CreateApiApiInternalRfpsSearchOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/internal/rfps/search${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(paginatedResponseRFPResponse, await res.json()) };