Skip to content

Commit

Permalink
Merge pull request #1133 from polywrap/pileks-allow-user-to-get-plugi…
Browse files Browse the repository at this point in the history
…n-config-from-client

Allow user to get plugin config from client
  • Loading branch information
dOrgJelli authored Sep 15, 2022
2 parents 7e4d960 + 2a95935 commit 796cf8a
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 10 deletions.
18 changes: 18 additions & 0 deletions packages/js/client/src/PolywrapClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ import {
GetUriResolverOptions,
Contextualized,
GetManifestOptions,
GetPluginOptions,
initWrapper,
IWrapPackage,
IUriResolutionContext,
UriPackageOrWrapper,
UriResolutionContext,
getEnvFromUriHistory,
PluginPackage,
} from "@polywrap/core-js";
import {
buildCleanUriHistory,
Expand Down Expand Up @@ -130,6 +132,16 @@ export class PolywrapClient implements Client {
return this._getConfig(options.contextId).plugins;
}

@Tracer.traceMethod("PolywrapClient: getPlugin")
public getPluginByUri<TUri extends Uri | string>(
uri: TUri,
options: GetPluginOptions = {}
): PluginPackage<unknown> | undefined {
return this.getPlugins(options).find((x) =>
Uri.equals(x.uri, Uri.from(uri))
)?.plugin;
}

@Tracer.traceMethod("PolywrapClient: getInterfaces")
public getInterfaces(
options: GetInterfacesOptions = {}
Expand Down Expand Up @@ -700,6 +712,12 @@ const contextualizeClient = (
getPlugins: (options: GetPluginsOptions = {}) => {
return client.getPlugins({ ...options, contextId });
},
getPluginByUri: <TUri extends Uri | string>(
uri: TUri,
options: GetPluginOptions = {}
) => {
return client.getPluginByUri(uri, { ...options, contextId });
},
getInterfaces: (options: GetInterfacesOptions = {}) => {
return client.getInterfaces({ ...options, contextId });
},
Expand Down
50 changes: 40 additions & 10 deletions packages/js/client/src/__tests__/core/plugin-wrapper.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { PolywrapClient, PluginModule } from "../..";
import { PolywrapClient, PluginModule, PluginPackage } from "../..";
import { getClient } from "../utils/getClient";
import { WrapManifest } from "@polywrap/wrap-manifest-types-js";


jest.setTimeout(200000);

const defaultPlugins = [
Expand Down Expand Up @@ -39,9 +38,10 @@ describe("plugin-wrapper", () => {
}

return {
factory: () => new MockMapPlugin({
map: new Map().set("a", 1).set("b", 2)
}),
factory: () =>
new MockMapPlugin({
map: new Map().set("a", 1).set("b", 2),
}),
manifest: {} as WrapManifest,
};
};
Expand Down Expand Up @@ -169,10 +169,40 @@ describe("plugin-wrapper", () => {
expect(registeredPlugin?.plugin).toEqual(pluginPackage2);
});

test("get plugin package by uri", async () => {
interface SamplePluginConfig {
bar: string;
}
class SamplePluginModule extends PluginModule<SamplePluginConfig> {}
const config: SamplePluginConfig = { bar: "test" };

const pluginPackage = <PluginPackage<SamplePluginConfig>>{
factory: () => new SamplePluginModule(config),
manifest: {},
};

const client = new PolywrapClient(
{
plugins: [
{
uri: "wrap://ens/some.plugin.eth",
plugin: pluginPackage,
},
],
}
);

const plugin = await client.getPluginByUri(
"wrap://ens/some.plugin.eth"
);

expect(plugin).toStrictEqual(pluginPackage);
});

test("get manifest should fetch wrap manifest from plugin", async () => {
const client = await getClient()
const manifest = await client.getManifest("ens/ipfs.polywrap.eth")
expect(manifest.type).toEqual("plugin")
expect(manifest.name).toEqual("Ipfs")
})
const client = await getClient();
const manifest = await client.getManifest("ens/ipfs.polywrap.eth");
expect(manifest.type).toEqual("plugin");
expect(manifest.name).toEqual("Ipfs");
});
});
8 changes: 8 additions & 0 deletions packages/js/core/src/types/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
PluginRegistration,
InterfaceImplementations,
Env,
PluginPackage,
} from "./";
import { IUriResolver } from "../uri-resolution";
import { UriResolverHandler } from "./UriResolver";
Expand Down Expand Up @@ -39,6 +40,8 @@ export interface GetManifestOptions extends Contextualized {
noValidate?: boolean;
}

export type GetPluginOptions = Contextualized;

export interface GetFileOptions extends Contextualized {
path: string;
encoding?: "utf-8" | string;
Expand All @@ -57,6 +60,11 @@ export interface Client

getPlugins(options?: GetPluginsOptions): readonly PluginRegistration<Uri>[];

getPluginByUri<TUri extends Uri | string>(
uri: TUri,
options?: GetPluginOptions
): PluginPackage<unknown> | undefined;

getInterfaces(
options?: GetInterfacesOptions
): readonly InterfaceImplementations<Uri>[];
Expand Down

0 comments on commit 796cf8a

Please sign in to comment.