Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Web3ApiClient - Preload API resolvers #777

Merged
merged 19 commits into from
Apr 9, 2022
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 68 additions & 21 deletions packages/js/client/src/Web3ApiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,16 @@ import {
sanitizeUriRedirects,
sanitizeEnvs,
ClientConfig,
ResolveUriError,
UriResolutionHistory,
resolveUri,
UriToApiResolver,
GetResolversOptions,
CacheResolver,
Contextualized,
ResolveUriOptions,
ApiAggregatorResolver,
coreInterfaceUris,
ResolveUriErrorType,
ResolveUriResult,
} from "@web3api/core-js";
import { Tracer } from "@web3api/tracing-js";

Expand Down Expand Up @@ -435,12 +437,7 @@ export class Web3ApiClient implements Client {
public async resolveUri<TUri extends Uri | string>(
uri: TUri,
options?: ResolveUriOptions<ClientConfig>
): Promise<{
api?: Api;
uri?: Uri;
uriHistory: UriResolutionHistory;
error?: ResolveUriError;
}> {
): Promise<ResolveUriResult> {
options = options || {};

const { contextId, shouldClearContext } = this._setContext(
Expand Down Expand Up @@ -486,6 +483,34 @@ export class Web3ApiClient implements Client {
};
}

@Tracer.traceMethod("Web3ApiClient: tryLoadApiResolvers")
public async tryLoadApiResolvers(): Promise<{
success: boolean;
failedResolverUris: string[];
}> {
const apiAggregatorResolver = this.getResolvers().find(
(x) => x.name === ApiAggregatorResolver.name
) as ApiAggregatorResolver;

if (!apiAggregatorResolver) {
throw new Error(
"Unnecessary tryLoadApiResolvers call, no ApiAggregatorResolver configured."
);
}

const resolverUris = getImplementations(
coreInterfaceUris.uriResolver,
this.getInterfaces({}),
this.getRedirects({})
);

return apiAggregatorResolver.tryLoadApiResolvers(
this,
this._apiCache,
resolverUris
);
}

private _addDefaultConfig() {
const defaultClientConfig = getDefaultClientConfig();

Expand Down Expand Up @@ -671,20 +696,41 @@ export class Web3ApiClient implements Client {
});

if (!api) {
if (error && error === ResolveUriError.InfiniteLoop) {
if (error) {
const errorMessage = error.error?.message ?? "";

switch (error.type) {
case ResolveUriErrorType.InfiniteLoop:
throw Error(
`Infinite loop while resolving URI "${uri}".\nResolution Stack: ${JSON.stringify(
uriHistory,
null,
2
)}`
);
break;
case ResolveUriErrorType.InternalResolver:
throw Error(
`URI resolution error while resolving URI "${uri}".\n${errorMessage}\nResolution Stack: ${JSON.stringify(
uriHistory,
null,
2
)}`
);
break;
default:
throw Error(`Unsupported URI resolution error type occurred`);
break;
}
} else {
throw Error(
`Infinite loop while resolving URI "${uri}".\nResolution Stack: ${JSON.stringify(
`Uknown URI resolution error while resolving URI "${uri}"\nResolution Stack: ${JSON.stringify(
uriHistory,
null,
2
)}`
);
}

throw Error(
`No Web3API found at URI: ${uri.uri}` +
`\nResolution history: ${JSON.stringify(uriHistory, null, 2)}`
);
}

return api;
Expand Down Expand Up @@ -771,13 +817,14 @@ const contextualizeClient = (
resolveUri: <TUri extends Uri | string>(
uri: TUri,
options?: ResolveUriOptions<ClientConfig>
): Promise<{
api?: Api;
uri?: Uri;
uriHistory: UriResolutionHistory;
error?: ResolveUriError;
}> => {
): Promise<ResolveUriResult> => {
return client.resolveUri(uri, { ...options, contextId });
},
tryLoadApiResolvers: (): Promise<{
success: boolean;
failedResolverUris: string[];
}> => {
return client.tryLoadApiResolvers();
},
}
: client;
Loading