From ea5360ef6647fe9384e8371c7f8b362cf9489598 Mon Sep 17 00:00:00 2001 From: Ted Klingenberg Date: Fri, 22 Mar 2024 10:17:31 -0400 Subject: [PATCH] Modified Module type in Options to accept dynamic imports or function returning dynamic import This should allow proper type definitions in module option of client when provided a partial module --- libs/rpc/src/client/index.test.ts | 21 +++++++++++++++++++-- libs/rpc/src/interfaces.ts | 6 +++++- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/libs/rpc/src/client/index.test.ts b/libs/rpc/src/client/index.test.ts index 6d89223a..22f9e44e 100644 --- a/libs/rpc/src/client/index.test.ts +++ b/libs/rpc/src/client/index.test.ts @@ -3,8 +3,8 @@ import { createPrimClient } from "./index" type ExampleModule = { hello: (name?: string) => string; goodbye: () => string } -test("client", async () => { - const client = createPrimClient({ +test("static import", async () => { + const client = createPrimClient<() => Promise>({ module: { hello(name: string) { return ["Hello", name].filter(given => given).join(" ") @@ -16,3 +16,20 @@ test("client", async () => { // FIXME: not implemented yet await expect(client.goodbye()).rejects.toBe("not implemented yet") }) + +test("dynamic import", async () => { + const client = createPrimClient({ + module: new Promise>(r => + r({ + // goodbye() { return "Bye!" }, + hello(name: string) { + return ["Hello", name].filter(given => given).join(" ") + }, + }) + ), + }) + // not async since module was provided locally + await expect(client.hello()).resolves.toBe("Hello") + // FIXME: not implemented yet + await expect(client.goodbye()).rejects.toBe("not implemented yet") +}) diff --git a/libs/rpc/src/interfaces.ts b/libs/rpc/src/interfaces.ts index 377fefa0..23f24b1b 100644 --- a/libs/rpc/src/interfaces.ts +++ b/libs/rpc/src/interfaces.ts @@ -276,7 +276,11 @@ export interface PrimOptions>> | null + module?: + | (() => Promise>>>) + | Promise>>> + | PartialDeep>> + | null /** * Provide the server URL where Prim is being used. This will be provided to the HTTP client as the endpoint * parameter.