UNPKG

mission.common

Version:

Request and Response dto object

143 lines (141 loc) 5.77 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RequestBuilder = void 0; var common_1 = require("./common"); var RequestBuilder = /** @class */ (function () { function RequestBuilder(request) { this.request = request; this.request = this.request || { include: [], attributes: [], filters: [], sorters: [] }; } RequestBuilder.prototype.filter = function (key, value, searchType, jPath) { this.request.filters.push({ key: key, value: value, searchType: searchType, jPath: jPath }); return this; }; RequestBuilder.prototype.addFilter = function (filters) { var list = filters instanceof Array ? filters : [filters]; this.request.filters = this.request.filters.concat(list); return this; }; RequestBuilder.prototype.clearFilter = function () { this.request.filters = []; return this; }; RequestBuilder.prototype.sort = function (key, orderBy) { this.request.sorters.push({ key: key, orderBy: orderBy }); return this; }; RequestBuilder.prototype.addSorter = function (sorters) { var list = sorters instanceof Array ? sorters : [sorters]; this.request.sorters = this.request.sorters.concat(sorters); return this; }; RequestBuilder.prototype.clearSorter = function () { this.request.sorters = []; return this; }; RequestBuilder.prototype.addAttribute = function (attributes) { var list = attributes instanceof Array ? attributes : [attributes]; if (this.request.attributes instanceof Array) { this.request.attributes = this.request.attributes.concat(list); } else { this.request.attributes = this.request.attributes.include.concat(list); } return this; }; RequestBuilder.prototype.clearAttribute = function () { this.request.attributes = []; return this; }; RequestBuilder.prototype.addInclude = function (include) { var list = include instanceof Array ? include : [include]; this.request.include = this.request.include.concat(list); return this; }; RequestBuilder.prototype.addIncludeWhere = function (path, where) { this.addWhere(path.split('.'), this.request.include || [], where); return this; }; RequestBuilder.prototype.clearIncludeWhere = function () { this.clearWhere(this.request.include); return this; }; RequestBuilder.prototype.clearInclude = function () { this.request.include = []; return this; }; RequestBuilder.prototype.pageContext = function (pageNumber, pageSize) { this.request.pageContext = { pageSize: pageSize, pageNumber: pageNumber }; return this; }; RequestBuilder.prototype.setPageContext = function (pageContext) { this.request.pageContext = pageContext; return this; }; RequestBuilder.prototype.resetPageContext = function () { this.request.pageContext = new common_1.Paginator(this.request.pageContext).firstPage(); return this; }; RequestBuilder.prototype.getRequest = function () { return this.request; }; RequestBuilder.prototype.getRequestForNextPage = function () { var pageNumber = this.request.pageContext.pageNumber + 1; this.request.pageContext.pageNumber = pageNumber < 0 ? 0 : pageNumber; return this.request; }; RequestBuilder.prototype.getRequestForPreviousPage = function () { var pageNumber = this.request.pageContext.pageNumber - 1; this.request.pageContext.pageNumber = pageNumber < 0 ? 0 : pageNumber; return this.request; }; RequestBuilder.prototype.clearWhere = function (includes) { var _this = this; return includes ? includes.forEach(function (include) { include.where = null; _this.clearWhere(include.include); }) : undefined; }; RequestBuilder.prototype.addWhere = function (path, includes, where) { if (path.length === 1) { var key_1 = path[0]; var include = includes.find(function (incld) { return incld.key === key_1; }); if (!include) { throw new Error("include '".concat(key_1, "' is not available in request.")); } include.where = Object.assign(include.where || {}, where); } else { var key_2 = path[0]; var include = includes.find(function (incld) { return incld.key === key_2; }); if (!include) { throw new Error("include '".concat(key_2, "' is not available in request.")); } this.addWhere(path.slice(1, path.length), include.include, where); } }; return RequestBuilder; }()); exports.RequestBuilder = RequestBuilder; // export class Opr { // public static inOpr(value: any[]): { $in: any[] } { // return { $in: value }; // } // } /* var t = new RequestBuilder<any>({ data: null }); t.addFilter({ key: Operaters.And, value: null, searchType: SearchType.Contains, jPath: { key: '', path: '' } }); t.addFilter([ { key: Operaters.And, value: null, searchType: SearchType.Contains, jPath: { key: '', path: '' } }, { key: Operaters.And, value: null, searchType: SearchType.Contains, jPath: { key: '', path: '' } } ]); t.addAttribute('Id'); t.addAttribute(['Name', 'Code']); t.addSorter({ key: 'id', orderBy: OrderBy.DESC }); t.addSorter([{ key: 'id', orderBy: OrderBy.DESC }, { key: 'name', orderBy: OrderBy.ASC }]); t.getRequest(); t.getRequestForNextPage(); t.getRequestForPreviousPage(); */ //# sourceMappingURL=request-builder.js.map