@microsoft/useragent-sdk
Version:
SDK for building decentralized identity wallets and enterprise agents.
56 lines • 2.33 kB
JavaScript
;
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
Object.defineProperty(exports, "__esModule", { value: true });
const UserAgentError_1 = require("../UserAgentError");
const CommitStrategyBasic_1 = require("../hubSession/CommitStrategyBasic");
/**
* Class that represents an object in a hub.
*/
class HubObject {
/**
* Create an instance for Hub Object using hub object's metadata.
* @param objectMetadata object metadata that represents an object in a hub.
*/
constructor(objectMetadata) {
this.objectMetadata = objectMetadata;
}
/**
* If payload is not defined, get the payload from hub session using metadata.
* @param hubSession the hub session that will be used to query object
* @param commitQueryRequest the commit query requests for getting all commits for certain object.
*/
async hydrate(hubSession, commitQueryRequest) {
if (this.payload) {
return this.payload;
}
if (this.objectMetadata.commit_strategy !== 'basic') {
throw new UserAgentError_1.default('Currently only the basic commit strategy is supported.');
}
const commitQueryResponse = await hubSession.send(commitQueryRequest);
const commits = commitQueryResponse.getCommits();
const strategy = new CommitStrategyBasic_1.default();
this.payload = await strategy.resolveObject(commits);
return this.payload;
}
/**
* Get the Payload of the Hub Object if object is hydrated.
* Throws an Error if object is not hydrated.
*/
getPayload() {
if (this.payload) {
return this.payload;
}
throw new UserAgentError_1.default(`payload undefined for '${this.objectMetadata.id}'`);
}
/**
* Get The Metadata of the Hub Object.
*/
getMetadata() {
return this.objectMetadata;
}
}
exports.default = HubObject;
//# sourceMappingURL=HubObject.js.map