import {z} from 'zod' export type TelegramChannelData = {chatId: string}; export type TelegramChannel = {type: 'telegram', data: TelegramChannelData}; export type SlackChannelData = {webhookUrl: string}; export type SlackChannel = {type: 'slack', data: SlackChannelData}; export type DiscordChannelData = {webhookUrl: string}; export type DiscordChannel = {type: 'discord', data: DiscordChannelData}; export type WebhookChannelData = {webhookUrl: string, secret?: string | undefined}; export type WebhookChannel = {type: 'webhook', data: WebhookChannelData}; export type CreateAlertRequest = {name: string, type: 'common-token-transfer' | 'sm-token-flows' | 'smart-contract-call', timeWindow: 'realtime' | '1m' | '5m' | '10m' | '30m' | '1h' | '4h' | '12h' | '1d' | '1w', channels: Array, data: {[key: string]: unknown}, description?: string | undefined, isEnabled?: boolean | undefined, createdBy?: string | undefined}; export type CreateApiApiV1SmartAlertArgs = {body: CreateAlertRequest}; export type CreateApiApiV1SmartAlertOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiApiV1SmartAlertResponse = z.unknown(); 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 }; /** * Create a smart alert * * POST /api/v1/smart-alert */ export const createApiApiV1SmartAlert = async (args: CreateApiApiV1SmartAlertArgs, options?: CreateApiApiV1SmartAlertOptions) => { const {baseUrl = "https://api.nansen.ai", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("apiKey", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/smart-alert${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(createApiApiV1SmartAlertResponse, await res.json()) };