import {z} from 'zod' export type GetApiV1CftcPositionsLatestArgs = {category?: string | undefined}; export type GetApiV1CftcPositionsLatestOptions = {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 cftcLatestPositionDto = z.object({marketCode: z.string().nullable().optional(), marketName: z.string().nullable().optional(), category: z.string().nullable().optional(), reportDate: z.string().nullable().optional(), openInterest: z.number().int().nullable().optional(), commNet: z.number().int().nullable().optional(), nonCommNet: z.number().int().nullable().optional()}); export const cftcLatestPositionDtoListResponse = z.object({listingNotice: listingNotice.optional(), data: z.array(cftcLatestPositionDto).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 }; /** * Latest COT positioning snapshot across all tracked futures contracts, sorted by category then market name. Net positions are long minus short; report fields are null for contracts without stored reports yet. * * GET /v1/cftc/positions/latest */ export const getApiV1CftcPositionsLatest = async (args: GetApiV1CftcPositionsLatestArgs, options?: GetApiV1CftcPositionsLatestOptions) => { 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.category != null) query.append("category", String(args.category)) if (ApiKey !== undefined) headers.set('authorization', `Bearer ${ApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/cftc/positions/latest${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(cftcLatestPositionDtoListResponse, await res.json()) };