@rpidanny/nepse.js
Version:
Fetch stock data from Nepal Stock Exchange
82 lines • 2.4 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.GenericClient = void 0;
const errors_1 = require("./errors");
class GenericClient {
constructor(got) {
this.got = got;
}
getBaseGotConfig() {
return {
prefixUrl: this.endpoint,
responseType: 'json',
timeout: this.getTimeoutConfig(),
retry: this.getRetryConfig(),
https: { rejectUnauthorized: false },
};
}
async getAuthHeader() {
return 'some-header';
}
async addAuthorizationHeader(options) {
const authHeader = await this.getAuthHeader();
if (options.headers) {
options.headers[`Authorization`] = authHeader;
}
else {
options.headers = {
Authorization: authHeader,
};
}
}
async getGotInstance() {
if (!this.gotInstance) {
this.gotInstance = this.got.extend(this.getBaseGotConfig());
}
return this.gotInstance;
}
async getGotInstanceWithAuth() {
if (!this.gotInstanceWithAuth) {
this.gotInstanceWithAuth = this.got.extend({
...this.getBaseGotConfig(),
hooks: {
beforeRequest: [this.addAuthorizationHeader.bind(this)],
},
});
}
return this.gotInstanceWithAuth;
}
async call(method, url, options = {}) {
try {
const instance = await this.getGotInstance();
return instance[method](url, options);
}
catch (error) {
throw new errors_1.GotInstanceInitializationError(error);
}
}
async callWithAuth(method, url, options = {}) {
try {
const instance = await this.getGotInstanceWithAuth();
return instance[method](url, options);
}
catch (error) {
throw new errors_1.GotInstanceInitializationError(error);
}
}
getTimeoutConfig() {
return {
connect: 1000,
request: 7000,
response: 5000,
};
}
getRetryConfig() {
return {
calculateDelay: ({ computedValue }) => computedValue / 5,
limit: 2,
};
}
}
exports.GenericClient = GenericClient;
//# sourceMappingURL=generic-client.js.map