@breautek/storm
Version:
Object-Oriented REST API framework
128 lines (125 loc) • 5.25 kB
JavaScript
;
/*
Copyright 2017-2021 Norman Breau
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.ServiceProvider = void 0;
const tslib_1 = require("tslib");
const HTTPMethod_1 = require("./HTTPMethod");
const ServiceResponse_1 = require("./ServiceResponse");
const http = tslib_1.__importStar(require("http"));
const TAG = 'ServiceProvider';
const NO_DATA = `|${0x0}|`;
class ServiceProvider {
constructor(app) {
this.$app = app;
}
_getApp() {
return this.$app;
}
getApp() {
return this.$app;
}
_getDomain() {
return '127.0.0.1';
}
$getSecret() {
return this.$app.getConfig().backend_authentication_secret;
}
urlSuffix() {
return '/';
}
_getProtocol() {
return 'http';
}
getVersion() {
return 'v1';
}
_createURL(url, queryParams) {
let queryString = '';
if (queryParams) {
for (let i in queryParams) {
if (queryString === '') {
queryString = '?' + i + '=' + queryParams[i];
}
else {
queryString += '&' + i + '=' + queryParams[i];
}
}
}
return `/api/${this._getBase()}/${this.getVersion()}/${url}${this.urlSuffix()}${queryString}`;
}
request(method, url, accessToken, data, headers, additionalOptions) {
return new Promise((resolve, reject) => {
if (!headers) {
headers = {};
}
headers[this.$app.getConfig().authentication_header] = accessToken;
headers[this.$app.getConfig().backend_authentication_header] = this.$getSecret();
if (!headers['content-type']) {
headers['content-type'] = 'application/json';
}
let httpOpts = {
port: this._getPort(),
hostname: `${this._getDomain()}`,
method: method,
path: url,
headers: headers || {}
};
this.$app.getLogger().trace(TAG, `ServiceProvider Request`);
this.$app.getLogger().trace(TAG, `METHOD: ${httpOpts.method}`);
this.$app.getLogger().trace(TAG, `HOSTNAME: ${httpOpts.hostname}`);
this.$app.getLogger().trace(TAG, `PORT: ${httpOpts.port}`);
this.$app.getLogger().trace(TAG, `PATH: ${httpOpts.path}`);
this.$app.getLogger().trace(TAG, `HEADERS: ${JSON.stringify(httpOpts.headers)}`);
let responseData = Buffer.from('');
let request = http.request(httpOpts, (response) => {
this.$app.getLogger().trace(TAG, `ServiceProvider Response Status: ${response.statusCode}`);
this.$app.getLogger().trace(TAG, `ServiceProvider Response Headers: ${JSON.stringify(response.headers)}`);
response.on('data', (chunk) => {
this.$app.getLogger().trace(TAG, `ServiceProvider Received Chunk: ${chunk}`);
responseData = Buffer.concat([responseData, chunk]);
});
response.on('end', () => {
this.$app.getLogger().trace(TAG, `ServiceProvider request has completed.`);
resolve(new ServiceResponse_1.ServiceResponse(responseData, response));
});
response.on('error', (e) => {
this.$app.getLogger().error(TAG, e);
reject(e);
});
});
if (data && data !== NO_DATA) {
data = JSON.stringify(data);
request.write(data);
}
this.$sendRequest(request);
});
}
$sendRequest(request) {
request.end();
}
get(url, accessToken, data, headers, additionalOptions) {
return this.request(HTTPMethod_1.HTTPMethod.GET, this._createURL(url, data), accessToken, NO_DATA, headers, additionalOptions);
}
post(url, accessToken, data, headers, additionalOptions) {
return this.request(HTTPMethod_1.HTTPMethod.POST, this._createURL(url), accessToken, data, headers, additionalOptions);
}
put(url, accessToken, data, headers, additionalOptions) {
return this.request(HTTPMethod_1.HTTPMethod.PUT, this._createURL(url), accessToken, data, headers, additionalOptions);
}
delete(url, accessToken, data, headers, additionalOptions) {
return this.request(HTTPMethod_1.HTTPMethod.DELETE, this._createURL(url), accessToken, data, headers, additionalOptions);
}
}
exports.ServiceProvider = ServiceProvider;
//# sourceMappingURL=ServiceProvider.js.map