@studyportals/sp-hs-misc
Version:
Miscellaneous code used in HouseStark's projects
134 lines • 6.12 kB
JavaScript
"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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ServiceLayerClient = void 0;
const resilient_service_layer_requests_sender_decorator_class_1 = require("./resilient-service-layer-requests-sender-decorator.class");
const service_layer_requests_sender_class_1 = require("./service-layer-requests-sender.class");
class ServiceLayerClient {
get baseServiceLayerUrl() {
return this._baseServiceLayerUrl;
}
get maximumRetries() {
return this._maximumRetries;
}
get getRequestsSender() {
return this._getRequestsSender;
}
get getCachedRequestsSender() {
return this._getCachedRequestsSender;
}
get getResilientRequestsSender() {
return this._resilientRequestsSender;
}
constructor(superAgentRequestsFactory, baseServiceLayerUrl = "", maximumRetries = 0, userAgent = "Studyportals-HouseStark-Misc/1.0") {
this.superAgentRequestsFactory = superAgentRequestsFactory;
this._baseServiceLayerUrl = baseServiceLayerUrl;
this._userAgent = userAgent;
this._maximumRetries = maximumRetries;
this.initializeRequestsSenders();
}
get(path, headers = {}) {
return __awaiter(this, void 0, void 0, function* () {
const request = this.createGetRequest(path);
for (const header in headers) {
if (headers.hasOwnProperty(header)) {
request.set(header, headers[header]);
}
}
request.set("Cache-Control", "no-cache");
return this.getRequestsSender.send(request);
});
}
getCached(path, headers = {}, type = "json") {
const request = this.createGetRequest(path, type);
for (const header in headers) {
if (headers.hasOwnProperty(header)) {
request.set(header, headers[header]);
}
}
return this.getCachedRequestsSender.send(request);
}
post(path, data, headers = {}, type = "json") {
return __awaiter(this, void 0, void 0, function* () {
const request = this.createPostRequest(path, data, type);
for (const header in headers) {
if (headers.hasOwnProperty(header)) {
request.set(header, headers[header]);
}
}
return this.getResilientRequestsSender.send(request);
});
}
put(path, data, headers = {}, type = "json") {
return __awaiter(this, void 0, void 0, function* () {
const request = this.createPutRequest(path, data, type);
for (const header in headers) {
if (headers.hasOwnProperty(header)) {
request.set(header, headers[header]);
}
}
return this.getResilientRequestsSender.send(request);
});
}
delete(path, headers = {}, type = "json") {
return __awaiter(this, void 0, void 0, function* () {
const request = this.createDeleteRequest(path, type);
for (const header in headers) {
if (headers.hasOwnProperty(header)) {
request.set(header, headers[header]);
}
}
return this.getResilientRequestsSender.send(request);
});
}
createGetRequest(path, type = "json") {
const request = this.superAgentRequestsFactory.get(this.buildUrl(path));
request.set("Accept-Language", "en-GB");
request.type(type);
return request;
}
createPostRequest(path, data, type = "json") {
const request = this.superAgentRequestsFactory.post(this.buildUrl(path));
request.send(data);
request.set("Accept-Language", "en-GB");
request.type(type);
return request;
}
createPutRequest(path, data, type = "json") {
const request = this.superAgentRequestsFactory.put(this.buildUrl(path));
request.send(data);
request.set("Accept-Language", "en-GB");
request.type(type);
return request;
}
createDeleteRequest(path, type = "json") {
const request = this.superAgentRequestsFactory.delete(this.buildUrl(path));
request.set("Accept-Language", "en-GB");
request.type(type);
return request;
}
buildUrl(path) {
return `${this.baseServiceLayerUrl}${path}`;
}
initializeRequestsSenders() {
this._getCachedRequestsSender = new resilient_service_layer_requests_sender_decorator_class_1.ResilientServiceLayerRequestsSenderDecorator(new service_layer_requests_sender_class_1.ServiceLayerRequestsSender({
userAgent: this._userAgent
}), this.maximumRetries);
this._getRequestsSender = new resilient_service_layer_requests_sender_decorator_class_1.ResilientServiceLayerRequestsSenderDecorator(new service_layer_requests_sender_class_1.ServiceLayerRequestsSender({
userAgent: this._userAgent
}), this.maximumRetries);
this._resilientRequestsSender = new resilient_service_layer_requests_sender_decorator_class_1.ResilientServiceLayerRequestsSenderDecorator(new service_layer_requests_sender_class_1.ServiceLayerRequestsSender({
userAgent: this._userAgent
}), this.maximumRetries);
}
}
exports.ServiceLayerClient = ServiceLayerClient;
//# sourceMappingURL=service-layer-client.class.js.map