Skip to content

Commit

Permalink
Nevermind, it didn't work but now it might (fix for typedoc hanging a…
Browse files Browse the repository at this point in the history
…fter build step)
  • Loading branch information
doseofted committed Mar 31, 2024
1 parent 507876d commit 3f12f5f
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 23 deletions.
13 changes: 10 additions & 3 deletions libs/rpc/src/client/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import type { PrimOptions } from "../interfaces"
import type { RpcModule, PossibleModule } from "../types/rpc-module"
import type { MergeModuleMethods } from "../types/merge"
import { createPrimOptions } from "../options"
import { isDefined } from "emery"
import { createMethodCatcher } from "./proxy"
import { handlePotentialPromise } from "./wrapper"
import { getUnfulfilledModule, handleLocalModuleMethod } from "./local"
import type { PrimOptions } from "../interfaces"
import type { RpcModule, PossibleModule } from "../types/rpc-module"
import type { MergeModuleMethods } from "../types/merge"

// export function createPrimClient<
// ModuleType extends PossibleModule = never,
// GivenOptions extends PrimOptions = PrimOptions,
// >(options?: GivenOptions) {
// return new Proxy({}, {}) as RpcModule<ModuleType>
// }

export function createPrimClient<
ModuleType extends PossibleModule = never,
Expand Down
9 changes: 9 additions & 0 deletions libs/rpc/src/types/rpc-module.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ describe("RpcModule produces expected types for developer", () => {
b: {
c(test: number): string
}
lol: string
}
what: string
}>
type ProducedModule = RpcModule<Module, false, false>
type ExpectedModule = {
Expand All @@ -63,12 +65,19 @@ describe("RpcModule produces expected types for developer", () => {
type Module = () => Promise<{
a: {
generator(test: number): Generator<string>
property: {
here: string
somethingElse: number
}
}
}>
type ProducedModule = RpcModule<Module>
type ExpectedModule = {
a: {
generator(test: number): AsyncGenerator<string>
generator(test: SubmitEvent | FormData | HTMLFormElement): AsyncGenerator<string>
// eslint-disable-next-line @typescript-eslint/ban-types
property: {}
}
}
expectTypeOf<ProducedModule>().toMatchTypeOf<ExpectedModule>()
Expand Down
74 changes: 54 additions & 20 deletions libs/rpc/src/types/rpc-module.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,44 @@
import type { ConditionalExcept } from "type-fest"
// import type { ConditionalExcept } from "type-fest"

export type PossibleModule = object

export type WithoutFunctionWrapper<Given> = Given extends (...args: infer _) => infer Returns ? Returns : Given
export type WithoutPromiseWrapper<Given> = Given extends PromiseLike<infer Value> ? Value : Given

// type A = {
// test: string
// testing: number
// tested: never
// }
// type B = {
// test: string
// testing: number
// tested: never
// what: {
// test(): string
// testing: never
// tested: symbol
// a: {
// test: number
// here: never
// }
// }
// }

type OmitNever<T> = {
[K in keyof T as T[K] extends never ? never : K]: T[K]
}
// eslint-disable-next-line @typescript-eslint/ban-types
type CleanUp<T, Keys extends keyof T = keyof T> = {} & { [Key in Keys]: T[Key] }
type OmitNeverRecursive<T> = {
[K in keyof T as T[K] extends never ? never : K]: T[K] extends (...args: unknown[]) => unknown
? T[K]
: T[K] extends object
? CleanUp<OmitNeverRecursive<T[K]>>
: T[K]
}
// type C = OmitNeverRecursive<B>

/** Given the parameters and return value of a function, create a second call signature that supports forms (unless disabled) */
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type FunctionWithFormParameter<Params extends unknown[], Result, F extends true | false = true> = [F] extends [false]
Expand All @@ -24,25 +58,25 @@ export type RpcModule<
Root extends true | false = true,
Keys extends keyof ModuleGiven = Extract<keyof ModuleGiven, string>,
> = [Root] extends [false]
? ConditionalExcept<
{
[Key in Keys]: ModuleGiven[Key] extends (...args: infer Args) => infer Returned
? FunctionWithFormParameter<
Args,
HandlePromise extends true
? Returned extends Generator<infer G>
? AsyncGenerator<G>
: Promise<Awaited<Returned>>
: Returned,
HandleForm
> &
RpcModule<ModuleGiven[Key], HandleForm, HandlePromise, false, false>
: ModuleGiven[Key] extends object
? RpcModule<ModuleGiven[Key], HandleForm, HandlePromise, Recursive, false>
: never
},
never
>
? // consider usage of `OmitNeverRecursive` if `as` condition doesn't work
{
[Key in Keys as ModuleGiven[Key] extends object ? Key : never]: ModuleGiven[Key] extends (
...args: infer Args
) => infer Returned
? FunctionWithFormParameter<
Args,
HandlePromise extends true
? Returned extends Generator<infer G>
? AsyncGenerator<G>
: Promise<Awaited<Returned>>
: Returned,
HandleForm
> &
RpcModule<ModuleGiven[Key], HandleForm, HandlePromise, false, false>
: ModuleGiven[Key] extends object
? RpcModule<ModuleGiven[Key], HandleForm, HandlePromise, Recursive, false>
: never
}
: RpcModule<
WithoutPromiseWrapper<WithoutFunctionWrapper<ModuleGiven>> extends object
? WithoutPromiseWrapper<WithoutFunctionWrapper<ModuleGiven>>
Expand Down

0 comments on commit 3f12f5f

Please sign in to comment.