UNPKG

@c8y/client

Version:

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

93 lines 2.78 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.EventBinaryService = void 0; const tslib_1 = require("tslib"); const form_data_1 = tslib_1.__importDefault(require("form-data")); const core_1 = require("../core"); class EventBinaryService extends core_1.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); * })(); * ``` */ async upload(file, entityOrId) { const method = 'POST'; const url = this.getDetailUrl(entityOrId); const body = new form_data_1.default(); body.append('file', file); const headers = { accept: 'application/json' }; const res = await this.fetch(url, { method, body, headers }); const data = await 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); * })(); */ async download(entityOrId) { const url = this.getDetailUrl(entityOrId); return await 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); * })(); * ``` */ async delete(entityOrId) { return super.delete(entityOrId); } getDetailUrl(entityOrId) { let id; if (typeof entityOrId === 'object' && entityOrId.id) { id = entityOrId.id; } else { id = entityOrId; } return `${this.listUrl}/${id}/binaries`; } } exports.EventBinaryService = EventBinaryService; //# sourceMappingURL=EventBinaryService.js.map