Skip to content

Commit

Permalink
fix: replace proxy by getter
Browse files Browse the repository at this point in the history
  • Loading branch information
shuffledex committed Jun 1, 2020
1 parent 2586be8 commit 48fab83
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 64 deletions.
44 changes: 20 additions & 24 deletions src/Polymesh.ts
Expand Up @@ -92,7 +92,7 @@ export class Polymesh {
}): Promise<Polymesh> {
const { nodeUrl, accountSeed, keyring, accountUri, harvester } = params;
let polymeshApi: ApiPromise;
let harvesterClient: ApolloClient<NormalizedCacheObject>;
let harvesterClient: ApolloClient<NormalizedCacheObject> | null = null;

try {
const { types, rpc } = polymesh;
Expand All @@ -103,53 +103,49 @@ export class Polymesh {
rpc,
});

harvesterClient = new ApolloClient({
link: setContext((_, { headers }) => {
return {
headers: {
...headers,
'x-api-key': harvester ? harvester.key : '',
},
};
}).concat(
ApolloLink.from([
new HttpLink({
uri: harvester ? harvester.link : '',
}),
])
),
cache: new InMemoryCache(),
});

const isApolloConfigured = typeof harvester !== 'undefined';
if (harvester) {
harvesterClient = new ApolloClient({
link: setContext((_, { headers }) => {
return {
headers: {
...headers,
'x-api-key': harvester ? harvester.key : '',
},
};
}).concat(
ApolloLink.from([
new HttpLink({
uri: harvester ? harvester.link : '',
}),
])
),
cache: new InMemoryCache(),
});
}

let context: Context;

if (accountSeed) {
context = await Context.create({
polymeshApi,
isApolloConfigured,
harvesterClient,
seed: accountSeed,
});
} else if (keyring) {
context = await Context.create({
polymeshApi,
isApolloConfigured,
harvesterClient,
keyring,
});
} else if (accountUri) {
context = await Context.create({
polymeshApi,
isApolloConfigured,
harvesterClient,
uri: accountUri,
});
} else {
context = await Context.create({
polymeshApi,
isApolloConfigured,
harvesterClient,
});
}
Expand Down
73 changes: 33 additions & 40 deletions src/context/index.ts
Expand Up @@ -25,8 +25,7 @@ interface SignerData {

interface ConstructorParams {
polymeshApi: ApiPromise;
isApolloConfigured: boolean;
harvesterClient: ApolloClient<NormalizedCacheObject>;
harvesterClient: ApolloClient<NormalizedCacheObject> | null;
keyring: Keyring;
pair?: SignerData;
}
Expand All @@ -52,13 +51,17 @@ export class Context {

private currentIdentity?: Identity;

public harvesterClient: ApolloClient<NormalizedCacheObject>;
private _harvesterClient?: ApolloClient<NormalizedCacheObject>;

/**
* @hidden
*/
private constructor(params: ConstructorParams) {
const { polymeshApi, isApolloConfigured, harvesterClient, keyring, pair } = params;
const { polymeshApi, harvesterClient, keyring, pair } = params;

if (harvesterClient) {
this._harvesterClient = harvesterClient;
}

this.polymeshApi = new Proxy(polymeshApi, {
get: (target, prop: keyof ApiPromise): ApiPromise[keyof ApiPromise] => {
Expand All @@ -78,70 +81,42 @@ export class Context {
this.currentPair = pair.currentPair;
this.currentIdentity = new Identity({ did: pair.did.toString() }, this);
}

this.harvesterClient = new Proxy(harvesterClient, {
get: (
target,
prop: keyof ApolloClient<NormalizedCacheObject>
): ApolloClient<NormalizedCacheObject>[keyof ApolloClient<NormalizedCacheObject>] => {
if (prop === 'query' && !isApolloConfigured) {
throw new PolymeshError({
code: ErrorCode.FatalError,
message: 'Cannot perform this action without an active harvester connection',
});
}

return target[prop];
},
});
}

static async create(params: {
polymeshApi: ApiPromise;
isApolloConfigured: boolean;
harvesterClient: ApolloClient<NormalizedCacheObject>;
harvesterClient: ApolloClient<NormalizedCacheObject> | null;
seed: string;
}): Promise<Context>;

static async create(params: {
polymeshApi: ApiPromise;
isApolloConfigured: boolean;
harvesterClient: ApolloClient<NormalizedCacheObject>;
harvesterClient: ApolloClient<NormalizedCacheObject> | null;
keyring: Keyring;
}): Promise<Context>;

static async create(params: {
polymeshApi: ApiPromise;
isApolloConfigured: boolean;
harvesterClient: ApolloClient<NormalizedCacheObject>;
harvesterClient: ApolloClient<NormalizedCacheObject> | null;
uri: string;
}): Promise<Context>;

static async create(params: {
polymeshApi: ApiPromise;
isApolloConfigured: boolean;
harvesterClient: ApolloClient<NormalizedCacheObject>;
harvesterClient: ApolloClient<NormalizedCacheObject> | null;
}): Promise<Context>;

/**
* Create the Context instance
*/
static async create(params: {
polymeshApi: ApiPromise;
isApolloConfigured: boolean;
harvesterClient: ApolloClient<NormalizedCacheObject>;
harvesterClient: ApolloClient<NormalizedCacheObject> | null;
seed?: string;
keyring?: Keyring;
uri?: string;
}): Promise<Context> {
const {
polymeshApi,
isApolloConfigured,
harvesterClient,
seed,
keyring: passedKeyring,
uri,
} = params;
const { polymeshApi, harvesterClient, seed, keyring: passedKeyring, uri } = params;

let keyring = new Keyring({ type: 'sr25519' });
let currentPair: IKeyringPair | undefined;
Expand Down Expand Up @@ -171,7 +146,6 @@ export class Context {

return new Context({
polymeshApi,
isApolloConfigured,
harvesterClient,
keyring,
pair: { currentPair, did },
Expand All @@ -184,7 +158,7 @@ export class Context {
}
}

return new Context({ polymeshApi, isApolloConfigured, harvesterClient, keyring });
return new Context({ polymeshApi, harvesterClient, keyring });
}

/**
Expand Down Expand Up @@ -340,4 +314,23 @@ export class Context {

return invalidDids;
}

/**
* Retrieve the harvester client
*
* @throws if credentials are not set
*/
public get harvesterClient(): ApolloClient<NormalizedCacheObject> {
const { _harvesterClient } = this;

if (!_harvesterClient) {
throw new PolymeshError({
code: ErrorCode.FatalError,
message: 'Cannot perform this action without an active harvester connection',
});
}

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return _harvesterClient!;
}
}

0 comments on commit 48fab83

Please sign in to comment.