import {z} from 'zod' export type GetApiAgentFriendlyTaSlugMacdArgs = {slug: string, short?: number | undefined, long?: number | undefined, signal?: number | undefined, fetchInterval?: ('m1' | 'm5' | 'm15' | 'm30' | 'h1' | 'h2' | 'h6' | 'h12' | 'd1') | undefined}; export type GetApiAgentFriendlyTaSlugMacdOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiAgentFriendlyTaSlugMacdResponse = z.object({}); 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 }; /** * Get MACD for trend and momentum analysis * * GET /agentFriendly/ta/{slug}/macd * * Also accepts auth: apiKey, x402 */ export const getApiAgentFriendlyTaSlugMacd = async (args: GetApiAgentFriendlyTaSlugMacdArgs, options?: GetApiAgentFriendlyTaSlugMacdOptions) => { const {baseUrl = "https://rest.coincap.io/v3", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.short != null) query.append("short", String(args.short)) if (args.long != null) query.append("long", String(args.long)) if (args.signal != null) query.append("signal", String(args.signal)) if (args.fetchInterval != null) query.append("fetchInterval", String(args.fetchInterval)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/agentFriendly/ta/${encodeURIComponent(String(args.slug))}/macd${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(getApiAgentFriendlyTaSlugMacdResponse, await res.json()) };