diff --git a/build/server/server.node.ts b/build/server/server.node.ts index f13ab935dd9..cf05e0ba594 100644 --- a/build/server/server.node.ts +++ b/build/server/server.node.ts @@ -6,8 +6,9 @@ namespace $ { expressGenerator() { const self = $mol_wire_async( this ) - return function( ... args: any[] ) { - return self.handleRequest.apply( self, args ) + + return function( req : any , res : any , next : () => void ) { + return self.handleRequest.call( self, req, res, next ) } } diff --git a/wire/async/async.test.ts b/wire/async/async.test.ts index 2b7546aa81a..86cf4fec124 100644 --- a/wire/async/async.test.ts +++ b/wire/async/async.test.ts @@ -1,6 +1,18 @@ namespace $ { $mol_test({ + 'test types'( $ ) { + class A { + static a() { + return '' + } + static b() { + return $mol_wire_async(this).a() + } + } + + type Check = $mol_type_assert, Promise> + }, async 'Latest method calls wins'( $ ) { diff --git a/wire/async/async.ts b/wire/async/async.ts index 2dd3c0124f5..98deb2dcbe3 100644 --- a/wire/async/async.ts +++ b/wire/async/async.ts @@ -30,20 +30,20 @@ namespace $ { return fiber.async() }, - } ) as any as ( - Host extends ( ... args: infer Args )=> infer Res - ? Res extends Promise - ? Host - : ( ... args: Args )=> Promise< Res > - : {} - ) & { - [ key in keyof Host ]: Host[ key ] extends ( ... args: infer Args )=> infer Res - ? Res extends Promise - ? Host[ key ] - : ( ... args: Args )=> Promise< Res > - : Host[ key ] - } + } ) as unknown as ObjectOrFunctionResultPromisify } - + + type FunctionResultPromisify = Some extends (...args: infer Args) => infer Res + ? Res extends PromiseLike ? Some : (...args: Args) => Promise + : Some + + type MethodsResultPromisify = { + [K in keyof Host]: FunctionResultPromisify + } + + type ObjectOrFunctionResultPromisify = ( + Some extends (...args: any) => unknown ? FunctionResultPromisify : {} + ) & ( Some extends Object ? MethodsResultPromisify : Some ) + } diff --git a/wire/sync/sync.test.ts b/wire/sync/sync.test.ts new file mode 100644 index 00000000000..7197e8b949c --- /dev/null +++ b/wire/sync/sync.test.ts @@ -0,0 +1,21 @@ +namespace $ { + + $mol_test({ + + 'test types'( $ ) { + class A { + static a() { + return Promise.resolve('') + } + + static b() { + return $mol_wire_sync(this).a() + } + } + + type Check = $mol_type_assert, string> + }, + }) + +} + diff --git a/wire/sync/sync.ts b/wire/sync/sync.ts index 9b8de135b2a..90a5a328eb2 100644 --- a/wire/sync/sync.ts +++ b/wire/sync/sync.ts @@ -26,17 +26,19 @@ namespace $ { return fiber.sync() }, - } ) as any as ( - Host extends ( ... args: infer Args )=> infer Res - ? Res extends Promise< infer Res2 > - ? ( ... args: Args )=> Res2 - : Host - : {} - ) & { - [ key in keyof Host ]: Host[ key ] extends ( ... args: infer Args )=> Promise< infer Res > - ? ( ... args: Args )=> Res - : Host[ key ] - } + } ) as unknown as ObjectOrFunctionResultAwaited } + type FunctionResultAwaited = Some extends (...args: infer Args) => infer Res + ? (...args: Args) => Awaited + : Some + + type MethodsResultAwaited = { + [K in keyof Host]: FunctionResultAwaited + } + + type ObjectOrFunctionResultAwaited = ( + Some extends (...args: any) => unknown ? FunctionResultAwaited : {} + ) & ( Some extends Object ? MethodsResultAwaited : Some ) + }