import {z} from 'zod' export type FundingStatusEnum = 'active' | 'expired' | 'no_funding_period'; export type AppTypesGrantGrantSearchFilters = {include_words?: (Array | null) | undefined, exclude_words?: (Array | null) | undefined, keyword_logic?: (string | null) | undefined, title_only?: boolean | undefined, min_amount?: (number | null) | undefined, max_amount?: (number | null) | undefined, funding_agencies_include?: (Array | null) | undefined, funding_agencies_exclude?: (Array | null) | undefined, funding_period_start?: (string | null) | undefined, funding_period_end?: (string | null) | undefined, funding_statuses?: (Array | null) | undefined, selected_states?: (Array | null) | undefined, selected_institution_types?: (Array | null) | undefined, institutions_include?: (Array | null) | undefined, offset?: number | undefined, limit?: number | undefined}; export type CreateApiApiV3GrantsSearchArgs = {body: AppTypesGrantGrantSearchFilters}; export type CreateApiApiV3GrantsSearchOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const grantTransactionResponse = z.object({id: z.string(), institution_id: z.string().nullable(), total_obligated_amount: z.number(), outlayed_amount: z.number().nullable(), period_of_performance_start_date: z.string().nullable(), period_of_performance_end_date: z.string().nullable(), last_modified_date: z.string().nullable(), source_url: z.string().nullable(), action_type: z.string().nullable(), action_type_description: z.string().nullable(), awardee_name: z.string().nullable().optional()}); export const grantDetailResponse = z.object({id: z.string(), title: z.string(), description: z.string().nullable().optional(), funding_agency: z.string(), awardee_name: z.string().nullable().optional(), total_obligated_amount: z.number(), period_of_performance_start_date: z.string().nullable().optional(), period_of_performance_end_date: z.string().nullable().optional(), last_modified_date: z.string(), grant_link: z.string().nullable().optional(), outlayed_amount: z.number().nullable().optional(), external_grant_id: z.string().nullable().optional(), transaction_count: z.number().int().optional(), institution_name: z.string().nullable().optional(), institution_state: z.string().nullable().optional(), institution_type: z.string().nullable().optional(), institution_id: z.string().nullable().optional(), relevance_rank: z.number().nullable().optional(), score: z.number().nullable().optional(), relevance_score: z.number().nullable().optional(), decay_score: z.number().nullable().optional(), transactions: z.array(grantTransactionResponse).optional()}); export const paginatedResponseGrantDetailResponse = z.object({results: z.array(grantDetailResponse), 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/v3/grants/search */ export const createApiApiV3GrantsSearch = async (args: CreateApiApiV3GrantsSearchArgs, options: CreateApiApiV3GrantsSearchOptions) => { 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/v3/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(paginatedResponseGrantDetailResponse, await res.json()) };