UNPKG

smartapi-typescript

Version:

TypeScript library for Angel One SmartAPI broker API

165 lines 5.98 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.del = exports.put = exports.post = exports.get = exports.handleApiError = exports.createHttpClient = void 0; const axios_1 = __importDefault(require("axios")); const errorCodes_1 = require("../constants/errorCodes"); /** * Creates a configured axios instance for making HTTP requests * @param baseURL The base URL for all requests * @param headers The default headers to include with each request * @returns Axios instance */ const createHttpClient = (baseURL, headers = {}) => { return axios_1.default.create({ baseURL, headers }); }; exports.createHttpClient = createHttpClient; /** * Handles API errors and formats error responses consistently * According to SmartAPI documentation format: * { * "status": false, * "message": "Error message", * "errorcode": "ERROR_CODE", * "data": null * } * @param error The error caught from an API request * @returns Formatted API error response */ const handleApiError = (error) => { var _a, _b, _c; if (error.response) { // Server responded with a status code outside of 2xx range let errorCode = ((_a = error.response.data) === null || _a === void 0 ? void 0 : _a.errorcode) || String(error.response.status); let errorMessage = ((_b = error.response.data) === null || _b === void 0 ? void 0 : _b.message) || 'API request failed'; // Use our error descriptions if we have a known error code if ((_c = error.response.data) === null || _c === void 0 ? void 0 : _c.errorcode) { const betterDescription = (0, errorCodes_1.getErrorDescription)(error.response.data.errorcode); if (betterDescription !== 'Unknown Error') { errorMessage = betterDescription; } } return { status: false, message: errorMessage, errorcode: errorCode, data: null // According to the documentation, failed responses have data: null }; } else if (error.request) { // Request was made but no response received return { status: false, message: 'No response received from server', errorcode: 'NETWORK_ERROR', data: null }; } else { // Error in setting up the request return { status: false, message: error.message || 'Unknown error occurred', errorcode: 'REQUEST_SETUP_ERROR', data: null }; } }; exports.handleApiError = handleApiError; /** * Makes an HTTP GET request to the specified endpoint * @param url The endpoint URL * @param headers Additional headers to include * @param params Query parameters * @returns Promise with the API response */ const get = (url_1, ...args_1) => __awaiter(void 0, [url_1, ...args_1], void 0, function* (url, headers = {}, params) { try { const config = { headers, params }; const response = yield axios_1.default.get(url, config); return response.data; } catch (error) { return (0, exports.handleApiError)(error); } }); exports.get = get; /** * Makes an HTTP POST request to the specified endpoint * @param url The endpoint URL * @param data The request body data * @param headers Additional headers to include * @returns Promise with the API response */ const post = (url_1, data_1, ...args_1) => __awaiter(void 0, [url_1, data_1, ...args_1], void 0, function* (url, data, headers = {}) { try { const config = { headers }; const response = yield axios_1.default.post(url, data, config); return response.data; } catch (error) { return (0, exports.handleApiError)(error); } }); exports.post = post; /** * Makes an HTTP PUT request to the specified endpoint * @param url The endpoint URL * @param data The request body data * @param headers Additional headers to include * @returns Promise with the API response */ const put = (url_1, data_1, ...args_1) => __awaiter(void 0, [url_1, data_1, ...args_1], void 0, function* (url, data, headers = {}) { try { const config = { headers }; const response = yield axios_1.default.put(url, data, config); return response.data; } catch (error) { return (0, exports.handleApiError)(error); } }); exports.put = put; /** * Makes an HTTP DELETE request to the specified endpoint * @param url The endpoint URL * @param headers Additional headers to include * @param params Query parameters * @returns Promise with the API response */ const del = (url_1, ...args_1) => __awaiter(void 0, [url_1, ...args_1], void 0, function* (url, headers = {}, params) { try { const config = { headers, params }; const response = yield axios_1.default.delete(url, config); return response.data; } catch (error) { return (0, exports.handleApiError)(error); } }); exports.del = del; //# sourceMappingURL=http.js.map