@@ -294,12 +294,21 @@ class DbServiceInstanceFromCdsConnectTo extends ServiceInstanceFromCdsConnectTo,
294
294
override UserDefinedApplicationService getDefinition ( ) { none ( ) }
295
295
}
296
296
297
+ class ServiceInstanceFromExportedClosureParameter extends ServiceInstance {
298
+ ExportedClosureApplicationServiceDefinition exportedClosure ;
299
+
300
+ ServiceInstanceFromExportedClosureParameter ( ) { this = exportedClosure .getParameter ( 0 ) }
301
+
302
+ override UserDefinedApplicationService getDefinition ( ) { result = exportedClosure }
303
+ }
304
+
297
305
/**
298
306
* A call to `before`, `on`, or `after` on an `cds.ApplicationService`.
299
307
* It registers an handler to be executed when an event is fired,
300
308
* to do something with the incoming request or event as its parameter.
301
309
*/
302
310
class HandlerRegistration extends MethodCallNode {
311
+ /** The instance of the service a handler is registered on. */
303
312
ServiceInstance srv ;
304
313
string methodName ;
305
314
@@ -308,6 +317,9 @@ class HandlerRegistration extends MethodCallNode {
308
317
methodName = [ "before" , "on" , "after" ]
309
318
}
310
319
320
+ /**
321
+ * Gets the instance of the service a handler is registered on.
322
+ */
311
323
ServiceInstance getService ( ) { result = srv }
312
324
313
325
/**
@@ -347,7 +359,7 @@ class HandlerRegistration extends MethodCallNode {
347
359
348
360
/**
349
361
* The first parameter of a handler, representing the request object received either directly
350
- * from a user, or from another service that may be internal (defined in the same application)
362
+ * from a user, or from another service that may be internal (defined in the same application)
351
363
* or external (defined in another application, or even served from a different server).
352
364
* e.g.
353
365
* ``` javascript
@@ -356,7 +368,7 @@ class HandlerRegistration extends MethodCallNode {
356
368
* this.before("SomeEvent", "SomeEntity", (req, next) => { ... });
357
369
* this.after("SomeEvent", "SomeEntity", (req, next) => { ... });
358
370
* }
359
- * ```
371
+ * ```
360
372
* All parameters named `req` above are captured. Also see `HandlerParameterOfExposedService`
361
373
* for a subset of this class that is only about handlers exposed to some protocol.
362
374
*/
@@ -506,7 +518,7 @@ abstract class UserDefinedApplicationService extends UserDefinedService {
506
518
/**
507
519
* Holds if this service supports access from the outside through any kind of protocol.
508
520
*/
509
- predicate isExposed ( ) { not this .isInternal ( ) }
521
+ predicate isExposed ( ) { exists ( this . getCdsDeclaration ( ) ) and not this .isInternal ( ) }
510
522
511
523
/**
512
524
* Holds if this service does not support access from the outside through any kind of protocol, thus being internal only.
@@ -554,6 +566,40 @@ class ImplMethodCallApplicationServiceDefinition extends MethodCallNode,
554
566
override FunctionNode getInitFunction ( ) { result = this .getArgument ( 0 ) }
555
567
}
556
568
569
+ /**
570
+ * A user-defined application service that comes in a form of an exported
571
+ * closure. e.g. Given the below code,
572
+ * ``` javascript
573
+ * const cds = require("@sap/cds");
574
+ *
575
+ * module.exports = (srv) => {
576
+ * srv.before("SomeEvent1", "SomeEntity", (req, res) => { ... })
577
+ * srv.on("SomeEvent2", (req) => { ... } )
578
+ * srv.after("SomeEvent3", (req) => { ... } )
579
+ * }
580
+ * ```
581
+ * This class captures the entire `(srv) => { ... }` function that is
582
+ * exported.
583
+ */
584
+ class ExportedClosureApplicationServiceDefinition extends FunctionNode ,
585
+ UserDefinedApplicationService
586
+ {
587
+ ExportedClosureApplicationServiceDefinition ( ) {
588
+ /*
589
+ * ==================== HACK ====================
590
+ * See issue #221.
591
+ */
592
+
593
+ exists ( PropWrite moduleExports |
594
+ moduleExports .getBase ( ) .asExpr ( ) .( VarAccess ) .getName ( ) = "module" and
595
+ moduleExports .getPropertyName ( ) = "exports" and
596
+ this = moduleExports .getRhs ( )
597
+ )
598
+ }
599
+
600
+ override FunctionNode getInitFunction ( ) { result = this }
601
+ }
602
+
557
603
abstract class InterServiceCommunicationMethodCall extends MethodCallNode {
558
604
string name ;
559
605
ServiceInstance recipient ;
0 commit comments