Skip to content

Commit

Permalink
fix: Bugfix on conditional context injection (#142)
Browse files Browse the repository at this point in the history
  • Loading branch information
Eengineer1 authored Feb 17, 2023
1 parent d69f1c6 commit b09c681
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
5 changes: 4 additions & 1 deletion src/modules/_.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any> = (base: QueryClient) => ({})
Expand All @@ -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
}
Expand Down
17 changes: 13 additions & 4 deletions src/modules/did.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ export class DIDModule extends AbstractCheqdSDKModule {

static readonly querierExtensionSetup: QueryExtensionSetup<DidExtension> = setupDidExtension

readonly querier: CheqdQuerier & DidExtension
querier: CheqdQuerier & DidExtension

constructor(signer: CheqdSigningStargateClient, querier: CheqdQuerier & DidExtension) {
super(signer, querier)
Expand All @@ -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),
}
}

Expand Down Expand Up @@ -393,6 +393,9 @@ export class DIDModule extends AbstractCheqdSDKModule {
}

async queryDidDoc(id: string, context?: IContext): Promise<DIDDocumentWithMetadata> {
if (!this.querier) {
this.querier = context!.sdk!.querier
}
const { didDoc, metadata } = await this.querier[defaultDidExtensionKey].didDoc(id)
return {
didDocument: await DIDModule.toSpecCompliantPayload(didDoc!),
Expand All @@ -401,6 +404,9 @@ export class DIDModule extends AbstractCheqdSDKModule {
}

async queryDidDocVersion(id: string, versionId: string, context?: IContext): Promise<DIDDocumentWithMetadata> {
if (!this.querier) {
this.querier = context!.sdk!.querier
}
const { didDoc, metadata } = await this.querier[defaultDidExtensionKey].didDocVersion(id, versionId)
return {
didDocument: await DIDModule.toSpecCompliantPayload(didDoc!),
Expand All @@ -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))),
Expand Down
11 changes: 10 additions & 1 deletion src/modules/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export class ResourceModule extends AbstractCheqdSDKModule {

static readonly querierExtensionSetup: QueryExtensionSetup<ResourceExtension> = setupResourceExtension

readonly querier: CheqdQuerier & ResourceExtension
querier: CheqdQuerier & ResourceExtension

constructor(signer: CheqdSigningStargateClient, querier: CheqdQuerier & ResourceExtension) {
super(signer, querier)
Expand Down Expand Up @@ -188,14 +188,23 @@ export class ResourceModule extends AbstractCheqdSDKModule {
}

async queryLinkedResource(collectionId: string, resourceId: string, context?: IContext): Promise<ResourceWithMetadata> {
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<Metadata> {
if (!this.querier) {
this.querier = context!.sdk!.querier
}
return await this.querier[defaultResourceExtensionKey].resourceMetadata(collectionId, resourceId)
}

async queryLinkedResources(collectionId: string, context?: IContext): Promise<QueryCollectionResourcesResponse> {
if (!this.querier) {
this.querier = context!.sdk!.querier
}
return await this.querier[defaultResourceExtensionKey].collectionResources(collectionId)
}

Expand Down

0 comments on commit b09c681

Please sign in to comment.