UNPKG

@affinidi-tdk/iota-browser

Version:

Browser module to fetch data through Affinidi Iota Framework

50 lines 1.93 kB
import { ChannelProvider, } from './helpers/channel-provider'; import { ResponseHandler, } from './helpers/response-handler'; import { VaultHandler } from './helpers/vault-handler'; import { IotaRequest } from './request'; import { IotaErrorCode, newIotaError } from './validators/error'; export class Session { channelProvider; credentials; vaultHandler; constructor(params) { const { credentials } = params; this.credentials = credentials; this.channelProvider = new ChannelProvider(); this.vaultHandler = new VaultHandler(); } async initialize() { await this.channelProvider.initialize(this.credentials); } async prepareRequest(params) { this.isChannelProviderInitialized(); const iotaRequest = await this.channelProvider.prepareRequest(params); const request = new IotaRequest({ session: this, correlationId: iotaRequest.correlationId, payload: iotaRequest.payload, }); return request; } prepareRequestWithCallback(params, callback) { this.prepareRequest(params) .then((request) => callback(null, request)) .catch((error) => callback(error, null)); } async getResponse(correlationId) { this.isChannelProviderInitialized(); const responseHandler = new ResponseHandler(this.channelProvider); return responseHandler.getResponse(correlationId); } getResponseWithCallback(correlationId, callback) { this.isChannelProviderInitialized(); const responseHandler = new ResponseHandler(this.channelProvider); responseHandler.getResponseWithCallback(correlationId, callback); } isChannelProviderInitialized() { if (!this.channelProvider) { throw newIotaError(IotaErrorCode.IOTA_SESSION_NOT_INITIALIZED); } } } //# sourceMappingURL=session.js.map