UNPKG

kalshi-typescript

Version:
73 lines (72 loc) 2.3 kB
/** * Kalshi Trade API Manual Endpoints * Manually defined OpenAPI spec for endpoints being migrated to spec-first approach * * The version of the OpenAPI document: 3.11.0 * * * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). * https://openapi-generator.tech * Do not edit the class manually. */ import globalAxios from 'axios'; import { KalshiAuth } from './auth'; export const BASE_PATH = "https://api.elections.kalshi.com/trade-api/v2".replace(/\/+$/, ""); /** * * @export */ export const COLLECTION_FORMATS = { csv: ",", ssv: " ", tsv: "\t", pipes: "|", }; /** * * @export * @class BaseAPI */ export class BaseAPI { constructor(configuration, basePath = BASE_PATH, axios = globalAxios) { this.basePath = basePath; this.axios = axios; if (configuration) { this.configuration = configuration; this.basePath = configuration.basePath || this.basePath; // Initialize auth if credentials are provided if (configuration.apiKey && (configuration.privateKeyPath || configuration.privateKeyPem)) { const privateKey = configuration.privateKeyPem || (configuration.privateKeyPath ? require('fs').readFileSync(configuration.privateKeyPath, 'utf8') : ''); this.auth = new KalshiAuth(configuration.apiKey, privateKey); } } // Add request interceptor for authentication this.axios.interceptors.request.use((config) => { if (this.auth && config.url && config.method) { // Extract path from URL const url = new URL(config.url, this.basePath); const path = url.pathname; const method = config.method.toUpperCase(); // Generate and add auth headers const authHeaders = this.auth.generateAuthHeaders(method, path); Object.assign(config.headers, authHeaders); } return config; }); } } ; /** * * @export * @class RequiredError * @extends {Error} */ export class RequiredError extends Error { constructor(field, msg) { super(msg); this.field = field; this.name = "RequiredError"; } }