import {z} from 'zod' export type GetApiRedditStocksV1TrendingArgs = {from?: (string | null) | undefined, to?: (string | null) | undefined, limit?: number | undefined, offset?: number | undefined, type?: ('stock' | 'etf' | 'all' | null) | undefined}; export type GetApiRedditStocksV1TrendingOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const trendingStock = z.object({ticker: z.string(), company_name: z.string().nullable().optional(), buzz_score: z.number().gte(0).lte(100), trend: z.enum(["rising", "falling", "stable"]), mentions: z.number().int(), unique_posts: z.number().int(), subreddit_count: z.number().int(), sentiment_score: z.number().gte(-1).lte(1), bullish_pct: z.number().int().gte(0).lte(100), bearish_pct: z.number().int().gte(0).lte(100), total_upvotes: z.number().int(), trend_history: z.array(z.number()).optional()}); export const getApiRedditStocksV1TrendingResponse = z.array(trendingStock); 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 }; /** * Trending stocks * * GET /reddit/stocks/v1/trending */ export const getApiRedditStocksV1Trending = async (args: GetApiRedditStocksV1TrendingArgs, options?: GetApiRedditStocksV1TrendingOptions) => { 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 (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.type != null) query.append("type", String(args.type)) if (ApiKeyAuth !== undefined) headers.set("X-API-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/reddit/stocks/v1/trending${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(getApiRedditStocksV1TrendingResponse, await res.json()) };