@c8y/client
Version:
Client application programming interface to access the Cumulocity IoT-Platform REST services.
154 lines • 4.62 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.IdentityService = void 0;
/**
* This class allwos for managing external identifiers.
*/
class IdentityService {
constructor(client) {
this.baseUrl = 'identity';
this.propertyName = 'externalIds';
this.client = client;
}
/**
* Gets the list of identities filtered by parameters.
*
* @returns Response wrapped in [[IResultList]]
*
* @param {object} filter Object containing filters for querying identity.
*
* **Example**
* ```typescript
*
* const filter: object = {
* pageSize: 100,
* withTotalPages: true
* };
*
* (async () => {
* const {data, res, paging} = await identityService.list(filter);
* })();
* ```
*/
async list(managedObjectId) {
const headers = { accept: 'application/json' };
const url = this.getExternalIdsOfGlobalIdUrl(managedObjectId);
const res = await this.fetch(url, { headers });
const json = await res.json();
const data = json[this.propertyName];
return { res, data };
}
/**
* Creates a new identity.
*
* @param {IExternalIdentity} identity Identity object with mandantory fragments.
*
* @returns Response wrapped in [[IResult]]
*
* **Example**
* ```typescript
* const linkedManagedObjectId = '123';
* const identity: IExternalIdentity = {
* type: 'type',
* externalId: '1',
* managedObject: {
* id: linkedManagedObjectId
* }
* };
*
* (async () => {
* const {data, res} = await identityService.create(identity);
* })();
* ```
*/
async create(identity) {
const headers = { 'content-type': 'application/json', accept: 'application/json' };
const method = 'POST';
const body = JSON.stringify(identity);
const url = this.getExternalIdsOfGlobalIdUrl(identity.managedObject.id);
const res = await this.fetch(url, { headers, method, body });
const data = await res.json();
return { res, data };
}
/**
* Gets the details of an identity.
*
* @param {IExternalIdentity} identity Identity object with mandantory fragments.
*
* @returns Response wrapped in [[IResult]]
*
* **Example**
* ```typescript
*
* const identity: IExternalIdentity = {
* type: 'type',
* externalId: '1'
* };
*
* (async () => {
* const {data, res} = await identityService.detail(identity);
* })();
* ```
*/
async detail(identity) {
const headers = { accept: 'application/json' };
const url = this.getExternalIdUrl(identity);
const res = await this.fetch(url, { headers });
const data = await res.json();
return { res, data };
}
/**
* Removes an identity with given id.
*
* @returns Response wrapped in [[IResult]]
*
* @param {IExternalIdentity} identity Identity object with mandantory fragments.
*
* **Example**
* ```typescript
*
* const identity: IExternalIdentity = {
* type: 'type',
* externalId: '1'
* };
*
* (async () => {
* const {data, res} = await identityService.delete(identity);
* })();
* ```
*/
async delete(identity) {
const headers = { accept: 'application/json' };
const method = 'DELETE';
const url = this.getExternalIdUrl(identity);
const res = await this.fetch(url, { headers, method });
return { res, data: null };
}
async fetch(url, init) {
const res = await this.client.fetch(url, init);
if (res.status >= 400) {
let data = null;
try {
data = await res.json();
}
catch (ex) {
try {
data = await res.text();
}
catch (ex) {
// do nothing
}
}
throw { res, data };
}
return res;
}
getExternalIdsOfGlobalIdUrl(managedObjectId) {
return `/${this.baseUrl}/globalIds/${managedObjectId}/externalIds`;
}
getExternalIdUrl(identity) {
return `/${this.baseUrl}/externalIds/${identity.type}/${identity.externalId}`;
}
}
exports.IdentityService = IdentityService;
//# sourceMappingURL=IdentityService.js.map