@wbg-mde/repository
Version:
Managing all common method for file system CRUD operations.
43 lines (42 loc) • 1.68 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const Request = require("request");
const app_repo_utility_1 = require("../shared/app.repo.utility");
class RestClient {
constructor(config) {
const { api_base_url, authentication_type, api_request_headers, end_points } = config;
this.baseRequest = Request.defaults({
headers: this.getRequestHeaders(api_request_headers),
baseUrl: api_base_url
});
this.endPoints = this.getEndpoints(end_points);
}
getResponse(args, callback) {
const { endPoint, path, options } = args;
let { uri, method } = this.endPoints[endPoint];
uri = app_repo_utility_1.App_Repository_Utility.formatURLPath(uri, path);
this.baseRequest(Object.assign(options, { uri, method }), (err, resp, body) => {
callback(Object.assign({}, { err, resp, body }));
});
}
getRequestHeaders(headers) {
let reqHeaders = {};
if (headers && headers.length > 0) {
headers.forEach((header) => {
reqHeaders = Object.assign(reqHeaders, header);
});
}
return reqHeaders;
}
getEndpoints(end_points) {
if (end_points instanceof Array && end_points.length > 0) {
end_points = end_points[0];
}
return Object.keys(end_points).reduce((methods, name) => {
const { endpoint, method } = end_points[name];
methods[name] = Object.assign({}, { uri: endpoint, method });
return methods;
}, {});
}
}
exports.RestClient = RestClient;