@@ -13,54 +13,52 @@ import {ZCAP_CLIENT as zcapClient} from './zcapClient.js';
13
13
14
14
const { config, util : { BedrockError} } = bedrock ;
15
15
16
- let WORKFLOWS_BY_NAME_MAP ;
17
- let WORKFLOWS_BY_ID_MAP ;
16
+ let DEFINITIONS_BY_TYPE_MAP ;
17
+ let DEFINITIONS_BY_ID_MAP ;
18
18
19
19
bedrock . events . on ( 'bedrock.init' , ( ) => {
20
20
const cfg = config [ 'profile-http' ] ;
21
21
22
22
// interactions feature is optional, return early if not enabled
23
- const { interactions} = cfg ;
24
- if ( ! interactions ?. enabled ) {
23
+ if ( ! cfg . interactions ?. enabled ) {
25
24
return ;
26
25
}
27
26
28
- // parse workflow configs when interactions are enabled
29
- const { workflows = { } } = interactions ;
30
- WORKFLOWS_BY_NAME_MAP = new Map ( ) ;
31
- WORKFLOWS_BY_ID_MAP = new Map ( ) ;
32
- for ( const workflowName in workflows ) {
33
- const { localInteractionId, zcaps} = workflows [ workflowName ] ;
27
+ // parse interaction types when enabled
28
+ const { types = { } } = cfg . interactions ;
29
+ DEFINITIONS_BY_TYPE_MAP = new Map ( ) ;
30
+ DEFINITIONS_BY_ID_MAP = new Map ( ) ;
31
+ for ( const typeName in types ) {
32
+ const { localInteractionId, zcaps} = types [ typeName ] ;
34
33
if ( ! localInteractionId ) {
35
34
throw new TypeError (
36
- '"bedrock.config.profile-http.interactions.workflows " must each ' +
35
+ '"bedrock.config.profile-http.interaction.types " must each ' +
37
36
'have "localInteractionId".' ) ;
38
37
}
39
- const workflow = {
38
+ const definition = {
39
+ name : typeName ,
40
40
localInteractionId,
41
- name : workflowName ,
42
41
zcaps : new Map ( )
43
42
} ;
44
43
for ( const zcapName in zcaps ) {
45
44
const zcap = zcaps [ zcapName ] ;
46
- workflow . zcaps . set ( zcapName , JSON . parse ( zcap ) ) ;
45
+ definition . zcaps . set ( zcapName , JSON . parse ( zcap ) ) ;
47
46
}
48
- if ( ! workflow . zcaps . has ( 'readWriteExchanges' ) ) {
47
+ if ( ! definition . zcaps . has ( 'readWriteExchanges' ) ) {
49
48
throw new TypeError (
50
- '"bedrock.config.profile-http.interactions.workflows " must each ' +
49
+ '"bedrock.config.profile-http.interaction.types " must each ' +
51
50
'have "zcaps.readWriteExchanges".' ) ;
52
51
}
53
- WORKFLOWS_BY_NAME_MAP . set ( workflowName , workflow ) ;
54
- WORKFLOWS_BY_ID_MAP . set ( localInteractionId , workflow ) ;
52
+ DEFINITIONS_BY_TYPE_MAP . set ( typeName , definition ) ;
53
+ DEFINITIONS_BY_ID_MAP . set ( localInteractionId , definition ) ;
55
54
}
56
55
} ) ;
57
56
58
57
bedrock . events . on ( 'bedrock-express.configure.routes' , app => {
59
58
const cfg = config [ 'profile-http' ] ;
60
59
61
- // interactions feature is optional, return early if not enabled
62
- const { interactions} = cfg ;
63
- if ( ! interactions ?. enabled ) {
60
+ // interaction feature is optional, return early if not enabled
61
+ if ( ! cfg . interactions ?. enabled ) {
64
62
return ;
65
63
}
66
64
@@ -77,11 +75,11 @@ bedrock.events.on('bedrock-express.configure.routes', app => {
77
75
validate ( { bodySchema : schemas . createInteraction } ) ,
78
76
asyncHandler ( async ( req , res ) => {
79
77
const { id : accountId } = req . user . account || { } ;
80
- const { workflowName , exchange : { variables} } = req . body ;
78
+ const { type , exchange : { variables} } = req . body ;
81
79
82
- const workflow = WORKFLOWS_BY_NAME_MAP . get ( workflowName ) ;
83
- if ( ! workflow ) {
84
- throw new BedrockError ( `Workflow "${ workflowName } " not found.` , {
80
+ const definition = DEFINITIONS_BY_TYPE_MAP . get ( type ) ;
81
+ if ( ! definition ) {
82
+ throw new BedrockError ( `Interaction type "${ type } " not found.` , {
85
83
name : 'NotFoundError' ,
86
84
details : {
87
85
httpStatusCode : 404 ,
@@ -101,18 +99,18 @@ bedrock.events.on('bedrock-express.configure.routes', app => {
101
99
accountId
102
100
}
103
101
} ;
104
- const capability = workflow . zcaps . get ( 'readWriteExchanges' ) ;
102
+ const capability = definition . zcaps . get ( 'readWriteExchanges' ) ;
105
103
const response = await zcapClient . write ( { json : exchange , capability} ) ;
106
104
const exchangeId = response . headers . get ( 'location' ) ;
107
- const { localInteractionId} = workflow ;
105
+ const { localInteractionId} = definition ;
108
106
// reuse `localExchangeId` in path
109
107
const localExchangeId = exchangeId . slice ( exchangeId . lastIndexOf ( '/' ) ) ;
110
108
const id = `${ config . server . baseUri } /${ routes . interactions } /` +
111
109
`${ localInteractionId } /${ localExchangeId } ` ;
112
110
res . json ( { interactionId : id , exchangeId} ) ;
113
111
} ) ) ;
114
112
115
- // gets an interaction by its "id"
113
+ // gets an interaction
116
114
app . get (
117
115
routes . interaction ,
118
116
ensureAuthenticated ,
@@ -124,20 +122,18 @@ bedrock.events.on('bedrock-express.configure.routes', app => {
124
122
query : { iuv}
125
123
} = req ;
126
124
127
- const workflow = WORKFLOWS_BY_ID_MAP . get ( localInteractionId ) ;
128
- if ( ! workflow ) {
125
+ // get interaction definition
126
+ const definition = DEFINITIONS_BY_ID_MAP . get ( localInteractionId ) ;
127
+ if ( ! definition ) {
129
128
throw new BedrockError (
130
- `Workflow "${ localInteractionId } " not found.` , {
129
+ `Interaction type for "${ localInteractionId } " not found.` , {
131
130
name : 'NotFoundError' ,
132
- details : {
133
- httpStatusCode : 404 ,
134
- public : true
135
- }
131
+ details : { httpStatusCode : 404 , public : true }
136
132
} ) ;
137
133
}
138
134
139
135
// determine full exchange ID based on related capability
140
- const capability = workflow . zcaps . get ( 'readWriteExchanges' ) ;
136
+ const capability = definition . zcaps . get ( 'readWriteExchanges' ) ;
141
137
const exchangeId = `${ capability . invocationTarget } /${ localExchangeId } ` ;
142
138
143
139
// if an "Interaction URL Version" is present send "protocols"
@@ -172,10 +168,10 @@ bedrock.events.on('bedrock-express.configure.routes', app => {
172
168
filterExchange ( { exchange/*, previousPollResult*/ } ) {
173
169
// ensure `accountId` matches exchange variables
174
170
if ( exchange ?. variables . accountId !== accountId ) {
175
- throw new BedrockError (
176
- 'Not authorized. ',
177
- 'NotAllowedError' ,
178
- { httpStatusCode : 403 , public : true } ) ;
171
+ throw new BedrockError ( 'Not authorized.' , {
172
+ name : 'NotAllowedError ',
173
+ details : { httpStatusCode : 403 , public : true }
174
+ } ) ;
179
175
}
180
176
// return only information that should be accessible to client
181
177
return {
0 commit comments