@botonic/core 1.0.0-dev.0 → 2.23.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +226 -0
- package/README.md +7 -9
- package/package.json +19 -57
- package/src/index.d.ts +4 -0
- package/src/index.js +5 -0
- package/src/index.js.map +1 -0
- package/src/lib/core-bot/index.d.ts +33 -0
- package/src/lib/core-bot/index.js +113 -0
- package/src/lib/core-bot/index.js.map +1 -0
- package/src/lib/hubtype-service/handoff.d.ts +4 -0
- package/src/lib/hubtype-service/handoff.js +32 -0
- package/src/lib/hubtype-service/handoff.js.map +1 -0
- package/src/lib/hubtype-service/index.d.ts +10 -0
- package/src/lib/hubtype-service/index.js +96 -0
- package/src/lib/hubtype-service/index.js.map +1 -0
- package/src/lib/messages/button.d.ts +8 -0
- package/src/lib/messages/button.js +10 -0
- package/src/lib/messages/button.js.map +1 -0
- package/src/lib/plugins/index.d.ts +2 -0
- package/src/lib/plugins/index.js +23 -0
- package/src/lib/plugins/index.js.map +1 -0
- package/src/lib/routes/index.d.ts +0 -0
- package/src/lib/routes/index.js +2 -0
- package/src/lib/routes/index.js.map +1 -0
- package/src/lib/routes/router.d.ts +14 -0
- package/src/lib/routes/router.js +68 -0
- package/src/lib/routes/router.js.map +1 -0
- package/src/lib/routes/types.d.ts +15 -0
- package/src/lib/routes/types.js +2 -0
- package/src/lib/routes/types.js.map +1 -0
- package/src/lib/utils/index.d.ts +7 -0
- package/{lib/esm/utils.js → src/lib/utils/index.js} +6 -34
- package/src/lib/utils/index.js.map +1 -0
- package/lib/cjs/constants.d.ts +0 -15
- package/lib/cjs/constants.js +0 -19
- package/lib/cjs/constants.js.map +0 -1
- package/lib/cjs/core-bot.d.ts +0 -33
- package/lib/cjs/core-bot.js +0 -85
- package/lib/cjs/core-bot.js.map +0 -1
- package/lib/cjs/debug/index.d.ts +0 -1
- package/lib/cjs/debug/index.js +0 -9
- package/lib/cjs/debug/index.js.map +0 -1
- package/lib/cjs/debug/inspector.d.ts +0 -24
- package/lib/cjs/debug/inspector.js +0 -72
- package/lib/cjs/debug/inspector.js.map +0 -1
- 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/handoff.d.ts +0 -67
- package/lib/cjs/handoff.js +0 -183
- package/lib/cjs/handoff.js.map +0 -1
- package/lib/cjs/hubtype-service.d.ts +0 -69
- package/lib/cjs/hubtype-service.js +0 -190
- package/lib/cjs/hubtype-service.js.map +0 -1
- package/lib/cjs/i18n.d.ts +0 -2
- package/lib/cjs/i18n.js +0 -11
- package/lib/cjs/i18n.js.map +0 -1
- package/lib/cjs/index.d.ts +0 -166
- package/lib/cjs/index.js +0 -58
- package/lib/cjs/index.js.map +0 -1
- package/lib/cjs/models/events/connections/index.d.ts +0 -9
- package/lib/cjs/models/events/connections/index.js +0 -9
- package/lib/cjs/models/events/connections/index.js.map +0 -1
- package/lib/cjs/models/events/index.d.ts +0 -21
- package/lib/cjs/models/events/index.js +0 -11
- package/lib/cjs/models/events/index.js.map +0 -1
- package/lib/cjs/models/events/message/buttons.d.ts +0 -18
- package/lib/cjs/models/events/message/buttons.js +0 -3
- package/lib/cjs/models/events/message/buttons.js.map +0 -1
- package/lib/cjs/models/events/message/carousel.d.ts +0 -11
- package/lib/cjs/models/events/message/carousel.js +0 -3
- package/lib/cjs/models/events/message/carousel.js.map +0 -1
- package/lib/cjs/models/events/message/custom.d.ts +0 -6
- package/lib/cjs/models/events/message/custom.js +0 -3
- package/lib/cjs/models/events/message/custom.js.map +0 -1
- package/lib/cjs/models/events/message/index.d.ts +0 -30
- package/lib/cjs/models/events/message/index.js +0 -32
- package/lib/cjs/models/events/message/index.js.map +0 -1
- package/lib/cjs/models/events/message/location.d.ts +0 -6
- package/lib/cjs/models/events/message/location.js +0 -3
- package/lib/cjs/models/events/message/location.js.map +0 -1
- package/lib/cjs/models/events/message/media.d.ts +0 -18
- package/lib/cjs/models/events/message/media.js +0 -11
- package/lib/cjs/models/events/message/media.js.map +0 -1
- package/lib/cjs/models/events/message/postback.d.ts +0 -4
- package/lib/cjs/models/events/message/postback.js +0 -3
- package/lib/cjs/models/events/message/postback.js.map +0 -1
- package/lib/cjs/models/events/message/replies.d.ts +0 -7
- package/lib/cjs/models/events/message/replies.js +0 -3
- package/lib/cjs/models/events/message/replies.js.map +0 -1
- package/lib/cjs/models/events/message/text.d.ts +0 -8
- package/lib/cjs/models/events/message/text.js +0 -3
- package/lib/cjs/models/events/message/text.js.map +0 -1
- package/lib/cjs/models/user.d.ts +0 -10
- package/lib/cjs/models/user.js +0 -3
- package/lib/cjs/models/user.js.map +0 -1
- package/lib/cjs/output-parser/factory.d.ts +0 -4
- package/lib/cjs/output-parser/factory.js +0 -33
- package/lib/cjs/output-parser/factory.js.map +0 -1
- package/lib/cjs/output-parser/index.d.ts +0 -16
- package/lib/cjs/output-parser/index.js +0 -37
- package/lib/cjs/output-parser/index.js.map +0 -1
- package/lib/cjs/output-parser/parsers.d.ts +0 -28
- package/lib/cjs/output-parser/parsers.js +0 -138
- package/lib/cjs/output-parser/parsers.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/plugins.d.ts +0 -7
- package/lib/cjs/plugins.js +0 -45
- package/lib/cjs/plugins.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/cjs/utils.d.ts +0 -10
- package/lib/cjs/utils.js +0 -84
- package/lib/cjs/utils.js.map +0 -1
- package/lib/esm/constants.d.ts +0 -15
- package/lib/esm/constants.js +0 -16
- package/lib/esm/constants.js.map +0 -1
- package/lib/esm/core-bot.d.ts +0 -33
- package/lib/esm/core-bot.js +0 -81
- package/lib/esm/core-bot.js.map +0 -1
- package/lib/esm/debug/index.d.ts +0 -1
- package/lib/esm/debug/index.js +0 -2
- package/lib/esm/debug/index.js.map +0 -1
- package/lib/esm/debug/inspector.d.ts +0 -24
- package/lib/esm/debug/inspector.js +0 -65
- package/lib/esm/debug/inspector.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/handoff.d.ts +0 -67
- package/lib/esm/handoff.js +0 -170
- package/lib/esm/handoff.js.map +0 -1
- package/lib/esm/hubtype-service.d.ts +0 -69
- package/lib/esm/hubtype-service.js +0 -185
- package/lib/esm/hubtype-service.js.map +0 -1
- package/lib/esm/i18n.d.ts +0 -2
- package/lib/esm/i18n.js +0 -7
- package/lib/esm/i18n.js.map +0 -1
- package/lib/esm/index.d.ts +0 -166
- package/lib/esm/index.js +0 -49
- package/lib/esm/index.js.map +0 -1
- package/lib/esm/models/events/connections/index.d.ts +0 -9
- package/lib/esm/models/events/connections/index.js +0 -6
- package/lib/esm/models/events/connections/index.js.map +0 -1
- package/lib/esm/models/events/index.d.ts +0 -21
- package/lib/esm/models/events/index.js +0 -8
- package/lib/esm/models/events/index.js.map +0 -1
- package/lib/esm/models/events/message/buttons.d.ts +0 -18
- package/lib/esm/models/events/message/buttons.js +0 -2
- package/lib/esm/models/events/message/buttons.js.map +0 -1
- package/lib/esm/models/events/message/carousel.d.ts +0 -11
- package/lib/esm/models/events/message/carousel.js +0 -2
- package/lib/esm/models/events/message/carousel.js.map +0 -1
- package/lib/esm/models/events/message/custom.d.ts +0 -6
- package/lib/esm/models/events/message/custom.js +0 -2
- package/lib/esm/models/events/message/custom.js.map +0 -1
- package/lib/esm/models/events/message/index.d.ts +0 -30
- package/lib/esm/models/events/message/index.js +0 -29
- package/lib/esm/models/events/message/index.js.map +0 -1
- package/lib/esm/models/events/message/location.d.ts +0 -6
- package/lib/esm/models/events/message/location.js +0 -2
- package/lib/esm/models/events/message/location.js.map +0 -1
- package/lib/esm/models/events/message/media.d.ts +0 -18
- package/lib/esm/models/events/message/media.js +0 -8
- package/lib/esm/models/events/message/media.js.map +0 -1
- package/lib/esm/models/events/message/postback.d.ts +0 -4
- package/lib/esm/models/events/message/postback.js +0 -2
- package/lib/esm/models/events/message/postback.js.map +0 -1
- package/lib/esm/models/events/message/replies.d.ts +0 -7
- package/lib/esm/models/events/message/replies.js +0 -2
- package/lib/esm/models/events/message/replies.js.map +0 -1
- package/lib/esm/models/events/message/text.d.ts +0 -8
- package/lib/esm/models/events/message/text.js +0 -2
- package/lib/esm/models/events/message/text.js.map +0 -1
- package/lib/esm/models/user.d.ts +0 -10
- package/lib/esm/models/user.js +0 -2
- package/lib/esm/models/user.js.map +0 -1
- package/lib/esm/output-parser/factory.d.ts +0 -4
- package/lib/esm/output-parser/factory.js +0 -29
- package/lib/esm/output-parser/factory.js.map +0 -1
- package/lib/esm/output-parser/index.d.ts +0 -16
- package/lib/esm/output-parser/index.js +0 -33
- package/lib/esm/output-parser/index.js.map +0 -1
- package/lib/esm/output-parser/parsers.d.ts +0 -28
- package/lib/esm/output-parser/parsers.js +0 -123
- package/lib/esm/output-parser/parsers.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/plugins.d.ts +0 -7
- package/lib/esm/plugins.js +0 -40
- package/lib/esm/plugins.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/lib/esm/utils.d.ts +0 -10
- package/lib/esm/utils.js.map +0 -1
- package/src/constants.ts +0 -15
- package/src/core-bot.ts +0 -144
- package/src/debug/index.ts +0 -6
- package/src/debug/inspector.ts +0 -102
- package/src/errors.ts +0 -11
- package/src/handoff.ts +0 -268
- package/src/hubtype-service.ts +0 -266
- package/src/i18n.ts +0 -12
- package/src/index.ts +0 -233
- package/src/models/events/connections/index.ts +0 -11
- package/src/models/events/index.ts +0 -39
- package/src/models/events/message/buttons.ts +0 -23
- package/src/models/events/message/carousel.ts +0 -13
- package/src/models/events/message/custom.ts +0 -7
- package/src/models/events/message/index.ts +0 -37
- package/src/models/events/message/location.ts +0 -7
- package/src/models/events/message/media.ts +0 -28
- package/src/models/events/message/postback.ts +0 -5
- package/src/models/events/message/replies.ts +0 -8
- package/src/models/events/message/text.ts +0 -12
- package/src/models/user.ts +0 -10
- package/src/output-parser/factory.ts +0 -40
- package/src/output-parser/index.ts +0 -38
- package/src/output-parser/parsers.ts +0 -190
- package/src/output-parser/util.ts +0 -8
- package/src/plugins.ts +0 -49
- package/src/router.ts +0 -316
- package/src/utils.ts +0 -84
|
@@ -1,190 +0,0 @@
|
|
|
1
|
-
import { EventTypes } from '../models/events'
|
|
2
|
-
import { BotonicMessageEvent } from '../models/events/message'
|
|
3
|
-
import { Button, WithButtons } from '../models/events/message/buttons'
|
|
4
|
-
import {
|
|
5
|
-
CarouselElement,
|
|
6
|
-
CarouselMessageEvent,
|
|
7
|
-
} from '../models/events/message/carousel'
|
|
8
|
-
import { CustomMessageEvent } from '../models/events/message/custom'
|
|
9
|
-
import { LocationMessageEvent } from '../models/events/message/location'
|
|
10
|
-
import {
|
|
11
|
-
AudioMessageEvent,
|
|
12
|
-
DocumentMessageEvent,
|
|
13
|
-
ImageMessageEvent,
|
|
14
|
-
VideoMessageEvent,
|
|
15
|
-
} from '../models/events/message/media'
|
|
16
|
-
import { PostbackMessageEvent } from '../models/events/message/postback'
|
|
17
|
-
import { Reply, WithReplies } from '../models/events/message/replies'
|
|
18
|
-
import { TextMessageEvent } from '../models/events/message/text'
|
|
19
|
-
import { TEXT_NODE_NAME } from '.'
|
|
20
|
-
import { parseBoolean, parseNumber } from './util'
|
|
21
|
-
|
|
22
|
-
export type ParseFunction<Out> = (args: {
|
|
23
|
-
toParse: any
|
|
24
|
-
parsed?: any
|
|
25
|
-
}) => {
|
|
26
|
-
toParse: any
|
|
27
|
-
parsed: Partial<Out>
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
// COMMON
|
|
31
|
-
export const parseMessage: ParseFunction<BotonicMessageEvent> = args => {
|
|
32
|
-
const typingAndDelay = {}
|
|
33
|
-
if (args.toParse.delay !== undefined) {
|
|
34
|
-
typingAndDelay['delay'] = parseNumber(args.toParse.delay)
|
|
35
|
-
}
|
|
36
|
-
if (args.toParse.typing !== undefined) {
|
|
37
|
-
typingAndDelay['typing'] = parseNumber(args.toParse.typing)
|
|
38
|
-
}
|
|
39
|
-
return {
|
|
40
|
-
toParse: args.toParse,
|
|
41
|
-
parsed: {
|
|
42
|
-
// Following properties added later before saving event: eventId, userId, createdAt, from, ack
|
|
43
|
-
eventType: EventTypes.MESSAGE,
|
|
44
|
-
type: args.toParse.type,
|
|
45
|
-
...typingAndDelay,
|
|
46
|
-
},
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
// BUTTONS
|
|
51
|
-
export const parseButton = (button: any): Button => {
|
|
52
|
-
const title = button[TEXT_NODE_NAME]
|
|
53
|
-
if ('payload' in button) {
|
|
54
|
-
return { title, payload: button.payload }
|
|
55
|
-
}
|
|
56
|
-
if ('url' in button) {
|
|
57
|
-
const b = { title, url: button.url, target: button.target }
|
|
58
|
-
if (button.target) b.target = button.target
|
|
59
|
-
return b
|
|
60
|
-
}
|
|
61
|
-
if ('webview' in button) {
|
|
62
|
-
return {
|
|
63
|
-
title,
|
|
64
|
-
webview: button.webview,
|
|
65
|
-
params: button.params,
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
throw new Error('Invalid parsed Button')
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
export const parseButtons: ParseFunction<WithButtons> = args => {
|
|
72
|
-
const hasButtons = args.toParse?.button?.length > 0
|
|
73
|
-
let buttons: Button[] = []
|
|
74
|
-
if (hasButtons) {
|
|
75
|
-
buttons = args.toParse.button.map(button => parseButton(button))
|
|
76
|
-
}
|
|
77
|
-
return {
|
|
78
|
-
toParse: args.toParse,
|
|
79
|
-
parsed: {
|
|
80
|
-
...args.parsed,
|
|
81
|
-
buttons,
|
|
82
|
-
},
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
// REPLIES
|
|
87
|
-
export const parseReply = (reply: any): Reply => ({
|
|
88
|
-
title: reply[TEXT_NODE_NAME],
|
|
89
|
-
payload: reply.payload,
|
|
90
|
-
})
|
|
91
|
-
|
|
92
|
-
export const parseReplies: ParseFunction<WithReplies> = args => {
|
|
93
|
-
const hasReplies = args.toParse?.reply?.length > 0
|
|
94
|
-
let replies: Reply[] = []
|
|
95
|
-
if (hasReplies) {
|
|
96
|
-
replies = args.toParse.reply.map(reply => parseReply(reply))
|
|
97
|
-
}
|
|
98
|
-
return {
|
|
99
|
-
toParse: args.toParse,
|
|
100
|
-
parsed: {
|
|
101
|
-
...args.parsed,
|
|
102
|
-
replies,
|
|
103
|
-
},
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
// TEXT
|
|
108
|
-
export const parseText: ParseFunction<TextMessageEvent> = args => {
|
|
109
|
-
return {
|
|
110
|
-
toParse: args.toParse,
|
|
111
|
-
parsed: {
|
|
112
|
-
...args.parsed,
|
|
113
|
-
text: args.toParse[TEXT_NODE_NAME] || args.toParse.data,
|
|
114
|
-
markdown: parseBoolean(args.toParse.markdown),
|
|
115
|
-
},
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
// POSTBACK
|
|
120
|
-
|
|
121
|
-
export const parsePostback: ParseFunction<PostbackMessageEvent> = args => {
|
|
122
|
-
return {
|
|
123
|
-
toParse: args.toParse,
|
|
124
|
-
parsed: {
|
|
125
|
-
...args.parsed,
|
|
126
|
-
payload: args.toParse.payload,
|
|
127
|
-
},
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
// MEDIA
|
|
132
|
-
export const parseMedia: ParseFunction<
|
|
133
|
-
| AudioMessageEvent
|
|
134
|
-
| DocumentMessageEvent
|
|
135
|
-
| ImageMessageEvent
|
|
136
|
-
| VideoMessageEvent
|
|
137
|
-
> = args => {
|
|
138
|
-
return {
|
|
139
|
-
toParse: args.toParse,
|
|
140
|
-
parsed: {
|
|
141
|
-
...args.parsed,
|
|
142
|
-
src: args.toParse.src || args.toParse.data,
|
|
143
|
-
},
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
// LOCATION
|
|
148
|
-
export const parseLocation: ParseFunction<LocationMessageEvent> = args => {
|
|
149
|
-
return {
|
|
150
|
-
toParse: args.toParse,
|
|
151
|
-
parsed: {
|
|
152
|
-
...args.parsed,
|
|
153
|
-
lat: args.toParse.lat,
|
|
154
|
-
long: args.toParse.long,
|
|
155
|
-
},
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
// CAROUSEL
|
|
160
|
-
export const parseElement = (element: any): CarouselElement => {
|
|
161
|
-
const e: CarouselElement = {
|
|
162
|
-
pic: element.pic,
|
|
163
|
-
title: element.title,
|
|
164
|
-
subtitle: element.desc,
|
|
165
|
-
}
|
|
166
|
-
const hasButtons = element.button !== undefined
|
|
167
|
-
if (hasButtons) e.buttons = element.button.map(b => parseButton(b))
|
|
168
|
-
return e
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
export const parseCarousel: ParseFunction<CarouselMessageEvent> = args => {
|
|
172
|
-
return {
|
|
173
|
-
toParse: args.toParse,
|
|
174
|
-
parsed: {
|
|
175
|
-
...args.parsed,
|
|
176
|
-
elements: args.toParse.element.map(element => parseElement(element)),
|
|
177
|
-
},
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
// CUSTOM
|
|
182
|
-
export const parseCustom: ParseFunction<CustomMessageEvent> = args => {
|
|
183
|
-
return {
|
|
184
|
-
toParse: args.toParse,
|
|
185
|
-
parsed: {
|
|
186
|
-
...args.parsed,
|
|
187
|
-
customTypeName: JSON.parse(args.toParse.json).customTypeName,
|
|
188
|
-
},
|
|
189
|
-
}
|
|
190
|
-
}
|
package/src/plugins.ts
DELETED
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import { Input, PluginConfig, Session } from './index'
|
|
2
|
-
import { BotonicEvent } from './models/events'
|
|
3
|
-
|
|
4
|
-
type PluginMode = 'pre' | 'post'
|
|
5
|
-
|
|
6
|
-
export function loadPlugins(plugins: PluginConfig<any>[]): any {
|
|
7
|
-
if (!plugins) return []
|
|
8
|
-
const _plugins = {}
|
|
9
|
-
const pluginsLength = plugins.length
|
|
10
|
-
for (let i = 0; i < pluginsLength; i++) {
|
|
11
|
-
const pluginRequired = plugins[i].resolve
|
|
12
|
-
const options = plugins[i].options
|
|
13
|
-
const Plugin = pluginRequired.default
|
|
14
|
-
const instance = new Plugin(options)
|
|
15
|
-
const id = plugins[i].id || `${instance.constructor.name}`
|
|
16
|
-
_plugins[id] = instance
|
|
17
|
-
_plugins[id].id = id
|
|
18
|
-
_plugins[id].config = options
|
|
19
|
-
_plugins[id].name = `${instance.constructor.name}`
|
|
20
|
-
}
|
|
21
|
-
return _plugins
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export async function runPlugins(
|
|
25
|
-
plugins: any, // // TODO: Add type for resolvedPlugins, they differ from loaded plugins
|
|
26
|
-
mode: PluginMode,
|
|
27
|
-
input: Input,
|
|
28
|
-
session: Session,
|
|
29
|
-
lastRoutePath: string,
|
|
30
|
-
response: string | null = null,
|
|
31
|
-
parsedResponse: Partial<BotonicEvent>[] | null = null
|
|
32
|
-
): Promise<void> {
|
|
33
|
-
for (const key in plugins) {
|
|
34
|
-
const p = await plugins[key]
|
|
35
|
-
try {
|
|
36
|
-
if (mode == 'pre') await p.pre({ input, session, lastRoutePath })
|
|
37
|
-
if (mode == 'post')
|
|
38
|
-
await p.post({
|
|
39
|
-
input,
|
|
40
|
-
session,
|
|
41
|
-
lastRoutePath,
|
|
42
|
-
response,
|
|
43
|
-
parsedResponse,
|
|
44
|
-
})
|
|
45
|
-
} catch (e) {
|
|
46
|
-
console.log(e)
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
}
|
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
|
-
}
|
package/src/utils.ts
DELETED
|
@@ -1,84 +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
|
-
export function isFunction(o: any): boolean {
|
|
46
|
-
return typeof o === 'function'
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
export const params2queryString = (params: { [key: string]: string }): string =>
|
|
50
|
-
Object.entries(params)
|
|
51
|
-
.map(([k, v]: any) => `${encodeURIComponent(k)}=${encodeURIComponent(v)}`)
|
|
52
|
-
.join('&')
|
|
53
|
-
|
|
54
|
-
// Joins path segments. Preserves initial "/" and resolves ".." and "."
|
|
55
|
-
// Does not support using ".." to go above/outside the root.
|
|
56
|
-
// This means that join("foo", "../../bar") will not resolve to "../bar"
|
|
57
|
-
export const join = (...segments: string[]): string => {
|
|
58
|
-
// Split the inputs into a list of path commands.
|
|
59
|
-
let parts: string[] = []
|
|
60
|
-
for (let i = 0, l = segments.length; i < l; i++) {
|
|
61
|
-
parts = parts.concat(segments[i].split('/'))
|
|
62
|
-
}
|
|
63
|
-
// Interpret the path commands to get the new resolved path.
|
|
64
|
-
const newParts: string[] = []
|
|
65
|
-
for (let i = 0, l = parts.length; i < l; i++) {
|
|
66
|
-
const part = parts[i]
|
|
67
|
-
// Remove leading and trailing slashes
|
|
68
|
-
// Also remove "." segments
|
|
69
|
-
if (!part || part === '.') continue
|
|
70
|
-
// Interpret ".." to pop the last segment
|
|
71
|
-
if (part === '..') newParts.pop()
|
|
72
|
-
// Push new path segments.
|
|
73
|
-
else newParts.push(part)
|
|
74
|
-
}
|
|
75
|
-
// Preserve the initial slash if there was one.
|
|
76
|
-
// @ts-ignore
|
|
77
|
-
if (parts[0] === '') newParts.unshift('')
|
|
78
|
-
// Turn back into a single string path.
|
|
79
|
-
return newParts.join('/') || (newParts.length ? '/' : '.')
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
// A simple function to get the dirname of a path
|
|
83
|
-
// Trailing slashes are ignored. Leading slash is preserved.
|
|
84
|
-
export const dirname = (path: string): string => join(path, '..')
|