UNPKG

baasic-sdk-javascript

Version:

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

167 lines (166 loc) 8.17 kB
"use strict"; /* globals module */ /** * @module blogClient * @description Blogs Client provides an easy way to consume Blogs REST API end-points. In order to obtain needed routes `articleClient` uses `articleRoute`. */ Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var inversify_1 = require("inversify"); ; var common_1 = require("../../common"); var httpApi_1 = require("../../httpApi"); var _1 = require("./"); var BlogClient = /** @class */ (function () { function BlogClient(blogRoute, apiClient) { this.blogRoute = blogRoute; this.apiClient = apiClient; this.utility = new common_1.Utility(); } Object.defineProperty(BlogClient.prototype, "routeDefinition", { /** * Provides direct access to `blogRoute`. * @method * @example blogClient.routeDefinition.get(); **/ get: function () { return this.blogRoute; }, enumerable: true, configurable: true }); /** * Returns a promise that is resolved once the find action has been performed. Success response returns a list of article resources matching the given criteria. * @method * @param options A promise that is resolved once the find action has been performed. * @returns A promise that is resolved once the find action has been performed. * @example articleClient.find({ pageNumber : 1, pageSize : 10, orderBy : '<field>', orderDirection : '<asc|desc>', search : '<search-phrase>' }) .then(function (collection) { // perform success action here }, function (response, status, headers, config) { // perform error handling here }); **/ BlogClient.prototype.find = function (options) { return this.apiClient.get(this.routeDefinition.find(options)); }; /** * Returns a promise that is resolved once the get action has been performed. Success response returns a single article resource. * @method * @param id Blog slug or id which uniquely identifies article resource that needs to be retrieved. * @param options Options object that contains embed items. * @returns a promise that is resolved once the get action has been performed. * @example articleClient.get('<article-id>') .then(function (data) { // perform success action here }, function (response, status, headers, config) { // perform error handling here }); **/ BlogClient.prototype.get = function (id, options) { return this.apiClient.get(this.routeDefinition.get(id, options)); }; /** * Returns a promise that is resolved once the create article action has been performed, this action creates a new article resource. * @method * @param data An article object that needs to be inserted into the system. * @returns a promise that is resolved once the create article action has been performed. * @example articleClient.create({ publishDate : new Date(), title : '<title>', content : '<content>', slug : '', status : baasicBlogService.statuses.draft, $tags : ['<tag1>', '<tag2>'] }) .then(function (data) { // perform success action here }, function (response, status, headers, config) { // perform error handling here }); **/ BlogClient.prototype.create = function (data) { return this.apiClient.post(this.routeDefinition.create(), this.routeDefinition.createParams(data)); }; /** * Returns a promise that is resolved once the update article action has been performed; this action updates an article resource. This route uses HAL enabled objects to obtain routes and therefore it doesn't apply `articleRoute` route template. Here is an example of how a route can be obtained from HAL enabled objects: * ``` * let params = modelMapper.updateParams(article); * let uri = params['model'].links('put').href; * ``` * @method * @param data An article object that needs to be updated into the system. * @returns A promise that is resolved once the update article action has been performed. * @example // article is a resource previously fetched using get action. article.title = '<title>'; articleClient.update(article) .then(function (data) { // perform success action here }, function (response, status, headers, config) { // perform error handling here }); **/ BlogClient.prototype.update = function (data) { return this.apiClient.put(this.routeDefinition.update(data), this.routeDefinition.updateParams(data)); }; /** * Returns a promise that is resolved once the remove article action has been performed. If the action is successfully completed, the article resource will be permanently removed from the system. This route uses HAL enabled objects to obtain routes and therefore it doesn't apply `articleRoute` route template. Here is an example of how a route can be obtained from HAL enabled objects: * ``` * let params = modelMapper.removeParams(article); * let uri = params['model'].links('delete').href; * ``` * @method * @param data An article object that needs to be removed from the system. * @returns A promise that is resolved once the remove article action has been performed. * @example // article is a resource previously fetched using get action. articleClient.remove(article) .then(function (data) { // perform success action here }, function (response, status, headers, config) { // perform error handling here }); **/ BlogClient.prototype.remove = function (data) { return this.apiClient.delete(this.routeDefinition.delete(data)); }; /** * Returns a promise that is resolved once the purge articles action has been performed. Please note that all article resources will be deleted from the system once the action is successfully completed and therefore it can only be executed by user assigned to account owner role. * @method * @example articleClient.purge() .then(function (data) { // perform success action here }, function (response, status, headers, config) { // perform error handling here }); **/ BlogClient.prototype.purge = function () { return this.apiClient.delete(this.routeDefinition.purge()); }; BlogClient = tslib_1.__decorate([ inversify_1.injectable(), tslib_1.__param(0, inversify_1.inject(_1.TYPES.BlogRoute)), tslib_1.__param(1, inversify_1.inject(httpApi_1.httpTYPES.ApiClient)), tslib_1.__metadata("design:paramtypes", [_1.BlogRoute, httpApi_1.ApiClient]) ], BlogClient); return BlogClient; }()); exports.BlogClient = BlogClient; /** * @overview ***Notes:** - Refer to the [REST API documentation](https://github.com/Baasic/baasic-rest-api/wiki) for detailed information about available Baasic REST API end-points. - All end-point objects are transformed by the associated route service. */