@c8y/client
Version:
Client application programming interface to access the Cumulocity IoT-Platform REST services.
329 lines • 10.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DeviceRegistrationService = void 0;
const b2a_1 = require("b2a");
const core_1 = require("../core");
const DeviceRegistrationStatus_1 = require("./DeviceRegistrationStatus");
/**
* This class allows registration of a new device.
*/
class DeviceRegistrationService extends core_1.Service {
constructor() {
super(...arguments);
this.baseUrl = 'devicecontrol';
this.listUrl = 'newDeviceRequests';
this.propertyName = 'newDeviceRequests';
}
/**
* Gets the details of device registration.
*
* @param {string|number|IIdentified} entityOrId Entity or Id of the entity.
*
* @returns Response wrapped in [[IResult]]
*
* **Example**
* ```typescript
*
* const entityId: number = 1;
*
* (async () => {
* const {data, res} = await deviceRegistrationService.detail(entityId);
* })();
* ```
*/
async detail(entityOrId) {
return super.detail(entityOrId);
}
/**
* Creates a new device registration.
*
* @param {IDeviceRegistrationCreate} entity Device registration object with mandantory fragments.
*
* @returns Response wrapped in [[IResult]]
*
* **Example**
* ```typescript
*
* const mandantoryObject: IDeviceRegistrationCreate = {
* id: 1,
* };
*
* (async () => {
* const {data, res} = await deviceRegistrationService.create(mandantoryObject);
* })();
* ```
*/
async create(entity) {
return super.create(entity);
}
/**
* Gets the list of device registrations by parameters.
*
* @returns Response wrapped in [[IResultList]]
*
* @param {object} filter Object containing filters for querying registrations.
*
* **Example**
* ```typescript
*
* const filter: object = {
* pageSize: 100,
* withTotalPages: true
* };
*
* (async () => {
* const {data, res, paging} = await deviceRegistrationService.list(filter);
* })();
* ```
*/
async list(filter = {}) {
return super.list(filter);
}
/**
* Removes an registration with given id.
*
* @returns Response wrapped in [[IResult]]
*
* @param {string | number | IIdentified} entityOrId entity or id of the registration.
*
* **Example**
* ```typescript
*
* const id = 'abc';
*
* (async () => {
* const {data, res} = await deviceRegistrationService.delete(id);
* // data will be null
* })();
* ```
*/
async delete(entityOrId) {
return super.delete(entityOrId);
}
/**
* Accepts the device registration for given id.
*
* @returns Response wrapped in [[IResult]]
*
* @param {string | number | IIdentified} entityOrId entity or id of registration. Using an entity object one can pass a securityToken as string next to the id.
*
* @param {string} [securityToken] optional security token to reduce the risk of devices which are not yet registered being taken over by threat actors.
*
* **Examples**
* ```typescript
*
* const id = 'abc';
*
* (async () => {
* const {data, res} = await deviceRegistrationService.accept(id);
* })();
* ```
*
* ```typescript
*
* const entity = { id: 'abc', securityToken: '<secretToken>' };
*
* (async () => {
* const {data, res} = await deviceRegistrationService.accept(entity);
* })();
* ```
*
* ```typescript
*
* const id = 'abc';
* const securityToken = '<secretToken>';
*
* (async () => {
* const {data, res} = await deviceRegistrationService.accept(id, securityToken);
* })();
* ```
*
* Providing a securityToken within an entity object together with another securityToken as a parameter of the
* method will always use the securityToken of the entity object. In the example below <secretTokenA> is
* used as the value. <secretTokenB> is obsolete and ignored.
*
* ```typescript
*
* const entity = { id: 'abc', securityToken: '<secretTokenA>'};
* const securityToken = '<secretTokenB>';
*
* (async () => {
* const {data, res} = await deviceRegistrationService.accept(entity, securityToken);
* })();
*
* ```
*/
async accept(entityOrId, securityToken) {
const token = this.getSecurityToken(entityOrId) || securityToken;
let payload = {
id: this.getIdString(entityOrId),
status: DeviceRegistrationStatus_1.DeviceRegistrationStatus.ACCEPTED
};
if (token) {
payload = {
...payload,
securityToken: token
};
}
return super.update(payload);
}
/**
* Accepts the device registration for all registrations in pending acceptance.
*
* @returns Response wrapped in [[IResultList]]
*
* **Example**
* ```typescript
*
* (async () => {
* const {data, res} = await deviceRegistrationService.acceptAll();
* })();
* ```
*/
async acceptAll() {
const res = await super.fetch(`${this.listUrl}/acceptAll`, { method: 'POST' });
const data = (await res.json());
return { res, data };
}
/**
* Gets limitation information for registering devices to the platform
*
* @returns Response wrapped in [[IResult]]
*
* **Example**
* ```typescript
*
* (async () => {
* const {data, res} = await deviceRegistrationService.limit();
* })();
* ```
*/
async limit() {
const res = await super.fetch(`${this.listUrl}/limit`);
const data = (await res.json());
return { res, data };
}
/**
* Bootstraps the device with given id.
*
* @param {string | number | IIdentified} entityOrId entity or id of registration. Using an entity object one can pass a securityToken as string next to the id.
* @param options for details see [[IDeviceBootstrapOptions]]
* @param {string} [securityToken] optional security token to reduce the risk of devices which are not yet registered being taken over by threat actors.
*
* **Examples**
* ```typescript
*
* const id: 'abc';
* const options: IDeviceBootstrapOptions = {
* basicAuthToken: 'Basic dGVuYW50L3VzZXJuYW1lOnBhc3N3b3Jk',
* basicAuth: {
* user: 'username',
* pass: 'password'
* }
* };
*
* (async () => {
* const {data, res} = await deviceRegistrationService.bootstrap(id, options);
* })();
* ```
*
* ```typescript
*
* const entity: { id: '123', securityToken: '<secretToken>'};
* const options: IDeviceBootstrapOptions = {
* basicAuthToken: 'Basic dGVuYW50L3VzZXJuYW1lOnBhc3N3b3Jk',
* basicAuth: {
* user: 'username',
* pass: 'password'
* }
* };
*
* (async () => {
* const {data, res} = await deviceRegistrationService.bootstrap(entity, options);
* })();
* ```
*
* ```typescript
*
* const id: '123';
* const options: IDeviceBootstrapOptions = {
* basicAuthToken: 'Basic dGVuYW50L3VzZXJuYW1lOnBhc3N3b3Jk',
* basicAuth: {
* user: 'username',
* pass: 'password'
* }
* };
* const securityToken: '<secretToken>'
*
* (async () => {
* const {data, res} = await deviceRegistrationService.bootstrap(id, options, securityToken);
* })();
* ```
*
* Providing a securityToken within an entity object together with another securityToken as a parameter of the
* method will always use the securityToken of the entity object. In the example below <secretTokenA> is
* used as the value. <secretTokenB> is obsolete and ignored.
*
* ```typescript
*
* const options: IDeviceBootstrapOptions = {
* basicAuthToken: 'Basic dGVuYW50L3VzZXJuYW1lOnBhc3N3b3Jk',
* basicAuth: {
* user: 'username',
* pass: 'password'
* }
* };
* const entity: { id: '123', securityToken: '<secretTokenA>'};
* const securityToken: '<secretTokenB>'
*
* (async () => {
* const {data, res} = await deviceRegistrationService.accept(entity, securityToken);
* })();
* ```
*/
async bootstrap(entityOrId, options, securityToken) {
const id = this.getIdString(entityOrId);
const token = this.getSecurityToken(entityOrId) || securityToken;
const body = token ? JSON.stringify({ id, securityToken: token }) : JSON.stringify({ id });
const url = `${this.baseUrl}/deviceCredentials`;
const { basicAuth } = options;
let { basicAuthToken } = options;
if (basicAuth) {
const { user, pass } = basicAuth;
basicAuthToken = (0, b2a_1.btoa)(`${user}:${pass}`);
}
const headers = {
Authorization: `Basic ${basicAuthToken}`,
accept: 'application/json',
'content-type': 'application/json'
};
const method = 'POST';
const res = await this.client.fetch(url, { body, headers, method });
const data = await res.json();
if (res.status > 400) {
throw { res, data };
}
return { res, data };
}
onBeforeCreate(entity) {
return entity;
}
onBeforeUpdate(entity) {
const noIdEntity = Object.assign({}, entity);
delete noIdEntity.id;
return noIdEntity;
}
getDetailUrl(entityOrId) {
const id = encodeURIComponent(this.getEntityId(entityOrId));
return `${this.listUrl}/${id}`;
}
getSecurityToken(entity) {
let securityToken;
if (typeof entity === 'object') {
securityToken = entity.securityToken ? String(entity.securityToken) : undefined;
}
return securityToken;
}
}
exports.DeviceRegistrationService = DeviceRegistrationService;
//# sourceMappingURL=DeviceRegistrationService.js.map