coloquent-test2
Version:
Library for retrieving model objects from a JSON-API, with a fluent syntax inspired by Laravel Eloquent.
133 lines • 6.14 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var FilterSpec_1 = require("./FilterSpec");
var SortSpec_1 = require("./SortSpec");
var PluralResponse_1 = require("./response/PluralResponse");
var SingularResponse_1 = require("./response/SingularResponse");
var Option_1 = require("./Option");
var PaginationStrategy_1 = require("./PaginationStrategy");
var OffsetBasedPaginationSpec_1 = require("./paginationspec/OffsetBasedPaginationSpec");
var PageBasedPaginationSpec_1 = require("./paginationspec/PageBasedPaginationSpec");
var Query_1 = require("./Query");
var SortDirection_1 = require("./SortDirection");
var Builder = /** @class */ (function () {
function Builder(modelType, queriedRelationName, baseModelJsonApiType, forceSingular) {
if (queriedRelationName === void 0) { queriedRelationName = null; }
if (baseModelJsonApiType === void 0) { baseModelJsonApiType = null; }
if (forceSingular === void 0) { forceSingular = false; }
this.modelType = modelType;
var modelInstance = (new modelType());
baseModelJsonApiType = baseModelJsonApiType
? baseModelJsonApiType
: modelInstance.getJsonApiType();
this.query = new Query_1.Query(baseModelJsonApiType, queriedRelationName);
this.initPaginationSpec();
this.httpClient = modelInstance.getHttpClient();
this.forceSingular = forceSingular;
}
Builder.prototype.get = function (page) {
var _this = this;
if (page === void 0) { page = 0; }
this.query.getPaginationSpec().setPage(page);
if (this.forceSingular) {
return this.getHttpClient()
.get(this.query.toString())
.then(function (response) {
return new SingularResponse_1.SingularResponse(response, _this.modelType, response.getData());
}, function (response) {
throw new Error(response.message);
});
}
else {
return this.getHttpClient()
.get(this.query.toString())
.then(function (response) {
return new PluralResponse_1.PluralResponse(response, _this.modelType, response.getData(), page);
}, function (response) {
throw new Error(response.message);
});
}
};
Builder.prototype.first = function () {
var _this = this;
this.query.getPaginationSpec().setPageLimit(1);
return this.getHttpClient()
.get(this.query.toString())
.then(function (response) {
return new SingularResponse_1.SingularResponse(response, _this.modelType, response.getData());
}, function (response) {
throw new Error(response.message);
});
};
Builder.prototype.find = function (id) {
var _this = this;
this.query.setIdToFind(id);
return this.getHttpClient()
.get(this.query.toString())
.then(function (response) {
return new SingularResponse_1.SingularResponse(response, _this.modelType, response.getData());
}, function (response) {
throw new Error(response.message);
});
};
Builder.prototype.where = function (attribute, value) {
this.query.addFilter(new FilterSpec_1.FilterSpec(attribute, value));
return this;
};
Builder.prototype.with = function (value) {
if (typeof value === 'string') {
this.query.addInclude(value);
}
else if (Array.isArray(value)) {
for (var _i = 0, value_1 = value; _i < value_1.length; _i++) {
var v = value_1[_i];
this.query.addInclude(v);
}
}
else {
throw new Error("The argument for 'with' must be a string or an array of strings.");
}
return this;
};
Builder.prototype.orderBy = function (attribute, direction) {
if (typeof direction === 'undefined' || direction === null) {
direction = SortDirection_1.SortDirection.ASC;
}
else if (typeof direction === 'string') {
if (direction === 'asc') {
direction = SortDirection_1.SortDirection.ASC;
}
else if (direction === 'desc') {
direction = SortDirection_1.SortDirection.DESC;
}
else {
throw new Error("The 'direction' parameter must be string of value 'asc' or 'desc', " +
"value '" + direction + "' invalid.");
}
}
this.query.addSort(new SortSpec_1.SortSpec(attribute, direction === SortDirection_1.SortDirection.ASC));
return this;
};
Builder.prototype.option = function (queryParameter, value) {
this.query.addOption(new Option_1.Option(queryParameter, value));
return this;
};
Builder.prototype.initPaginationSpec = function () {
var paginationStrategy = this.modelType.getPaginationStrategy();
if (paginationStrategy === PaginationStrategy_1.PaginationStrategy.OffsetBased) {
this.query.setPaginationSpec(new OffsetBasedPaginationSpec_1.OffsetBasedPaginationSpec(this.modelType.getPaginationOffsetParamName(), this.modelType.getPaginationLimitParamName(), this.modelType.getPageSize()));
}
else if (paginationStrategy === PaginationStrategy_1.PaginationStrategy.PageBased) {
this.query.setPaginationSpec(new PageBasedPaginationSpec_1.PageBasedPaginationSpec(this.modelType.getPaginationPageNumberParamName(), this.modelType.getPaginationPageSizeParamName(), this.modelType.getPageSize()));
}
else {
throw new Error('Illegal state: Pagination strategy is not set.');
}
};
Builder.prototype.getHttpClient = function () {
return this.httpClient;
};
return Builder;
}());
exports.Builder = Builder;
//# sourceMappingURL=Builder.js.map