@@ -9,37 +9,35 @@ import type { SessionData } from "~/auth/types.ts";
99import type { InCloud } from "~/in-cloud.ts" ;
1010
1111export type ActionMethod <
12- K extends PropertyKey = PropertyKey ,
13- P extends Array < CloudParam < K > > = Array < CloudParam < K > > ,
14- > = ( args : {
12+ P extends Array < InField > | undefined = undefined ,
13+ > = (
14+ args : P extends undefined ? ActionMethodNoParams : {
15+ inCloud : InCloud ;
16+ orm : InSpatialORM ;
17+ params : P extends Array < infer F > ? ExtractParams < P > : never ;
18+ inRequest : InRequest ;
19+ inResponse : InResponse ;
20+ } ,
21+ ) => Promise < any > | any ;
22+
23+ type ActionMethodNoParams = {
1524 inCloud : InCloud ;
1625 orm : InSpatialORM ;
17- params : ExtractParams < K , P > ;
1826 inRequest : InRequest ;
1927 inResponse : InResponse ;
20- } ) => Promise < any > | any ;
21-
22- export type ActionConfig <
23- K extends PropertyKey = PropertyKey ,
24- P extends Array < CloudParam < K > > = Array < CloudParam < K > > ,
25- R extends ActionMethod < K , P > = ActionMethod < K , P > ,
26- > = {
27- run : R ;
28- /**
29- * Whether to skip reading the request body. Should be set to true if the action
30- * will be reading the request body itself, such as when uploading files.
31- */
32- raw ?: boolean ;
33- description ?: string ;
28+ } ;
29+
30+ export interface ActionConfigBase {
3431 label ?: string ;
32+ description ?: string ;
33+ raw ?: boolean ;
3534 authRequired ?: boolean ;
3635 hideFromApi ?: boolean ;
37- params : P ;
38- } ;
36+ }
37+
3938export class CloudAPIAction <
40- K extends PropertyKey = PropertyKey ,
41- P extends Array < CloudParam < K > > = Array < CloudParam < K > > ,
42- R extends ActionMethod < K , P > = ActionMethod < K , P > ,
39+ K extends string = string ,
40+ AP extends Array < InField & { key : K } > | undefined = any ,
4341> {
4442 description : string = "This is a Cloud API action" ;
4543 label ?: string ;
@@ -52,23 +50,20 @@ export class CloudAPIAction<
5250 params : Map < string , CloudParam < PropertyKey > > ;
5351 requiredParams : string [ ] = [ ] ;
5452
55- #_run: ( args : {
56- inCloud : InCloud ;
57- orm : InSpatialORM ;
58- params : any ;
59- inRequest : InRequest ;
60- inResponse : InResponse ;
61- } ) => Promise < any > | any ;
53+ #_run: ActionMethod < AP > ;
6254
6355 raiseError ( message : string ) : never {
6456 raiseServerException ( 400 , message ) ;
6557 }
6658
6759 constructor (
6860 actionName : string ,
69- config : ActionConfig < K , P , R > ,
61+ config : ActionConfigBase & {
62+ params ?: AP extends undefined ? never : AP ;
63+ action : ActionMethod < AP > ;
64+ } ,
7065 ) {
71- this . #_run = config . run ;
66+ this . #_run = config . action ;
7267 this . actionName = actionName ;
7368 this . raw = config . raw || false ;
7469 this . label = config . label || this . label ||
@@ -80,10 +75,10 @@ export class CloudAPIAction<
8075 if ( config . hideFromApi === true ) {
8176 this . includeInAPI = false ;
8277 }
83- this . params = new Map ( config . params . map ( ( p ) => [ p . key as string , p ] ) ) ;
84- this . requiredParams = config . params . filter ( ( param ) => param . required ) . map (
78+ this . params = new Map ( config . params ? .map ( ( p ) => [ p . key as string , p ] ) ) ;
79+ this . requiredParams = config . params ? .filter ( ( param ) => param . required ) . map (
8580 ( p ) => p . key as string ,
86- ) ;
81+ ) || [ ] ;
8782 }
8883
8984 #validateParams(
@@ -186,6 +181,25 @@ export class CloudAPIAction<
186181 runObject . orm = args . inCloud . orm . withUser ( user ) ;
187182 }
188183
189- return await this . #_run( runObject ) ;
184+ return await this . #_run( runObject as any ) ;
190185 }
191186}
187+
188+ /** Define a Cloud API Action
189+ *
190+ * @param actionName The name of the action
191+ * @param config The configuration for the action
192+ * @returns A new CloudAPIAction instance
193+ */
194+ export function defineAPIAction <
195+ K extends string ,
196+ AP extends Array < InField & { key : K } > | undefined ,
197+ > (
198+ actionName : string ,
199+ config : ActionConfigBase & {
200+ params ?: AP extends undefined ? never : AP ;
201+ action : ActionMethod < AP > ;
202+ } ,
203+ ) : CloudAPIAction < K , AP > {
204+ return new CloudAPIAction ( actionName , config ) ;
205+ }
0 commit comments