From b09c681369de1f577faeeb4568e8103344e123c1 Mon Sep 17 00:00:00 2001 From: Tasos Derisiotis <50984242+Eengineer1@users.noreply.github.com> Date: Fri, 17 Feb 2023 13:41:59 +0200 Subject: [PATCH] fix: Bugfix on conditional context injection (#142) --- src/modules/_.ts | 5 ++++- src/modules/did.ts | 17 +++++++++++++---- src/modules/resource.ts | 11 ++++++++++- 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/modules/_.ts b/src/modules/_.ts index 8264d1ef..21f55b8c 100644 --- a/src/modules/_.ts +++ b/src/modules/_.ts @@ -10,7 +10,7 @@ import { CheqdQuerier } from "../querier.js"; export abstract class AbstractCheqdSDKModule { _signer: CheqdSigningStargateClient methods: IModuleMethodMap = {} - readonly querier: CheqdQuerier + querier: CheqdQuerier readonly _protectedMethods: string[] = ['constructor', 'getRegistryTypes', 'getQuerierExtensionSetup'] static readonly registryTypes: Iterable<[string, GeneratedType]> = [] static readonly querierExtensionSetup: QueryExtensionSetup = (base: QueryClient) => ({}) @@ -19,6 +19,9 @@ export abstract class AbstractCheqdSDKModule { if (!signer) { throw new Error("signer is required") } + if (!querier) { + throw new Error("querier is required") + } this._signer = signer this.querier = querier } diff --git a/src/modules/did.ts b/src/modules/did.ts index 09c30595..517ae090 100644 --- a/src/modules/did.ts +++ b/src/modules/did.ts @@ -191,7 +191,7 @@ export class DIDModule extends AbstractCheqdSDKModule { static readonly querierExtensionSetup: QueryExtensionSetup = setupDidExtension - readonly querier: CheqdQuerier & DidExtension + querier: CheqdQuerier & DidExtension constructor(signer: CheqdSigningStargateClient, querier: CheqdQuerier & DidExtension) { super(signer, querier) @@ -200,9 +200,9 @@ export class DIDModule extends AbstractCheqdSDKModule { createDidDocTx: this.createDidDocTx.bind(this), updateDidDocTx: this.updateDidDocTx.bind(this), deactivateDidDocTx: this.deactivateDidDocTx.bind(this), - queryDidDoc: this.querier.did.didDoc.bind(this), - queryDidDocVersion: this.querier.did.didDocVersion.bind(this), - queryAllDidDocVersionsMetadata: this.querier.did.allDidDocVersionsMetadata.bind(this), + queryDidDoc: this.queryDidDoc.bind(this), + queryDidDocVersion: this.queryDidDocVersion.bind(this), + queryAllDidDocVersionsMetadata: this.queryAllDidDocVersionsMetadata.bind(this), } } @@ -393,6 +393,9 @@ export class DIDModule extends AbstractCheqdSDKModule { } async queryDidDoc(id: string, context?: IContext): Promise { + if (!this.querier) { + this.querier = context!.sdk!.querier + } const { didDoc, metadata } = await this.querier[defaultDidExtensionKey].didDoc(id) return { didDocument: await DIDModule.toSpecCompliantPayload(didDoc!), @@ -401,6 +404,9 @@ export class DIDModule extends AbstractCheqdSDKModule { } async queryDidDocVersion(id: string, versionId: string, context?: IContext): Promise { + if (!this.querier) { + this.querier = context!.sdk!.querier + } const { didDoc, metadata } = await this.querier[defaultDidExtensionKey].didDocVersion(id, versionId) return { didDocument: await DIDModule.toSpecCompliantPayload(didDoc!), @@ -409,6 +415,9 @@ export class DIDModule extends AbstractCheqdSDKModule { } async queryAllDidDocVersionsMetadata(id: string, context?: IContext): Promise<{ didDocumentVersionsMetadata: DIDDocumentMetadata[], pagination: QueryAllDidDocVersionsMetadataResponse['pagination']}> { + if (!this.querier) { + this.querier = context!.sdk!.querier + } const { versions, pagination } = await this.querier[defaultDidExtensionKey].allDidDocVersionsMetadata(id) return { didDocumentVersionsMetadata: await Promise.all(versions.map(async (m) => await DIDModule.toSpecCompliantMetadata(m))), diff --git a/src/modules/resource.ts b/src/modules/resource.ts index 1830e335..97a125c9 100644 --- a/src/modules/resource.ts +++ b/src/modules/resource.ts @@ -105,7 +105,7 @@ export class ResourceModule extends AbstractCheqdSDKModule { static readonly querierExtensionSetup: QueryExtensionSetup = setupResourceExtension - readonly querier: CheqdQuerier & ResourceExtension + querier: CheqdQuerier & ResourceExtension constructor(signer: CheqdSigningStargateClient, querier: CheqdQuerier & ResourceExtension) { super(signer, querier) @@ -188,14 +188,23 @@ export class ResourceModule extends AbstractCheqdSDKModule { } async queryLinkedResource(collectionId: string, resourceId: string, context?: IContext): Promise { + if (!this.querier) { + this.querier = context!.sdk!.querier + } return await this.querier[defaultResourceExtensionKey].resource(collectionId, resourceId) } async queryLinkedResourceMetadata(collectionId: string, resourceId: string, context?: IContext): Promise { + if (!this.querier) { + this.querier = context!.sdk!.querier + } return await this.querier[defaultResourceExtensionKey].resourceMetadata(collectionId, resourceId) } async queryLinkedResources(collectionId: string, context?: IContext): Promise { + if (!this.querier) { + this.querier = context!.sdk!.querier + } return await this.querier[defaultResourceExtensionKey].collectionResources(collectionId) }