@c8y/client
Version:
Client application programming interface to access the Cumulocity IoT-Platform REST services.
98 lines • 2.96 kB
JavaScript
import { __awaiter } from "tslib";
import FormData from 'form-data';
import { Service } from '../core/index.js';
export class EventBinaryService extends Service {
constructor() {
super(...arguments);
this.baseUrl = 'event';
this.listUrl = 'events';
}
/**
* Uploads an event binary.
* @returns Response wrapped in [[IResult]]
*
* @param {Stream | Buffer | File | Blob} file file to upload.
* @param {string | number | IEvent} eventOrId Event or Id of the Event.
*
* **Example**
* ```typescript
*
* const file = Buffer.from('aaa');
* const eventId: string | number = 123;
*
* (async () => {
* const {data, res} = await eventBinaryService.upload(file, eventId);
* })();
* ```
*/
upload(file, entityOrId) {
return __awaiter(this, void 0, void 0, function* () {
const method = 'POST';
const url = this.getDetailUrl(entityOrId);
const body = new FormData();
body.append('file', file);
const headers = {
accept: 'application/json'
};
const res = yield this.fetch(url, { method, body, headers });
const data = yield res.json();
return { res, data };
});
}
/**
* Downloads the binary for a given event.
* @returns Response wrapped in [[IFetchResponse]]
*
* @param {string | number | IEvent} eventOrId Event or Id of the Event.
*
* **Example**
* ```typescript
*
* const eventId: string | number = 123;
*
* (async () => {
* const res = await eventBinaryService.download(eventId);
* })();
*/
download(entityOrId) {
return __awaiter(this, void 0, void 0, function* () {
const url = this.getDetailUrl(entityOrId);
return yield this.fetch(url);
});
}
/**
* Removes the binary for a given event.
* @returns Response wrapped in [[IResult]]
*
* @param {string | number | IEvent} eventOrId Event or Id of the Event.
*
* **Example**
* ```typescript
*
* const eventId: string | number = 123;
*
* (async () => {
* const {data, res} = await eventBinaryService.delete(eventId);
* })();
* ```
*/
delete(entityOrId) {
const _super = Object.create(null, {
delete: { get: () => super.delete }
});
return __awaiter(this, void 0, void 0, function* () {
return _super.delete.call(this, entityOrId);
});
}
getDetailUrl(entityOrId) {
let id;
if (typeof entityOrId === 'object' && entityOrId.id) {
id = entityOrId.id;
}
else {
id = entityOrId;
}
return `${this.listUrl}/${id}/binaries`;
}
}
//# sourceMappingURL=EventBinaryService.js.map