import {z} from 'zod' export type GrantStatus = 'active' | 'expired' | 'no_period'; export type AppInternalTypesGrantGrantSearchFilters = {query?: (string | null) | undefined, status?: (GrantStatus | null) | undefined, period_start_after?: (string | null) | undefined, period_end_before?: (string | null) | undefined, min_amount?: (number | null) | undefined, max_amount?: (number | null) | undefined, funding_agencies?: (Array | null) | undefined, exclude_funding_agencies?: (Array | null) | undefined, institution_ids?: (Array | null) | undefined, states?: (Array | null) | undefined, source_tables?: (Array | null) | undefined, sort?: ('last_modified_desc' | 'period_end_asc' | 'period_end_desc' | 'amount_desc' | 'relevance') | undefined, offset?: number | undefined, limit?: number | undefined}; export type CreateApiApiInternalGrantsSearchArgs = {body: AppInternalTypesGrantGrantSearchFilters}; export type CreateApiApiInternalGrantsSearchOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const grantStatus = z.enum(["active", "expired", "no_period"]); export const appInternalTypesGrantGrantResponse = z.object({id: z.string(), external_grant_id: z.string().nullable().optional(), title: z.string().nullable().optional(), description: z.string().nullable().optional(), funding_agency: z.string().nullable().optional(), awardee_name: z.string().nullable().optional(), awardee_state: z.string().nullable().optional(), total_obligated_amount: z.number().nullable().optional(), outlayed_amount: z.number().nullable().optional(), period_of_performance_start_date: z.string().nullable().optional(), period_of_performance_end_date: z.string().nullable().optional(), last_modified_date: z.string().nullable().optional(), grant_link: z.string().nullable().optional(), transaction_count: z.number().int().nullable().optional(), source: 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: grantStatus.nullable().optional(), days_until_end: z.number().int().nullable().optional(), relevance: z.number().nullable().optional()}); export const paginatedResponseGrantResponse = z.object({results: z.array(appInternalTypesGrantGrantResponse), 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 Grants * * POST /api/internal/grants/search */ export const createApiApiInternalGrantsSearch = async (args: CreateApiApiInternalGrantsSearchArgs, options: CreateApiApiInternalGrantsSearchOptions) => { 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/grants/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(paginatedResponseGrantResponse, await res.json()) };