baasic-sdk-javascript
Version:
JavaScript SDK provides core functionality for building web and mobile applications on [Baasic](http://www.baasic.com/).
217 lines (215 loc) • 11.8 kB
JavaScript
"use strict";
/**
* @module dynamicResourceClient
* @description Dynamic Resource Client provides an easy way to consume Dynamic Resource REST API end-points. In order to obtain needed routes `dynamicResourceClient` uses `dynamicResourceRoute`.
*/
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var inversify_1 = require("inversify");
;
var httpApi_1 = require("../../httpApi");
var _1 = require("./");
var DynamicResourceClient = /** @class */ (function () {
function DynamicResourceClient(dynamicResourceRoute, dynamicResourceBatchClient, dynamicResourceACLClient, dynamicSchemaClient, apiClient) {
this.dynamicResourceRoute = dynamicResourceRoute;
this.dynamicResourceBatchClient = dynamicResourceBatchClient;
this.dynamicResourceACLClient = dynamicResourceACLClient;
this.dynamicSchemaClient = dynamicSchemaClient;
this.apiClient = apiClient;
}
Object.defineProperty(DynamicResourceClient.prototype, "routeDefinition", {
/**
* Provides direct access to `dynamicResourceRoute`.
* @method
* @example dynamicResourceClient.routeDefinition.get(schemaName, id, options)
**/
get: function () {
return this.dynamicResourceRoute;
},
enumerable: true,
configurable: true
});
Object.defineProperty(DynamicResourceClient.prototype, "acl", {
get: function () {
return this.dynamicResourceACLClient;
},
enumerable: true,
configurable: true
});
Object.defineProperty(DynamicResourceClient.prototype, "schema", {
get: function () {
return this.dynamicSchemaClient;
},
enumerable: true,
configurable: true
});
Object.defineProperty(DynamicResourceClient.prototype, "batch", {
get: function () {
return this.dynamicResourceBatchClient;
},
enumerable: true,
configurable: true
});
/**
* Returns a promise that is resolved once the find action has been performed. Success response returns a list of dynamic resources matching the given criteria.
* @method
* @param schemaName Name of dynamic resource schema whose dynamic resources need to be retrieved.
* @param options Query resource options object.
* @returns Promise that is resolved once the find action has been performed.
* @example dynamicResourceClient.find('<schema-name>', {
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
});
**/
DynamicResourceClient.prototype.find = function (schemaName, options) {
return this.apiClient.get(this.dynamicResourceRoute.find(schemaName, options));
};
/**
* Returns a promise that is resolved once the get action has been performed. Success response returns the specified dynamic resource.
* @method
* @example dynamicResourceClient.get('<schema-name>', '<dynamic-resource-id>')
.then(function (data) {
// perform success action here
},
function (response, status, headers, config) {
// perform error handling here
});
**/
DynamicResourceClient.prototype.get = function (schemaName, id, options) {
return this.apiClient.get(this.dynamicResourceRoute.get(id, schemaName, options));
};
/**
* Returns a promise that is resolved once the create dynamic resource action has been performed; this action creates a new dynamic resource item.
* @method
* @param schemaName Name of dynamic resource schema that needs to be updated with new dynamic resource.
* @param data A JSON object that needs to be inserted into the system as dynamic resource. JSON object is an unordered collection of zero or more key/value pairs structured using the standard JSON syntax rules.
* @example dynamicResourceClient.create('<schema-name>', {
id : '',
description : '<description>'
})
.then(function (data) {
// perform success action here
},
function (response, status, headers, config) {
// perform error handling here
});
**/
DynamicResourceClient.prototype.create = function (schemaName, data) {
return this.apiClient.post(this.routeDefinition.create(schemaName), this.routeDefinition.createParams(data));
};
/**
* Returns a promise that is resolved once the update action has been performed; this action updates a dynamic resource item. This route uses HAL enabled objects to obtain routes and therefore it doesn't apply `dynamicResourceRoute` route template. Here is an example of how a route can be obtained from HAL enabled objects:
* ```
* let params = modelMapper.removeParams(dynamicResource);
* let uri = params['model'].links('put').href;
* ```
* @method
* @param data A JSON object used to update specified dynamic resource. JSON object is an unordered collection of zero or more key/value pairs structured using the standard JSON syntax rules.
* @param options Options object.
* @example // dynamicResource is a resource previously fetched using get action.
dynamicResource.description = '<description>';
dynamicResourceClient.update(dynamicResource, {
query: "where field = 'value' "
})
.then(function (data) {
// perform success action here
},
function (response, status, headers, config) {
// perform error handling here
});
**/
DynamicResourceClient.prototype.update = function (schemaName, data, options) {
return this.apiClient.put(this.routeDefinition.update(schemaName, data, options), this.routeDefinition.updateParams(data));
};
/**
* Returns a promise that is resolved once the patch action has been performed; this action patches an existing dynamic resource. This route uses HAL enabled objects to obtain routes and therefore it doesn't apply `dynamicResourceRoute` route template. Here is an example of how a route can be obtained from HAL enabled objects:
* ```
* let params = modelMapper.updateParams(dynamicResource);
* let uri = params['model'].links('patch').href;
* ```
* @method
* @param data JSON object used for partial update of specified dynamic resource. JSON object is an unordered collection of zero or more key/value pairs structured using the standard JSON syntax rules.
* @param options Options object.
* @example // dynamicResource is a resource previously fetched using get action.
dynamicResource.description = '<new-description>';
dynamicResource.newField = '<newfield-value>';
dynamicResourceClient.patch(dynamicResource, {
query: "where field = 'value' "
})
.then(function (data) {
// perform success action here
},
function (response, status, headers, config) {
// perform error handling here
});
**/
DynamicResourceClient.prototype.patch = function (schemaName, data, options) {
return this.apiClient.patch(this.routeDefinition.patch(schemaName, data, options), this.routeDefinition.updateParams(data));
};
/**
* Returns a promise that is resolved once the remove action has been performed. This action will remove a dynamic resource from the system if successfully completed. This route uses HAL enabled objects to obtain routes and therefore it doesn't apply `dynamicResourceRoute` route template. Here is an example of how a route can be obtained from HAL enabled objects:
* ```
* let params = modelMapper.removeParams(dynamicResource);
* let uri = params['model'].links('delete').href;
* ```
* @method
* @param data JSON object used to delete specified dynamic resource. JSON object is an unordered collection of zero or more key/value pairs structured using the standard JSON syntax rules.
* @example // dynamicResource is a resource previously fetched using get action.
dynamicResourceClient.remove(dynamicResource, {
query: "where field = 'value' "
})
.then(function (data) {
// perform success action here
},
function (response, status, headers, config) {
// perform error handling here
});
**/
DynamicResourceClient.prototype.remove = function (schemaName, data, options) {
return this.apiClient.delete(this.dynamicResourceRoute.delete(schemaName, data, options));
};
/**
* Returns a promise that is resolved once the purge action has been performed. This action will remove all dynamic resources from the system if successfully completed. This route uses HAL enabled objects to obtain routes and therefore it doesn't apply `dynamicResourceRoute` route template. Here is an example of how a route can be obtained from HAL enabled objects:
* ```
* let params = modelMapper.removeParams(dynamicResource);
* let uri = params['model'].links('purge').href;
* ```
* @method
* @param data JSON object used to purge dynamic resources.
* @example // dynamicResource is a resource previously fetched using get action.
dynamicResourceClient.purge('<schema-name>')
.then(function (data) {
// perform success action here
},
function (response, status, headers, config) {
// perform error handling here
});
**/
DynamicResourceClient.prototype.purge = function (schemaName) {
return this.apiClient.delete(this.dynamicResourceRoute.purge(schemaName));
};
DynamicResourceClient = tslib_1.__decorate([
inversify_1.injectable(),
tslib_1.__param(0, inversify_1.inject(_1.TYPES.DynamicResourceRoute)),
tslib_1.__param(1, inversify_1.inject(_1.TYPES.DynamicResourceBatchClient)),
tslib_1.__param(2, inversify_1.inject(_1.TYPES.DynamicResourceACLClient)),
tslib_1.__param(3, inversify_1.inject(_1.TYPES.DynamicSchemaClient)),
tslib_1.__param(4, inversify_1.inject(httpApi_1.httpTYPES.ApiClient)),
tslib_1.__metadata("design:paramtypes", [_1.DynamicResourceRoute,
_1.DynamicResourceBatchClient,
_1.DynamicResourceACLClient,
_1.DynamicSchemaClient,
httpApi_1.ApiClient])
], DynamicResourceClient);
return DynamicResourceClient;
}());
exports.DynamicResourceClient = DynamicResourceClient;