@rpidanny/nepse.js
Version:
Fetch stock data from Nepal Stock Exchange
59 lines • 2.43 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Nepse = void 0;
const generic_client_1 = require("../utils/generic-client");
const errors_1 = require("./errors");
class Nepse extends generic_client_1.GenericClient {
constructor() {
super(...arguments);
this.upstreamName = 'nepse';
this.endpoint = 'https://newweb.nepalstock.com/api';
}
async getAuthHeader() {
const { accessToken } = await this.authenticate();
const token = this.patchAccessToken(accessToken);
return `Salter ${token}`;
}
handleResponse(response, expectedStatusCode = 200) {
const { statusCode, body } = response;
if (expectedStatusCode === statusCode) {
return body;
}
throw new errors_1.UnexpectedUpstreamResponseError(this.upstreamName, statusCode, body);
}
patchAccessToken(token) {
const bodyPrefix = 'eyJpc3MiOiJ5Y28iLCJzdWIiOiIxMiIsImlhdCI6M';
const temp = token.split('.');
const bodySuffix = temp[1].slice(bodyPrefix.length + 2);
return `${temp[0]}.${bodyPrefix}${bodySuffix}.${temp[2]}`;
}
async authenticate() {
const res = await this.call('get', 'authenticate/prove', {});
return this.handleResponse(res);
}
async getTodaysPricesExport(date) {
const res = await this.call('get', `nots/market/export/todays-price/${date}`, {
responseType: undefined,
});
return this.handleResponse(res);
}
async getFloorSheet(page = 0, size = 500) {
const res = await this.callWithAuth('post', `nots/nepse-data/floorsheet?&sort=contractId,desc&size=${size}&page=${page}`, {
json: { id: 259 },
retry: {
methods: ['POST'],
},
});
return this.handleResponse(res);
}
async getSecurities(includeDelisted = false) {
const res = await this.callWithAuth('get', `nots/security?nonDelisted=${!includeDelisted}`, {});
return this.handleResponse(res);
}
async getSecurityHistory(securityId, startDate, endDate, page = 0, size = 500) {
const res = await this.callWithAuth('get', `nots/market/history/security/${securityId}?&page=${page}&size=${size}&startDate=${startDate}&endDate=${endDate}`);
return this.handleResponse(res);
}
}
exports.Nepse = Nepse;
//# sourceMappingURL=nepse.js.map