UNPKG

@c8y/client

Version:

Client application programming interface to access the Cumulocity IoT-Platform REST services.

169 lines • 6.21 kB
import { __awaiter } from "tslib"; import FormData from 'form-data'; import { Service } from '../core'; export class InventoryBinaryService extends Service { constructor() { super(...arguments); this.baseUrl = 'inventory'; this.listUrl = 'binaries'; this.propertyName = 'managedObjects'; } /** * Uploads a file and creates a managed object with its metadata. * @param file A file to be uploaded. * @param managedObject An object containing metadata about the file. * Note that you can specify `fileType` and `fileName` in case `file` does not define them * but these two properties will be removed from `managedObject` before saving. */ create(file_1) { return __awaiter(this, arguments, void 0, function* (file, managedObject = {}) { const method = 'POST'; const url = this.listUrl; const body = new FormData(); let fileName; let fileType; if (managedObject.fileName) { fileName = managedObject.fileName; delete managedObject.fileName; } if (managedObject.fileType) { fileType = managedObject.fileType; delete managedObject.fileType; } if (!fileName) { fileName = 'bin'; } if (typeof File !== 'undefined' && file instanceof File) { fileName = file.name; fileType = file.type; } if (!managedObject.name) { managedObject.name = fileName; } if (!managedObject.type) { managedObject.type = fileType || 'c8y_upload'; } body.append('file', file, fileName); body.append('object', JSON.stringify(managedObject)); const headers = { accept: 'application/json' }; const res = yield this.fetch(url, { method, body, headers }); const data = yield res.json(); return { res, data }; }); } list() { const _super = Object.create(null, { list: { get: () => super.list } }); return __awaiter(this, arguments, void 0, function* (filter = {}) { return _super.list.call(this, filter); }); } delete(managedObjectOrId) { const _super = Object.create(null, { delete: { get: () => super.delete } }); return __awaiter(this, void 0, void 0, function* () { return _super.delete.call(this, managedObjectOrId); }); } download(managedObjectOrId, init) { return __awaiter(this, void 0, void 0, function* () { const url = this.getDetailUrl(managedObjectOrId); return yield this.fetch(url, init); }); } /** * Uploads a file and creates a managed object with its metadata. What's more, it invokes an upload progress callback. * @param file A file to be uploaded. * @param managedObject An object containing metadata about the file. * @param onProgress Event handler for progress update, invoked while the browser is uploading the file. */ createWithProgress(file, onProgress, managedObject = {}) { const url = `/${this.baseUrl}/${this.listUrl}`; const method = 'POST'; const body = new FormData(); let fileName; let fileType; if (managedObject.fileName) { fileName = managedObject.fileName; delete managedObject.fileName; } if (managedObject.fileType) { fileType = managedObject.fileType; delete managedObject.fileType; } if (!fileName) { fileName = 'bin'; } if (typeof File !== 'undefined' && file instanceof File) { fileName = file.name; fileType = file.type; } if (!managedObject.name) { managedObject.name = fileName; } if (!managedObject.type) { managedObject.type = fileType || 'c8y_upload'; } body.append('file', file, fileName); body.append('object', JSON.stringify(managedObject)); let bodyHeaders; if (typeof body.getHeaders === 'function') { bodyHeaders = body.getHeaders(); } const headers = this.client.getFetchOptions().headers; Object.assign(headers, { accept: 'application/json' }, bodyHeaders); const xhr = new XMLHttpRequest(); xhr.open(method, url, true); for (const key in headers) { if (headers.hasOwnProperty(key)) { xhr.setRequestHeader(key, headers[key]); } } xhr.upload.addEventListener('progress', onProgress); let xhrBody; if (typeof body.getBuffer === 'function') { xhrBody = body.getBuffer(); } else { xhrBody = body; } xhr.send(xhrBody); return xhr; } getXMLHttpResponse(xhr) { return new Promise((res, rej) => { xhr.addEventListener('loadend', () => { xhr.readyState === 4 && (xhr.status === 200 || xhr.status === 201) ? res(JSON.parse(xhr.responseText)) : rej(xhr.responseText ? { data: JSON.parse(xhr.responseText) } : 'Could not upload file.'); }); }); } /** * Gets binary managed object's id from its download or self URL. * * @param {string} url URL string. * * @returns {number} Binary managed object's id. * * **Example** * ```typescript * * const id = InventoryBinaryService.getIdFromUrl('http://mytenant.cumulocity.com/inventory/binaries/12345'); * ``` */ getIdFromUrl(url) { if (!!url) { const regexp = new RegExp('\\/inventory\\/binaries\\/(\\d+)|\\/inventory\\/managedObjects\\/(\\d+)'); const matches = url.match(regexp); return matches && (matches[1] || matches[2]); } } } //# sourceMappingURL=InventoryBinaryService.js.map