@botonic/core 1.0.0-dev.2 → 2.23.1

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.
Files changed (311) hide show
  1. package/CHANGELOG.md +226 -0
  2. package/README.md +18 -9
  3. package/package.json +19 -57
  4. package/src/index.d.ts +4 -0
  5. package/src/index.js +5 -0
  6. package/src/index.js.map +1 -0
  7. package/src/lib/core-bot/index.d.ts +33 -0
  8. package/src/lib/core-bot/index.js +113 -0
  9. package/src/lib/core-bot/index.js.map +1 -0
  10. package/src/lib/hubtype-service/handoff.d.ts +4 -0
  11. package/src/lib/hubtype-service/handoff.js +32 -0
  12. package/src/lib/hubtype-service/handoff.js.map +1 -0
  13. package/src/lib/hubtype-service/index.d.ts +10 -0
  14. package/src/lib/hubtype-service/index.js +96 -0
  15. package/src/lib/hubtype-service/index.js.map +1 -0
  16. package/src/lib/messages/button.d.ts +8 -0
  17. package/src/lib/messages/button.js +10 -0
  18. package/src/lib/messages/button.js.map +1 -0
  19. package/src/lib/plugins/index.d.ts +2 -0
  20. package/src/lib/plugins/index.js +23 -0
  21. package/src/lib/plugins/index.js.map +1 -0
  22. package/src/lib/routes/index.d.ts +0 -0
  23. package/src/lib/routes/index.js +2 -0
  24. package/src/lib/routes/index.js.map +1 -0
  25. package/src/lib/routes/router.d.ts +14 -0
  26. package/src/lib/routes/router.js +68 -0
  27. package/src/lib/routes/router.js.map +1 -0
  28. package/src/lib/routes/types.d.ts +15 -0
  29. package/src/lib/routes/types.js +2 -0
  30. package/src/lib/routes/types.js.map +1 -0
  31. package/{lib/cjs/utils.d.ts → src/lib/utils/index.d.ts} +0 -4
  32. package/{lib/esm/utils.js → src/lib/utils/index.js} +1 -34
  33. package/src/lib/utils/index.js.map +1 -0
  34. package/lib/cjs/constants.d.ts +0 -4
  35. package/lib/cjs/constants.js +0 -8
  36. package/lib/cjs/constants.js.map +0 -1
  37. package/lib/cjs/core-bot.d.ts +0 -36
  38. package/lib/cjs/core-bot.js +0 -96
  39. package/lib/cjs/core-bot.js.map +0 -1
  40. package/lib/cjs/debug/index.d.ts +0 -1
  41. package/lib/cjs/debug/index.js +0 -5
  42. package/lib/cjs/debug/index.js.map +0 -1
  43. package/lib/cjs/debug/inspector.d.ts +0 -24
  44. package/lib/cjs/debug/inspector.js +0 -72
  45. package/lib/cjs/debug/inspector.js.map +0 -1
  46. package/lib/cjs/handoff.d.ts +0 -67
  47. package/lib/cjs/handoff.js +0 -186
  48. package/lib/cjs/handoff.js.map +0 -1
  49. package/lib/cjs/hubtype-service.d.ts +0 -69
  50. package/lib/cjs/hubtype-service.js +0 -190
  51. package/lib/cjs/hubtype-service.js.map +0 -1
  52. package/lib/cjs/i18n.d.ts +0 -2
  53. package/lib/cjs/i18n.js +0 -11
  54. package/lib/cjs/i18n.js.map +0 -1
  55. package/lib/cjs/index.d.ts +0 -11
  56. package/lib/cjs/index.js +0 -15
  57. package/lib/cjs/index.js.map +0 -1
  58. package/lib/cjs/models/bot-state.d.ts +0 -11
  59. package/lib/cjs/models/bot-state.js +0 -3
  60. package/lib/cjs/models/bot-state.js.map +0 -1
  61. package/lib/cjs/models/channels.d.ts +0 -9
  62. package/lib/cjs/models/channels.js +0 -14
  63. package/lib/cjs/models/channels.js.map +0 -1
  64. package/lib/cjs/models/events/base-event.d.ts +0 -20
  65. package/lib/cjs/models/events/base-event.js +0 -17
  66. package/lib/cjs/models/events/base-event.js.map +0 -1
  67. package/lib/cjs/models/events/botonic-event.d.ts +0 -4
  68. package/lib/cjs/models/events/botonic-event.js +0 -3
  69. package/lib/cjs/models/events/botonic-event.js.map +0 -1
  70. package/lib/cjs/models/events/connection/connection-event.d.ts +0 -9
  71. package/lib/cjs/models/events/connection/connection-event.js +0 -9
  72. package/lib/cjs/models/events/connection/connection-event.js.map +0 -1
  73. package/lib/cjs/models/events/connection/index.d.ts +0 -1
  74. package/lib/cjs/models/events/connection/index.js +0 -5
  75. package/lib/cjs/models/events/connection/index.js.map +0 -1
  76. package/lib/cjs/models/events/index.d.ts +0 -4
  77. package/lib/cjs/models/events/index.js +0 -8
  78. package/lib/cjs/models/events/index.js.map +0 -1
  79. package/lib/cjs/models/events/integration/index.d.ts +0 -1
  80. package/lib/cjs/models/events/integration/index.js +0 -5
  81. package/lib/cjs/models/events/integration/index.js.map +0 -1
  82. package/lib/cjs/models/events/integration/integration-event.d.ts +0 -6
  83. package/lib/cjs/models/events/integration/integration-event.js +0 -3
  84. package/lib/cjs/models/events/integration/integration-event.js.map +0 -1
  85. package/lib/cjs/models/events/message/buttons.d.ts +0 -18
  86. package/lib/cjs/models/events/message/buttons.js +0 -3
  87. package/lib/cjs/models/events/message/buttons.js.map +0 -1
  88. package/lib/cjs/models/events/message/carousel.d.ts +0 -11
  89. package/lib/cjs/models/events/message/carousel.js +0 -3
  90. package/lib/cjs/models/events/message/carousel.js.map +0 -1
  91. package/lib/cjs/models/events/message/custom.d.ts +0 -6
  92. package/lib/cjs/models/events/message/custom.js +0 -3
  93. package/lib/cjs/models/events/message/custom.js.map +0 -1
  94. package/lib/cjs/models/events/message/index.d.ts +0 -9
  95. package/lib/cjs/models/events/message/index.js +0 -13
  96. package/lib/cjs/models/events/message/index.js.map +0 -1
  97. package/lib/cjs/models/events/message/location.d.ts +0 -6
  98. package/lib/cjs/models/events/message/location.js +0 -3
  99. package/lib/cjs/models/events/message/location.js.map +0 -1
  100. package/lib/cjs/models/events/message/media.d.ts +0 -18
  101. package/lib/cjs/models/events/message/media.js +0 -11
  102. package/lib/cjs/models/events/message/media.js.map +0 -1
  103. package/lib/cjs/models/events/message/message-event.d.ts +0 -30
  104. package/lib/cjs/models/events/message/message-event.js +0 -32
  105. package/lib/cjs/models/events/message/message-event.js.map +0 -1
  106. package/lib/cjs/models/events/message/postback.d.ts +0 -4
  107. package/lib/cjs/models/events/message/postback.js +0 -3
  108. package/lib/cjs/models/events/message/postback.js.map +0 -1
  109. package/lib/cjs/models/events/message/replies.d.ts +0 -7
  110. package/lib/cjs/models/events/message/replies.js +0 -3
  111. package/lib/cjs/models/events/message/replies.js.map +0 -1
  112. package/lib/cjs/models/events/message/text.d.ts +0 -8
  113. package/lib/cjs/models/events/message/text.js +0 -3
  114. package/lib/cjs/models/events/message/text.js.map +0 -1
  115. package/lib/cjs/models/index.d.ts +0 -6
  116. package/lib/cjs/models/index.js +0 -10
  117. package/lib/cjs/models/index.js.map +0 -1
  118. package/lib/cjs/models/legacy-types.d.ts +0 -184
  119. package/lib/cjs/models/legacy-types.js +0 -59
  120. package/lib/cjs/models/legacy-types.js.map +0 -1
  121. package/lib/cjs/models/session.d.ts +0 -3
  122. package/lib/cjs/models/session.js +0 -3
  123. package/lib/cjs/models/session.js.map +0 -1
  124. package/lib/cjs/models/user.d.ts +0 -14
  125. package/lib/cjs/models/user.js +0 -3
  126. package/lib/cjs/models/user.js.map +0 -1
  127. package/lib/cjs/output-parser/botonic-output-parser.d.ts +0 -16
  128. package/lib/cjs/output-parser/botonic-output-parser.js +0 -37
  129. package/lib/cjs/output-parser/botonic-output-parser.js.map +0 -1
  130. package/lib/cjs/output-parser/factory.d.ts +0 -4
  131. package/lib/cjs/output-parser/factory.js +0 -32
  132. package/lib/cjs/output-parser/factory.js.map +0 -1
  133. package/lib/cjs/output-parser/index.d.ts +0 -3
  134. package/lib/cjs/output-parser/index.js +0 -7
  135. package/lib/cjs/output-parser/index.js.map +0 -1
  136. package/lib/cjs/output-parser/parsers.d.ts +0 -22
  137. package/lib/cjs/output-parser/parsers.js +0 -147
  138. package/lib/cjs/output-parser/parsers.js.map +0 -1
  139. package/lib/cjs/plugins.d.ts +0 -6
  140. package/lib/cjs/plugins.js +0 -48
  141. package/lib/cjs/plugins.js.map +0 -1
  142. package/lib/cjs/routing/index.d.ts +0 -2
  143. package/lib/cjs/routing/index.js +0 -6
  144. package/lib/cjs/routing/index.js.map +0 -1
  145. package/lib/cjs/routing/router-utils.d.ts +0 -11
  146. package/lib/cjs/routing/router-utils.js +0 -89
  147. package/lib/cjs/routing/router-utils.js.map +0 -1
  148. package/lib/cjs/routing/router.d.ts +0 -54
  149. package/lib/cjs/routing/router.js +0 -319
  150. package/lib/cjs/routing/router.js.map +0 -1
  151. package/lib/cjs/utils.js +0 -90
  152. package/lib/cjs/utils.js.map +0 -1
  153. package/lib/esm/constants.d.ts +0 -4
  154. package/lib/esm/constants.js +0 -5
  155. package/lib/esm/constants.js.map +0 -1
  156. package/lib/esm/core-bot.d.ts +0 -36
  157. package/lib/esm/core-bot.js +0 -92
  158. package/lib/esm/core-bot.js.map +0 -1
  159. package/lib/esm/debug/index.d.ts +0 -1
  160. package/lib/esm/debug/index.js +0 -2
  161. package/lib/esm/debug/index.js.map +0 -1
  162. package/lib/esm/debug/inspector.d.ts +0 -24
  163. package/lib/esm/debug/inspector.js +0 -65
  164. package/lib/esm/debug/inspector.js.map +0 -1
  165. package/lib/esm/handoff.d.ts +0 -67
  166. package/lib/esm/handoff.js +0 -173
  167. package/lib/esm/handoff.js.map +0 -1
  168. package/lib/esm/hubtype-service.d.ts +0 -69
  169. package/lib/esm/hubtype-service.js +0 -185
  170. package/lib/esm/hubtype-service.js.map +0 -1
  171. package/lib/esm/i18n.d.ts +0 -2
  172. package/lib/esm/i18n.js +0 -7
  173. package/lib/esm/i18n.js.map +0 -1
  174. package/lib/esm/index.d.ts +0 -11
  175. package/lib/esm/index.js +0 -12
  176. package/lib/esm/index.js.map +0 -1
  177. package/lib/esm/models/bot-state.d.ts +0 -11
  178. package/lib/esm/models/bot-state.js +0 -2
  179. package/lib/esm/models/bot-state.js.map +0 -1
  180. package/lib/esm/models/channels.d.ts +0 -9
  181. package/lib/esm/models/channels.js +0 -11
  182. package/lib/esm/models/channels.js.map +0 -1
  183. package/lib/esm/models/events/base-event.d.ts +0 -20
  184. package/lib/esm/models/events/base-event.js +0 -14
  185. package/lib/esm/models/events/base-event.js.map +0 -1
  186. package/lib/esm/models/events/botonic-event.d.ts +0 -4
  187. package/lib/esm/models/events/botonic-event.js +0 -2
  188. package/lib/esm/models/events/botonic-event.js.map +0 -1
  189. package/lib/esm/models/events/connection/connection-event.d.ts +0 -9
  190. package/lib/esm/models/events/connection/connection-event.js +0 -6
  191. package/lib/esm/models/events/connection/connection-event.js.map +0 -1
  192. package/lib/esm/models/events/connection/index.d.ts +0 -1
  193. package/lib/esm/models/events/connection/index.js +0 -2
  194. package/lib/esm/models/events/connection/index.js.map +0 -1
  195. package/lib/esm/models/events/index.d.ts +0 -4
  196. package/lib/esm/models/events/index.js +0 -5
  197. package/lib/esm/models/events/index.js.map +0 -1
  198. package/lib/esm/models/events/integration/index.d.ts +0 -1
  199. package/lib/esm/models/events/integration/index.js +0 -2
  200. package/lib/esm/models/events/integration/index.js.map +0 -1
  201. package/lib/esm/models/events/integration/integration-event.d.ts +0 -6
  202. package/lib/esm/models/events/integration/integration-event.js +0 -2
  203. package/lib/esm/models/events/integration/integration-event.js.map +0 -1
  204. package/lib/esm/models/events/message/buttons.d.ts +0 -18
  205. package/lib/esm/models/events/message/buttons.js +0 -2
  206. package/lib/esm/models/events/message/buttons.js.map +0 -1
  207. package/lib/esm/models/events/message/carousel.d.ts +0 -11
  208. package/lib/esm/models/events/message/carousel.js +0 -2
  209. package/lib/esm/models/events/message/carousel.js.map +0 -1
  210. package/lib/esm/models/events/message/custom.d.ts +0 -6
  211. package/lib/esm/models/events/message/custom.js +0 -2
  212. package/lib/esm/models/events/message/custom.js.map +0 -1
  213. package/lib/esm/models/events/message/index.d.ts +0 -9
  214. package/lib/esm/models/events/message/index.js +0 -10
  215. package/lib/esm/models/events/message/index.js.map +0 -1
  216. package/lib/esm/models/events/message/location.d.ts +0 -6
  217. package/lib/esm/models/events/message/location.js +0 -2
  218. package/lib/esm/models/events/message/location.js.map +0 -1
  219. package/lib/esm/models/events/message/media.d.ts +0 -18
  220. package/lib/esm/models/events/message/media.js +0 -8
  221. package/lib/esm/models/events/message/media.js.map +0 -1
  222. package/lib/esm/models/events/message/message-event.d.ts +0 -30
  223. package/lib/esm/models/events/message/message-event.js +0 -29
  224. package/lib/esm/models/events/message/message-event.js.map +0 -1
  225. package/lib/esm/models/events/message/postback.d.ts +0 -4
  226. package/lib/esm/models/events/message/postback.js +0 -2
  227. package/lib/esm/models/events/message/postback.js.map +0 -1
  228. package/lib/esm/models/events/message/replies.d.ts +0 -7
  229. package/lib/esm/models/events/message/replies.js +0 -2
  230. package/lib/esm/models/events/message/replies.js.map +0 -1
  231. package/lib/esm/models/events/message/text.d.ts +0 -8
  232. package/lib/esm/models/events/message/text.js +0 -2
  233. package/lib/esm/models/events/message/text.js.map +0 -1
  234. package/lib/esm/models/index.d.ts +0 -6
  235. package/lib/esm/models/index.js +0 -7
  236. package/lib/esm/models/index.js.map +0 -1
  237. package/lib/esm/models/legacy-types.d.ts +0 -184
  238. package/lib/esm/models/legacy-types.js +0 -56
  239. package/lib/esm/models/legacy-types.js.map +0 -1
  240. package/lib/esm/models/session.d.ts +0 -3
  241. package/lib/esm/models/session.js +0 -2
  242. package/lib/esm/models/session.js.map +0 -1
  243. package/lib/esm/models/user.d.ts +0 -14
  244. package/lib/esm/models/user.js +0 -2
  245. package/lib/esm/models/user.js.map +0 -1
  246. package/lib/esm/output-parser/botonic-output-parser.d.ts +0 -16
  247. package/lib/esm/output-parser/botonic-output-parser.js +0 -33
  248. package/lib/esm/output-parser/botonic-output-parser.js.map +0 -1
  249. package/lib/esm/output-parser/factory.d.ts +0 -4
  250. package/lib/esm/output-parser/factory.js +0 -28
  251. package/lib/esm/output-parser/factory.js.map +0 -1
  252. package/lib/esm/output-parser/index.d.ts +0 -3
  253. package/lib/esm/output-parser/index.js +0 -4
  254. package/lib/esm/output-parser/index.js.map +0 -1
  255. package/lib/esm/output-parser/parsers.d.ts +0 -22
  256. package/lib/esm/output-parser/parsers.js +0 -130
  257. package/lib/esm/output-parser/parsers.js.map +0 -1
  258. package/lib/esm/plugins.d.ts +0 -6
  259. package/lib/esm/plugins.js +0 -43
  260. package/lib/esm/plugins.js.map +0 -1
  261. package/lib/esm/routing/index.d.ts +0 -2
  262. package/lib/esm/routing/index.js +0 -3
  263. package/lib/esm/routing/index.js.map +0 -1
  264. package/lib/esm/routing/router-utils.d.ts +0 -11
  265. package/lib/esm/routing/router-utils.js +0 -79
  266. package/lib/esm/routing/router-utils.js.map +0 -1
  267. package/lib/esm/routing/router.d.ts +0 -54
  268. package/lib/esm/routing/router.js +0 -315
  269. package/lib/esm/routing/router.js.map +0 -1
  270. package/lib/esm/utils.d.ts +0 -11
  271. package/lib/esm/utils.js.map +0 -1
  272. package/src/constants.ts +0 -6
  273. package/src/core-bot.ts +0 -181
  274. package/src/debug/index.ts +0 -1
  275. package/src/debug/inspector.ts +0 -102
  276. package/src/handoff.ts +0 -271
  277. package/src/hubtype-service.ts +0 -266
  278. package/src/i18n.ts +0 -12
  279. package/src/index.ts +0 -11
  280. package/src/models/bot-state.ts +0 -12
  281. package/src/models/channels.ts +0 -9
  282. package/src/models/events/base-event.ts +0 -22
  283. package/src/models/events/botonic-event.ts +0 -26
  284. package/src/models/events/connection/connection-event.ts +0 -11
  285. package/src/models/events/connection/index.ts +0 -1
  286. package/src/models/events/index.ts +0 -4
  287. package/src/models/events/integration/index.ts +0 -1
  288. package/src/models/events/integration/integration-event.ts +0 -13
  289. package/src/models/events/message/buttons.ts +0 -23
  290. package/src/models/events/message/carousel.ts +0 -13
  291. package/src/models/events/message/custom.ts +0 -7
  292. package/src/models/events/message/index.ts +0 -9
  293. package/src/models/events/message/location.ts +0 -7
  294. package/src/models/events/message/media.ts +0 -28
  295. package/src/models/events/message/message-event.ts +0 -37
  296. package/src/models/events/message/postback.ts +0 -5
  297. package/src/models/events/message/replies.ts +0 -8
  298. package/src/models/events/message/text.ts +0 -12
  299. package/src/models/index.ts +0 -6
  300. package/src/models/legacy-types.ts +0 -262
  301. package/src/models/session.ts +0 -3
  302. package/src/models/user.ts +0 -16
  303. package/src/output-parser/botonic-output-parser.ts +0 -38
  304. package/src/output-parser/factory.ts +0 -38
  305. package/src/output-parser/index.ts +0 -3
  306. package/src/output-parser/parsers.ts +0 -202
  307. package/src/plugins.ts +0 -59
  308. package/src/routing/index.ts +0 -2
  309. package/src/routing/router-utils.ts +0 -100
  310. package/src/routing/router.ts +0 -415
  311. package/src/utils.ts +0 -90
