import {z} from 'zod' export type GetApiV1CftcContractsArgs = {query?: string | undefined, limit?: number | undefined}; export type GetApiV1CftcContractsOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const listingNotice = z.object({status: z.string().nullable().optional(), ticker: z.string().nullable().optional(), delistedOn: z.string().nullable().optional(), successorTicker: z.string().nullable().optional()}); export const cftcContractDto = z.object({marketCode: z.string().nullable().optional(), marketName: z.string().nullable().optional(), category: z.string().nullable().optional()}); export const cftcContractDtoListResponse = z.object({listingNotice: listingNotice.optional(), data: z.array(cftcContractDto).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 }; /** * Search tracked CFTC futures contracts. An exact market code or verified futures alias wins; otherwise every punctuation-independent query word must match before a sparse any-word fallback. Results are sorted by category then market name. The tracked universe is a curated set of ~35 major contracts; omit query to list all of them (the default limit fits the full universe). * * GET /v1/cftc/contracts */ export const getApiV1CftcContracts = async (args: GetApiV1CftcContractsArgs, options?: GetApiV1CftcContractsOptions) => { const {baseUrl = "https://api.equibles.com", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.query != null) query.append("query", String(args.query)) if (args.limit != null) query.append("limit", String(args.limit)) if (ApiKey !== undefined) headers.set('authorization', `Bearer ${ApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/cftc/contracts${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(cftcContractDtoListResponse, await res.json()) };