@botonic/core 0.19.0-beta.1 → 0.20.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 (361) hide show
  1. package/lib/cjs/constants.d.ts +4 -0
  2. package/lib/cjs/constants.js +8 -0
  3. package/lib/cjs/constants.js.map +1 -0
  4. package/lib/cjs/core-bot.d.ts +36 -0
  5. package/lib/cjs/core-bot.js +115 -0
  6. package/lib/cjs/core-bot.js.map +1 -0
  7. package/lib/cjs/data-provider/dynamodb-data-provider.d.ts +26 -0
  8. package/lib/cjs/data-provider/dynamodb-data-provider.js +81 -0
  9. package/lib/cjs/data-provider/dynamodb-data-provider.js.map +1 -0
  10. package/lib/cjs/data-provider/dynamodb-utils.d.ts +11 -0
  11. package/lib/cjs/data-provider/dynamodb-utils.js +120 -0
  12. package/lib/cjs/data-provider/dynamodb-utils.js.map +1 -0
  13. package/lib/cjs/data-provider/factory.d.ts +25 -0
  14. package/lib/cjs/data-provider/factory.js +31 -0
  15. package/lib/cjs/data-provider/factory.js.map +1 -0
  16. package/lib/cjs/data-provider/index.d.ts +4 -0
  17. package/lib/cjs/data-provider/index.js +8 -0
  18. package/lib/cjs/data-provider/index.js.map +1 -0
  19. package/lib/cjs/data-provider/local-data-provider.d.ts +26 -0
  20. package/lib/cjs/data-provider/local-data-provider.js +137 -0
  21. package/lib/cjs/data-provider/local-data-provider.js.map +1 -0
  22. package/lib/cjs/debug/index.d.ts +1 -0
  23. package/lib/cjs/debug/index.js +5 -0
  24. package/lib/cjs/debug/index.js.map +1 -0
  25. package/lib/cjs/debug/inspector.d.ts +24 -0
  26. package/lib/{debug → cjs/debug}/inspector.js +5 -3
  27. package/lib/cjs/debug/inspector.js.map +1 -0
  28. package/lib/cjs/handoff.d.ts +67 -0
  29. package/lib/{handoff.js → cjs/handoff.js} +2 -1
  30. package/lib/cjs/handoff.js.map +1 -0
  31. package/lib/cjs/hubtype-service.d.ts +69 -0
  32. package/lib/{hubtype-service.js → cjs/hubtype-service.js} +39 -12
  33. package/lib/cjs/hubtype-service.js.map +1 -0
  34. package/lib/cjs/i18n.d.ts +2 -0
  35. package/lib/{i18n.js → cjs/i18n.js} +0 -1
  36. package/lib/cjs/i18n.js.map +1 -0
  37. package/lib/cjs/index.d.ts +11 -0
  38. package/lib/cjs/index.js +17 -0
  39. package/lib/cjs/index.js.map +1 -0
  40. package/lib/cjs/models/events/base-event.d.ts +13 -0
  41. package/lib/{models/events/index.js → cjs/models/events/base-event.js} +1 -1
  42. package/lib/cjs/models/events/base-event.js.map +1 -0
  43. package/lib/cjs/models/events/botonic-event.d.ts +3 -0
  44. package/lib/cjs/models/events/botonic-event.js +3 -0
  45. package/lib/cjs/models/events/botonic-event.js.map +1 -0
  46. package/lib/{models/events/connections/index.d.ts → cjs/models/events/connections/connection-event.d.ts} +1 -1
  47. package/lib/{models/events/connections/index.js → cjs/models/events/connections/connection-event.js} +1 -1
  48. package/lib/cjs/models/events/connections/connection-event.js.map +1 -0
  49. package/lib/cjs/models/events/connections/index.d.ts +1 -0
  50. package/lib/cjs/models/events/connections/index.js +5 -0
  51. package/lib/cjs/models/events/connections/index.js.map +1 -0
  52. package/lib/cjs/models/events/index.d.ts +4 -0
  53. package/lib/cjs/models/events/index.js +8 -0
  54. package/lib/cjs/models/events/index.js.map +1 -0
  55. package/lib/{models → cjs/models}/events/message/buttons.d.ts +0 -0
  56. package/lib/{models → cjs/models}/events/message/buttons.js +0 -0
  57. package/lib/cjs/models/events/message/buttons.js.map +1 -0
  58. package/lib/{models → cjs/models}/events/message/carousel.d.ts +1 -1
  59. package/lib/{models → cjs/models}/events/message/carousel.js +0 -0
  60. package/lib/cjs/models/events/message/carousel.js.map +1 -0
  61. package/lib/{models → cjs/models}/events/message/custom.d.ts +2 -2
  62. package/lib/{models → cjs/models}/events/message/custom.js +0 -0
  63. package/lib/cjs/models/events/message/custom.js.map +1 -0
  64. package/lib/cjs/models/events/message/index.d.ts +9 -0
  65. package/lib/cjs/models/events/message/index.js +13 -0
  66. package/lib/cjs/models/events/message/index.js.map +1 -0
  67. package/lib/{models → cjs/models}/events/message/location.d.ts +1 -1
  68. package/lib/{models → cjs/models}/events/message/location.js +0 -0
  69. package/lib/cjs/models/events/message/location.js.map +1 -0
  70. package/lib/{models → cjs/models}/events/message/media.d.ts +1 -1
  71. package/lib/cjs/models/events/message/media.js +11 -0
  72. package/lib/cjs/models/events/message/media.js.map +1 -0
  73. package/lib/{models/events/message/index.d.ts → cjs/models/events/message/message-event.d.ts} +1 -1
  74. package/lib/{models/events/message/index.js → cjs/models/events/message/message-event.js} +1 -1
  75. package/lib/cjs/models/events/message/message-event.js.map +1 -0
  76. package/lib/{models → cjs/models}/events/message/postback.d.ts +1 -1
  77. package/lib/{models → cjs/models}/events/message/postback.js +0 -0
  78. package/lib/cjs/models/events/message/postback.js.map +1 -0
  79. package/lib/{models → cjs/models}/events/message/replies.d.ts +0 -0
  80. package/lib/{models → cjs/models}/events/message/replies.js +0 -0
  81. package/lib/cjs/models/events/message/replies.js.map +1 -0
  82. package/lib/{models → cjs/models}/events/message/text.d.ts +1 -1
  83. package/lib/{models → cjs/models}/events/message/text.js +0 -0
  84. package/lib/cjs/models/events/message/text.js.map +1 -0
  85. package/lib/cjs/models/index.d.ts +3 -0
  86. package/lib/cjs/models/index.js +7 -0
  87. package/lib/cjs/models/index.js.map +1 -0
  88. package/lib/cjs/models/legacy-types.d.ts +207 -0
  89. package/lib/cjs/models/legacy-types.js +60 -0
  90. package/lib/cjs/models/legacy-types.js.map +1 -0
  91. package/lib/{models → cjs/models}/user.d.ts +1 -1
  92. package/lib/{models → cjs/models}/user.js +0 -0
  93. package/lib/{models → cjs/models}/user.js.map +1 -1
  94. package/lib/{output-parser/index.d.ts → cjs/output-parser/botonic-output-parser.d.ts} +1 -1
  95. package/lib/{output-parser/index.js → cjs/output-parser/botonic-output-parser.js} +2 -1
  96. package/lib/cjs/output-parser/botonic-output-parser.js.map +1 -0
  97. package/lib/{output-parser → cjs/output-parser}/factory.d.ts +1 -1
  98. package/lib/{output-parser → cjs/output-parser}/factory.js +7 -8
  99. package/lib/cjs/output-parser/factory.js.map +1 -0
  100. package/lib/cjs/output-parser/index.d.ts +3 -0
  101. package/lib/cjs/output-parser/index.js +7 -0
  102. package/lib/cjs/output-parser/index.js.map +1 -0
  103. package/lib/{output-parser → cjs/output-parser}/parsers.d.ts +3 -9
  104. package/lib/{output-parser → cjs/output-parser}/parsers.js +21 -12
  105. package/lib/cjs/output-parser/parsers.js.map +1 -0
  106. package/lib/cjs/plugins.d.ts +7 -0
  107. package/lib/{plugins.js → cjs/plugins.js} +8 -5
  108. package/lib/cjs/plugins.js.map +1 -0
  109. package/lib/cjs/routing/index.d.ts +2 -0
  110. package/lib/cjs/routing/index.js +6 -0
  111. package/lib/cjs/routing/index.js.map +1 -0
  112. package/lib/cjs/routing/router-utils.d.ts +11 -0
  113. package/lib/cjs/routing/router-utils.js +89 -0
  114. package/lib/cjs/routing/router-utils.js.map +1 -0
  115. package/lib/cjs/routing/router.d.ts +54 -0
  116. package/lib/cjs/routing/router.js +315 -0
  117. package/lib/cjs/routing/router.js.map +1 -0
  118. package/lib/cjs/utils.d.ts +11 -0
  119. package/lib/{utils.js → cjs/utils.js} +14 -5
  120. package/lib/cjs/utils.js.map +1 -0
  121. package/lib/esm/constants.d.ts +4 -0
  122. package/lib/esm/constants.js +5 -0
  123. package/lib/esm/constants.js.map +1 -0
  124. package/lib/esm/core-bot.d.ts +36 -0
  125. package/lib/esm/core-bot.js +111 -0
  126. package/lib/esm/core-bot.js.map +1 -0
  127. package/lib/esm/data-provider/dynamodb-data-provider.d.ts +26 -0
  128. package/lib/esm/data-provider/dynamodb-data-provider.js +77 -0
  129. package/lib/esm/data-provider/dynamodb-data-provider.js.map +1 -0
  130. package/lib/esm/data-provider/dynamodb-utils.d.ts +11 -0
  131. package/lib/esm/data-provider/dynamodb-utils.js +113 -0
  132. package/lib/esm/data-provider/dynamodb-utils.js.map +1 -0
  133. package/lib/esm/data-provider/factory.d.ts +25 -0
  134. package/lib/esm/data-provider/factory.js +27 -0
  135. package/lib/esm/data-provider/factory.js.map +1 -0
  136. package/lib/esm/data-provider/index.d.ts +4 -0
  137. package/lib/esm/data-provider/index.js +5 -0
  138. package/lib/esm/data-provider/index.js.map +1 -0
  139. package/lib/esm/data-provider/local-data-provider.d.ts +26 -0
  140. package/lib/esm/data-provider/local-data-provider.js +133 -0
  141. package/lib/esm/data-provider/local-data-provider.js.map +1 -0
  142. package/lib/esm/debug/index.d.ts +1 -0
  143. package/lib/esm/debug/index.js +2 -0
  144. package/lib/esm/debug/index.js.map +1 -0
  145. package/lib/esm/debug/inspector.d.ts +24 -0
  146. package/lib/esm/debug/inspector.js +65 -0
  147. package/lib/esm/debug/inspector.js.map +1 -0
  148. package/lib/esm/handoff.d.ts +67 -0
  149. package/lib/esm/handoff.js +171 -0
  150. package/lib/esm/handoff.js.map +1 -0
  151. package/lib/esm/hubtype-service.d.ts +69 -0
  152. package/lib/esm/hubtype-service.js +209 -0
  153. package/lib/esm/hubtype-service.js.map +1 -0
  154. package/lib/esm/i18n.d.ts +2 -0
  155. package/lib/esm/i18n.js +7 -0
  156. package/lib/esm/i18n.js.map +1 -0
  157. package/lib/esm/index.d.ts +11 -0
  158. package/lib/esm/index.js +14 -0
  159. package/lib/esm/index.js.map +1 -0
  160. package/lib/esm/models/events/base-event.d.ts +13 -0
  161. package/lib/esm/models/events/base-event.js +8 -0
  162. package/lib/esm/models/events/base-event.js.map +1 -0
  163. package/lib/esm/models/events/botonic-event.d.ts +3 -0
  164. package/lib/esm/models/events/botonic-event.js +2 -0
  165. package/lib/esm/models/events/botonic-event.js.map +1 -0
  166. package/lib/esm/models/events/connections/connection-event.d.ts +9 -0
  167. package/lib/esm/models/events/connections/connection-event.js +6 -0
  168. package/lib/esm/models/events/connections/connection-event.js.map +1 -0
  169. package/lib/esm/models/events/connections/index.d.ts +1 -0
  170. package/lib/esm/models/events/connections/index.js +2 -0
  171. package/lib/esm/models/events/connections/index.js.map +1 -0
  172. package/lib/esm/models/events/index.d.ts +4 -0
  173. package/lib/esm/models/events/index.js +5 -0
  174. package/lib/esm/models/events/index.js.map +1 -0
  175. package/lib/esm/models/events/message/buttons.d.ts +18 -0
  176. package/lib/esm/models/events/message/buttons.js +2 -0
  177. package/lib/esm/models/events/message/buttons.js.map +1 -0
  178. package/lib/esm/models/events/message/carousel.d.ts +11 -0
  179. package/lib/esm/models/events/message/carousel.js +2 -0
  180. package/lib/esm/models/events/message/carousel.js.map +1 -0
  181. package/lib/esm/models/events/message/custom.d.ts +6 -0
  182. package/lib/esm/models/events/message/custom.js +2 -0
  183. package/lib/esm/models/events/message/custom.js.map +1 -0
  184. package/lib/esm/models/events/message/index.d.ts +9 -0
  185. package/lib/esm/models/events/message/index.js +10 -0
  186. package/lib/esm/models/events/message/index.js.map +1 -0
  187. package/lib/esm/models/events/message/location.d.ts +6 -0
  188. package/lib/esm/models/events/message/location.js +2 -0
  189. package/lib/esm/models/events/message/location.js.map +1 -0
  190. package/lib/esm/models/events/message/media.d.ts +18 -0
  191. package/lib/esm/models/events/message/media.js +8 -0
  192. package/lib/esm/models/events/message/media.js.map +1 -0
  193. package/lib/esm/models/events/message/message-event.d.ts +30 -0
  194. package/lib/esm/models/events/message/message-event.js +29 -0
  195. package/lib/esm/models/events/message/message-event.js.map +1 -0
  196. package/lib/esm/models/events/message/postback.d.ts +4 -0
  197. package/lib/esm/models/events/message/postback.js +2 -0
  198. package/lib/esm/models/events/message/postback.js.map +1 -0
  199. package/lib/esm/models/events/message/replies.d.ts +7 -0
  200. package/lib/esm/models/events/message/replies.js +2 -0
  201. package/lib/esm/models/events/message/replies.js.map +1 -0
  202. package/lib/esm/models/events/message/text.d.ts +8 -0
  203. package/lib/esm/models/events/message/text.js +2 -0
  204. package/lib/esm/models/events/message/text.js.map +1 -0
  205. package/lib/esm/models/index.d.ts +3 -0
  206. package/lib/esm/models/index.js +4 -0
  207. package/lib/esm/models/index.js.map +1 -0
  208. package/lib/esm/models/legacy-types.d.ts +207 -0
  209. package/lib/esm/models/legacy-types.js +57 -0
  210. package/lib/esm/models/legacy-types.js.map +1 -0
  211. package/lib/esm/models/user.d.ts +10 -0
  212. package/lib/esm/models/user.js +2 -0
  213. package/lib/esm/models/user.js.map +1 -0
  214. package/lib/esm/output-parser/botonic-output-parser.d.ts +16 -0
  215. package/lib/esm/output-parser/botonic-output-parser.js +33 -0
  216. package/lib/esm/output-parser/botonic-output-parser.js.map +1 -0
  217. package/lib/esm/output-parser/factory.d.ts +4 -0
  218. package/lib/esm/output-parser/factory.js +28 -0
  219. package/lib/esm/output-parser/factory.js.map +1 -0
  220. package/lib/esm/output-parser/index.d.ts +3 -0
  221. package/lib/esm/output-parser/index.js +4 -0
  222. package/lib/esm/output-parser/index.js.map +1 -0
  223. package/lib/esm/output-parser/parsers.d.ts +22 -0
  224. package/lib/esm/output-parser/parsers.js +130 -0
  225. package/lib/esm/output-parser/parsers.js.map +1 -0
  226. package/lib/esm/plugins.d.ts +7 -0
  227. package/lib/esm/plugins.js +42 -0
  228. package/lib/esm/plugins.js.map +1 -0
  229. package/lib/esm/routing/index.d.ts +2 -0
  230. package/lib/esm/routing/index.js +3 -0
  231. package/lib/esm/routing/index.js.map +1 -0
  232. package/lib/esm/routing/router-utils.d.ts +11 -0
  233. package/lib/esm/routing/router-utils.js +79 -0
  234. package/lib/esm/routing/router-utils.js.map +1 -0
  235. package/lib/esm/routing/router.d.ts +54 -0
  236. package/lib/esm/routing/router.js +311 -0
  237. package/lib/esm/routing/router.js.map +1 -0
  238. package/lib/esm/utils.d.ts +11 -0
  239. package/lib/esm/utils.js +78 -0
  240. package/lib/esm/utils.js.map +1 -0
  241. package/package.json +22 -9
  242. package/src/constants.ts +6 -0
  243. package/src/core-bot.ts +213 -0
  244. package/src/data-provider/dynamodb-data-provider.ts +103 -0
  245. package/src/data-provider/dynamodb-utils.ts +149 -0
  246. package/src/data-provider/factory.ts +60 -0
  247. package/src/data-provider/index.ts +4 -0
  248. package/src/data-provider/local-data-provider.ts +154 -0
  249. package/src/{types/debug/index.d.ts → debug/index.ts} +0 -0
  250. package/src/debug/{inspector.js → inspector.ts} +39 -14
  251. package/src/{handoff.js → handoff.ts} +88 -24
  252. package/src/{hubtype-service.js → hubtype-service.ts} +99 -34
  253. package/src/{i18n.js → i18n.ts} +7 -1
  254. package/src/index.ts +13 -0
  255. package/src/models/events/base-event.ts +14 -0
  256. package/src/models/events/botonic-event.ts +24 -0
  257. package/src/models/events/connections/connection-event.ts +11 -0
  258. package/src/models/events/connections/index.ts +1 -11
  259. package/src/models/events/index.ts +4 -39
  260. package/src/models/events/message/carousel.ts +1 -1
  261. package/src/models/events/message/custom.ts +2 -2
  262. package/src/models/events/message/index.ts +9 -37
  263. package/src/models/events/message/location.ts +1 -1
  264. package/src/models/events/message/media.ts +1 -1
  265. package/src/models/events/message/message-event.ts +37 -0
  266. package/src/models/events/message/postback.ts +1 -1
  267. package/src/models/events/message/text.ts +1 -1
  268. package/src/models/index.ts +3 -0
  269. package/src/{types/index.d.ts → models/legacy-types.ts} +141 -80
  270. package/src/models/user.ts +2 -1
  271. package/src/output-parser/botonic-output-parser.ts +38 -0
  272. package/src/output-parser/factory.ts +1 -3
  273. package/src/output-parser/index.ts +3 -37
  274. package/src/output-parser/parsers.ts +27 -17
  275. package/src/{plugins.js → plugins.ts} +21 -12
  276. package/src/routing/index.ts +2 -0
  277. package/src/routing/router-utils.ts +100 -0
  278. package/src/routing/router.ts +409 -0
  279. package/src/{utils.js → utils.ts} +26 -13
  280. package/index.d.ts +0 -1
  281. package/lib/constants.d.ts +0 -15
  282. package/lib/constants.js +0 -19
  283. package/lib/constants.js.map +0 -1
  284. package/lib/core-bot.d.ts +0 -40
  285. package/lib/core-bot.js +0 -78
  286. package/lib/core-bot.js.map +0 -1
  287. package/lib/debug/index.d.ts +0 -1
  288. package/lib/debug/index.js +0 -9
  289. package/lib/debug/index.js.map +0 -1
  290. package/lib/debug/inspector.d.ts +0 -20
  291. package/lib/debug/inspector.js.map +0 -1
  292. package/lib/debug/inspector.test.d.ts +0 -1
  293. package/lib/debug/inspector.test.js +0 -8
  294. package/lib/debug/inspector.test.js.map +0 -1
  295. package/lib/errors.d.ts +0 -4
  296. package/lib/errors.js +0 -11
  297. package/lib/errors.js.map +0 -1
  298. package/lib/handoff.d.ts +0 -34
  299. package/lib/handoff.js.map +0 -1
  300. package/lib/handoff.test.d.ts +0 -1
  301. package/lib/handoff.test.js +0 -62
  302. package/lib/handoff.test.js.map +0 -1
  303. package/lib/hubtype-service.d.ts +0 -58
  304. package/lib/hubtype-service.js.map +0 -1
  305. package/lib/hubtype-service.test.d.ts +0 -1
  306. package/lib/hubtype-service.test.js +0 -21
  307. package/lib/hubtype-service.test.js.map +0 -1
  308. package/lib/i18n.d.ts +0 -1
  309. package/lib/i18n.js.map +0 -1
  310. package/lib/index.d.ts +0 -48
  311. package/lib/index.js +0 -58
  312. package/lib/index.js.map +0 -1
  313. package/lib/models/events/connections/index.js.map +0 -1
  314. package/lib/models/events/index.d.ts +0 -21
  315. package/lib/models/events/index.js.map +0 -1
  316. package/lib/models/events/message/buttons.js.map +0 -1
  317. package/lib/models/events/message/carousel.js.map +0 -1
  318. package/lib/models/events/message/custom.js.map +0 -1
  319. package/lib/models/events/message/index.js.map +0 -1
  320. package/lib/models/events/message/location.js.map +0 -1
  321. package/lib/models/events/message/media.js +0 -11
  322. package/lib/models/events/message/media.js.map +0 -1
  323. package/lib/models/events/message/postback.js.map +0 -1
  324. package/lib/models/events/message/replies.js.map +0 -1
  325. package/lib/models/events/message/text.js.map +0 -1
  326. package/lib/output-parser/factory.js.map +0 -1
  327. package/lib/output-parser/index.js.map +0 -1
  328. package/lib/output-parser/parsers.js.map +0 -1
  329. package/lib/output-parser/util.d.ts +0 -2
  330. package/lib/output-parser/util.js +0 -14
  331. package/lib/output-parser/util.js.map +0 -1
  332. package/lib/plugins.d.ts +0 -2
  333. package/lib/plugins.js.map +0 -1
  334. package/lib/router.d.ts +0 -55
  335. package/lib/router.js +0 -268
  336. package/lib/router.js.map +0 -1
  337. package/lib/router.test.d.ts +0 -1
  338. package/lib/router.test.js +0 -271
  339. package/lib/router.test.js.map +0 -1
  340. package/lib/utils.d.ts +0 -8
  341. package/lib/utils.js.map +0 -1
  342. package/src/constants.js +0 -15
  343. package/src/core-bot.js +0 -108
  344. package/src/debug/index.js +0 -6
  345. package/src/debug/inspector.test.js +0 -6
  346. package/src/errors.js +0 -8
  347. package/src/handoff.test.js +0 -61
  348. package/src/hubtype-service.test.js +0 -23
  349. package/src/index.js +0 -52
  350. package/src/output-parser/util.ts +0 -8
  351. package/src/router.js +0 -273
  352. package/src/router.test.js +0 -345
  353. package/src/types/constants.d.ts +0 -14
  354. package/src/types/core-bot.d.ts +0 -46
  355. package/src/types/debug/inspector.d.ts +0 -33
  356. package/src/types/handoff.d.ts +0 -84
  357. package/src/types/hubtype-service.d.ts +0 -52
  358. package/src/types/i18n.d.ts +0 -8
  359. package/src/types/plugins.d.ts +0 -13
  360. package/src/types/router.d.ts +0 -57
  361. package/src/types/utils.d.ts +0 -12
