UNPKG

@c8y/client

Version:

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

61 lines 2.15 kB
import { __awaiter } from "tslib"; import FormData from 'form-data'; /** * This class allows for bulk registering devices. */ export class DeviceRegistrationBulkService { /** * Instantiate class and assign client * @param {IFetchClient} client */ constructor(client) { this.client = client; } /** * A new device registration as bulk. * * @param {Stream | Buffer} csv * * @returns Response wrapped in [[IResult]] * * **Example** * ```typescript * * const csvString = ` * ID,CREDENTIALS,TENANT,TYPE,NAME,ICCID,IDTYPE,PATH,SHELL * e2eDeviceRegistrationId,e2epassword123!,e2edocker,c8y_e2eDevice,e2eDevice,123456789,89456,e2eDeviceGroup,1 * 900001,e2epassword123!,e2edocker,c8y_e2eDevice1,e2eDevice1,123456789,89456,e2eDeviceGroup,1 * 900002,e2epassword123!,e2edocker,c8y_e2eDevice2,e2eDevice2,123456789,89456,e2eDeviceGroup,1`.trim(); * * const csvBuffer = Buffer.from(csvString): * * (async () => { * const {data, res} = await deviceRegistrationBulkService.create(csvBuffer); * })(); * ``` */ create(inputFile) { return __awaiter(this, void 0, void 0, function* () { let csv = inputFile; const url = '/devicecontrol/bulkNewDeviceRequests'; const method = 'POST'; const body = new FormData(); let fileName = 'bulk-registration.csv'; if (typeof File !== 'undefined' && csv instanceof File) { fileName = csv.name; const checkPattern = /\.csv$/; if (csv.name.match(checkPattern)) { csv = new File([csv], csv.name, { type: 'text/csv' }); } } body.append('file', csv, fileName); const headers = { accept: 'application/json' }; const res = yield this.client.fetch(url, { method, body, headers }); const data = yield res.json(); return { res, data }; }); } } //# sourceMappingURL=DeviceRegistrationBulkService.js.map