@c8y/client
Version:
Client application programming interface to access the Cumulocity IoT-Platform REST services.
159 lines • 6.05 kB
JavaScript
import { __awaiter } from "tslib";
import { Paging } from './Paging.js';
export class Service {
constructor(client, realtime) {
this.client = client;
this.realtime = realtime;
this.cache = new Map();
const methodsToHide = ['list', 'create', 'update', 'detail', 'delete'];
const prototype = Object.getPrototypeOf(this);
methodsToHide.forEach(method => {
if (!prototype.hasOwnProperty(method)) {
Object.defineProperty(this, method, {
get() {
return undefined;
}
});
}
});
}
list() {
return __awaiter(this, arguments, void 0, function* (filter = {}) {
const headers = { accept: 'application/json' };
const url = this.listUrl;
const res = yield this.fetch(url, this.changeFetchOptions({ headers, params: filter }, url));
const json = yield res.json();
const data = this.propertyName ? json[this.propertyName] : json;
const paging = this.getPaging(json, filter);
return { res, data, paging };
});
}
detail(entityOrId_1) {
return __awaiter(this, arguments, void 0, function* (entityOrId, filter = {}) {
const headers = { accept: 'application/json' };
const url = this.getDetailUrl(entityOrId);
const res = yield this.fetch(url, this.changeFetchOptions({ headers, params: Object.assign({}, filter) }, url));
const data = yield res.json();
return { res, data };
});
}
create(entity_1) {
return __awaiter(this, arguments, void 0, function* (entity, params = {}) {
const url = this.listUrl;
const method = 'POST';
const body = JSON.stringify(this.onBeforeCreate(entity));
const headers = { 'content-type': 'application/json', accept: 'application/json' };
const res = yield this.fetch(url, this.changeFetchOptions({ method, body, headers, params: Object.assign({}, params) }, url));
const data = yield res.json();
return { res, data };
});
}
update(entity_1) {
return __awaiter(this, arguments, void 0, function* (entity, params = {}) {
const url = this.getDetailUrl(entity);
const method = 'PUT';
const body = JSON.stringify(this.onBeforeUpdate(entity));
const headers = { 'content-type': 'application/json', accept: 'application/json' };
const res = yield this.fetch(url, this.changeFetchOptions({ method, body, headers, params: Object.assign({}, params) }, url));
const data = yield res.json();
return { res, data };
});
}
delete(entityOrId, params) {
return __awaiter(this, void 0, void 0, function* () {
const method = 'DELETE';
const url = this.getDetailUrl(entityOrId);
const res = yield this.fetch(url, this.changeFetchOptions({ method, params }, url));
return { res, data: null };
});
}
onBeforeCreate(obj) {
delete obj.id;
return obj;
}
onBeforeUpdate(objWithId) {
return objWithId;
}
changeFetchOptions(options, _url = '') {
return options;
}
getUrl(url = '') {
const baseUrl = this.baseUrl.replace(/\/+$/, '');
const partialUrl = url.replace(/^\/+/, '');
return `${baseUrl}/${partialUrl}`;
}
getEntityId(entityOrId) {
if (entityOrId === undefined || entityOrId === null) {
return '';
}
if (typeof entityOrId === 'object') {
return this.getEntityId(entityOrId.id);
}
return `${entityOrId}`;
}
getDetailUrl(entityOrId) {
const id = this.getEntityId(entityOrId);
return `${this.listUrl}/${id}`;
}
fetch(url, init) {
return __awaiter(this, void 0, void 0, function* () {
const fullUrl = this.getUrl(url);
const res = yield this.client.fetch(fullUrl, init);
return yield this.handleErrorStatusCodes(res);
});
}
/**
* Checks the response for errors and throws exceptions, otherwise returns the response as is.
*
* @param response The response from server.
*
* @returns If no errors are detected, it returns the same response.
*
* @throws If an error is detected, it throws `{ res, data }`, where `data` contains error details from server.
*/
handleErrorStatusCodes(response) {
return __awaiter(this, void 0, void 0, function* () {
if (response.status >= 400) {
let data = null;
try {
data = yield response.json();
}
catch (ex) {
try {
data = yield response.text();
}
catch (ex) {
// do nothing
}
}
throw { res: response, data };
}
return response;
});
}
mimeType(type) {
return `application/vnd.com.nsn.cumulocity.${type}+json`;
}
getIdString(reference) {
let id;
if (typeof reference === 'object') {
id = reference.id;
}
else {
id = reference;
}
return String(id);
}
getPaging(json, filter) {
if (json.statistics) {
const statistics = Object.assign(Object.assign({}, json.statistics), { nextPage: this.getCurrentPageFromLink(json.next), prevPage: this.getCurrentPageFromLink(json.prev) });
return new Paging(this, statistics, filter);
}
return null;
}
getCurrentPageFromLink(link = '') {
const matches = link.match(/currentPage=(-{0,1}\d+)/);
return matches && parseInt(matches[1], 10);
}
}
//# sourceMappingURL=Service.js.map