import {z} from 'zod' export type GetApiFundingV1ProjectsArgs = {limit?: number | undefined, page?: number | undefined, id?: Array | undefined, category?: Array | undefined, sector?: Array | undefined, tags?: Array | undefined, foundedBefore?: string | undefined, foundedAfter?: string | undefined}; export type GetApiFundingV1ProjectsOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const fundingLink = z.record(z.string(), z.unknown()); export const fundingAsset = z.record(z.string(), z.unknown()); export const project = z.object({background: z.string(), category: z.string().optional(), foundedDate: z.string().optional(), id: z.string(), links: z.array(fundingLink), location: z.string().optional(), name: z.string().optional(), organizations: z.array(z.string()), overview: z.string(), primaryAsset: fundingAsset.optional(), secondaryAssets: z.array(z.string()), sector: z.string().optional(), tags: z.array(z.string())}); export const pagination = z.object({limit: z.number().int(), page: z.number().int(), totalPages: z.number().int(), totalRows: z.number().int()}); export const getApiFundingV1ProjectsResponse = z.object({data: z.array(project), error: z.string().optional(), metadata: pagination.optional()}); 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 }; /** * GetProjects * * GET /funding/v1/projects */ export const getApiFundingV1Projects = async (args: GetApiFundingV1ProjectsArgs, options?: GetApiFundingV1ProjectsOptions) => { const {baseUrl = "https://api.messari.io", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.page != null) query.append("page", String(args.page)) for (const item of args.id ?? []) query.append("id", String(item)) for (const item of args.category ?? []) query.append("category", String(item)) for (const item of args.sector ?? []) query.append("sector", String(item)) for (const item of args.tags ?? []) query.append("tags", String(item)) if (args.foundedBefore != null) query.append("foundedBefore", String(args.foundedBefore)) if (args.foundedAfter != null) query.append("foundedAfter", String(args.foundedAfter)) if (apiKey !== undefined) headers.set("X-Messari-API-Key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/funding/v1/projects${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(getApiFundingV1ProjectsResponse, await res.json()) };