import {z} from 'zod' export type GetApiRedditCryptoV1SearchArgs = {q: string, limit?: number | undefined}; export type GetApiRedditCryptoV1SearchOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const cryptoSearchSummary = z.object({mentions: z.number().int().gte(0), buzz_score: z.number().gte(0).lte(100).nullable(), trend: z.enum(["rising", "falling", "stable"]).nullable().optional(), sentiment_score: z.number().gte(-1).lte(1).nullable().optional()}); export const cryptoSearchItem = z.object({symbol: z.string(), name: z.string(), market_cap_rank: z.number().int().nullable().optional(), market_cap_usd: z.number().nullable().optional(), aliases: z.array(z.string()).optional(), summary: cryptoSearchSummary}); export const cryptoSearchResponse = z.object({query: z.string(), count: z.number().int(), period_days: z.number().int().gte(1), results: z.array(cryptoSearchItem)}); 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 }; /** * Search tokens * * GET /reddit/crypto/v1/search */ export const getApiRedditCryptoV1Search = async (args: GetApiRedditCryptoV1SearchArgs, options?: GetApiRedditCryptoV1SearchOptions) => { 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.q != null) query.append("q", String(args.q)) if (args.limit != null) query.append("limit", String(args.limit)) if (ApiKeyAuth !== undefined) headers.set("X-API-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/reddit/crypto/v1/search${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(cryptoSearchResponse, await res.json()) };