UNPKG

kalshi-typescript

Version:

Official TypeScript SDK for the Kalshi API

78 lines (77 loc) 2.53 kB
"use strict"; /** * Kalshi Trade API Manual Endpoints * Manually defined OpenAPI spec for endpoints being migrated to spec-first approach * * The version of the OpenAPI document: 3.6.0 * * * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). * https://openapi-generator.tech * Do not edit the class manually. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.RequiredError = exports.BaseAPI = exports.COLLECTION_FORMATS = exports.BASE_PATH = void 0; const axios_1 = require("axios"); const auth_1 = require("./auth"); exports.BASE_PATH = "https://api.elections.kalshi.com/trade-api/v2".replace(/\/+$/, ""); /** * * @export */ exports.COLLECTION_FORMATS = { csv: ",", ssv: " ", tsv: "\t", pipes: "|", }; /** * * @export * @class BaseAPI */ class BaseAPI { constructor(configuration, basePath = exports.BASE_PATH, axios = axios_1.default) { 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 auth_1.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; }); } } exports.BaseAPI = BaseAPI; ; /** * * @export * @class RequiredError * @extends {Error} */ class RequiredError extends Error { constructor(field, msg) { super(msg); this.field = field; this.name = "RequiredError"; } } exports.RequiredError = RequiredError;