@botonic/core 1.0.0-dev.0 → 1.0.0-dev.2
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/lib/cjs/constants.d.ts +4 -15
- package/lib/cjs/constants.js +5 -16
- package/lib/cjs/constants.js.map +1 -1
- package/lib/cjs/core-bot.d.ts +12 -9
- package/lib/cjs/core-bot.js +54 -43
- package/lib/cjs/core-bot.js.map +1 -1
- package/lib/cjs/debug/index.d.ts +1 -1
- package/lib/cjs/debug/index.js +2 -6
- package/lib/cjs/debug/index.js.map +1 -1
- package/lib/cjs/debug/inspector.d.ts +1 -1
- package/lib/cjs/handoff.d.ts +5 -5
- package/lib/cjs/handoff.js +13 -10
- package/lib/cjs/handoff.js.map +1 -1
- package/lib/cjs/hubtype-service.d.ts +4 -4
- package/lib/cjs/hubtype-service.js +1 -1
- package/lib/cjs/hubtype-service.js.map +1 -1
- package/lib/cjs/i18n.d.ts +1 -1
- package/lib/cjs/index.d.ts +8 -163
- package/lib/cjs/index.js +8 -51
- package/lib/cjs/index.js.map +1 -1
- package/lib/cjs/models/bot-state.d.ts +11 -0
- package/lib/cjs/models/bot-state.js +3 -0
- package/lib/cjs/models/bot-state.js.map +1 -0
- package/lib/cjs/models/channels.d.ts +9 -0
- package/lib/cjs/models/channels.js +14 -0
- package/lib/cjs/models/channels.js.map +1 -0
- package/lib/cjs/models/events/base-event.d.ts +20 -0
- package/lib/cjs/models/events/base-event.js +17 -0
- package/lib/cjs/models/events/base-event.js.map +1 -0
- package/lib/cjs/models/events/botonic-event.d.ts +4 -0
- package/lib/cjs/models/events/botonic-event.js +3 -0
- package/lib/cjs/models/events/botonic-event.js.map +1 -0
- package/lib/{esm/models/events/connections/index.d.ts → cjs/models/events/connection/connection-event.d.ts} +1 -1
- package/lib/cjs/models/events/{connections/index.js → connection/connection-event.js} +1 -1
- package/lib/cjs/models/events/connection/connection-event.js.map +1 -0
- package/lib/cjs/models/events/connection/index.d.ts +1 -0
- package/lib/cjs/models/events/connection/index.js +5 -0
- package/lib/cjs/models/events/connection/index.js.map +1 -0
- package/lib/cjs/models/events/index.d.ts +4 -21
- package/lib/cjs/models/events/index.js +5 -8
- package/lib/cjs/models/events/index.js.map +1 -1
- package/lib/cjs/models/events/integration/index.d.ts +1 -0
- package/lib/cjs/models/events/integration/index.js +5 -0
- package/lib/cjs/models/events/integration/index.js.map +1 -0
- package/lib/cjs/models/events/integration/integration-event.d.ts +6 -0
- package/lib/cjs/models/events/integration/integration-event.js +3 -0
- package/lib/cjs/models/events/integration/integration-event.js.map +1 -0
- package/lib/cjs/models/events/message/carousel.d.ts +1 -1
- package/lib/cjs/models/events/message/custom.d.ts +2 -2
- package/lib/cjs/models/events/message/index.d.ts +9 -30
- package/lib/cjs/models/events/message/index.js +10 -29
- package/lib/cjs/models/events/message/index.js.map +1 -1
- package/lib/cjs/models/events/message/location.d.ts +1 -1
- package/lib/cjs/models/events/message/media.d.ts +1 -1
- package/lib/cjs/models/events/message/media.js +5 -5
- package/lib/cjs/models/events/message/media.js.map +1 -1
- package/lib/cjs/models/events/message/message-event.d.ts +30 -0
- package/lib/cjs/models/events/message/message-event.js +32 -0
- package/lib/cjs/models/events/message/message-event.js.map +1 -0
- package/lib/cjs/models/events/message/postback.d.ts +1 -1
- package/lib/cjs/models/events/message/text.d.ts +1 -1
- package/lib/cjs/models/index.d.ts +6 -0
- package/lib/cjs/models/index.js +10 -0
- package/lib/cjs/models/index.js.map +1 -0
- package/lib/cjs/models/legacy-types.d.ts +184 -0
- package/lib/cjs/models/legacy-types.js +59 -0
- package/lib/cjs/models/legacy-types.js.map +1 -0
- package/lib/cjs/models/session.d.ts +3 -0
- package/lib/cjs/models/session.js +3 -0
- package/lib/cjs/models/session.js.map +1 -0
- package/lib/cjs/models/user.d.ts +8 -4
- package/lib/cjs/output-parser/botonic-output-parser.d.ts +16 -0
- package/lib/cjs/output-parser/botonic-output-parser.js +37 -0
- package/lib/cjs/output-parser/botonic-output-parser.js.map +1 -0
- package/lib/cjs/output-parser/factory.d.ts +1 -1
- package/lib/cjs/output-parser/factory.js +7 -8
- package/lib/cjs/output-parser/factory.js.map +1 -1
- package/lib/cjs/output-parser/index.d.ts +3 -16
- package/lib/cjs/output-parser/index.js +4 -34
- package/lib/cjs/output-parser/index.js.map +1 -1
- package/lib/cjs/output-parser/parsers.d.ts +3 -9
- package/lib/cjs/output-parser/parsers.js +21 -12
- package/lib/cjs/output-parser/parsers.js.map +1 -1
- package/lib/cjs/plugins.d.ts +2 -3
- package/lib/cjs/plugins.js +9 -6
- package/lib/cjs/plugins.js.map +1 -1
- package/lib/cjs/routing/index.d.ts +2 -0
- package/lib/cjs/routing/index.js +6 -0
- package/lib/cjs/routing/index.js.map +1 -0
- package/lib/cjs/routing/router-utils.d.ts +11 -0
- package/lib/cjs/routing/router-utils.js +89 -0
- package/lib/cjs/routing/router-utils.js.map +1 -0
- package/lib/cjs/routing/router.d.ts +54 -0
- package/lib/cjs/routing/router.js +319 -0
- package/lib/cjs/routing/router.js.map +1 -0
- package/lib/cjs/utils.d.ts +1 -0
- package/lib/cjs/utils.js +7 -1
- package/lib/cjs/utils.js.map +1 -1
- package/lib/esm/constants.d.ts +4 -15
- package/lib/esm/constants.js +4 -15
- package/lib/esm/constants.js.map +1 -1
- package/lib/esm/core-bot.d.ts +12 -9
- package/lib/esm/core-bot.js +52 -41
- package/lib/esm/core-bot.js.map +1 -1
- package/lib/esm/debug/index.d.ts +1 -1
- package/lib/esm/debug/index.js +1 -1
- package/lib/esm/debug/index.js.map +1 -1
- package/lib/esm/debug/inspector.d.ts +1 -1
- package/lib/esm/handoff.d.ts +5 -5
- package/lib/esm/handoff.js +13 -10
- package/lib/esm/handoff.js.map +1 -1
- package/lib/esm/hubtype-service.d.ts +4 -4
- package/lib/esm/hubtype-service.js +1 -1
- package/lib/esm/hubtype-service.js.map +1 -1
- package/lib/esm/i18n.d.ts +1 -1
- package/lib/esm/index.d.ts +8 -163
- package/lib/esm/index.js +8 -45
- package/lib/esm/index.js.map +1 -1
- package/lib/esm/models/bot-state.d.ts +11 -0
- package/lib/esm/models/bot-state.js +2 -0
- package/lib/esm/models/bot-state.js.map +1 -0
- package/lib/esm/models/channels.d.ts +9 -0
- package/lib/esm/models/channels.js +11 -0
- package/lib/esm/models/channels.js.map +1 -0
- package/lib/esm/models/events/base-event.d.ts +20 -0
- package/lib/esm/models/events/base-event.js +14 -0
- package/lib/esm/models/events/base-event.js.map +1 -0
- package/lib/esm/models/events/botonic-event.d.ts +4 -0
- package/lib/esm/models/events/botonic-event.js +2 -0
- package/lib/esm/models/events/botonic-event.js.map +1 -0
- package/lib/{cjs/models/events/connections/index.d.ts → esm/models/events/connection/connection-event.d.ts} +1 -1
- package/lib/esm/models/events/{connections/index.js → connection/connection-event.js} +1 -1
- package/lib/esm/models/events/connection/connection-event.js.map +1 -0
- package/lib/esm/models/events/connection/index.d.ts +1 -0
- package/lib/esm/models/events/connection/index.js +2 -0
- package/lib/esm/models/events/connection/index.js.map +1 -0
- package/lib/esm/models/events/index.d.ts +4 -21
- package/lib/esm/models/events/index.js +4 -7
- package/lib/esm/models/events/index.js.map +1 -1
- package/lib/esm/models/events/integration/index.d.ts +1 -0
- package/lib/esm/models/events/integration/index.js +2 -0
- package/lib/esm/models/events/integration/index.js.map +1 -0
- package/lib/esm/models/events/integration/integration-event.d.ts +6 -0
- package/lib/esm/models/events/integration/integration-event.js +2 -0
- package/lib/esm/models/events/integration/integration-event.js.map +1 -0
- package/lib/esm/models/events/message/carousel.d.ts +1 -1
- package/lib/esm/models/events/message/custom.d.ts +2 -2
- package/lib/esm/models/events/message/index.d.ts +9 -30
- package/lib/esm/models/events/message/index.js +9 -28
- package/lib/esm/models/events/message/index.js.map +1 -1
- package/lib/esm/models/events/message/location.d.ts +1 -1
- package/lib/esm/models/events/message/media.d.ts +1 -1
- package/lib/esm/models/events/message/media.js +1 -1
- package/lib/esm/models/events/message/media.js.map +1 -1
- package/lib/esm/models/events/message/message-event.d.ts +30 -0
- package/lib/esm/models/events/message/message-event.js +29 -0
- package/lib/esm/models/events/message/message-event.js.map +1 -0
- package/lib/esm/models/events/message/postback.d.ts +1 -1
- package/lib/esm/models/events/message/text.d.ts +1 -1
- package/lib/esm/models/index.d.ts +6 -0
- package/lib/esm/models/index.js +7 -0
- package/lib/esm/models/index.js.map +1 -0
- package/lib/esm/models/legacy-types.d.ts +184 -0
- package/lib/esm/models/legacy-types.js +56 -0
- package/lib/esm/models/legacy-types.js.map +1 -0
- package/lib/esm/models/session.d.ts +3 -0
- package/lib/esm/models/session.js +2 -0
- package/lib/esm/models/session.js.map +1 -0
- package/lib/esm/models/user.d.ts +8 -4
- package/lib/esm/output-parser/botonic-output-parser.d.ts +16 -0
- package/lib/esm/output-parser/botonic-output-parser.js +33 -0
- package/lib/esm/output-parser/botonic-output-parser.js.map +1 -0
- package/lib/esm/output-parser/factory.d.ts +1 -1
- package/lib/esm/output-parser/factory.js +1 -2
- package/lib/esm/output-parser/factory.js.map +1 -1
- package/lib/esm/output-parser/index.d.ts +3 -16
- package/lib/esm/output-parser/index.js +3 -32
- package/lib/esm/output-parser/index.js.map +1 -1
- package/lib/esm/output-parser/parsers.d.ts +3 -9
- package/lib/esm/output-parser/parsers.js +14 -7
- package/lib/esm/output-parser/parsers.js.map +1 -1
- package/lib/esm/plugins.d.ts +2 -3
- package/lib/esm/plugins.js +9 -6
- package/lib/esm/plugins.js.map +1 -1
- package/lib/esm/routing/index.d.ts +2 -0
- package/lib/esm/routing/index.js +3 -0
- package/lib/esm/routing/index.js.map +1 -0
- package/lib/esm/routing/router-utils.d.ts +11 -0
- package/lib/esm/routing/router-utils.js +79 -0
- package/lib/esm/routing/router-utils.js.map +1 -0
- package/lib/esm/routing/router.d.ts +54 -0
- package/lib/esm/routing/router.js +315 -0
- package/lib/esm/routing/router.js.map +1 -0
- package/lib/esm/utils.d.ts +1 -0
- package/lib/esm/utils.js +5 -0
- package/lib/esm/utils.js.map +1 -1
- package/package.json +6 -6
- package/src/constants.ts +6 -15
- package/src/core-bot.ts +84 -47
- package/src/debug/index.ts +1 -6
- package/src/debug/inspector.ts +1 -1
- package/src/handoff.ts +15 -12
- package/src/hubtype-service.ts +5 -5
- package/src/i18n.ts +1 -1
- package/src/index.ts +8 -230
- package/src/models/bot-state.ts +12 -0
- package/src/models/channels.ts +9 -0
- package/src/models/events/base-event.ts +22 -0
- package/src/models/events/botonic-event.ts +26 -0
- package/src/models/events/{connections/index.ts → connection/connection-event.ts} +1 -1
- package/src/models/events/connection/index.ts +1 -0
- package/src/models/events/index.ts +4 -39
- package/src/models/events/integration/index.ts +1 -0
- package/src/models/events/integration/integration-event.ts +13 -0
- package/src/models/events/message/carousel.ts +1 -1
- package/src/models/events/message/custom.ts +2 -2
- package/src/models/events/message/index.ts +9 -37
- package/src/models/events/message/location.ts +1 -1
- package/src/models/events/message/media.ts +1 -1
- package/src/models/events/message/message-event.ts +37 -0
- package/src/models/events/message/postback.ts +1 -1
- package/src/models/events/message/text.ts +1 -1
- package/src/models/index.ts +6 -0
- package/src/models/legacy-types.ts +262 -0
- package/src/models/session.ts +3 -0
- package/src/models/user.ts +10 -4
- package/src/output-parser/botonic-output-parser.ts +38 -0
- package/src/output-parser/factory.ts +1 -3
- package/src/output-parser/index.ts +3 -38
- package/src/output-parser/parsers.ts +29 -17
- package/src/plugins.ts +18 -8
- package/src/routing/index.ts +2 -0
- package/src/routing/router-utils.ts +100 -0
- package/src/routing/router.ts +415 -0
- package/src/utils.ts +6 -0
- package/lib/cjs/errors.d.ts +0 -5
- package/lib/cjs/errors.js +0 -11
- package/lib/cjs/errors.js.map +0 -1
- package/lib/cjs/models/events/connections/index.js.map +0 -1
- package/lib/cjs/output-parser/util.d.ts +0 -2
- package/lib/cjs/output-parser/util.js +0 -14
- package/lib/cjs/output-parser/util.js.map +0 -1
- package/lib/cjs/router.d.ts +0 -35
- package/lib/cjs/router.js +0 -262
- package/lib/cjs/router.js.map +0 -1
- package/lib/esm/errors.d.ts +0 -5
- package/lib/esm/errors.js +0 -7
- package/lib/esm/errors.js.map +0 -1
- package/lib/esm/models/events/connections/index.js.map +0 -1
- package/lib/esm/output-parser/util.d.ts +0 -2
- package/lib/esm/output-parser/util.js +0 -9
- package/lib/esm/output-parser/util.js.map +0 -1
- package/lib/esm/router.d.ts +0 -35
- package/lib/esm/router.js +0 -258
- package/lib/esm/router.js.map +0 -1
- package/src/errors.ts +0 -11
- package/src/output-parser/util.ts +0 -8
- package/src/router.ts +0 -316
package/src/router.ts
DELETED
|
@@ -1,316 +0,0 @@
|
|
|
1
|
-
import { RouteInspector } from './debug/inspector'
|
|
2
|
-
import { NoMatchingRouteError } from './errors'
|
|
3
|
-
import { Input, Route, Routes, Session } from './index'
|
|
4
|
-
import { isFunction } from './utils'
|
|
5
|
-
|
|
6
|
-
interface RouteParams {
|
|
7
|
-
route: Route
|
|
8
|
-
params: any
|
|
9
|
-
}
|
|
10
|
-
type MatchingProp =
|
|
11
|
-
| 'text'
|
|
12
|
-
| 'payload'
|
|
13
|
-
| 'intent'
|
|
14
|
-
| 'type'
|
|
15
|
-
| 'input'
|
|
16
|
-
| 'session'
|
|
17
|
-
| 'request'
|
|
18
|
-
|
|
19
|
-
type Matcher = string | RegExp | ((args) => boolean)
|
|
20
|
-
|
|
21
|
-
export class Router {
|
|
22
|
-
routes: Routes
|
|
23
|
-
routeInspector: RouteInspector
|
|
24
|
-
lastRoutePath: string | null
|
|
25
|
-
/**
|
|
26
|
-
* @param {Route[]} routes
|
|
27
|
-
* @param routeInspector
|
|
28
|
-
*/
|
|
29
|
-
constructor(
|
|
30
|
-
routes: Routes,
|
|
31
|
-
routeInspector: RouteInspector | undefined = undefined
|
|
32
|
-
) {
|
|
33
|
-
this.routes = routes
|
|
34
|
-
this.routeInspector = routeInspector || new RouteInspector()
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
// eslint-disable-next-line complexity
|
|
38
|
-
processInput(
|
|
39
|
-
input: Input,
|
|
40
|
-
session: Partial<Session> = {},
|
|
41
|
-
lastRoutePath: string | null = null
|
|
42
|
-
): any {
|
|
43
|
-
let routeParams: any = {}
|
|
44
|
-
if (input.payload && input.payload.includes('__PATH_PAYLOAD__')) {
|
|
45
|
-
const pathParam = input.payload.split('__PATH_PAYLOAD__')
|
|
46
|
-
routeParams.route = this.getRouteByPath(pathParam[1], this.routes)
|
|
47
|
-
}
|
|
48
|
-
const pathParams = this.getOnFinishParams(input)
|
|
49
|
-
let brokenFlow = false
|
|
50
|
-
const lastRoute = this.getRouteByPath(lastRoutePath, this.routes)
|
|
51
|
-
if (!lastRoute && input.path)
|
|
52
|
-
routeParams.route = this.getRouteByPath(input.path, this.routes)
|
|
53
|
-
if (lastRoute && lastRoute.childRoutes && !routeParams.route)
|
|
54
|
-
//get route depending of current ChildRoute
|
|
55
|
-
routeParams = this.getRoute(
|
|
56
|
-
input,
|
|
57
|
-
lastRoute.childRoutes,
|
|
58
|
-
session as Session,
|
|
59
|
-
lastRoutePath
|
|
60
|
-
)
|
|
61
|
-
if (!routeParams || !Object.keys(routeParams).length) {
|
|
62
|
-
/*
|
|
63
|
-
we couldn't find a route in the state of the lastRoute, so let's find in
|
|
64
|
-
the general conf.route
|
|
65
|
-
*/
|
|
66
|
-
brokenFlow = Boolean(lastRoutePath)
|
|
67
|
-
routeParams = this.getRoute(
|
|
68
|
-
input,
|
|
69
|
-
this.routes,
|
|
70
|
-
session as Session,
|
|
71
|
-
lastRoutePath
|
|
72
|
-
)
|
|
73
|
-
}
|
|
74
|
-
try {
|
|
75
|
-
if (pathParams) {
|
|
76
|
-
const searchParams = new URLSearchParams(pathParams)
|
|
77
|
-
for (const [key, value] of searchParams) {
|
|
78
|
-
routeParams.params
|
|
79
|
-
? (routeParams.params[key] = value)
|
|
80
|
-
: (routeParams.params = { [key]: value })
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
} catch (e) {}
|
|
84
|
-
if (routeParams && Object.keys(routeParams).length) {
|
|
85
|
-
//get in childRoute if one has path ''
|
|
86
|
-
let defaultAction
|
|
87
|
-
if (routeParams.route) {
|
|
88
|
-
if (
|
|
89
|
-
!routeParams.route.path &&
|
|
90
|
-
routeParams.route.childRoutes &&
|
|
91
|
-
routeParams.route.childRoutes.length
|
|
92
|
-
) {
|
|
93
|
-
defaultAction = this.getRoute(
|
|
94
|
-
{ path: '' },
|
|
95
|
-
routeParams.route.childRoutes,
|
|
96
|
-
session as Session,
|
|
97
|
-
lastRoutePath
|
|
98
|
-
)
|
|
99
|
-
}
|
|
100
|
-
if ('action' in routeParams.route) {
|
|
101
|
-
if (
|
|
102
|
-
brokenFlow &&
|
|
103
|
-
routeParams.route.ignoreRetry != true &&
|
|
104
|
-
lastRoute &&
|
|
105
|
-
(session.__retries || 0) < (lastRoute.retry || 0) &&
|
|
106
|
-
routeParams.route.path != lastRoute.action
|
|
107
|
-
) {
|
|
108
|
-
session.__retries = session.__retries ? session.__retries + 1 : 1
|
|
109
|
-
// The flow was broken, but we want to recover it
|
|
110
|
-
return {
|
|
111
|
-
action: routeParams.route.action,
|
|
112
|
-
params: routeParams.params,
|
|
113
|
-
retryAction: lastRoute ? lastRoute.action : null,
|
|
114
|
-
defaultAction: defaultAction ? defaultAction.route.action : null,
|
|
115
|
-
lastRoutePath: lastRoutePath,
|
|
116
|
-
}
|
|
117
|
-
} else {
|
|
118
|
-
session.__retries = 0
|
|
119
|
-
if (lastRoutePath && !brokenFlow)
|
|
120
|
-
lastRoutePath = `${lastRoutePath}/${routeParams.route.path}`
|
|
121
|
-
else lastRoutePath = routeParams.route.path
|
|
122
|
-
return {
|
|
123
|
-
action: routeParams.route.action,
|
|
124
|
-
params: routeParams.params,
|
|
125
|
-
retryAction: null,
|
|
126
|
-
defaultAction: defaultAction ? defaultAction.route.action : null,
|
|
127
|
-
lastRoutePath: lastRoutePath,
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
} else if (defaultAction) {
|
|
131
|
-
return {
|
|
132
|
-
action: defaultAction.route.action,
|
|
133
|
-
params: defaultAction.params,
|
|
134
|
-
lastRoutePath: lastRoutePath,
|
|
135
|
-
}
|
|
136
|
-
} else if ('redirect' in routeParams.route) {
|
|
137
|
-
lastRoutePath = routeParams.route.redirect
|
|
138
|
-
const redirectRoute = this.getRouteByPath(lastRoutePath, this.routes)
|
|
139
|
-
if (redirectRoute) {
|
|
140
|
-
return {
|
|
141
|
-
action: redirectRoute.action,
|
|
142
|
-
params: redirectRoute.params,
|
|
143
|
-
lastRoutePath: lastRoutePath,
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
const notFound = this.getRouteByPath('404', this.routes)
|
|
150
|
-
if (!notFound) throw new NoMatchingRouteError(input)
|
|
151
|
-
if (lastRoute && (session.__retries || 0) < (lastRoute.retry || 0)) {
|
|
152
|
-
session.__retries = session.__retries ? session.__retries + 1 : 1
|
|
153
|
-
return {
|
|
154
|
-
action: notFound.action,
|
|
155
|
-
params: {},
|
|
156
|
-
retryAction: lastRoute.action,
|
|
157
|
-
lastRoutePath: lastRoutePath,
|
|
158
|
-
}
|
|
159
|
-
} else {
|
|
160
|
-
this.lastRoutePath = null
|
|
161
|
-
session.__retries = 0
|
|
162
|
-
return {
|
|
163
|
-
action: notFound.action,
|
|
164
|
-
params: {},
|
|
165
|
-
retryAction: null,
|
|
166
|
-
lastRoutePath: lastRoutePath,
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
getOnFinishParams(input: Input): string | undefined {
|
|
172
|
-
try {
|
|
173
|
-
if (!input.payload) {
|
|
174
|
-
return undefined
|
|
175
|
-
}
|
|
176
|
-
const params = input.payload.split('__PATH_PAYLOAD__')
|
|
177
|
-
if (params.length < 2) {
|
|
178
|
-
return undefined
|
|
179
|
-
}
|
|
180
|
-
const pathParams = params[1].split('?')
|
|
181
|
-
if (pathParams.length > 0) {
|
|
182
|
-
input.path = pathParams[0]
|
|
183
|
-
delete input.payload
|
|
184
|
-
}
|
|
185
|
-
if (pathParams.length > 1) {
|
|
186
|
-
return pathParams[1]
|
|
187
|
-
}
|
|
188
|
-
} catch (e) {
|
|
189
|
-
console.error('ERROR getOnFinishParams', e)
|
|
190
|
-
}
|
|
191
|
-
return undefined
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
/**
|
|
195
|
-
* @return {null|RouteParams}
|
|
196
|
-
*/
|
|
197
|
-
getRoute(
|
|
198
|
-
input: Input | Partial<Input>,
|
|
199
|
-
routes: Routes,
|
|
200
|
-
session: Session,
|
|
201
|
-
lastRoutePath: string | null
|
|
202
|
-
): RouteParams | null {
|
|
203
|
-
const computedRoutes = isFunction(routes)
|
|
204
|
-
? // @ts-ignore
|
|
205
|
-
routes({ input, session, lastRoutePath })
|
|
206
|
-
: routes
|
|
207
|
-
/* Find the route that matches the given input, if it match with some of the entries,
|
|
208
|
-
return the whole Route of the entry with optional params captured if matcher was a regex */
|
|
209
|
-
/** @type {{ [key: string]: string }}*/
|
|
210
|
-
let params = {}
|
|
211
|
-
const route = computedRoutes.find(r =>
|
|
212
|
-
Object.entries(r)
|
|
213
|
-
.filter(([key, _]) => key != 'action' && key != 'childRoutes')
|
|
214
|
-
.some(([key, value]) => {
|
|
215
|
-
const match = this.matchRoute(
|
|
216
|
-
r,
|
|
217
|
-
key as MatchingProp,
|
|
218
|
-
value as Matcher,
|
|
219
|
-
input as Input,
|
|
220
|
-
session,
|
|
221
|
-
lastRoutePath
|
|
222
|
-
)
|
|
223
|
-
try {
|
|
224
|
-
params = match.groups
|
|
225
|
-
} catch (e) {}
|
|
226
|
-
return Boolean(match)
|
|
227
|
-
})
|
|
228
|
-
)
|
|
229
|
-
if (route) {
|
|
230
|
-
return { route, params }
|
|
231
|
-
}
|
|
232
|
-
return null
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
getRouteByPath(
|
|
236
|
-
path: string | null,
|
|
237
|
-
routeList: Routes | null = null
|
|
238
|
-
): Route | null {
|
|
239
|
-
if (!path) return null
|
|
240
|
-
let route: Route | null = null
|
|
241
|
-
routeList = routeList || this.routes
|
|
242
|
-
const [currentPath, ...childPath] = path.split('/')
|
|
243
|
-
if (Array.isArray(routeList)) {
|
|
244
|
-
for (const r of routeList) {
|
|
245
|
-
//iterate over all routeList
|
|
246
|
-
if (r.path == currentPath) {
|
|
247
|
-
route = r
|
|
248
|
-
if (r.childRoutes && r.childRoutes.length && childPath.length > 0) {
|
|
249
|
-
//evaluate childroute over next actions
|
|
250
|
-
route = this.getRouteByPath(childPath.join('/'), r.childRoutes)
|
|
251
|
-
if (route) return route
|
|
252
|
-
} else if (childPath.length === 0) return route //last action and found route
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
return null
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
/**
|
|
260
|
-
* @return {Params|boolean}
|
|
261
|
-
*/
|
|
262
|
-
matchRoute(
|
|
263
|
-
route: Route,
|
|
264
|
-
prop: MatchingProp,
|
|
265
|
-
matcher: Matcher,
|
|
266
|
-
input: Input,
|
|
267
|
-
session: Session,
|
|
268
|
-
lastRoutePath: string | null
|
|
269
|
-
): any {
|
|
270
|
-
/*
|
|
271
|
-
prop: ('text' | 'payload' | 'intent' | 'type' | 'input' | 'session' | 'request' ...)
|
|
272
|
-
matcher: (string: exact match | regex: regular expression match | function: return true)
|
|
273
|
-
input: user input object, ex: {type: 'text', data: 'Hi'}
|
|
274
|
-
*/
|
|
275
|
-
/** @type {any} */
|
|
276
|
-
let value: any = ''
|
|
277
|
-
if (Object.keys(input).indexOf(prop) > -1) value = input[prop]
|
|
278
|
-
if (prop === 'text') {
|
|
279
|
-
if (input.type === 'text') value = input.data
|
|
280
|
-
} else if (prop === 'input') value = input
|
|
281
|
-
else if (prop === 'session') value = session
|
|
282
|
-
else if (prop === 'request') value = { input, session, lastRoutePath }
|
|
283
|
-
const matched = this.matchValue(matcher, value)
|
|
284
|
-
if (matched) {
|
|
285
|
-
this.routeInspector.routeMatched(route, prop, matcher, value)
|
|
286
|
-
} else {
|
|
287
|
-
this.routeInspector.routeNotMatched(route, prop, matcher, value)
|
|
288
|
-
}
|
|
289
|
-
return matched
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
/**
|
|
293
|
-
*
|
|
294
|
-
* @return {*|boolean|Params}
|
|
295
|
-
*/
|
|
296
|
-
matchValue(
|
|
297
|
-
matcher: string | RegExp | ((args) => boolean),
|
|
298
|
-
value: any
|
|
299
|
-
): boolean {
|
|
300
|
-
if (typeof matcher === 'string') {
|
|
301
|
-
// TODO should this be === to avoid matching '' with undefined?
|
|
302
|
-
return value == matcher
|
|
303
|
-
}
|
|
304
|
-
if (matcher instanceof RegExp) {
|
|
305
|
-
// check if undefined to avoid conversion to 'undefined'
|
|
306
|
-
if (value === undefined) {
|
|
307
|
-
return false
|
|
308
|
-
}
|
|
309
|
-
return Boolean(matcher.exec(value))
|
|
310
|
-
}
|
|
311
|
-
if (typeof matcher === 'function') {
|
|
312
|
-
return matcher(value)
|
|
313
|
-
}
|
|
314
|
-
return false
|
|
315
|
-
}
|
|
316
|
-
}
|