@owlmeans/flow 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +514 -0
- package/build/.gitkeep +0 -0
- package/build/consts.d.ts +10 -0
- package/build/consts.d.ts.map +1 -0
- package/build/consts.js +10 -0
- package/build/consts.js.map +1 -0
- package/build/errors.d.ts +34 -0
- package/build/errors.d.ts.map +1 -0
- package/build/errors.js +65 -0
- package/build/errors.js.map +1 -0
- package/build/flows/auth.d.ts +19 -0
- package/build/flows/auth.d.ts.map +1 -0
- package/build/flows/auth.js +146 -0
- package/build/flows/auth.js.map +1 -0
- package/build/flows/index.d.ts +5 -0
- package/build/flows/index.d.ts.map +1 -0
- package/build/flows/index.js +5 -0
- package/build/flows/index.js.map +1 -0
- package/build/flows/oidc.d.ts +14 -0
- package/build/flows/oidc.d.ts.map +1 -0
- package/build/flows/oidc.js +136 -0
- package/build/flows/oidc.js.map +1 -0
- package/build/flows/oidp.d.ts +13 -0
- package/build/flows/oidp.d.ts.map +1 -0
- package/build/flows/oidp.js +189 -0
- package/build/flows/oidp.js.map +1 -0
- package/build/flows/stab.d.ts +3 -0
- package/build/flows/stab.d.ts.map +1 -0
- package/build/flows/stab.js +16 -0
- package/build/flows/stab.js.map +1 -0
- package/build/helper.d.ts +5 -0
- package/build/helper.d.ts.map +1 -0
- package/build/helper.js +14 -0
- package/build/helper.js.map +1 -0
- package/build/i18n/en.json +8 -0
- package/build/i18n.d.ts +2 -0
- package/build/i18n.d.ts.map +1 -0
- package/build/i18n.js +4 -0
- package/build/i18n.js.map +1 -0
- package/build/index.d.ts +8 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js +7 -0
- package/build/index.js.map +1 -0
- package/build/state.d.ts +3 -0
- package/build/state.d.ts.map +1 -0
- package/build/state.js +118 -0
- package/build/state.js.map +1 -0
- package/build/types.d.ts +100 -0
- package/build/types.d.ts.map +1 -0
- package/build/types.js +2 -0
- package/build/types.js.map +1 -0
- package/build/utils/flow.d.ts +4 -0
- package/build/utils/flow.d.ts.map +1 -0
- package/build/utils/flow.js +65 -0
- package/build/utils/flow.js.map +1 -0
- package/build/utils/index.d.ts +2 -0
- package/build/utils/index.d.ts.map +1 -0
- package/build/utils/index.js +2 -0
- package/build/utils/index.js.map +1 -0
- package/package.json +39 -0
- package/src/consts.ts +18 -0
- package/src/errors.ts +82 -0
- package/src/flows/auth.ts +157 -0
- package/src/flows/index.ts +5 -0
- package/src/flows/oidc.ts +148 -0
- package/src/flows/oidp.ts +205 -0
- package/src/flows/stab.ts +17 -0
- package/src/helper.ts +19 -0
- package/src/i18n/en.json +8 -0
- package/src/i18n.ts +5 -0
- package/src/index.ts +8 -0
- package/src/state.ts +138 -0
- package/src/types.ts +103 -0
- package/src/utils/flow.ts +74 -0
- package/src/utils/index.ts +2 -0
- package/tsconfig.json +15 -0
- package/tsconfig.tsbuildinfo +1 -0
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import type { ShallowFlow } from '../types.js'
|
|
2
|
+
import { STD_OIDC_FLOW, TARGET_SERVICE } from '../consts.js'
|
|
3
|
+
import { AUTH_QUERY } from '@owlmeans/auth'
|
|
4
|
+
|
|
5
|
+
export enum OidcAuthStep {
|
|
6
|
+
Dispatch = 'dispatch',
|
|
7
|
+
Authen = 'authen',
|
|
8
|
+
PostAuthen = 'post-authen',
|
|
9
|
+
OrgChoice = 'org-choice',
|
|
10
|
+
Payment = 'payment',
|
|
11
|
+
Success = 'success',
|
|
12
|
+
Target = 'target',
|
|
13
|
+
Ephemeral = 'ephemeral'
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export const PURPOSE_PAYLOAD = 'purpose'
|
|
17
|
+
|
|
18
|
+
// OwlMeans OIDC Based Auth Flow
|
|
19
|
+
export const stdOidcFlow: ShallowFlow = {
|
|
20
|
+
// naming - preserve space in serialized state
|
|
21
|
+
flow: STD_OIDC_FLOW,
|
|
22
|
+
initialStep: OidcAuthStep.Dispatch,
|
|
23
|
+
|
|
24
|
+
steps: {
|
|
25
|
+
|
|
26
|
+
[OidcAuthStep.Dispatch]: {
|
|
27
|
+
index: 0,
|
|
28
|
+
step: OidcAuthStep.Dispatch,
|
|
29
|
+
service: '$auth',
|
|
30
|
+
initial: true,
|
|
31
|
+
module: '$auth.choice',
|
|
32
|
+
transitions: {
|
|
33
|
+
[OidcAuthStep.Authen]: {
|
|
34
|
+
transition: OidcAuthStep.Authen,
|
|
35
|
+
step: OidcAuthStep.Authen,
|
|
36
|
+
},
|
|
37
|
+
[OidcAuthStep.Ephemeral]: {
|
|
38
|
+
transition: OidcAuthStep.Ephemeral,
|
|
39
|
+
step: OidcAuthStep.Ephemeral,
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
|
|
44
|
+
[OidcAuthStep.Authen]: {
|
|
45
|
+
index: 1,
|
|
46
|
+
step: OidcAuthStep.Authen,
|
|
47
|
+
service: '$iam',
|
|
48
|
+
module: '$iam.authen',
|
|
49
|
+
transitions: {
|
|
50
|
+
[OidcAuthStep.PostAuthen]: {
|
|
51
|
+
transition: OidcAuthStep.PostAuthen,
|
|
52
|
+
step: OidcAuthStep.PostAuthen,
|
|
53
|
+
},
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
|
|
57
|
+
[OidcAuthStep.PostAuthen]: {
|
|
58
|
+
index: 2,
|
|
59
|
+
step: OidcAuthStep.PostAuthen,
|
|
60
|
+
service: '$auth',
|
|
61
|
+
module: '$auth.flow-param',
|
|
62
|
+
transitions: {
|
|
63
|
+
[OidcAuthStep.OrgChoice]: {
|
|
64
|
+
transition: OidcAuthStep.OrgChoice,
|
|
65
|
+
step: OidcAuthStep.OrgChoice,
|
|
66
|
+
},
|
|
67
|
+
[OidcAuthStep.Payment]: {
|
|
68
|
+
transition: OidcAuthStep.Payment,
|
|
69
|
+
step: OidcAuthStep.Payment,
|
|
70
|
+
},
|
|
71
|
+
[OidcAuthStep.Target]: {
|
|
72
|
+
transition: OidcAuthStep.Target,
|
|
73
|
+
step: OidcAuthStep.Target,
|
|
74
|
+
},
|
|
75
|
+
},
|
|
76
|
+
payloadMap: { [PURPOSE_PAYLOAD]: 0 }
|
|
77
|
+
},
|
|
78
|
+
|
|
79
|
+
[OidcAuthStep.OrgChoice]: {
|
|
80
|
+
index: 3,
|
|
81
|
+
step: OidcAuthStep.OrgChoice,
|
|
82
|
+
service: '$auth',
|
|
83
|
+
module: '$auth.flow',
|
|
84
|
+
transitions: {
|
|
85
|
+
[OidcAuthStep.Payment]: {
|
|
86
|
+
transition: OidcAuthStep.Payment,
|
|
87
|
+
step: OidcAuthStep.Payment,
|
|
88
|
+
},
|
|
89
|
+
[OidcAuthStep.Target]: {
|
|
90
|
+
transition: OidcAuthStep.Target,
|
|
91
|
+
step: OidcAuthStep.Target,
|
|
92
|
+
},
|
|
93
|
+
},
|
|
94
|
+
payloadMap: {
|
|
95
|
+
[AUTH_QUERY]: 0,
|
|
96
|
+
[PURPOSE_PAYLOAD]: 1,
|
|
97
|
+
}
|
|
98
|
+
},
|
|
99
|
+
|
|
100
|
+
[OidcAuthStep.Payment]: {
|
|
101
|
+
index: 4,
|
|
102
|
+
step: OidcAuthStep.Payment,
|
|
103
|
+
service: '$payment',
|
|
104
|
+
module: '$payment.plan',
|
|
105
|
+
transitions: {
|
|
106
|
+
[OidcAuthStep.Success]: {
|
|
107
|
+
transition: OidcAuthStep.Success,
|
|
108
|
+
step: OidcAuthStep.Success,
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
},
|
|
112
|
+
|
|
113
|
+
[OidcAuthStep.Success]: {
|
|
114
|
+
index: 5,
|
|
115
|
+
step: OidcAuthStep.Success,
|
|
116
|
+
service: '$payment',
|
|
117
|
+
module: '$payment.success',
|
|
118
|
+
transitions: {
|
|
119
|
+
[OidcAuthStep.Target]: {
|
|
120
|
+
transition: OidcAuthStep.Target,
|
|
121
|
+
step: OidcAuthStep.Target,
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
},
|
|
125
|
+
|
|
126
|
+
[OidcAuthStep.Target]: {
|
|
127
|
+
index: 6,
|
|
128
|
+
step: OidcAuthStep.Target,
|
|
129
|
+
service: '$auth',
|
|
130
|
+
module: '$auth.flow',
|
|
131
|
+
transitions: {
|
|
132
|
+
[OidcAuthStep.Ephemeral]: {
|
|
133
|
+
transition: OidcAuthStep.Ephemeral,
|
|
134
|
+
step: OidcAuthStep.Ephemeral,
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
},
|
|
138
|
+
|
|
139
|
+
[OidcAuthStep.Ephemeral]: {
|
|
140
|
+
index: 7,
|
|
141
|
+
step: OidcAuthStep.Ephemeral,
|
|
142
|
+
service: TARGET_SERVICE,
|
|
143
|
+
module: '$dispatcher',
|
|
144
|
+
transitions: {}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
}
|
|
148
|
+
}
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
|
|
2
|
+
import type { ShallowFlow } from '../types.js'
|
|
3
|
+
import { STD_OIDP_FLOW, TARGET_SERVICE } from '../consts.js'
|
|
4
|
+
|
|
5
|
+
export enum OidpAuthStep {
|
|
6
|
+
Unknown = 'unknown',
|
|
7
|
+
|
|
8
|
+
CreateId = 'create-id',
|
|
9
|
+
|
|
10
|
+
Auth = 'auth',
|
|
11
|
+
PostAuth = 'post-auth',
|
|
12
|
+
LinkId = 'link-id',
|
|
13
|
+
Denied = 'denied',
|
|
14
|
+
Dispatch = 'dispatch',
|
|
15
|
+
|
|
16
|
+
Target = 'target',
|
|
17
|
+
|
|
18
|
+
// Compatibility
|
|
19
|
+
// MasterAuth = 'authentication',
|
|
20
|
+
// MasterReg = 'registration',
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export const stdOidpFlow: ShallowFlow = {
|
|
24
|
+
flow: STD_OIDP_FLOW,
|
|
25
|
+
initialStep: OidpAuthStep.Unknown,
|
|
26
|
+
|
|
27
|
+
steps: {
|
|
28
|
+
[OidpAuthStep.Unknown]: {
|
|
29
|
+
index: 0,
|
|
30
|
+
step: OidpAuthStep.Unknown,
|
|
31
|
+
service: '$auth',
|
|
32
|
+
module: '$auth.interaction',
|
|
33
|
+
initial: true,
|
|
34
|
+
transitions: {
|
|
35
|
+
[OidpAuthStep.Dispatch]: {
|
|
36
|
+
transition: OidpAuthStep.Dispatch,
|
|
37
|
+
step: OidpAuthStep.Dispatch,
|
|
38
|
+
},
|
|
39
|
+
[OidpAuthStep.Auth]: {
|
|
40
|
+
transition: OidpAuthStep.Auth,
|
|
41
|
+
step: OidpAuthStep.Auth,
|
|
42
|
+
},
|
|
43
|
+
[OidpAuthStep.LinkId]: {
|
|
44
|
+
transition: OidpAuthStep.LinkId,
|
|
45
|
+
step: OidpAuthStep.LinkId,
|
|
46
|
+
},
|
|
47
|
+
[OidpAuthStep.Denied]: {
|
|
48
|
+
transition: OidpAuthStep.LinkId,
|
|
49
|
+
step: OidpAuthStep.LinkId,
|
|
50
|
+
},
|
|
51
|
+
[OidpAuthStep.CreateId]: {
|
|
52
|
+
transition: OidpAuthStep.CreateId,
|
|
53
|
+
step: OidpAuthStep.CreateId,
|
|
54
|
+
},
|
|
55
|
+
},
|
|
56
|
+
payloadMap: {
|
|
57
|
+
["uid"]: 0
|
|
58
|
+
}
|
|
59
|
+
},
|
|
60
|
+
|
|
61
|
+
[OidpAuthStep.CreateId]: {
|
|
62
|
+
index: 1,
|
|
63
|
+
step: OidpAuthStep.CreateId,
|
|
64
|
+
service: '$auth',
|
|
65
|
+
module: '$auth.interaction',
|
|
66
|
+
initial: true,
|
|
67
|
+
transitions: {
|
|
68
|
+
[OidpAuthStep.Dispatch]: {
|
|
69
|
+
transition: OidpAuthStep.Dispatch,
|
|
70
|
+
step: OidpAuthStep.Dispatch,
|
|
71
|
+
},
|
|
72
|
+
// [OidpAuthStep.MasterReg]: {
|
|
73
|
+
// transition: OidpAuthStep.MasterReg,
|
|
74
|
+
// step: OidpAuthStep.MasterReg,
|
|
75
|
+
// explicit: true,
|
|
76
|
+
// }
|
|
77
|
+
},
|
|
78
|
+
payloadMap: {
|
|
79
|
+
["uid"]: 0
|
|
80
|
+
}
|
|
81
|
+
},
|
|
82
|
+
|
|
83
|
+
// [OidpAuthStep.MasterReg]: {
|
|
84
|
+
// index: 2,
|
|
85
|
+
// step: OidpAuthStep.MasterReg,
|
|
86
|
+
// service: '$auth',
|
|
87
|
+
// module: '$auth.interaction',
|
|
88
|
+
// initial: true,
|
|
89
|
+
// transitions: {
|
|
90
|
+
// [OidpAuthStep.Dispatch]: {
|
|
91
|
+
// transition: OidpAuthStep.Dispatch,
|
|
92
|
+
// step: OidpAuthStep.Dispatch,
|
|
93
|
+
// },
|
|
94
|
+
// [OidpAuthStep.MasterAuth]: {
|
|
95
|
+
// transition: OidpAuthStep.MasterAuth,
|
|
96
|
+
// step: OidpAuthStep.MasterAuth,
|
|
97
|
+
// explicit: true,
|
|
98
|
+
// }
|
|
99
|
+
// },
|
|
100
|
+
// payloadMap: {
|
|
101
|
+
// ["uid"]: 0
|
|
102
|
+
// }
|
|
103
|
+
// },
|
|
104
|
+
|
|
105
|
+
[OidpAuthStep.Auth]: {
|
|
106
|
+
index: 2,
|
|
107
|
+
step: OidpAuthStep.Auth,
|
|
108
|
+
service: '$auth',
|
|
109
|
+
module: '$auth.interaction',
|
|
110
|
+
transitions: {
|
|
111
|
+
// In case of owlmeans.net classic idp chaining
|
|
112
|
+
[OidpAuthStep.PostAuth]: {
|
|
113
|
+
transition: OidpAuthStep.PostAuth,
|
|
114
|
+
step: OidpAuthStep.PostAuth,
|
|
115
|
+
},
|
|
116
|
+
// In case of successfull authentication
|
|
117
|
+
[OidpAuthStep.Dispatch]: {
|
|
118
|
+
transition: OidpAuthStep.Dispatch,
|
|
119
|
+
step: OidpAuthStep.Dispatch,
|
|
120
|
+
},
|
|
121
|
+
// In case new account is created an resitration denied
|
|
122
|
+
[OidpAuthStep.Denied]: {
|
|
123
|
+
transition: OidpAuthStep.Denied,
|
|
124
|
+
step: OidpAuthStep.Denied,
|
|
125
|
+
},
|
|
126
|
+
}
|
|
127
|
+
},
|
|
128
|
+
|
|
129
|
+
[OidpAuthStep.PostAuth]: {
|
|
130
|
+
index: 3,
|
|
131
|
+
step: OidpAuthStep.PostAuth,
|
|
132
|
+
service: '$auth',
|
|
133
|
+
module: '$auth.int-with-flow',
|
|
134
|
+
transitions: {
|
|
135
|
+
[OidpAuthStep.Dispatch]: {
|
|
136
|
+
transition: OidpAuthStep.Dispatch,
|
|
137
|
+
step: OidpAuthStep.Dispatch,
|
|
138
|
+
},
|
|
139
|
+
[OidpAuthStep.LinkId]: {
|
|
140
|
+
transition: OidpAuthStep.LinkId,
|
|
141
|
+
step: OidpAuthStep.LinkId,
|
|
142
|
+
},
|
|
143
|
+
[OidpAuthStep.Denied]: {
|
|
144
|
+
transition: OidpAuthStep.Denied,
|
|
145
|
+
step: OidpAuthStep.Denied,
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
},
|
|
149
|
+
|
|
150
|
+
[OidpAuthStep.LinkId]: {
|
|
151
|
+
index: 4,
|
|
152
|
+
step: OidpAuthStep.LinkId,
|
|
153
|
+
service: '$auth',
|
|
154
|
+
module: '$auth.interaction',
|
|
155
|
+
transitions: {
|
|
156
|
+
[OidpAuthStep.Dispatch]: {
|
|
157
|
+
transition: OidpAuthStep.Dispatch,
|
|
158
|
+
step: OidpAuthStep.Dispatch,
|
|
159
|
+
},
|
|
160
|
+
}
|
|
161
|
+
},
|
|
162
|
+
|
|
163
|
+
[OidpAuthStep.Denied]: {
|
|
164
|
+
index: 5,
|
|
165
|
+
step: OidpAuthStep.Denied,
|
|
166
|
+
service: '$auth',
|
|
167
|
+
module: '$auth.interaction',
|
|
168
|
+
transitions: {
|
|
169
|
+
}
|
|
170
|
+
},
|
|
171
|
+
|
|
172
|
+
[OidpAuthStep.Dispatch]: {
|
|
173
|
+
index: 6,
|
|
174
|
+
step: OidpAuthStep.Dispatch,
|
|
175
|
+
service: '$auth',
|
|
176
|
+
module: '$auth.interaction',
|
|
177
|
+
transitions: {
|
|
178
|
+
[OidpAuthStep.Target]: {
|
|
179
|
+
transition: OidpAuthStep.Target,
|
|
180
|
+
step: OidpAuthStep.Target,
|
|
181
|
+
},
|
|
182
|
+
// It's unclear if these transitions are really required
|
|
183
|
+
[OidpAuthStep.LinkId]: {
|
|
184
|
+
transition: OidpAuthStep.LinkId,
|
|
185
|
+
step: OidpAuthStep.LinkId,
|
|
186
|
+
},
|
|
187
|
+
[OidpAuthStep.Denied]: {
|
|
188
|
+
transition: OidpAuthStep.Denied,
|
|
189
|
+
step: OidpAuthStep.Denied,
|
|
190
|
+
}
|
|
191
|
+
},
|
|
192
|
+
payloadMap: {
|
|
193
|
+
["uid"]: 0
|
|
194
|
+
}
|
|
195
|
+
},
|
|
196
|
+
|
|
197
|
+
[OidpAuthStep.Target]: {
|
|
198
|
+
index: 7,
|
|
199
|
+
step: OidpAuthStep.Target,
|
|
200
|
+
service: TARGET_SERVICE,
|
|
201
|
+
module: '$dispatcher',
|
|
202
|
+
transitions: {}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { ShallowFlow } from '../types.js'
|
|
2
|
+
import { STD_STAB_FLOW } from '../consts.js'
|
|
3
|
+
|
|
4
|
+
export const stdStabFlow: ShallowFlow = {
|
|
5
|
+
flow: STD_STAB_FLOW,
|
|
6
|
+
initialStep: STD_STAB_FLOW,
|
|
7
|
+
steps: {
|
|
8
|
+
[STD_STAB_FLOW]: {
|
|
9
|
+
index: 0,
|
|
10
|
+
step: STD_STAB_FLOW,
|
|
11
|
+
service: STD_STAB_FLOW,
|
|
12
|
+
module: STD_STAB_FLOW,
|
|
13
|
+
initial: true,
|
|
14
|
+
transitions: {}
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
}
|
package/src/helper.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { CommonConfig } from '@owlmeans/config'
|
|
2
|
+
import { toConfigRecord } from '@owlmeans/config'
|
|
3
|
+
import type { FlowConfig, ShallowFlow, WithFlowConfig } from './types.js'
|
|
4
|
+
import { CFG_FLOW_PREFIX, FLOW_RECORD } from './consts.js'
|
|
5
|
+
|
|
6
|
+
export const flow = <C extends CommonConfig>(cfg: C, flow: ShallowFlow): C => {
|
|
7
|
+
if (cfg.records == null) {
|
|
8
|
+
cfg.records = []
|
|
9
|
+
}
|
|
10
|
+
cfg.records.push({ ...toConfigRecord(flow), id: `${CFG_FLOW_PREFIX}:${flow.flow}`, recordType: FLOW_RECORD })
|
|
11
|
+
|
|
12
|
+
return cfg
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export const configureFlows = <C extends CommonConfig>(cfg: C & WithFlowConfig, config: FlowConfig): C & WithFlowConfig => {
|
|
16
|
+
cfg.flowConfig = config
|
|
17
|
+
|
|
18
|
+
return cfg
|
|
19
|
+
}
|
package/src/i18n/en.json
ADDED
package/src/i18n.ts
ADDED
package/src/index.ts
ADDED
package/src/state.ts
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { FlowStepError, UnknownFlow, UnknownFlowStep, UnknownTransition } from './errors.js'
|
|
2
|
+
import type { FlowModel, FlowProvider, FlowState, ShallowFlow } from './types.js'
|
|
3
|
+
import { serializeState, unserializeState } from './utils/flow.js'
|
|
4
|
+
|
|
5
|
+
export const makeFlowModel = async (flow: string | ShallowFlow, provider?: FlowProvider): Promise<FlowModel> => {
|
|
6
|
+
let state: FlowState | null = null
|
|
7
|
+
if (typeof flow === 'string') {
|
|
8
|
+
if (provider == null) {
|
|
9
|
+
throw new UnknownFlow('_provider')
|
|
10
|
+
}
|
|
11
|
+
try {
|
|
12
|
+
flow = await provider(flow)
|
|
13
|
+
state = {
|
|
14
|
+
flow: flow.flow,
|
|
15
|
+
step: flow.initialStep,
|
|
16
|
+
service: '',
|
|
17
|
+
ok: true
|
|
18
|
+
}
|
|
19
|
+
} catch {
|
|
20
|
+
state = await unserializeState(flow as string, provider)
|
|
21
|
+
flow = await provider(state.flow)
|
|
22
|
+
}
|
|
23
|
+
} else {
|
|
24
|
+
state = {
|
|
25
|
+
flow: flow.flow,
|
|
26
|
+
step: flow.initialStep,
|
|
27
|
+
service: '',
|
|
28
|
+
ok: true
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const model: FlowModel = {
|
|
33
|
+
target: service => {
|
|
34
|
+
state!.service = service
|
|
35
|
+
return model
|
|
36
|
+
},
|
|
37
|
+
|
|
38
|
+
entity: entityId => {
|
|
39
|
+
state!.entityId = entityId
|
|
40
|
+
|
|
41
|
+
return model
|
|
42
|
+
},
|
|
43
|
+
|
|
44
|
+
state: () => state!,
|
|
45
|
+
|
|
46
|
+
setState: _state => {
|
|
47
|
+
state = _state
|
|
48
|
+
return model
|
|
49
|
+
},
|
|
50
|
+
|
|
51
|
+
payload: () => state!.payload ?? {} as any,
|
|
52
|
+
|
|
53
|
+
step: step => flow.steps[step ?? state!.step],
|
|
54
|
+
|
|
55
|
+
enter: step => {
|
|
56
|
+
step = step ?? flow.initialStep
|
|
57
|
+
const info = flow.steps[step]
|
|
58
|
+
if (info.initial !== true) {
|
|
59
|
+
throw new FlowStepError(`initial:${step}`)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
state!.step = step
|
|
63
|
+
return model
|
|
64
|
+
},
|
|
65
|
+
|
|
66
|
+
steps: enter => {
|
|
67
|
+
if (enter == null) {
|
|
68
|
+
return Object.values(flow.steps)
|
|
69
|
+
}
|
|
70
|
+
if (enter) {
|
|
71
|
+
return Object.values(flow.steps).filter(s => s.initial)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return Object.values(flow.steps).filter(s => !s.initial)
|
|
75
|
+
},
|
|
76
|
+
|
|
77
|
+
transitions: explicit => {
|
|
78
|
+
if (explicit == null) {
|
|
79
|
+
return Object.values(flow.steps[state!.step].transitions)
|
|
80
|
+
}
|
|
81
|
+
if (explicit) {
|
|
82
|
+
return Object.values(flow.steps[state!.step].transitions).filter(t => t.explicit)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return Object.values(flow.steps[state!.step].transitions).filter(t => !t.explicit)
|
|
86
|
+
},
|
|
87
|
+
|
|
88
|
+
transition: transition => {
|
|
89
|
+
const t = flow.steps[state!.step].transitions[transition]
|
|
90
|
+
if (t == null) {
|
|
91
|
+
throw new UnknownTransition(transition)
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return t
|
|
95
|
+
},
|
|
96
|
+
|
|
97
|
+
next: () => {
|
|
98
|
+
const transition = model.transitions().find(t => t.explicit !== true)
|
|
99
|
+
if (transition == null) {
|
|
100
|
+
throw new UnknownFlowStep('next')
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return transition
|
|
104
|
+
},
|
|
105
|
+
|
|
106
|
+
transit: (transition, ok, message, payload) => {
|
|
107
|
+
const t = model.transition(transition)
|
|
108
|
+
state!.previous = state!.step
|
|
109
|
+
|
|
110
|
+
if (t.reversible !== true) {
|
|
111
|
+
delete state!.previous
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
state!.step = t.step
|
|
115
|
+
state!.ok = ok
|
|
116
|
+
if (typeof message !== 'string') {
|
|
117
|
+
payload = message
|
|
118
|
+
message = undefined
|
|
119
|
+
}
|
|
120
|
+
if (message != null) {
|
|
121
|
+
state!.message = message
|
|
122
|
+
} else if (state!.message != null) {
|
|
123
|
+
delete state!.message
|
|
124
|
+
}
|
|
125
|
+
if (payload != null) {
|
|
126
|
+
state!.payload = payload
|
|
127
|
+
} else if (state!.payload != null) {
|
|
128
|
+
delete state!.payload
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return serializeState(flow, state!)
|
|
132
|
+
},
|
|
133
|
+
|
|
134
|
+
serialize: () => serializeState(flow, state!)
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
return model
|
|
138
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import type { ResourceRecord } from '@owlmeans/resource'
|
|
2
|
+
import type { FLOW_RECORD } from './consts.js'
|
|
3
|
+
|
|
4
|
+
export interface Flow extends ShallowFlow {
|
|
5
|
+
config: FlowConfig
|
|
6
|
+
prefabs: { [step: string]: string }
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface ShallowFlow {
|
|
10
|
+
flow: string
|
|
11
|
+
initialStep: string
|
|
12
|
+
steps: { [key: string]: FlowStep }
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface FlowConfig {
|
|
16
|
+
queryParam?: string
|
|
17
|
+
services?: { [ref: string]: string }
|
|
18
|
+
modules?: { [ref: string]: string }
|
|
19
|
+
pathes?: { [ref: string]: string }
|
|
20
|
+
defaultFlow?: string
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface FlowStep {
|
|
24
|
+
index: number
|
|
25
|
+
step: string
|
|
26
|
+
service: string
|
|
27
|
+
path?: string
|
|
28
|
+
module?: string
|
|
29
|
+
initial?: boolean
|
|
30
|
+
payloadMap?: { [key: string]: number }
|
|
31
|
+
transitions: { [key: string]: FlowTransition }
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface FlowTransition {
|
|
35
|
+
transition: string
|
|
36
|
+
step: string
|
|
37
|
+
/**
|
|
38
|
+
* Whether user can choose this transition as one of possibilities
|
|
39
|
+
*/
|
|
40
|
+
explicit?: boolean
|
|
41
|
+
/**
|
|
42
|
+
* Whether user may get back
|
|
43
|
+
*/
|
|
44
|
+
reversible?: boolean
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface FlowState {
|
|
48
|
+
flow: string
|
|
49
|
+
step: string
|
|
50
|
+
// Previous step if reversible
|
|
51
|
+
previous?: string
|
|
52
|
+
entityId?: string | null
|
|
53
|
+
service: string
|
|
54
|
+
payload?: FlowPayload
|
|
55
|
+
message?: string
|
|
56
|
+
ok: boolean
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export interface FlowPayload extends Record<symbol | string, string | number | boolean | undefined | null> { }
|
|
60
|
+
|
|
61
|
+
export interface SerializedFlow {
|
|
62
|
+
// {flow}:{step}:{prev setp}
|
|
63
|
+
f: string
|
|
64
|
+
// Entity
|
|
65
|
+
e?: string
|
|
66
|
+
// Target service
|
|
67
|
+
s: string
|
|
68
|
+
// Payload serialized according to transition palyoadMap
|
|
69
|
+
p?: string
|
|
70
|
+
// Message
|
|
71
|
+
m?: string
|
|
72
|
+
// Is everything ok (true) or error false
|
|
73
|
+
o: number
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export interface FlowModel {
|
|
77
|
+
target: (service: string) => FlowModel
|
|
78
|
+
entity: (entityId: string) => FlowModel
|
|
79
|
+
enter: (step?: string) => FlowModel
|
|
80
|
+
steps: (enter?: boolean) => FlowStep[]
|
|
81
|
+
state: () => FlowState
|
|
82
|
+
setState: (state: FlowState | null) => FlowModel
|
|
83
|
+
payload: <T extends FlowPayload>() => T
|
|
84
|
+
step: (step?: string) => FlowStep
|
|
85
|
+
transitions: (explicit?: boolean) => FlowTransition[]
|
|
86
|
+
transition: (transition: string) => FlowTransition
|
|
87
|
+
next: () => FlowTransition
|
|
88
|
+
transit: (transition: string, ok: boolean, message?: string | FlowPayload, payload?: FlowPayload) => string
|
|
89
|
+
serialize: () => string
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export interface FlowProvider {
|
|
93
|
+
(flow: string): Promise<Flow>
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export interface WithFlowConfig {
|
|
97
|
+
flowConfig?: FlowConfig
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export interface FlowConfigRecord extends ResourceRecord, ShallowFlow {
|
|
101
|
+
id: string
|
|
102
|
+
recordType: typeof FLOW_RECORD
|
|
103
|
+
}
|