UNPKG

@microsoft/useragent-sdk

Version:

SDK for building decentralized identity wallets and enterprise agents.

154 lines 6.12 kB
"use strict"; /*--------------------------------------------------------------------------------------------- * 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 Commit_1 = require("../hubSession/Commit"); const HubClient_1 = require("../hubClient/HubClient"); const HubObjectQueryRequest_1 = require("../hubSession/requests/HubObjectQueryRequest"); const HubCommitQueryRequest_1 = require("../hubSession/requests/HubCommitQueryRequest"); const IHubClient_1 = require("../hubClient/IHubClient"); /** * Constants that represent what type of commit strategy to be used. */ var CommitStrategyType; (function (CommitStrategyType) { CommitStrategyType["Basic"] = "basic"; CommitStrategyType["LastWriterWins"] = "lastWriterWins"; })(CommitStrategyType = exports.CommitStrategyType || (exports.CommitStrategyType = {})); /** * Constants that represent what interface type the hub request payload will be. */ var HubInterfaceType; (function (HubInterfaceType) { HubInterfaceType["Actions"] = "Actions"; HubInterfaceType["Collections"] = "Collections"; HubInterfaceType["Permissions"] = "Permissions"; HubInterfaceType["Profile"] = "Profile"; })(HubInterfaceType = exports.HubInterfaceType || (exports.HubInterfaceType = {})); /** * Hub Operations */ var Operation; (function (Operation) { Operation["Create"] = "create"; Operation["Read"] = "read"; Operation["Update"] = "update"; Operation["Delete"] = "delete"; })(Operation = exports.Operation || (exports.Operation = {})); /** * Interface for defining options for HubInterface. */ class HubInterfaceOptions extends IHubClient_1.HubClientOptions { constructor() { super(...arguments); /** * Commit Strategy to define what strategy to use when compiling commits. */ this.commitStrategy = CommitStrategyType.Basic; /** * Hub Interface to define the type of interface the hub request payload will be. */ this.hubInterface = HubInterfaceType.Collections; } } exports.HubInterfaceOptions = HubInterfaceOptions; /** * An Abstract Class for Hub Interfaces. * */ class HubInterface { /** * Creates an instance of HubMethods that will be used to send hub requests and responses. * @param [hubInterfaceOptions] for configuring how to form hub requests and responses. */ constructor(hubInterfaceOptions) { if (!hubInterfaceOptions.context) { throw new UserAgentError_1.default(`Hub Interface Options missing context parameter`); } if (!hubInterfaceOptions.type) { throw new UserAgentError_1.default(`Hub Interface Options missing type parameter`); } this.context = hubInterfaceOptions.context; this.type = hubInterfaceOptions.type; this.hubInterface = hubInterfaceOptions.hubInterface; this.commitStrategy = hubInterfaceOptions.commitStrategy; // set up hub client const hubClientOptions = { hubOwner: hubInterfaceOptions.hubOwner, clientIdentifier: hubInterfaceOptions.clientIdentifier, recipientsPublicKeys: hubInterfaceOptions.recipientsPublicKeys, hubProtectionStrategy: hubInterfaceOptions.hubProtectionStrategy, cryptoOptions: hubInterfaceOptions.cryptoOptions }; this.hubClient = new HubClient_1.default(hubClientOptions); } /** * Add object to Hub Owner's hub. * @param object object to be added to hub owned by hub owner. */ async addObject(object) { const commit = new Commit_1.default({ committed_at: (new Date()).toISOString(), iss: this.hubClient.clientIdentifier.id, sub: this.hubClient.hubOwner.id, interface: this.hubInterface, context: this.context, type: this.type, operation: Operation.Create, commit_strategy: this.commitStrategy, payload: object }); return this.hubClient.commit(commit); } /** * Get all unhydrated hubObjects of specific type. */ async getUnHydratedObjects() { const queryRequest = new HubObjectQueryRequest_1.default({ interface: this.hubInterface, context: this.context, type: this.type, }); const objects = await this.hubClient.queryObjects(queryRequest); console.log(objects); return objects; } /** * create and return hydrated hubObject. * @param hubObject unhydrated hubObject containing on object metadata. */ async getObject(hubObject) { const metadata = hubObject.getMetadata(); const commitQueryRequest = new HubCommitQueryRequest_1.default({ object_id: [metadata.id], }); return this.hubClient.queryObject(commitQueryRequest, hubObject); } /** * Get a list of all hydrated HubObjects containing both metadata and payload. */ async getObjects() { const objects = await this.getUnHydratedObjects(); objects.forEach(async (obj) => { obj = await this.getObject(obj); }); return objects; } /** * Update Hub Object in hub owner's hub. */ async updateObject(hubObject) { throw new UserAgentError_1.default(`Not Implemented for ${hubObject}`); } /** * Update Hub Object in hub owner's hub. */ async deleteObject(hubObject) { throw new UserAgentError_1.default(`Not Implemented for ${hubObject}`); } } exports.default = HubInterface; //# sourceMappingURL=HubInterface.js.map