package/src/router.js DELETED
@@ -1,273 +0,0 @@
1
- import { RouteInspector } from './debug/inspector'
2
- import { NoMatchingRouteError } from './errors'
3
- import { isFunction } from './utils'
4
-
5
- export class Router {
6
- /**
7
- * @param {Route[]} routes
8
- * @param routeInspector
9
- */
10
- constructor(routes, routeInspector = undefined) {
11
- this.routes = routes
12
- this.routeInspector = routeInspector || new RouteInspector()
13
- }
14
-
15
- // eslint-disable-next-line complexity
16
- processInput(input, session = {}, lastRoutePath = null) {
17
- let routeParams = {}
18
- if (input.payload && input.payload.includes('__PATH_PAYLOAD__')) {
19
- const pathParam = input.payload.split('__PATH_PAYLOAD__')
20
- routeParams.route = this.getRouteByPath(pathParam[1], this.routes)
21
- }
22
- const pathParams = this.getOnFinishParams(input)
23
- let brokenFlow = false
24
- const lastRoute = this.getRouteByPath(lastRoutePath, this.routes)
25
- if (!lastRoute && input.path)
26
- routeParams.route = this.getRouteByPath(input.path, this.routes)
27
- if (lastRoute && lastRoute.childRoutes && !routeParams.route)
28
- //get route depending of current ChildRoute
29
- routeParams = this.getRoute(
30
- input,
31
- lastRoute.childRoutes,
32
- session,
33
- lastRoutePath
34
- )
35
- if (!routeParams || !Object.keys(routeParams).length) {
36
- /*
37
- we couldn't find a route in the state of the lastRoute, so let's find in
38
- the general conf.route
39
- */
40
- brokenFlow = Boolean(lastRoutePath)
41
- routeParams = this.getRoute(input, this.routes, session, lastRoutePath)
42
- }
43
- try {
44
- if (pathParams) {
45
- const searchParams = new URLSearchParams(pathParams)
46
- for (const [key, value] of searchParams) {
47
- routeParams.params
48
- ? (routeParams.params[key] = value)
49
- : (routeParams.params = { [key]: value })
50
- }
51
- }
52
- } catch (e) {}
53
- if (routeParams && Object.keys(routeParams).length) {
54
- //get in childRoute if one has path ''
55
- let defaultAction
56
- if (routeParams.route) {
57
- if (
58
- !routeParams.route.path &&
59
- routeParams.route.childRoutes &&
60
- routeParams.route.childRoutes.length
61
- ) {
62
- defaultAction = this.getRoute(
63
- { path: '' },
64
- routeParams.route.childRoutes,
65
- session,
66
- lastRoutePath
67
- )
68
- }
69
- if ('action' in routeParams.route) {
70
- if (
71
- brokenFlow &&
72
- routeParams.route.ignoreRetry != true &&
73
- lastRoute &&
74
- (session.__retries || 0) < (lastRoute.retry || 0) &&
75
- routeParams.route.path != lastRoute.action
76
- ) {
77
- session.__retries = session.__retries ? session.__retries + 1 : 1
78
- // The flow was broken, but we want to recover it
79
- return {
80
- action: routeParams.route.action,
81
- params: routeParams.params,
82
- retryAction: lastRoute ? lastRoute.action : null,
83
- defaultAction: defaultAction ? defaultAction.route.action : null,
84
- lastRoutePath: lastRoutePath,
85
- }
86
- } else {
87
- session.__retries = 0
88
- if (lastRoutePath && !brokenFlow)
89
- lastRoutePath = `${lastRoutePath}/${routeParams.route.path}`
90
- else lastRoutePath = routeParams.route.path
91
- return {
92
- action: routeParams.route.action,
93
- params: routeParams.params,
94
- retryAction: null,
95
- defaultAction: defaultAction ? defaultAction.route.action : null,
96
- lastRoutePath: lastRoutePath,
97
- }
98
- }
99
- } else if (defaultAction) {
100
- return {
101
- action: defaultAction.route.action,
102
- params: defaultAction.params,
103
- lastRoutePath: lastRoutePath,
104
- }
105
- } else if ('redirect' in routeParams.route) {
106
- lastRoutePath = routeParams.route.redirect
107
- const redirectRoute = this.getRouteByPath(lastRoutePath, this.routes)
108
- if (redirectRoute) {
109
- return {
110
- action: redirectRoute.action,
111
- params: redirectRoute.params,
112
- lastRoutePath: lastRoutePath,
113
- }
114
- }
115
- }
116
- }
117
- }
118
- const notFound = this.getRouteByPath('404', this.routes)
119
- if (!notFound) throw new NoMatchingRouteError(input)
120
- if (lastRoute && (session.__retries || 0) < (lastRoute.retry || 0)) {
121
- session.__retries = session.__retries ? session.__retries + 1 : 1
122
- return {
123
- action: notFound.action,
124
- params: {},
125
- retryAction: lastRoute.action,
126
- lastRoutePath: lastRoutePath,
127
- }
128
- } else {
129
- this.lastRoutePath = null
130
- session.__retries = 0
131
- return {
132
- action: notFound.action,
133
- params: {},
134
- retryAction: null,
135
- lastRoutePath: lastRoutePath,
136
- }
137
- }
138
- }
139
-
140
- /**
141
- * @param {Input} input
142
- * @return {string|undefined}
143
- */
144
- getOnFinishParams(input) {
145
- try {
146
- if (!input.payload) {
147
- return undefined
148
- }
149
- const params = input.payload.split('__PATH_PAYLOAD__')
150
- if (params.length < 2) {
151
- return undefined
152
- }
153
- const pathParams = params[1].split('?')
154
- if (pathParams.length > 0) {
155
- input.path = pathParams[0]
156
- delete input.payload
157
- }
158
- if (pathParams.length > 1) {
159
- return pathParams[1]
160
- }
161
- } catch (e) {
162
- console.error('ERROR getOnFinishParams', e)
163
- }
164
- return undefined
165
- }
166
-
167
- /**
168
- * @return {null|RouteParams}
169
- */
170
- getRoute(input, routes, session, lastRoutePath) {
171
- const computedRoutes = isFunction(routes)
172
- ? routes({ input, session, lastRoutePath })
173
- : routes
174
- /* Find the route that matches the given input, if it match with some of the entries,
175
- return the whole Route of the entry with optional params captured if matcher was a regex */
176
- /** @type {{ [key: string]: string }}*/
177
- let params = {}
178
- const route = computedRoutes.find(r =>
179
- Object.entries(r)
180
- .filter(([key, {}]) => key != 'action' && key != 'childRoutes')
181
- .some(([key, value]) => {
182
- const match = this.matchRoute(
183
- r,
184
- key,
185
- value,
186
- input,
187
- session,
188
- lastRoutePath
189
- )
190
- try {
191
- params = match.groups
192
- } catch (e) {}
193
- return Boolean(match)
194
- })
195
- )
196
- if (route) {
197
- return { route, params }
198
- }
199
- return null
200
- }
201
-
202
- /**
203
- * @param {string|null} path
204
- * @param {Route[]?} routeList
205
- * @return {null|Route}
206
- */
207
- getRouteByPath(path, routeList = null) {
208
- if (!path) return null
209
- let route = null
210
- routeList = routeList || this.routes
211
- const [currentPath, ...childPath] = path.split('/')
212
- for (const r of routeList) {
213
- //iterate over all routeList
214
- if (r.path == currentPath) {
215
- route = r
216
- if (r.childRoutes && r.childRoutes.length && childPath.length > 0) {
217
- //evaluate childroute over next actions
218
- route = this.getRouteByPath(childPath.join('/'), r.childRoutes)
219
- if (route) return route
220
- } else if (childPath.length === 0) return route //last action and found route
221
- }
222
- }
223
- return null
224
- }
225
-
226
- /**
227
- * @return {Params|boolean}
228
- */
229
- matchRoute(route, prop, matcher, input, session, lastRoutePath) {
230
- /*
231
- prop: ('text' | 'payload' | 'intent' | 'type' | 'input' | 'session' | 'request' ...)
232
- matcher: (string: exact match | regex: regular expression match | function: return true)
233
- input: user input object, ex: {type: 'text', data: 'Hi'}
234
- */
235
- /** @type {any} */
236
- let value = ''
237
- if (Object.keys(input).indexOf(prop) > -1) value = input[prop]
238
- if (prop === 'text') {
239
- if (input.type === 'text') value = input.data
240
- } else if (prop === 'input') value = input
241
- else if (prop === 'session') value = session
242
- else if (prop === 'request') value = { input, session, lastRoutePath }
243
- const matched = this.matchValue(matcher, value)
244
- if (matched) {
245
- this.routeInspector.routeMatched(route, prop, matcher, value)
246
- } else {
247
- this.routeInspector.routeNotMatched(route, prop, matcher, value)
248
- }
249
- return matched
250
- }
251
-
252
- /**
253
- *
254
- * @return {*|boolean|Params}
255
- */
256
- matchValue(matcher, value) {
257
- if (typeof matcher === 'string') {
258
- // TODO should this be === to avoid matching '' with undefined?
259
- return value == matcher
260
- }
261
- if (matcher instanceof RegExp) {
262
- // check if undefined to avoid conversion to 'undefined'
263
- if (value === undefined) {
264
- return false
265
- }
266
- return matcher.exec(value)
267
- }
268
- if (typeof matcher === 'function') {
269
- return matcher(value)
270
- }
271
- return false
272
- }
273
- }
@@ -1,345 +0,0 @@
1
- import { NoMatchingRouteError } from './errors'
2
- import { PROVIDER } from './index'
3
- import { Router } from './router'
4
-
5
- /** @type Input */
6
- const textInput = { type: 'text', data: 'hi' }
7
- /** @type Input */
8
- const textInputComplex = { type: 'text', data: 'Cömplêx input &% 🚀' }
9
- /** @type Input */
10
- const textPayloadInput = { type: 'text', data: 'hi', payload: 'foo' }
11
- /** @type Input */
12
- const postbackInput = { type: 'postback', payload: 'foo' }
13
-
14
- const requestInput = {
15
- input: textInput,
16
- session: { organization: 'myOrg' },
17
- lastRoutePath: 'initial',
18
- }
19
-
20
- /**
21
- * @return {Route}
22
- */
23
- function testRoute() {
24
- return {}
25
- }
26
-
27
- /**
28
- * @return {Session}
29
- */
30
- function testSession() {
31
- return {
32
- user: { id: 'userid', provider: PROVIDER.DEV },
33
- bot: { id: 'bot_id' },
34
- is_first_interaction: true,
35
- }
36
- }
37
-
38
- describe('Bad router initialization', () => {
39
- test('empty routes throw TypeError', () => {
40
- const router = new Router([])
41
- expect(() => router.processInput(textInput, testSession())).toThrow(
42
- NoMatchingRouteError
43
- )
44
- })
45
- test('null routes throw TypeError', () => {
46
- // @ts-ignore
47
- const router = new Router()
48
- expect(() => router.processInput(textInput, testSession())).toThrow(
49
- TypeError
50
- )
51
- })
52
- })
53
-
54
- test('Router returns 404', () => {
55
- const router = new Router([{ path: '404', action: '404Action' }])
56
- const { action } = router.processInput(textInput, testSession())
57
- expect(action).toBe('404Action')
58
- })
59
-
60
- describe('Match route by MATCHER <> INPUT', () => {
61
- const router = new Router([])
62
- const matchTextProp = (matcher, textInput) =>
63
- router.matchRoute(testRoute(), 'text', matcher, textInput, testSession())
64
- const matchPayloadProp = (matcher, payload) =>
65
- router.matchRoute(testRoute(), 'payload', matcher, payload, testSession())
66
- const matchRequestProp = (matcher, request) =>
67
- router.matchRoute(
68
- testRoute(),
69
- 'request',
70
- matcher,
71
- request.input,
72
- request.session,
73
- request.lastRoutePath
74
- )
75
- test('text <> text', () => {
76
- expect(matchTextProp('hi', textInput)).toBeTruthy()
77
- expect(matchTextProp('hii', textInput)).toBeFalsy()
78
- expect(matchTextProp('bye', textInput)).toBeFalsy()
79
- expect(matchTextProp('', textInput)).toBeFalsy()
80
- expect(matchTextProp(null, textInput)).toBeFalsy()
81
- expect(matchTextProp('Cömplêx input &% 🚀', textInputComplex)).toBeTruthy()
82
- expect(matchTextProp(' Cömplêx input &% 🚀', textInputComplex)).toBeFalsy() // has a space at the beginning
83
- })
84
- test('regex <> text', () => {
85
- expect(matchTextProp(/hi/, textInput)).toBeTruthy()
86
- expect(matchTextProp(/bye/, textInput)).toBeFalsy()
87
- expect(matchTextProp(/🚀/, textInputComplex)).toBeTruthy()
88
- expect(matchTextProp(/complex/, textInputComplex)).toBeFalsy()
89
- })
90
- test('function <> text', () => {
91
- expect(matchTextProp(v => v.startsWith('hi'), textInput)).toBeTruthy()
92
- expect(matchTextProp(v => !v.startsWith('hi'), textInput)).toBeFalsy()
93
- })
94
- test('input <> text', () => {
95
- expect(
96
- router.matchRoute(
97
- testRoute(),
98
- 'input',
99
- i => i.data.startsWith('hi'),
100
- textInput,
101
- testSession()
102
- )
103
- ).toBeTruthy()
104
- expect(
105
- router.matchRoute(
106
- testRoute(),
107
- 'input',
108
- i => !i.data.startsWith('hi'),
109
- textInput,
110
- testSession()
111
- )
112
- ).toBeFalsy()
113
- })
114
- test('text <> text payload', () => {
115
- expect(matchPayloadProp('foo', textPayloadInput)).toBeTruthy()
116
- expect(matchPayloadProp('fooo', textPayloadInput)).toBeFalsy()
117
- expect(matchPayloadProp('bar', textPayloadInput)).toBeFalsy()
118
- expect(matchPayloadProp('', textPayloadInput)).toBeFalsy()
119
- expect(matchPayloadProp(null, textPayloadInput)).toBeFalsy()
120
- })
121
- test('regex <> text payload', () => {
122
- expect(matchPayloadProp(/foo/, textPayloadInput)).toBeTruthy()
123
- expect(matchPayloadProp(/bar/, textPayloadInput)).toBeFalsy()
124
- })
125
- test('function <> text payload', () => {
126
- expect(
127
- matchPayloadProp(v => v.startsWith('fo'), textPayloadInput)
128
- ).toBeTruthy()
129
- expect(
130
- matchPayloadProp(v => !v.startsWith('fo'), textPayloadInput)
131
- ).toBeFalsy()
132
- })
133
- test('text <> postback', () => {
134
- expect(matchPayloadProp('foo', postbackInput)).toBeTruthy()
135
- expect(matchPayloadProp('fooo', postbackInput)).toBeFalsy()
136
- expect(matchPayloadProp('bar', postbackInput)).toBeFalsy()
137
- expect(matchPayloadProp('', postbackInput)).toBeFalsy()
138
- expect(matchPayloadProp(null, postbackInput)).toBeFalsy()
139
- })
140
- test('regex <> postback', () => {
141
- expect(matchPayloadProp(/foo/, postbackInput)).toBeTruthy()
142
- expect(matchPayloadProp(/bar/, postbackInput)).toBeFalsy()
143
- })
144
- test('function <> postback', () => {
145
- expect(
146
- matchPayloadProp(v => v.startsWith('fo'), postbackInput)
147
- ).toBeTruthy()
148
- expect(
149
- matchPayloadProp(v => !v.startsWith('fo'), postbackInput)
150
- ).toBeFalsy()
151
- })
152
- test('function <> request', () => {
153
- expect(
154
- matchRequestProp(
155
- request =>
156
- request.input.data === 'hi' &&
157
- request.session.organization === 'myOrg' &&
158
- request.lastRoutePath === 'initial',
159
- requestInput
160
- )
161
- ).toBeTruthy()
162
- expect(
163
- matchRequestProp(
164
- request =>
165
- request.input.data === 'hello' &&
166
- request.session.organization === 'myOrg' &&
167
- request.lastRoutePath === 'initial',
168
- requestInput
169
- )
170
- ).toBeFalsy()
171
- })
172
- })
173
-
174
- describe('Get route by path', () => {
175
- const externalRoutes = [
176
- { path: '', action: 'DefaultAction' },
177
- { path: 'child', action: 'ChildAction' },
178
- ]
179
- const router = new Router([
180
- { path: 'initial', action: 'Initial' },
181
- {
182
- path: 'flow-1',
183
- action: 'Flow1',
184
- childRoutes: [
185
- {
186
- path: '1',
187
- action: 'Flow1.1',
188
- childRoutes: [
189
- { path: '1', action: 'Flow1.1.1' },
190
- { path: '2', action: 'Flow1.1.2' },
191
- { path: '3', action: 'Flow1.1.3' },
192
- ],
193
- },
194
- { path: '2', action: 'Flow1.2', childRoutes: externalRoutes },
195
- {
196
- path: '3',
197
- action: 'Flow1.3',
198
- childRoutes: [
199
- { path: '1', action: 'Flow1.3.1' },
200
- { path: '2', action: 'Flow1.3.2' },
201
- { path: '3', action: 'Flow1.3.3' },
202
- ],
203
- },
204
- ],
205
- },
206
- { path: '404', action: '404Action' },
207
- ])
208
- function getRouteActionByPath(path) {
209
- // @ts-ignore
210
- return router.getRouteByPath(path).action
211
- }
212
- test('path exists', () => {
213
- expect(getRouteActionByPath('initial')).toBe('Initial')
214
- expect(getRouteActionByPath('flow-1/1')).toBe('Flow1.1')
215
- expect(getRouteActionByPath('flow-1/3/2')).toBe('Flow1.3.2')
216
- })
217
- test('path exists in composed child routes', () => {
218
- expect(getRouteActionByPath('flow-1/2')).toBe('Flow1.2')
219
- expect(getRouteActionByPath('flow-1/2/child')).toBe('ChildAction')
220
- })
221
- test('path does not exist', () => {
222
- expect(router.getRouteByPath('')).toBeNull()
223
- expect(router.getRouteByPath('foo')).toBeNull()
224
- expect(router.getRouteByPath('flow-1/3/2/6')).toBeNull()
225
- })
226
- })
227
-
228
- describe('Process input (v<0.9)', () => {
229
- const externalRoutes = [
230
- { path: '', action: 'DefaultAction' },
231
- { path: 'child', action: 'ChildAction' },
232
- ]
233
- const router = new Router([
234
- { path: 'help', payload: 'help', action: 'Help' },
235
- {
236
- path: 'initial',
237
- intent: /greeting/,
238
- action: 'Flow1',
239
- childRoutes: [
240
- {
241
- path: '1',
242
- payload: '1',
243
- action: 'Flow1.1',
244
- childRoutes: [
245
- { path: '1', payload: '1', action: 'Flow1.1.1' },
246
- { path: '2', payload: '2', action: 'Flow1.1.2' },
247
- { path: '3', payload: '3', action: 'Flow1.1.3' },
248
- ],
249
- },
250
- {
251
- path: '2',
252
- payload: '2',
253
- action: 'Flow1.2',
254
- childRoutes: externalRoutes,
255
- },
256
- {
257
- path: '3',
258
- payload: '3',
259
- action: 'Flow1.3',
260
- childRoutes: [
261
- { path: '1', payload: '1', action: 'Flow1.3.1' },
262
- { path: '2', payload: '2', action: 'Flow1.3.2' },
263
- { path: '3', payload: '3', action: 'Flow1.3.3' },
264
- ],
265
- },
266
- ],
267
- },
268
- { path: '404', action: '404Action' },
269
- ])
270
- test('text input, root level route', () => {
271
- /** @type Input */
272
- const input = { type: 'text', data: 'hi', intent: 'greeting' }
273
- const lastRoutePath = null
274
- expect(
275
- router.processInput(input, testSession(), lastRoutePath).action
276
- ).toBe('Flow1')
277
- })
278
- test('payload input, 2nd level route', () => {
279
- /** @type Input */
280
- const input = { type: 'postback', payload: '2' }
281
- const lastRoutePath = 'initial'
282
- expect(
283
- router.processInput(input, testSession(), lastRoutePath).action
284
- ).toBe('Flow1.2')
285
- })
286
- test('old protocol:path payload input, root level route', () => {
287
- /** @type Input */
288
- const input = { type: 'postback', payload: '__PATH_PAYLOAD__initial' }
289
- const lastRoutePath = ''
290
- expect(
291
- router.processInput(input, testSession(), lastRoutePath).action
292
- ).toBe('Flow1')
293
- })
294
- test('old protocol:path payload input, root level route with composed path', () => {
295
- /** @type Input */
296
- const input = { type: 'postback', path: 'initial/2' }
297
- const lastRoutePath = ''
298
- expect(
299
- router.processInput(input, testSession(), lastRoutePath).action
300
- ).toBe('Flow1.2')
301
- })
302
- test('old protocol: path payload input, 2nd level route with lastRoutePath', () => {
303
- /** @type Input */
304
- const input = { type: 'postback', payload: '__PATH_PAYLOAD__2' }
305
- const lastRoutePath = 'initial'
306
- expect(
307
- router.processInput(input, testSession(), lastRoutePath).action
308
- ).toBe('Flow1.2')
309
- })
310
- test('path payload input with deprecated protocol, root level route', () => {
311
- /** @type Input */
312
- const input = { type: 'postback', payload: '__PATH_PAYLOAD__initial/2' }
313
- const lastRoutePath = ''
314
- expect(
315
- router.processInput(input, testSession(), lastRoutePath).action
316
- ).toBe('Flow1.2')
317
- })
318
- test('old protocol:path payload input with deprecated protocol, 2nd level route', () => {
319
- /** @type Input */
320
- const input = { type: 'postback', payload: '__PATH_PAYLOAD__initial/2' }
321
- const lastRoutePath = 'initial'
322
- expect(
323
- router.processInput(input, testSession(), lastRoutePath).action
324
- ).toBe('Flow1.2')
325
- })
326
- })
327
-
328
- test.each([
329
- [undefined, undefined, undefined],
330
- ['', undefined, undefined],
331
- ['bad_input', undefined, undefined],
332
- ['__PATH_PAYLOAD__', '', undefined],
333
- ['xx__PATH_PAYLOAD__path1', 'path1', undefined],
334
- ['xx__PATH_PAYLOAD__path1?path1', 'path1', 'path1'],
335
- ])(
336
- 'getOnFinishParams(%s)=>%s',
337
- (inputPayload, expectedPath, expectedParams) => {
338
- const router = new Router([])
339
- /** @type Input */
340
- const input = { type: 'postback', payload: inputPayload }
341
- expect(router.getOnFinishParams(input)).toEqual(expectedParams)
342
- expect(input.path).toEqual(expectedPath)
343
- expect(input.payload).toEqual(input.path ? undefined : input.payload)
344
- }
345
- )
@@ -1,14 +0,0 @@
1
- export const Providers: Readonly<{
2
- Messaging: {
3
- FACEBOOK: 'facebook'
4
- GENERIC: 'generic'
5
- IMBEE: 'imbee'
6
- INTERCOM: 'intercom'
7
- SMOOCH: 'smooch'
8
- TELEGRAM: 'telegram'
9
- TWITTER: 'twitter'
10
- WEBCHAT: 'webchat'
11
- WECHAT: 'wechat'
12
- WHATSAPP: 'whatsapp'
13
- }
14
- }>
@@ -1,46 +0,0 @@
1
- import {
2
- BotRequest,
3
- BotResponse,
4
- Locales,
5
- Plugin,
6
- PluginConfig,
7
- Routes,
8
- Session,
9
- } from '.'
10
- import { Inspector } from './debug'
11
- import { Router } from './router'
12
-
13
- export interface BotOptions {
14
- /** The plugin configurations */
15
- appId?: string
16
- defaultDelay?: number
17
- defaultRoutes?: Routes
18
- defaultTyping?: number
19
- inspector?: Inspector
20
- locales: Locales
21
- plugins?: PluginConfig<any>
22
- renderer: any
23
- routes: Routes
24
- theme?: any
25
- }
26
-
27
- export declare class CoreBot {
28
- constructor(options: BotOptions)
29
-
30
- appId?: string
31
- defaultDelay?: number
32
- defaultRoutes?: Routes
33
- defaultTyping?: number
34
- inspector?: Inspector
35
- locales: Locales
36
- plugins?: Record<string, Plugin>
37
- renderer: any
38
- rootElement: any
39
- router: Router
40
- routes: Routes
41
- theme?: any
42
-
43
- getString(stringID: string, session: Session): string
44
- setLocale(locale: string, session: Session): void
45
- input(request: BotRequest): Promise<BotResponse>
46
- }