UNPKG

baasic-sdk-javascript

Version:

JavaScript SDK provides core functionality for building web and mobile applications on [Baasic](http://www.baasic.com/).

148 lines (147 loc) 6.61 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var uritemplate = require("uritemplate"); var _1 = require("./"); var inversify_1 = require("inversify"); var BaseRoute = /** @class */ (function () { function BaseRoute(appOptions) { this.appOptions = appOptions; this.utility = new _1.Utility(); this.modelMapper = new _1.ModelMapper(); this.dateFormatter = new _1.DateFormatter(); } /** * Parses resources route which can be expanded with additional options. Supported items are: * - `searchQuery` - A string value used to identify resources using the phrase search. * - `page` - A value used to set the page number, i.e. to retrieve certain resource subset from the storage. * - `rpp` - A value used to limit the size of result set per page. * - `sort` - A string used to set the resource property to sort the result collection by. * - `embed` - Comma separated list of resources to be contained within the current representation. * @method * @returns Query resources uri with search params * @example baasicBaseDefinition.find(); **/ BaseRoute.prototype.baseFind = function (route, options) { var params = this.modelMapper.findParams(options); return uritemplate.parse(route).expand(params); }; /** * Parses get resource route which must be expanded with the Id of the previously created resource in the system. * @returns get resource uri * @method * @example baseRoute.get(route, id); **/ BaseRoute.prototype.baseGet = function (route, id, options, propName) { if (options !== null && !this.utility.isUndefined(options) && !this.utility.isUndefined(options.dateUpdated)) { options.t = this.dateFormatter.FormatToString(new Date(options.dateUpdated), null); } return uritemplate.parse(route).expand(this.modelMapper.getParams(id, options, propName)); }; /** * Parses get resource route which must be expanded with the Id of the previously created resource in the system. * @returns get resource uri * @method * @example baseRoute.create(); **/ BaseRoute.prototype.baseCreate = function (route, data) { if (data !== null && !this.utility.isUndefined(data) && !this.utility.isUndefined(data.dateUpdated)) { data.t = this.dateFormatter.FormatToString(new Date(data.dateUpdated), null); } return uritemplate.parse(route).expand(data); }; /** * Parses get resource route. * @returns update resource uri * @method * @example baseRoute.update(); */ BaseRoute.prototype.baseUpdate = function (route, data, options, linkName) { var link = linkName ? linkName : 'put'; var params = this.modelMapper.updateParams(data); var model = params[this.modelMapper.modelPropertyName]; if (typeof options === 'undefined') { if (this.appOptions.enableHALJSON && model.links) { return model.links(link).href; } else { return uritemplate.parse(route).expand(model); } } else { var modelCopy = this.utility.extend({}, model); var opt = this.utility.extend(modelCopy, options); if (this.appOptions.enableHALJSON && model.links) { return uritemplate.parse(model.links(link).href).expand(opt); } else { return uritemplate.parse(route).expand(opt); } } }; /** * Parses delete resource route. * @returns delete resource uri. * @method * @example baseRoute.delete(); */ BaseRoute.prototype.baseDelete = function (route, data, options, linkName) { var link = linkName ? linkName : 'delete'; var params = this.modelMapper.removeParams(data); var model = params[this.modelMapper.modelPropertyName]; if (typeof options === 'undefined') { if (this.appOptions.enableHALJSON && model.links) { return model.links(link).href; } else { return uritemplate.parse(route).expand(model); } } else { var opt = this.utility.extend(model, options); if (this.appOptions.enableHALJSON && model.links) { return uritemplate.parse(model.links(link).href).expand(opt); } else { return uritemplate.parse(route).expand(opt); } } }; BaseRoute.prototype.createParams = function (data, prop) { var propertyName = prop || this.modelMapper.modelPropertyName; return this.modelMapper.createParams(data)[propertyName]; }; BaseRoute.prototype.updateParams = function (data) { return this.modelMapper.updateParams(data)[this.modelMapper.modelPropertyName]; }; BaseRoute.prototype.deleteParams = function (data) { return this.modelMapper.removeParams(data)[this.modelMapper.modelPropertyName]; }; BaseRoute.prototype.deleteBatchParams = function (data) { return this.modelMapper.batchRemoveParams(data); }; /** * Parses and expands URI templates based on [RFC6570](http://tools.ietf.org/html/rfc6570) specifications. For more information please visit the project [GitHub](https://github.com/Baasic/uritemplate-js) page. * @method * @example baseRoute.parse('<route>/{?embed,fields,options}').expand({embed: '<embedded-resource>'}); **/ BaseRoute.prototype.parse = function (route) { return uritemplate.parse(route); }; BaseRoute = tslib_1.__decorate([ inversify_1.injectable(), tslib_1.__metadata("design:paramtypes", [Object]) ], BaseRoute); return BaseRoute; }()); exports.BaseRoute = BaseRoute; /** * @copyright (c) 2017 Mono Ltd * @license MIT * @author Mono Ltd * @overview ***Notes:** - Refer to the [Baasic REST API](http://dev.baasic.com/api/reference/home) for detailed information about available Baasic REST API end-points. - [URI Template](https://github.com/Baasic/uritemplate-js) syntax enables expanding the Baasic route templates to Baasic REST URIs providing it with an object that contains URI parameters. - All end-point objects are transformed by the associated route service. */