11'use strict'
22
3+ const debug = / t r u e / i. test ( process . env . DEBUG )
4+
35function DefaultInboundAdapter ( port ) {
46 return async function ( { model, args } ) {
57 return port . apply ( model , args )
@@ -8,28 +10,35 @@ function DefaultInboundAdapter (port) {
810
911/**
1012 * In a hex arch, ports and adapters control I/O between
11- * the application core (domain) and the outside world.
12- * Either a port invokes an outbound adapter or it is invoked
13- * by an inbound one. This function calls adapter factory
14- * functions to inject each adapter with its port or service
15- * dependency.
16- *
17- * It returns an object containing the set of port functions
18- * defined for the domain model in the model spec. These functions
19- * are invoked by a universal function that handles error recovery,
13+ * the application core and the outside world. Inbound
14+ * adapters invoke ports and ports invoke outbound adapters.
15+ * Optionally, outbound adapters invoke services.
16+ *
17+ * To set the above each adapter's factory function
18+ * to inject its port or service dependency--I.e. to bind
19+ * it to a port or service.
20+ *
21+ * It returns an object containing the set of port functions
22+ * defined in the model spec for the domain model. These functions
23+ * are invoked by a port function, which handles error recovery,
2024 * instrumentation, authorization, flow control and other port features.
2125 *
22- * @param {import('.').ports } ports - domain interfaces
23- * @param {{[x:string]:function(*):function(*):any} } adapters - service adapters
24- * @param {* } [services] - (micro-)services
26+ * @param {{
27+ * portSpec:import('.').ports
28+ * ports:{[x:string]:function()}
29+ * adapters:{[x:string]:function()}
30+ * services:{[x:string]:function()}
31+ * }}
32+ *
2533 */
2634export default function bindAdapters ( {
2735 ports,
2836 adapters,
2937 services,
3038 portSpec
3139} = { } ) {
32- if ( ! portSpec || ! adapters ) {
40+ if ( ! portSpec || ! adapters || ! ports ) {
41+ debug && console . debug ( 'missing params' )
3342 return
3443 }
3544
@@ -44,26 +53,18 @@ export default function bindAdapters ({
4453
4554 return Object . keys ( portSpec )
4655 . map ( portName => {
47- try {
48- const spec = portSpec [ portName ]
49- const type = spec . type
50- const port = ports [ portName ]
51- const adapter = adapters [ portName ]
52- const service = services [ spec . service ]
53-
54- if ( ! adapter && type === 'outbound' ) {
55- console . warn ( 'no adapter for port' , portName , spec )
56- return
57- }
56+ const spec = portSpec [ portName ]
57+ const type = spec ?. type ? spec . type : null
58+ const port = ports [ portName ]
59+ const adapter = adapters [ portName ]
60+ const service = services && spec . service ? services [ spec . service ] : null
5861
59- return bindings [ type ] ( portName , port , adapter , service )
60- } catch ( error ) {
61- console . warn ( {
62- fn : bindAdapters . name ,
63- error,
64- spec : portSpec [ portName ]
65- } )
62+ if ( ! spec || ! type || ! port || ! adapter ) {
63+ debug && console . debug ( 'bad port configuration' , portName , spec )
64+ return
6665 }
66+
67+ return bindings [ type ] ( portName , port , adapter , service )
6768 } )
6869 . reduce ( ( p , c ) => ( { ...p , ...c } ) )
6970}
0 commit comments