UNPKG

@c8y/client

Version:

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

172 lines • 7.57 kB
import { __awaiter } from "tslib"; import { Service } from '../core/index.js'; import FormData from 'form-data'; export class ApplicationBinaryService extends Service { constructor(client, applicationOrId) { super(client); this.listUrl = 'binaries'; this.propertyName = 'attachments'; this.baseUrl = `application/applications/${this.getIdString(applicationOrId)}`; } upload(binary, fileName, uploadParamsOverride) { return __awaiter(this, void 0, void 0, function* () { const url = (uploadParamsOverride === null || uploadParamsOverride === void 0 ? void 0 : uploadParamsOverride.listUrl) || this.listUrl; const method = 'POST'; const body = this.createBinaryRequestBody(binary, fileName, uploadParamsOverride); let bodyHeaders; if (typeof body.getHeaders === 'function') { bodyHeaders = body.getHeaders(); } const headers = Object.assign((uploadParamsOverride === null || uploadParamsOverride === void 0 ? void 0 : uploadParamsOverride.headers) || { Accept: 'application/json' }, bodyHeaders); const res = yield this.fetch(url, { method, body, headers }); const data = yield res.json(); return { res, data }; }); } uploadApplicationVersion(archive, version, tags = []) { return this.upload(archive, archive.name, { listUrl: 'versions', headers: { Accept: 'application/vnd.com.nsn.cumulocity.applicationVersion+json;charset=UTF-8;ver=0.9' }, bodyFileProperty: 'applicationBinary', requestBody: { applicationVersion: Object.assign({ version }, ((tags === null || tags === void 0 ? void 0 : tags.length) && { tags })) } }); } uploadWithProgressXhr(binary, onProgress, fileName, uploadParamsOverride) { const url = `/${this.baseUrl}/${(uploadParamsOverride === null || uploadParamsOverride === void 0 ? void 0 : uploadParamsOverride.listUrl) || this.listUrl}`; const method = 'POST'; const body = this.createBinaryRequestBody(binary, fileName, uploadParamsOverride); let bodyHeaders; if (typeof body.getHeaders === 'function') { bodyHeaders = body.getHeaders(); } const headers = this.client.getFetchOptions().headers; Object.assign(headers, (uploadParamsOverride === null || uploadParamsOverride === void 0 ? void 0 : uploadParamsOverride.headers) || { Accept: 'application/json' }); Object.assign(headers, 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.'); }); }); } list(filter) { const _super = Object.create(null, { list: { get: () => super.list } }); return __awaiter(this, void 0, void 0, function* () { return _super.list.call(this, filter); }); } delete(binaryOrId) { const _super = Object.create(null, { delete: { get: () => super.delete } }); return __awaiter(this, void 0, void 0, function* () { return _super.delete.call(this, binaryOrId); }); } listPlugins() { return __awaiter(this, void 0, void 0, function* () { const headers = { accept: 'application/json' }; const url = `${this.listUrl}/plugins`; const res = yield this.fetch(url, { headers }); const data = yield res.json(); return { res, data }; }); } addPlugin(pluginName, pluginFile) { return __awaiter(this, void 0, void 0, function* () { const url = `${this.listUrl}/plugins/${encodeURIComponent(pluginName)}`; const method = 'POST'; const body = new FormData(); const bufferOrStream = pluginFile instanceof ArrayBuffer ? Buffer.from(pluginFile) : pluginFile; body.append('file', bufferOrStream); const headers = { accept: 'application/json' }; const res = yield this.fetch(url, { method, body, headers }); const data = yield res.json(); return { res, data }; }); } removePlugin(pluginName) { return __awaiter(this, void 0, void 0, function* () { const method = 'DELETE'; const headers = { accept: 'application/json' }; const url = `${this.listUrl}/plugins/${pluginName}`; const res = yield this.fetch(url, { method, headers }); const data = yield res.json(); return { res, data }; }); } updateFiles(files) { return __awaiter(this, void 0, void 0, function* () { const url = `${this.listUrl}/files`; const method = 'POST'; const body = new FormData(); files.forEach(file => { const bufferOrStream = file.contents instanceof ArrayBuffer ? Buffer.from(file.contents) : file.contents; body.append(file.path, bufferOrStream); }); const headers = { accept: 'application/json' }; const res = yield this.fetch(url, { method, body, headers }); const data = yield res.json(); return { res, data }; }); } downloadArchive(binaryId) { return __awaiter(this, void 0, void 0, function* () { const url = `${this.listUrl}/${binaryId}`; return yield this.fetch(url); }); } createBinaryRequestBody(binary, fileName, uploadParamsOverride) { const body = new FormData(); const bufferOrStream = binary instanceof ArrayBuffer ? Buffer.from(binary) : binary; let uploadFileName = fileName; if (typeof File !== 'undefined' && binary instanceof File) { uploadFileName = binary.name; } body.append((uploadParamsOverride === null || uploadParamsOverride === void 0 ? void 0 : uploadParamsOverride.bodyFileProperty) || 'file', bufferOrStream, uploadFileName); body.append('fileName', uploadFileName); if (uploadParamsOverride === null || uploadParamsOverride === void 0 ? void 0 : uploadParamsOverride.requestBody) { for (const key in uploadParamsOverride.requestBody) { if (uploadParamsOverride.requestBody.hasOwnProperty(key)) { body.append(key, JSON.stringify(uploadParamsOverride.requestBody[key])); } } } return body; } } //# sourceMappingURL=ApplicationBinaryService.js.map