@@ -1,415 +0,0 @@
1
- import { NOT_FOUND_PATH } from '../constants'
2
- import { RouteInspector } from '../debug/inspector'
3
- import {
4
- BotState,
5
- Input,
6
- MatchedValue,
7
- Matcher,
8
- MatchingProp,
9
- Nullable,
10
- ProcessInputResult,
11
- Route,
12
- RouteParams,
13
- RoutePath,
14
- RoutingState,
15
- Session,
16
- } from '../models'
17
- import { cloneObject } from '../utils'
18
- import {
19
- getEmptyAction,
20
- getNotFoundAction,
21
- getPathParamsFromPathPayload,
22
- isPathPayload,
23
- } from './router-utils'
24
-
25
- export class Router {
26
- routes: Route[]
27
- routeInspector: RouteInspector
28
-
29
- constructor(
30
- routes: Route[],
31
- routeInspector: RouteInspector | undefined = undefined
32
- ) {
33
- this.routes = routes
34
- this.routeInspector = routeInspector || new RouteInspector()
35
- }
36
-
37
- /**
38
- * Processes an input and return a representation of the new bot state.
39
- * The algorithm is splitted in two main parts:
40
- * 1. Getting the current routing state.
41
- * 2. Given a routing state, resolve the different possible scenarios and return the new bot state.
42
- * The new bot state can return three type of actions:
43
- * - action: an action directly resolved from a matching route
44
- * - emptyAction: optional action that can exists or not only within childRoutes
45
- * - fallbackAction: any other action that acts as a fallback (404, )
46
- */
47
-
48
- // eslint-disable-next-line complexity
49
- processInput(
50
- input: Input,
51
- session: Session,
52
- botState: BotState
53
- ): ProcessInputResult {
54
- botState.retries = botState.retries ?? 0
55
-
56
- // 1. Getting the current routing state.
57
- const {
58
- currentRoute,
59
- matchedRoute,
60
- params,
61
- isFlowBroken,
62
- } = this.getRoutingState(input, session, botState)
63
-
64
- const currentRoutePath = currentRoute?.path ?? null
65
- const matchedRoutePath = matchedRoute?.path ?? null
66
-
67
- // 2. Given a routing state, resolve the different possible scenarios and return the new bot state.
68
-
69
- /**
70
- * Redirect Scenario:
71
- * We have matched a redirect route with a given redirection path, so we try to obtain the redirectionRoute with getRouteByPath.
72
- * Independently of whether the redirectionRoute is found or not, the intent is to trigger a redirection which by definition breaks the flow, so retries are set to 0.
73
- * It has preference over ignoring retries.
74
- */
75
- if (matchedRoute && matchedRoute.redirect) {
76
- botState.retries = 0
77
- const redirectionRoute = this.getRouteByPath(matchedRoute.redirect)
78
- if (redirectionRoute) {
79
- botState.lastRoutePath = matchedRoute.redirect
80
- return {
81
- action: redirectionRoute.action,
82
- emptyAction: getEmptyAction(redirectionRoute.childRoutes),
83
- fallbackAction: null,
84
- botState,
85
- params,
86
- }
87
- }
88
- botState.lastRoutePath = null
89
- return {
90
- action: null,
91
- emptyAction: null,
92
- fallbackAction: getNotFoundAction(input, this.routes),
93
- botState,
94
- params,
95
- }
96
- }
97
-
98
- /**
99
- * Ignore Retry Scenario:
100
- * We have matched a route with an ignore retry, so we return directly the new bot state. The intent is to break the flow, so retries are set to 0.
101
- */
102
- if (matchedRoute && matchedRoute.ignoreRetry) {
103
- botState.retries = 0
104
- botState.lastRoutePath = matchedRoutePath
105
- return {
106
- action: matchedRoute.action,
107
- emptyAction: getEmptyAction(matchedRoute.childRoutes),
108
- fallbackAction: null,
109
- botState,
110
- params,
111
- }
112
- }
113
-
114
- /**
115
- * Retry Scenario:
116
- * We were in a route which had retries enabled, so we check if the number of retries is exceeded.
117
- * If we have not surpassed the limit of retries and we haven't matched an ignoreRetry route, update them, and then return the new bot state.
118
- */
119
- if (
120
- isFlowBroken &&
121
- currentRoute &&
122
- currentRoute.retry &&
123
- botState.retries < currentRoute.retry
124
- ) {
125
- botState.retries = botState.retries !== 0 ? botState.retries + 1 : 1
126
- botState.lastRoutePath = currentRoutePath
127
- if (matchedRoute && matchedRoutePath !== NOT_FOUND_PATH) {
128
- return {
129
- action: currentRoute.action,
130
- emptyAction: getEmptyAction(matchedRoute.childRoutes),
131
- fallbackAction: matchedRoute.action,
132
- botState,
133
- params,
134
- }
135
- }
136
- return {
137
- action: currentRoute.action ?? null,
138
- emptyAction: getEmptyAction(currentRoute.childRoutes),
139
- fallbackAction: getNotFoundAction(input, this.routes),
140
- botState,
141
- params,
142
- }
143
- }
144
-
145
- /**
146
- * Default Scenario:
147
- * We have matched a route or not, but we don't need to execute retries logic, so retries stay to 0.
148
- */
149
- botState.retries = 0
150
-
151
- /**
152
- * Matching Route Scenario:
153
- * We have matched a route, so we return the new bot state.
154
- */
155
- if (matchedRoute && matchedRoutePath !== NOT_FOUND_PATH) {
156
- botState.lastRoutePath = matchedRoutePath
157
- return {
158
- action: matchedRoute.action ?? null,
159
- emptyAction: getEmptyAction(matchedRoute.childRoutes),
160
- fallbackAction: null,
161
- botState,
162
- params,
163
- }
164
- }
165
-
166
- /**
167
- * 404 Scenario (No Route Found):
168
- * We have not matched any route, so we return the new bot state.
169
- */
170
- botState.lastRoutePath = currentRoutePath
171
- return {
172
- action: null,
173
- emptyAction: null,
174
- fallbackAction: getNotFoundAction(input, this.routes),
175
- params,
176
- botState,
177
- }
178
- }
179
-
180
- /**
181
- * Find the route that matches the given input, if it match with some of the entries, return the whole Route of the entry with optional params captured if matcher was a regex.
182
- * IMPORTANT: It returns a cloned route instead of the route itself to avoid modifying original routes and introduce side effects
183
- * */
184
- getRoute(
185
- input: Input | Partial<Input>,
186
- routes: Route[],
187
- session: Session,
188
- lastRoutePath: RoutePath
189
- ): RouteParams | null {
190
- let params = {}
191
- const route = routes.find(r =>
192
- Object.entries(r)
193
- .filter(
194
- ([key, _]) =>
195
- key !== 'action' && key !== 'childRoutes' && key !== 'path'
196
- )
197
- .some(([key, value]) => {
198
- const match = this.matchRoute(
199
- r,
200
- key as MatchingProp,
201
- value as Matcher,
202
- input as Input,
203
- session,
204
- lastRoutePath
205
- )
206
- try {
207
- if (match !== null && typeof match !== 'boolean' && match.groups) {
208
- // Strip '[Object: null prototype]' from groups result: https://stackoverflow.com/a/62945609/6237608
209
- params = { ...match.groups }
210
- }
211
- } catch (e) {}
212
- return Boolean(match)
213
- })
214
- )
215
- if (route) return { route: cloneObject(route), params }
216
- return null
217
- }
218
-
219
- /**
220
- * Find the route that matches the given path. Path can include concatenations, e.g: 'Flow1/Subflow1.1'.
221
- * IMPORTANT: It returns a cloned route instead of the route itself to avoid modifying original routes and introduce side effects
222
- * */
223
- getRouteByPath(
224
- path: RoutePath,
225
- routeList: Route[] = this.routes
226
- ): Nullable<Route> {
227
- if (!path) return null
228
- const [currentPath, ...childPath] = path.split('/')
229
- for (const route of routeList) {
230
- // iterate over all routeList
231
- if (route.path === currentPath) {
232
- if (
233
- route.childRoutes &&
234
- route.childRoutes.length &&
235
- childPath.length > 0
236
- ) {
237
- // evaluate childroute over next actions
238
- const computedRoute = this.getRouteByPath(
239
- childPath.join('/'),
240
- route.childRoutes
241
- )
242
- // IMPORTANT: Returning a new object to avoid modifying dev routes and introduce side effects
243
- if (computedRoute) return cloneObject(computedRoute)
244
- } else if (childPath.length === 0) {
245
- return cloneObject(route) // last action and found route
246
- }
247
- }
248
- }
249
- return null
250
- }
251
-
252
- /**
253
- * Returns the matched value for a specific route.
254
- * Matching Props: ('text' | 'payload' | 'intent' | 'type' | 'input' | 'session' | 'request' ...)
255
- * Matchers: (string: exact match | regex: regular expression match | function: return true)
256
- * input: user input object, e.g.: {type: 'text', data: 'Hi'}
257
- * */
258
- matchRoute(
259
- route: Route,
260
- prop: MatchingProp,
261
- matcher: Matcher,
262
- input: Input,
263
- session: Session,
264
- lastRoutePath: RoutePath
265
- ): MatchedValue {
266
- let value: any = null
267
- if (Object.keys(input).indexOf(prop) > -1) value = input[prop]
268
- else if (prop === 'input') value = input
269
- else if (prop === 'session') value = session
270
- else if (prop === 'request') value = { input, session, lastRoutePath }
271
- const matched = this.matchValue(matcher, value)
272
- if (matched) {
273
- this.routeInspector.routeMatched(route, prop, matcher, value)
274
- } else {
275
- this.routeInspector.routeNotMatched(route, prop, matcher, value)
276
- }
277
- return matched
278
- }
279
-
280
- /**
281
- * Runs the matcher against the given value.
282
- * If there is a match, it will return a truthy value (true, RegExp result), o.w., it will return a falsy value.
283
- * */
284
- matchValue(matcher: Matcher, value: any): MatchedValue {
285
- if (typeof matcher === 'string') return value === matcher
286
- if (matcher instanceof RegExp) {
287
- if (value === undefined || value === null) return false
288
- return matcher.exec(value)
289
- }
290
- if (typeof matcher === 'function') return matcher(value)
291
- return false
292
- }
293
-
294
- /**
295
- * It resolves the current state of navigation. Two scenarios:
296
- * 1. Given a path payload input (__PATH_PAYLOAD__somePath?someParam=someValue), we can resolve the routing state directly from it (using getRouteByPath).
297
- * 2. Given any other type of input, we resolve the routing state with normal resolution (using getRoute).
298
- * */
299
- getRoutingState(
300
- input: Input,
301
- session: Session,
302
- botState: BotState
303
- ): RoutingState {
304
- const currentRoute = this.getRouteByPath(botState.lastRoutePath)
305
- if (currentRoute && botState.lastRoutePath)
306
- currentRoute.path = botState.lastRoutePath
307
- if (typeof input.payload === 'string' && isPathPayload(input.payload)) {
308
- return this.getRoutingStateFromPathPayload(currentRoute, input.payload)
309
- }
310
- return this.getRoutingStateFromInput(currentRoute, input, session)
311
- }
312
-
313
- /**
314
- * Given a non path payload input, try to run it against the routes, update matching routes information in consequence and dictamine if the flow has been broken.
315
- * */
316
- getRoutingStateFromInput(
317
- currentRoute: Nullable<Route>,
318
- input: Input,
319
- session: Session
320
- ): RoutingState {
321
- // get route depending of current ChildRoutes
322
- if (currentRoute && currentRoute.childRoutes) {
323
- const routeParams = this.getRoute(
324
- input,
325
- currentRoute.childRoutes,
326
- session,
327
- currentRoute.path
328
- )
329
- if (routeParams) {
330
- return {
331
- currentRoute,
332
- matchedRoute: {
333
- ...routeParams.route,
334
- path:
335
- routeParams.route && currentRoute.path
336
- ? `${currentRoute.path}/${routeParams.route.path}`
337
- : currentRoute.path,
338
- },
339
- params: routeParams.params,
340
- isFlowBroken: false,
341
- }
342
- }
343
- }
344
- /**
345
- * we couldn't find a route in the state of the currentRoute childRoutes,
346
- * so let's find in the general routes
347
- */
348
- const routeParams = this.getRoute(
349
- input,
350
- this.routes,
351
- session,
352
- currentRoute?.path ?? null
353
- )
354
- const isFlowBroken = Boolean(currentRoute?.path)
355
- if (routeParams?.route) {
356
- return {
357
- currentRoute,
358
- matchedRoute: {
359
- ...routeParams.route,
360
- path: routeParams.route?.path ?? null,
361
- },
362
- params: routeParams.params,
363
- isFlowBroken,
364
- }
365
- }
366
- return {
367
- currentRoute,
368
- matchedRoute: null,
369
- params: {},
370
- isFlowBroken,
371
- }
372
- }
373
-
374
- /**
375
- * Given a path payload input, try to run the path against the routes, update matching routes information in consequence and dictamine if the flow has been broken.
376
- * */
377
- getRoutingStateFromPathPayload(
378
- currentRoute: Nullable<Route>,
379
- pathPayload: string
380
- ): RoutingState {
381
- const { path, params } = getPathParamsFromPathPayload(pathPayload)
382
- /**
383
- * shorthand function to update the matching information given a path
384
- */
385
- const getRoutingStateFromPath = (seekPath: string): RoutingState => {
386
- const matchedRoute = this.getRouteByPath(seekPath)
387
- if (!matchedRoute) {
388
- return {
389
- currentRoute,
390
- matchedRoute: null,
391
- params: {},
392
- isFlowBroken: true,
393
- }
394
- }
395
- return {
396
- currentRoute,
397
- matchedRoute: { ...matchedRoute, path: seekPath },
398
- params,
399
- isFlowBroken: false,
400
- }
401
- }
402
- /**
403
- * Given a valid path: 'Flow1/Subflow1' we are in one of the two scenarios below.
404
- */
405
- // 1. Received __PATH_PAYLOAD__Subflow2, so we need to first try to concatenate it with Flow1 (lastRoutePath)
406
- if (currentRoute?.path) {
407
- const routingState = getRoutingStateFromPath(
408
- `${currentRoute.path}/${path}`
409
- )
410
- if (routingState.matchedRoute) return routingState
411
- }
412
- // 2. Received __PATH_PAYLOAD__Flow1/Subflow1, so we can resolve it directly
413
- return getRoutingStateFromPath(path as string)
414
- }
415
- }
package/src/utils.ts DELETED
@@ -1,90 +0,0 @@
1
- export const isNode = (): boolean => {
2
- // @ts-ignore
3
- return typeof IS_NODE !== 'undefined'
4
- ? // @ts-ignore
5
- IS_NODE
6
- : typeof process !== 'undefined' &&
7
- process.versions !== null &&
8
- process.versions.node !== null
9
- }
10
-
11
- export const isBrowser = (): boolean => {
12
- // @ts-ignore
13
- return typeof IS_BROWSER !== 'undefined'
14
- ? // @ts-ignore
15
- IS_BROWSER
16
- : typeof window !== 'undefined' &&
17
- typeof window.document !== 'undefined' &&
18
- !window.process
19
- }
20
-
21
- export function getWebpackEnvVar(
22
- webpackEnvVar: string | false,
23
- name: string,
24
- defaultValue: string
25
- ): string {
26
- return (
27
- webpackEnvVar ||
28
- (typeof process !== 'undefined' && process.env[name]) ||
29
- defaultValue
30
- )
31
- }
32
-
33
- export const isMobile = (mobileBreakpoint = 460): boolean => {
34
- if (isBrowser()) {
35
- const w = Math.max(
36
- document.documentElement.clientWidth,
37
- window.innerWidth || 0
38
- )
39
- if (w < mobileBreakpoint) {
40
- return true
41
- }
42
- }
43
- return false
44
- }
45
-
46
- export function isFunction(o: any): boolean {
47
- return typeof o === 'function'
48
- }
49
-
50
- export function cloneObject(object: any): any {
51
- if (!object) return {}
52
- return { ...object }
53
- }
54
-
55
- export const params2queryString = (params: { [key: string]: string }): string =>
56
- Object.entries(params)
57
- .map(([k, v]: any) => `${encodeURIComponent(k)}=${encodeURIComponent(v)}`)
58
- .join('&')
59
-
60
- // Joins path segments. Preserves initial "/" and resolves ".." and "."
61
- // Does not support using ".." to go above/outside the root.
62
- // This means that join("foo", "../../bar") will not resolve to "../bar"
63
- export const join = (...segments: string[]): string => {
64
- // Split the inputs into a list of path commands.
65
- let parts: string[] = []
66
- for (let i = 0, l = segments.length; i < l; i++) {
67
- parts = parts.concat(segments[i].split('/'))
68
- }
69
- // Interpret the path commands to get the new resolved path.
70
- const newParts: string[] = []
71
- for (let i = 0, l = parts.length; i < l; i++) {
72
- const part = parts[i]
73
- // Remove leading and trailing slashes
74
- // Also remove "." segments
75
- if (!part || part === '.') continue
76
- // Interpret ".." to pop the last segment
77
- if (part === '..') newParts.pop()
78
- // Push new path segments.
79
- else newParts.push(part)
80
- }
81
- // Preserve the initial slash if there was one.
82
- // @ts-ignore
83
- if (parts[0] === '') newParts.unshift('')
84
- // Turn back into a single string path.
85
- return newParts.join('/') || (newParts.length ? '/' : '.')
86
- }
87
-
88
- // A simple function to get the dirname of a path
89
- // Trailing slashes are ignored. Leading slash is preserved.
90
- export const dirname = (path: string): string => join(path, '..')