import {z} from 'zod' export type GetApiV1SymbolsArgs = {asset_class?: string | undefined, venue?: string | undefined, q?: string | undefined, cursor?: string | undefined, limit?: number | undefined}; export type GetApiV1SymbolsOptions = {baseUrl?: string | undefined, ApiKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const catalogInstrument = z.object({slug: z.string(), asset_class: z.enum(["stocks", "crypto", "dex", "forex", "commodities"]), venue: z.string(), market_id: z.string().optional(), symbol: z.string(), name: z.string().optional(), base_asset: z.string().optional(), quote_asset: z.string().optional(), currency: z.string().optional(), status: z.enum(["active", "inactive", "delisted"]), details: z.record(z.string(), z.unknown())}); export const symbolCatalogListResponse = z.object({data: z.array(catalogInstrument), meta: z.object({as_of: z.string(), count: z.number().int().gte(0), next_cursor: z.string().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 }; /** * List active instruments * * GET /v1/symbols * * Also accepts auth: ApiKeyQuery */ export const getApiV1Symbols = async (args: GetApiV1SymbolsArgs, options?: GetApiV1SymbolsOptions) => { const {baseUrl = "https://api.sifting.io", fetch: fetchFn = fetch, ApiKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.asset_class != null) query.append("asset_class", String(args.asset_class)) if (args.venue != null) query.append("venue", String(args.venue)) if (args.q != null) query.append("q", String(args.q)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.limit != null) query.append("limit", String(args.limit)) if (ApiKeyHeader !== undefined) headers.set("X-API-Key", ApiKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v1/symbols${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(symbolCatalogListResponse, await res.json()) };