import {z} from 'zod' export type GetApiRedditCryptoV1TokenSymbolArgs = {symbol: string, from?: (string | null) | undefined, to?: (string | null) | undefined}; export type GetApiRedditCryptoV1TokenSymbolOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const cryptoSubredditCount = z.object({subreddit: z.string(), mentions: z.number().int().gte(0), sentiment_score: z.number().gte(-1).lte(1).nullable().optional(), buzz_score: z.number().gte(0).lte(100).nullable().optional(), count: z.number().int().gte(0)}); export const cryptoDailyTrendItem = z.object({date: z.string(), mentions: z.number().int(), sentiment_score: z.number().gte(-1).lte(1).nullable().optional(), buzz_score: z.number().gte(0).lte(100).nullable().optional(), bullish_pct: z.number().int().gte(0).lte(100).nullable().optional(), bearish_pct: z.number().int().gte(0).lte(100).nullable().optional()}); export const cryptoTopMention = z.object({text_snippet: z.string(), sentiment_score: z.number().gte(-1).lte(1).nullable().optional(), sentiment_label: z.string().nullable().optional(), upvotes: z.number().int(), subreddit: z.string().nullable().optional(), created_utc: z.string()}); export const cryptoTokenSentiment = z.object({symbol: z.string(), name: z.string().nullable().optional(), found: z.boolean(), buzz_score: z.number().gte(0).lte(100).nullable().optional(), mentions: z.number().int().nullable().optional(), sentiment_score: z.number().gte(-1).lte(1).nullable().optional(), positive_count: z.number().int().nullable().optional(), negative_count: z.number().int().nullable().optional(), neutral_count: z.number().int().nullable().optional(), total_upvotes: z.number().int().nullable().optional(), unique_posts: z.number().int().nullable().optional(), subreddit_count: z.number().int().nullable().optional(), trend: z.enum(["rising", "falling", "stable"]).nullable().optional(), bullish_pct: z.number().int().gte(0).lte(100).nullable().optional(), bearish_pct: z.number().int().gte(0).lte(100).nullable().optional(), period_days: z.number().int().nullable().optional(), top_subreddits: z.array(cryptoSubredditCount).nullable().optional(), daily_trend: z.array(cryptoDailyTrendItem).nullable().optional(), top_mentions: z.array(cryptoTopMention).nullable().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 }; /** * Token sentiment * * GET /reddit/crypto/v1/token/{symbol} */ export const getApiRedditCryptoV1TokenSymbol = async (args: GetApiRedditCryptoV1TokenSymbolArgs, options?: GetApiRedditCryptoV1TokenSymbolOptions) => { const {baseUrl = "https://api.adanos.com", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) if (ApiKeyAuth !== undefined) headers.set("X-API-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/reddit/crypto/v1/token/${encodeURIComponent(String(args.symbol))}${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(cryptoTokenSentiment, await res.json()) };