Skip to content

Commit

Permalink
Merge pull request #1614 from polywrap/nk/refactor-env-plugin-ts
Browse files Browse the repository at this point in the history
refactor(plugin-js): move env to plugin method params
  • Loading branch information
dOrgJelli authored Mar 15, 2023
2 parents 7f433da + 7d200b0 commit 7833bcd
Show file tree
Hide file tree
Showing 30 changed files with 187 additions and 124 deletions.
1 change: 0 additions & 1 deletion packages/cli/src/__tests__/e2e/build.plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ describe("e2e tests for build command - plugin project", () => {
}, {
cwd: testCaseDir,
});

testCliOutput(testCaseDir, code, output, error);
testBuildOutput(testCaseDir, buildDir);
});
Expand Down
2 changes: 1 addition & 1 deletion packages/js/client-config-builder/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ export const uriResolverExts: UriResolverExtBootloader = [

interface IDefaultPlugin {
uri: Uri;
plugin: PluginPackage<unknown>;
plugin: IWrapPackage;
implements: Uri[];
}

Expand Down
1 change: 0 additions & 1 deletion packages/js/client-config-builder/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
"@polywrap/file-system-plugin-js": "~0.10.0-pre",
"@polywrap/http-plugin-js": "~0.10.0-pre",
"@polywrap/logger-plugin-js": "0.10.0-pre.10",
"@polywrap/plugin-js": "0.10.0-pre.12",
"@polywrap/uri-resolver-extensions-js": "0.10.0-pre.12",
"@polywrap/uri-resolvers-js": "0.10.0-pre.12",
"@polywrap/wasm-js": "0.10.0-pre.12",
Expand Down
3 changes: 1 addition & 2 deletions packages/js/client-config-builder/src/bundles/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import * as ipfsHttpClient from "./embeds/ipfs-http-client/wrap";
import * as ipfsResolver from "./embeds/async-ipfs-resolver/wrap";

import { IWrapPackage, Uri } from "@polywrap/core-js";
import { PluginPackage } from "@polywrap/plugin-js";
import {
ethereumProviderPlugin,
Connection,
Expand Down Expand Up @@ -64,7 +63,7 @@ export const uriResolverExts: UriResolverExtBootloader = [

interface IDefaultPlugin {
uri: Uri;
plugin: PluginPackage<unknown>;
plugin: IWrapPackage;
implements: Uri[];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,9 @@ export const envTestCases = (implementation: string) => {
resolver: RecursiveResolver.from([
{
uri: implementationUri,
package: PluginPackage.from<MockEnv>((module) => ({
mockEnv: (): MockEnv => {
return module.env;
package: PluginPackage.from<MockEnv>(() => ({
mockEnv: (_, __, env: MockEnv): MockEnv => {
return env;
},
})),
},
Expand Down
4 changes: 2 additions & 2 deletions packages/js/client/src/__tests__/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ export const mockPluginRegistration = (uri: string | Uri) => {
methodThatThrows: (_: unknown): string => {
throw Error("I'm throwing!");
},
mockEnv(): { a: number } & Record<string, unknown> {
return this.env;
mockEnv(_, __, env): { a: number } & Record<string, unknown> {
return env as unknown as { a: number } & Record<string, unknown>;
},
})),
};
Expand Down
3 changes: 3 additions & 0 deletions packages/js/plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
"@polywrap/tracing-js": "0.10.0-pre.12",
"@polywrap/wrap-manifest-types-js": "0.10.0-pre.12"
},
"peerDependencies": {
"@polywrap/core-js": "0.10.x"
},
"devDependencies": {
"@types/jest": "26.0.8",
"jest": "26.6.3",
Expand Down
5 changes: 3 additions & 2 deletions packages/js/plugin/src/PluginMethod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ import { CoreClient, MaybeAsync } from "@polywrap/core-js";
*/
export type PluginMethod<
TArgs extends Record<string, unknown> = Record<string, unknown>,
TResult = unknown
> = (args: TArgs, client: CoreClient) => MaybeAsync<TResult>;
TResult = unknown,
TEnv extends Record<string, unknown> = Record<string, unknown>
> = (args: TArgs, client: CoreClient, env: TEnv) => MaybeAsync<TResult>;
23 changes: 8 additions & 15 deletions packages/js/plugin/src/PluginModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,26 @@ export abstract class PluginModule<
TConfig,
TEnv extends Record<string, unknown> = Record<string, unknown>
> {
private _env: TEnv;
private _config: TConfig;

constructor(config: TConfig) {
this._config = config;
}

public get env(): TEnv {
return this._env;
}

public get config(): TConfig {
return this._config;
}

public setEnv(env: TEnv): void {
this._env = env;
}

public async _wrap_invoke<
TArgs extends Record<string, unknown> = Record<string, unknown>,
TResult = unknown
>(
method: string,
args: TArgs,
client: CoreClient
client: CoreClient,
env: TEnv
): Promise<Result<TResult, Error>> {
const fn = this.getMethod<TArgs, TResult>(method);
const fn = this.getMethod<TArgs, TResult, TEnv>(method);

if (!fn) {
return ResultErr(Error(`Plugin missing method "${method}"`));
Expand All @@ -48,7 +40,7 @@ export abstract class PluginModule<
}

try {
const data = await fn(args, client);
const data = await fn(args, client, env);
return ResultOk(data);
} catch (e) {
e.code = WrapErrorCode.WRAPPER_INVOKE_ABORTED;
Expand All @@ -58,13 +50,14 @@ export abstract class PluginModule<

public getMethod<
TArgs extends Record<string, unknown> = Record<string, unknown>,
TResult = unknown
TResult = unknown,
TEnv extends Record<string, unknown> = Record<string, unknown>
>(method: string): PluginMethod<TArgs, TResult> | undefined {
const fn:
| PluginMethod<TArgs, TResult>
| PluginMethod<TArgs, TResult, TEnv>
| undefined = ((this as unknown) as Record<
string,
PluginMethod<TArgs, TResult>
PluginMethod<TArgs, TResult, TEnv>
>)[method];

return fn?.bind(this);
Expand Down
8 changes: 4 additions & 4 deletions packages/js/plugin/src/PluginPackage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class PluginPackage<
TConfig,
TEnv extends Record<string, unknown> = Record<string, unknown>
>(
pluginModule: PluginModule<TConfig>,
pluginModule: PluginModule<TConfig, TEnv>,
manifest?: WrapManifest
): PluginPackage<TConfig, TEnv>;
static from<TEnv extends Record<string, unknown> = Record<string, unknown>>(
Expand All @@ -31,15 +31,15 @@ export class PluginPackage<
TEnv extends Record<string, unknown> = Record<string, unknown>
>(
pluginModuleOrGetPluginFuncs:
| PluginModule<TConfig>
| PluginModule<TConfig, TEnv>
| GetPluginMethodsFunc<TEnv>,
manifest?: WrapManifest
): PluginPackage<TConfig, TEnv> {
if (typeof pluginModuleOrGetPluginFuncs === "function") {
const getPluginFuncs = pluginModuleOrGetPluginFuncs as GetPluginMethodsFunc<TEnv>;
const getPluginFuncs = pluginModuleOrGetPluginFuncs as GetPluginMethodsFunc;

return new PluginPackage<TConfig, TEnv>(
new PluginModuleWithMethods<TEnv>(getPluginFuncs),
new PluginModuleWithMethods(getPluginFuncs),
manifest || ({} as WrapManifest)
) as PluginPackage<TConfig, TEnv>;
} else {
Expand Down
15 changes: 12 additions & 3 deletions packages/js/plugin/src/PluginWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,12 @@ export class PluginWrapper implements Wrapper {
return ResultErr(error);
}

// Set the module's environment
this._module.setEnv(options.env || {});
// NOTE: this is used just in case the module instance
// we're interacting with is from versions < 0.10
const genericModule = (this._module as unknown) as Record<string, unknown>;
if (genericModule.setEnv) {
(genericModule.setEnv as (env: unknown) => void)(options.env || {});
}

let jsArgs: Record<string, unknown>;

Expand All @@ -77,7 +81,12 @@ export class PluginWrapper implements Wrapper {
}

// Invoke the function
const result = await this._module._wrap_invoke(method, jsArgs, client);
const result = await this._module._wrap_invoke(
method,
jsArgs,
client,
options.env || {}
);

if (result.ok) {
const data = result.value;
Expand Down
5 changes: 4 additions & 1 deletion packages/js/plugin/src/utils/GetPluginMethodsFunc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ export type GetPluginMethodsFunc<
TEnv extends Record<string, unknown> = Record<string, unknown>
> = (
module: PluginModule<never, TEnv>
) => Record<string, PluginMethod<Record<string, unknown>, unknown>>;
) => Record<
string,
PluginMethod<Record<string, unknown>, unknown, Record<string, unknown>>
>;
15 changes: 10 additions & 5 deletions packages/js/plugin/src/utils/PluginModuleWithMethods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ export class PluginModuleWithMethods<
>(
method: string,
args: TArgs,
client: CoreClient
client: CoreClient,
env: TEnv
): Promise<Result<TResult, Error>> {
const fn = this.getMethod<TArgs, TResult>(method);

Expand All @@ -35,7 +36,7 @@ export class PluginModuleWithMethods<
}

try {
const data = await fn(args, client);
const data = await fn(args, client, env);
return ResultOk(data);
} catch (e) {
e.code = WrapErrorCode.WRAPPER_INVOKE_ABORTED;
Expand All @@ -47,9 +48,13 @@ export class PluginModuleWithMethods<
TArgs extends Record<string, unknown> = Record<string, unknown>,
TResult = unknown
>(method: string): PluginMethod<TArgs, TResult> | undefined {
const fn: PluginMethod<TArgs, TResult> | undefined = this._getPluginMethods(
this
)[method] as PluginMethod<TArgs, TResult>;
const fn:
| PluginMethod<TArgs, TResult, TEnv>
| undefined = this._getPluginMethods(this)[method] as PluginMethod<
TArgs,
TResult,
TEnv
>;

return fn?.bind(this);
}
Expand Down
3 changes: 3 additions & 0 deletions packages/js/wasm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
"@polywrap/tracing-js": "0.10.0-pre.12",
"@polywrap/wrap-manifest-types-js": "0.10.0-pre.12"
},
"peerDependencies": {
"@polywrap/core-js": "0.10.x"
},
"devDependencies": {
"@polywrap/cli-js": "0.10.0-pre.12",
"@types/jest": "26.0.8",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ export abstract class Module<TConfig> extends PluginModule<TConfig{{#envType}},
{{#methods}}
abstract {{name}}(
args: Args_{{name}},
client: CoreClient
client: CoreClient,
{{^env}}env?: null{{/env}}{{#env}}env{{^required}}?{{/required}}: Types.Env{{^required}} | null{{/required}}{{/env}}
): MaybeAsync<{{#return}}{{#toTypescript}}{{toGraphQLType}}{{/toTypescript}}{{/return}}>;
{{^last}}

Expand Down
12 changes: 8 additions & 4 deletions packages/test-cases/cases/bind/sanity/output/plugin-ts/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,25 @@ export interface Args_if {
export abstract class Module<TConfig> extends PluginModule<TConfig, Types.Env> {
abstract moduleMethod(
args: Args_moduleMethod,
client: CoreClient
client: CoreClient,
env?: null
): MaybeAsync<Types.Int>;

abstract objectMethod(
args: Args_objectMethod,
client: CoreClient
client: CoreClient,
env: Types.Env
): MaybeAsync<Types.AnotherType | null>;

abstract optionalEnvMethod(
args: Args_optionalEnvMethod,
client: CoreClient
client: CoreClient,
env?: Types.Env | null
): MaybeAsync<Types.AnotherType | null>;

abstract if(
args: Args_if,
client: CoreClient
client: CoreClient,
env?: null
): MaybeAsync<Types._else>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ export interface Args_methodTwo {
export abstract class Module<TConfig> extends PluginModule<TConfig, Types.Env> {
abstract methodOne(
args: Args_methodOne,
client: CoreClient
client: CoreClient,
env?: null
): MaybeAsync<Types.Object>;

abstract methodTwo(
args: Args_methodTwo,
client: CoreClient
client: CoreClient,
env?: null
): MaybeAsync<Types.String>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface Args_method {
export abstract class Module<TConfig> extends PluginModule<TConfig, Types.Env> {
abstract method(
args: Args_method,
client: CoreClient
client: CoreClient,
env?: null
): MaybeAsync<Types.Object>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface Args_method {
export abstract class Module<TConfig> extends PluginModule<TConfig, Types.Env> {
abstract method(
args: Args_method,
client: CoreClient
client: CoreClient,
env: Types.Env
): MaybeAsync<Types.String>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ export const manifest: WrapManifest = {
"type": "String"
}
],
"env": {
"required": true
},
"kind": 64,
"name": "method",
"required": true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ type Env {
type Module {
method(
str: String!
): String!
): String! @env(required: true)
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface Args_method {
export abstract class Module<TConfig> extends PluginModule<TConfig, Types.Env> {
abstract method(
args: Args_method,
client: CoreClient
client: CoreClient,
env?: Types.Env | null
): MaybeAsync<Types.String>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ export const manifest: WrapManifest = {
"type": "String"
}
],
"env": {
"required": false
},
"kind": 64,
"name": "method",
"required": true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ type Env {
type Module {
method(
str: String!
): String!
): String! @env(required: false)
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ export interface Args_methodTwo {
export abstract class Module<TConfig> extends PluginModule<TConfig, Types.Env> {
abstract methodOne(
args: Args_methodOne,
client: CoreClient
client: CoreClient,
env?: null
): MaybeAsync<Types.Object>;

abstract methodTwo(
args: Args_methodTwo,
client: CoreClient
client: CoreClient,
env?: null
): MaybeAsync<Types.String>;
}
Loading

0 comments on commit 7833bcd

Please sign in to comment.