@@ -11,6 +11,23 @@ export interface PlexusActionHooks {
1111 ignoreInit ( ) : void
1212 batch < ReturnType = any > ( fn : ( ) => ReturnType ) : ReturnType
1313}
14+ export type ActionFunction <
15+ Params extends any [ ] | [ ] = any [ ] ,
16+ Response = any
17+ > = (
18+ helpers : PlexusActionHooks ,
19+ ...args : Params
20+ ) => Response | Promise < Response >
21+
22+ export type InnerFunction < InputFn extends ActionFunction > = InputFn extends (
23+ helpers : PlexusActionHooks ,
24+ ...args : infer Params
25+ ) => ReturnType < InputFn >
26+ ? ( ...args : Params ) => ReturnType < InputFn >
27+ : never
28+
29+ export type PlexusAction = typeof _action
30+
1431/**
1532 * The action helpers for a defined plexus action
1633 */
@@ -94,40 +111,11 @@ class PlexusActionHelpers {
94111 }
95112}
96113
97- export type FunctionArgs = [ helpers : PlexusActionHooks , ...args : any [ ] ]
98- export type InnerFunctionArgs = [ helpers ?: PlexusActionHooks , ...args : any [ ] ]
99-
100- export type FunctionType < Response = any > = (
101- ...args : FunctionArgs
102- ) => Response | Promise < Response >
103-
104- export type InnerFunction < ResultFn extends FunctionType > = ResultFn extends (
105- helpers : PlexusActionHooks ,
106- ...args : infer Params
107- ) => ReturnType < ResultFn >
108- ? ( ...args : Params ) => ReturnType < ResultFn >
109- : never
110-
111- // export type PlexusAction = <ReturnData=FunctionType>(fn: FunctionType) => (...args: any) => ReturnData | Promise<ReturnData>
112- export type PlexusAction = typeof _action
113-
114- export function _action < Response , Fn extends FunctionType > (
115- instance : ( ) => PlexusInstance ,
116- fn : ( ( ...args : FunctionArgs ) => Response ) & Fn ,
117- batched ?: boolean
118- ) : ( ...args : InnerFunctionArgs ) => Response
119-
120- export function _action < Response , Fn extends FunctionType > (
121- instance : ( ) => PlexusInstance ,
122- fn : ( ( ...args : FunctionArgs ) => Promise < Response > ) & Fn ,
123- batched ?: boolean
124- ) : ( ...args : InnerFunctionArgs ) => Promise < Response >
125-
126- export function _action < Response , Fn extends FunctionType > (
114+ export function _action < Returns , Fn extends ActionFunction > (
127115 instance : ( ) => PlexusInstance ,
128- fn : ( ( ...args : FunctionArgs ) => Promise < Response > | Response ) & Fn ,
116+ fn : Fn & ( ( helpers : PlexusActionHooks , ...args : any [ ] ) => Returns ) ,
129117 batched ?: boolean
130- ) {
118+ ) : InnerFunction < Fn > {
131119 const helpers = new PlexusActionHelpers ( instance )
132120
133121 console . log ( 'function constructor' , fn . constructor . name , isAsyncFunction ( fn ) )
@@ -185,8 +173,7 @@ export function _action<Response, Fn extends FunctionType>(
185173 }
186174 }
187175 // return the proxy function
188- return newAction as InnerFunction < Fn >
189- // return proxyFn as InnerFunction<Fn>
176+ return newAction as InnerFunction < typeof fn >
190177
191178 // const newAction = async (...args) => {
192179 // try {
@@ -223,29 +210,17 @@ export function _action<Response, Fn extends FunctionType>(
223210 * @param fn The Plexus action function to run
224211 * @returns The intended return value of fn, or null if an error is caught
225212 */
226- export function action < Response > (
227- fn : ( ...args : FunctionArgs ) => Response
228- ) : ( ...args : InnerFunctionArgs ) => Response
229- export function action < Response > (
230- fn : ( ...args : FunctionArgs ) => Promise < Response >
231- ) : ( ...args : InnerFunctionArgs ) => Promise < Response >
232- export function action < Response > (
233- fn : ( ...args : FunctionArgs ) => Promise < Response > | Response
234- ) {
235- return _action ( ( ) => instance ( ) , fn )
213+ export function action < Fn extends ActionFunction > ( fn : Fn ) : InnerFunction < Fn > {
214+ return _action ( ( ) => instance ( ) , fn ) as InnerFunction < Fn >
236215}
237216
238217/**
239218 * Generate a Plexus Action
240219 * @param fn The Plexus action function to run
241220 * @returns The intended return value of fn, or null if an error is caught
242221 */
243- export function batchAction < Response > ( fn : ( ...args : FunctionArgs ) => Response )
244- export function batchAction < Response > (
245- fn : ( ...args : FunctionArgs ) => Promise < Response >
246- )
247- export function batchAction < Response > (
248- fn : ( ...args : FunctionArgs ) => Promise < Response > | Response
249- ) {
222+ export function batchAction < Fn extends ActionFunction > (
223+ fn : Fn
224+ ) : InnerFunction < Fn > {
250225 return _action ( ( ) => instance ( ) , fn , true )
251226}
0 commit comments