import {z} from 'zod' export type GetApiIntelV1EventsArgs = {limit?: number | undefined, page?: number | undefined, primaryAssets?: Array | undefined, secondaryAssets?: Array | undefined, primaryOrSecondaryAssets?: Array | undefined, startTime?: string | undefined, endTime?: string | undefined, importance?: Array | undefined, category?: Array | undefined, subcategory?: Array | undefined, tag?: Array | undefined, status?: Array | undefined, globalEvent?: boolean | undefined}; export type GetApiIntelV1EventsOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const intelAsset = z.object({id: z.string(), name: z.string(), slug: z.string(), symbol: z.string()}); export const resource = z.record(z.string(), z.unknown()); export const event = z.object({activationBlock: z.number().optional(), category: z.string(), eventDate: z.string().optional(), eventDetails: z.string(), eventName: z.string(), globalEvent: z.boolean(), id: z.string(), importance: z.string(), primaryAssets: z.array(intelAsset), resources: z.array(resource), secondaryAssets: z.array(intelAsset), status: z.string(), subcategory: z.string(), submissionDate: z.string(), tag: z.string(), updateDetails: z.string().optional()}); export const pagination = z.object({limit: z.number().int(), page: z.number().int(), totalPages: z.number().int(), totalRows: z.number().int()}); export const getApiIntelV1EventsResponse = z.object({data: z.array(event), 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 }; /** * List all events * * GET /intel/v1/events */ export const getApiIntelV1Events = async (args: GetApiIntelV1EventsArgs, options?: GetApiIntelV1EventsOptions) => { 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.primaryAssets ?? []) query.append("primaryAssets", String(item)) for (const item of args.secondaryAssets ?? []) query.append("secondaryAssets", String(item)) for (const item of args.primaryOrSecondaryAssets ?? []) query.append("primaryOrSecondaryAssets", String(item)) if (args.startTime != null) query.append("startTime", String(args.startTime)) if (args.endTime != null) query.append("endTime", String(args.endTime)) for (const item of args.importance ?? []) query.append("importance", String(item)) for (const item of args.category ?? []) query.append("category", String(item)) for (const item of args.subcategory ?? []) query.append("subcategory", String(item)) for (const item of args.tag ?? []) query.append("tag", String(item)) for (const item of args.status ?? []) query.append("status", String(item)) if (args.globalEvent != null) query.append("globalEvent", String(args.globalEvent)) if (apiKey !== undefined) headers.set("X-Messari-API-Key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/intel/v1/events${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(getApiIntelV1EventsResponse, await res.json()) };