Skip to content

Commit

Permalink
using union type instead of generics for Uri
Browse files Browse the repository at this point in the history
  • Loading branch information
krisbitney committed Oct 31, 2023
1 parent 4aa7c5a commit 16d6ec4
Show file tree
Hide file tree
Showing 6 changed files with 521 additions and 491 deletions.
34 changes: 16 additions & 18 deletions packages/client/src/PolywrapClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,38 +75,36 @@ export class PolywrapClient extends PolywrapCoreClient {
}

@Tracer.traceMethod("PolywrapClient: getEnvByUri")
public getEnvByUri<TUri extends Uri | string = Uri>(
uri: TUri
): WrapperEnv | undefined {
public getEnvByUri(uri: Uri | string): WrapperEnv | undefined {
return super.getEnvByUri(Uri.from(uri));
}

@Tracer.traceMethod("PolywrapClient: getManifest")
public async getManifest<TUri extends Uri | string = Uri>(
uri: TUri
public async getManifest(
uri: Uri | string
): Promise<Result<WrapManifest, WrapError>> {
return super.getManifest(Uri.from(uri));
}

@Tracer.traceMethod("PolywrapClient: getFile")
public async getFile<TUri extends Uri | string = Uri>(
uri: TUri,
public async getFile(
uri: Uri | string,
options: GetFileOptions
): Promise<Result<string | Uint8Array, WrapError>> {
return super.getFile(Uri.from(uri), options);
}

@Tracer.traceMethod("PolywrapClient: getImplementations")
public async getImplementations<TUri extends Uri | string = Uri>(
uri: TUri,
public async getImplementations(
uri: Uri | string,
options?: GetImplementationsOptions
): Promise<Result<Uri[], WrapError>> {
return super.getImplementations(Uri.from(uri), options);
}

@Tracer.traceMethod("PolywrapClient: invokeWrapper")
public async invokeWrapper<TData = unknown, TUri extends Uri | string = Uri>(
options: InvokerOptions<TUri> & { wrapper: Wrapper }
public async invokeWrapper<TData = unknown>(
options: InvokerOptions & { wrapper: Wrapper }
): Promise<InvokeResult<TData>> {
return super.invokeWrapper({
...options,
Expand All @@ -115,8 +113,8 @@ export class PolywrapClient extends PolywrapCoreClient {
}

@Tracer.traceMethod("PolywrapClient: invoke")
public async invoke<TData = unknown, TUri extends Uri | string = Uri>(
options: InvokerOptions<TUri>
public async invoke<TData = unknown>(
options: InvokerOptions
): Promise<InvokeResult<TData>> {
return super.invoke({
...options,
Expand All @@ -125,8 +123,8 @@ export class PolywrapClient extends PolywrapCoreClient {
}

@Tracer.traceMethod("PolywrapClient: tryResolveUri")
public async tryResolveUri<TUri extends Uri | string = Uri>(
options: TryResolveUriOptions<TUri>
public async tryResolveUri(
options: TryResolveUriOptions
): Promise<Result<UriPackageOrWrapper, unknown>> {
return super.tryResolveUri({
...options,
Expand All @@ -153,8 +151,8 @@ export class PolywrapClient extends PolywrapCoreClient {
* @returns A Promise with a Result containing a boolean or Error
*/
@Tracer.traceMethod("PolywrapClient: validateConfig")
public async validate<TUri extends Uri | string> /* $ */(
uri: TUri,
public async validate /* $ */(
uri: Uri | string,
options: ValidateOptions
): Promise<Result<true, Error>> {
const wrapper = await this.loadWrapper(Uri.from(uri));
Expand All @@ -167,7 +165,7 @@ export class PolywrapClient extends PolywrapCoreClient {
abi.importedModuleTypes || [];

const importUri = (importedModuleType: ImportedModuleDefinition) => {
return this.tryResolveUri({ uri: importedModuleType.uri });
return this.tryResolveUri({ uri: Uri.from(importedModuleType.uri) });
};
const resolvedModules = await Promise.all(importedModules.map(importUri));
const modulesNotFound = resolvedModules.filter(({ ok }) => !ok) as {
Expand Down
12 changes: 6 additions & 6 deletions packages/client/src/__tests__/core/error-structure.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe("Error structure", () => {
describe("URI resolution", () => {
test("Invoke a wrapper that is not found", async () => {
const client = new PolywrapClient();
const result = await client.invoke<string, string>({
const result = await client.invoke<string>({
uri: asSubinvokeWrapperUri.uri + "-not-found",
method: "simpleMethod",
args: {
Expand Down Expand Up @@ -249,7 +249,7 @@ describe("Error structure", () => {
});
test("Invoke a wrapper with incompatible version", async () => {
const client = new PolywrapClient();
const result = await client.invoke<string, string>({
const result = await client.invoke<string>({
uri: "wrap://fs/tmp",
method: "simpleMethod",
});
Expand Down Expand Up @@ -414,7 +414,7 @@ describe("Error structure", () => {

test("Invoke a plugin wrapper with malformed args", async () => {
const client = await createClient();
const result = await client.invoke<Uint8Array, string>({
const result = await client.invoke<Uint8Array>({
uri: SysBundle.bundle.fileSystem.uri,
method: "readFile",
args: {
Expand Down Expand Up @@ -444,7 +444,7 @@ describe("Error structure", () => {

test("Invoke a plugin wrapper with a method that doesn't exist", async () => {
const client = await createClient();
const result = await client.invoke<Uint8Array, string>({
const result = await client.invoke<Uint8Array>({
uri: SysBundle.bundle.fileSystem.uri,
method: "readFileNotFound",
args: {
Expand All @@ -468,7 +468,7 @@ describe("Error structure", () => {

test("Invoke a plugin wrapper that throws explicitly", async () => {
const client = await createClient();
const result = await client.invoke<string, string>({
const result = await client.invoke<string>({
uri: "wrap://plugin/mock",
method: "methodThatThrows",
});
Expand All @@ -493,7 +493,7 @@ describe("Error structure", () => {

test("Invoke a plugin wrapper that throws unexpectedly", async () => {
const client = await createClient();
const result = await client.invoke<Uint8Array, string>({
const result = await client.invoke<Uint8Array>({
uri: SysBundle.bundle.fileSystem.uri,
method: "readFile",
args: {
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/__tests__/core/wasm-wrapper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe("wasm-wrapper", () => {

test("can invoke with typed URI", async () => {
const client = new PolywrapClient();
const result = await client.invoke<number, Uri>({
const result = await client.invoke<number>({
uri: wrapperUri,
method: "add",
args: {
Expand Down
4 changes: 2 additions & 2 deletions packages/client/src/types/InvokerOptions.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { IUriResolutionContext, Uri } from "@polywrap/core-js";

export interface InvokerOptions<TUri extends Uri | string = Uri> {
export interface InvokerOptions {
/** The Wrapper's URI */
uri: TUri;
uri: Uri | string;

/** Method to be executed. */
method: string;
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/types/UriResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Result } from "@polywrap/result";
/** Options required for URI resolution. */
export interface TryResolveUriOptions {
/** The Wrapper's URI */
uri: Uri;
uri: Uri | string;

/** A URI resolution context */
resolutionContext?: IUriResolutionContext;
Expand Down
Loading

0 comments on commit 16d6ec4

Please sign in to comment.