Skip to content

Commit

Permalink
Modified Module type in Options to accept dynamic imports or function…
Browse files Browse the repository at this point in the history
… returning dynamic import

This should allow proper type definitions in module option of client when provided a partial module
  • Loading branch information
doseofted committed Mar 22, 2024
1 parent 4641b42 commit ea5360e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
21 changes: 19 additions & 2 deletions libs/rpc/src/client/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { createPrimClient } from "./index"

type ExampleModule = { hello: (name?: string) => string; goodbye: () => string }

test("client", async () => {
const client = createPrimClient<ExampleModule>({
test("static import", async () => {
const client = createPrimClient<() => Promise<ExampleModule>>({
module: {
hello(name: string) {
return ["Hello", name].filter(given => given).join(" ")
Expand All @@ -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<ExampleModule>({
module: new Promise<Partial<ExampleModule>>(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")
})
6 changes: 5 additions & 1 deletion libs/rpc/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,11 @@ export interface PrimOptions<M extends PossibleModule = object, J extends JsonHa
* be made.
*/
// NOTE: `PartialDeep` allows for partial modules to be provided while full type definitions are provided as generic
module?: PartialDeep<RemoveDynamicImport<RemoveFunctionWrapper<M>>> | null
module?:
| (() => Promise<PartialDeep<RemoveDynamicImport<RemoveFunctionWrapper<M>>>>)
| Promise<PartialDeep<RemoveDynamicImport<RemoveFunctionWrapper<M>>>>
| PartialDeep<RemoveDynamicImport<RemoveFunctionWrapper<M>>>
| null
/**
* Provide the server URL where Prim is being used. This will be provided to the HTTP client as the endpoint
* parameter.
Expand Down

0 comments on commit ea5360e

Please sign in to comment.