architect-node-sdk
Version:
Essentialz Architect Nodejs SDK
86 lines (85 loc) • 4.01 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ResourceService = void 0;
var qs_1 = __importDefault(require("qs"));
var camelcase_keys_1 = __importDefault(require("camelcase-keys"));
var snakecase_keys_1 = __importDefault(require("snakecase-keys"));
var const_1 = require("../../const");
var ResourceService = /** @class */ (function () {
function ResourceService(resourceName, httpClient, config) {
this.resourceName = resourceName;
this.httpClient = httpClient;
this.config = config;
}
ResourceService.prototype.getAll = function (options) {
var _this = this;
return this.httpClient
.get(this.getResourceUrl(), this.getInit(options))
.then(function (data) { return _this.parseResponseData(data); });
};
ResourceService.prototype.search = function (query, options) {
var _this = this;
var params = typeof query === 'string' ? query : qs_1.default.stringify(query);
var url = this.getResourceUrl() + "?" + params;
return this.httpClient.get(url, this.getInit(options)).then(function (data) { return _this.parseResponseData(data); });
};
ResourceService.prototype.get = function (id, options) {
var _this = this;
return this.httpClient
.get(this.getResourceUrl(id), this.getInit(options))
.then(function (data) { return _this.parseResponseData(data); });
};
ResourceService.prototype.create = function (body, options) {
var _this = this;
return this.httpClient
.post(this.getResourceUrl(), this.prepareBody(body), this.getInit(options))
.then(function (data) { return _this.parseResponseData(data); });
};
ResourceService.prototype.update = function (id, body, options) {
var _this = this;
return this.httpClient
.put(this.getResourceUrl(id), this.prepareBody(body), this.getInit(options))
.then(function (data) { return _this.parseResponseData(data); });
};
ResourceService.prototype.delete = function (id, options) {
var _this = this;
return this.httpClient
.delete(this.getResourceUrl(id), this.getInit(options))
.then(function (data) { return _this.parseResponseData(data); });
};
ResourceService.prototype.getResourceUrl = function (id) {
var resource = id ? this.resourceName + "/" + id : this.resourceName;
return "api/" + resource;
};
// This should be extracted to http request builder
ResourceService.prototype.getInit = function (options) {
var _a;
var headers = (_a = {},
_a[const_1.HEADER_KEYS.CONTENT_TYPE] = const_1.CONTENT_TYPES.JSON,
_a);
if (options === null || options === void 0 ? void 0 : options.authKey) {
headers.Authorization = "Bearer " + options.authKey;
}
if (options === null || options === void 0 ? void 0 : options.perPage) {
headers[const_1.HEADER_KEYS.PAGINATION_PER_PAGE] = options.perPage;
}
if (options === null || options === void 0 ? void 0 : options.page) {
headers[const_1.HEADER_KEYS.PAGINATION_PAGE] = options.page;
}
if (this.config.developerKey) {
headers[const_1.HEADER_KEYS.DEVELOPER_KEY] = this.config.developerKey;
}
return { headers: headers };
};
ResourceService.prototype.prepareBody = function (body) {
return this.config.recommendedCase ? (0, snakecase_keys_1.default)(body, { deep: true }) : body;
};
ResourceService.prototype.parseResponseData = function (data) {
return this.config.recommendedCase ? (0, camelcase_keys_1.default)(data, { deep: true }) : data;
};
return ResourceService;
}());
exports.ResourceService = ResourceService;