import {z} from 'zod' export type GetApiApiLibraryIndicatorsQueryArgs = {text?: string | undefined, creator?: string | undefined, tier?: string | undefined, platform?: string | undefined, authorName?: string | undefined, family?: ('trend' | 'momentum' | 'volatility' | 'volume-orderflow' | 'market-structure' | 'smc-ict' | 'wyckoff' | 'elliott-harmonics' | 'patterns' | 'levels' | 'statistics' | 'machine-learning' | 'time-seasonality' | 'sentiment-breadth' | 'risk-exits' | 'meta-composition' | 'validation') | undefined, concept?: string | undefined, slugs?: string | undefined, fields?: ('full' | 'card') | undefined, sort?: ('name' | 'creationDateDisplayed' | 'family') | undefined, direction?: ('asc' | 'desc') | undefined, pageIndex?: string | undefined, pageQuantity?: string | undefined}; export type GetApiApiLibraryIndicatorsQueryOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiLibraryIndicatorsQueryResponse = z.object({data: z.object({indicators: z.array(z.object({id: z.string(), name: z.string().nullable(), slug: z.string(), imageUrl: z.string().nullable(), description: z.string().nullable(), body: z.string().nullable(), authorName: z.string().nullable(), creationDateDisplayed: z.string(), isPublished: z.boolean(), pineScriptCodeId: z.string().nullable(), family: z.enum(["trend", "momentum", "volatility", "volume-orderflow", "market-structure", "smc-ict", "wyckoff", "elliott-harmonics", "patterns", "levels", "statistics", "machine-learning", "time-seasonality", "sentiment-breadth", "risk-exits", "meta-composition", "validation"]).nullable(), creator: z.object({slug: z.string()}), plans: z.array(z.object({id: z.string()}))})), count: z.number()}), errors: 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 }; /** * Public: filtered/paginated published library indicators with LuxAlgo pinning * * GET /api/library/indicators/query */ export const getApiApiLibraryIndicatorsQuery = async (args: GetApiApiLibraryIndicatorsQueryArgs, options?: GetApiApiLibraryIndicatorsQueryOptions) => { const {baseUrl = "https://app.luxalgo.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.text != null) query.append("text", String(args.text)) if (args.creator != null) query.append("creator", String(args.creator)) if (args.tier != null) query.append("tier", String(args.tier)) if (args.platform != null) query.append("platform", String(args.platform)) if (args.authorName != null) query.append("authorName", String(args.authorName)) if (args.family != null) query.append("family", String(args.family)) if (args.concept != null) query.append("concept", String(args.concept)) if (args.slugs != null) query.append("slugs", String(args.slugs)) if (args.fields != null) query.append("fields", String(args.fields)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.direction != null) query.append("direction", String(args.direction)) if (args.pageIndex != null) query.append("pageIndex", String(args.pageIndex)) if (args.pageQuantity != null) query.append("pageQuantity", String(args.pageQuantity)) const url = `${baseUrl.replace(/\/+$/, '')}/api/library/indicators/query${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(getApiApiLibraryIndicatorsQueryResponse, await res.json()) };