@botonic/plugin-flow-builder 0.21.1 → 0.21.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cjs/action.js +4 -2
- package/lib/cjs/action.js.map +1 -1
- package/lib/cjs/content-fields/content-fields-base.d.ts +1 -1
- package/lib/cjs/content-fields/content-fields-base.js.map +1 -1
- package/lib/cjs/content-fields/flow-button.d.ts +1 -1
- package/lib/cjs/content-fields/flow-button.js +3 -3
- package/lib/cjs/content-fields/flow-button.js.map +1 -1
- package/lib/cjs/content-fields/flow-carousel.d.ts +1 -1
- package/lib/cjs/content-fields/flow-carousel.js +4 -5
- package/lib/cjs/content-fields/flow-carousel.js.map +1 -1
- package/lib/cjs/content-fields/flow-image.d.ts +1 -1
- package/lib/cjs/content-fields/flow-image.js +2 -2
- package/lib/cjs/content-fields/flow-image.js.map +1 -1
- package/lib/cjs/content-fields/flow-text.d.ts +1 -1
- package/lib/cjs/content-fields/flow-text.js +3 -3
- package/lib/cjs/content-fields/flow-text.js.map +1 -1
- package/lib/cjs/content-fields/flow-video.d.ts +1 -1
- package/lib/cjs/content-fields/flow-video.js +2 -2
- package/lib/cjs/content-fields/flow-video.js.map +1 -1
- package/lib/cjs/helpers.d.ts +1 -0
- package/lib/cjs/helpers.js +14 -1
- package/lib/cjs/helpers.js.map +1 -1
- package/lib/cjs/index.d.ts +3 -9
- package/lib/cjs/index.js +13 -28
- package/lib/cjs/index.js.map +1 -1
- package/lib/cjs/types.d.ts +13 -0
- package/lib/cjs/types.js +9 -0
- package/lib/cjs/types.js.map +1 -0
- package/lib/cjs/utils.d.ts +3 -0
- package/lib/cjs/utils.js +19 -1
- package/lib/cjs/utils.js.map +1 -1
- package/lib/esm/action.js +4 -2
- package/lib/esm/action.js.map +1 -1
- package/lib/esm/content-fields/content-fields-base.d.ts +1 -1
- package/lib/esm/content-fields/content-fields-base.js.map +1 -1
- package/lib/esm/content-fields/flow-button.d.ts +1 -1
- package/lib/esm/content-fields/flow-button.js +3 -3
- package/lib/esm/content-fields/flow-button.js.map +1 -1
- package/lib/esm/content-fields/flow-carousel.d.ts +1 -1
- package/lib/esm/content-fields/flow-carousel.js +4 -5
- package/lib/esm/content-fields/flow-carousel.js.map +1 -1
- package/lib/esm/content-fields/flow-image.d.ts +1 -1
- package/lib/esm/content-fields/flow-image.js +2 -2
- package/lib/esm/content-fields/flow-image.js.map +1 -1
- package/lib/esm/content-fields/flow-text.d.ts +1 -1
- package/lib/esm/content-fields/flow-text.js +3 -3
- package/lib/esm/content-fields/flow-text.js.map +1 -1
- package/lib/esm/content-fields/flow-video.d.ts +1 -1
- package/lib/esm/content-fields/flow-video.js +2 -2
- package/lib/esm/content-fields/flow-video.js.map +1 -1
- package/lib/esm/helpers.d.ts +1 -0
- package/lib/esm/helpers.js +12 -0
- package/lib/esm/helpers.js.map +1 -1
- package/lib/esm/index.d.ts +3 -9
- package/lib/esm/index.js +13 -28
- package/lib/esm/index.js.map +1 -1
- package/lib/esm/types.d.ts +13 -0
- package/lib/esm/types.js +6 -0
- package/lib/esm/types.js.map +1 -0
- package/lib/esm/utils.d.ts +3 -0
- package/lib/esm/utils.js +17 -0
- package/lib/esm/utils.js.map +1 -1
- package/package.json +8 -7
- package/src/action.tsx +6 -2
- package/src/content-fields/content-fields-base.ts +1 -1
- package/src/content-fields/flow-button.tsx +3 -3
- package/src/content-fields/flow-carousel.tsx +4 -5
- package/src/content-fields/flow-image.tsx +2 -2
- package/src/content-fields/flow-text.tsx +4 -4
- package/src/content-fields/flow-video.tsx +2 -2
- package/src/helpers.ts +21 -0
- package/src/index.ts +14 -45
- package/src/types.ts +16 -0
- package/src/utils.ts +24 -0
package/src/index.ts
CHANGED
|
@@ -22,47 +22,41 @@ import {
|
|
|
22
22
|
IntentNode,
|
|
23
23
|
KeywordNode,
|
|
24
24
|
NodeComponent,
|
|
25
|
-
NodeLink,
|
|
26
25
|
NodeType,
|
|
27
26
|
StartNode,
|
|
28
27
|
} from './flow-builder-models'
|
|
29
28
|
import { DEFAULT_FUNCTIONS } from './functions'
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
flow?: FlowBuilderData
|
|
34
|
-
customFunctions?: Record<any, any>
|
|
35
|
-
getLocale: (session: Session) => string
|
|
36
|
-
getAccessToken: () => string
|
|
37
|
-
}
|
|
29
|
+
import { BotonicPluginFlowBuilderOptions } from './types'
|
|
30
|
+
import { resolveGetAccessToken } from './utils'
|
|
31
|
+
import { updateButtonUrls } from './helpers'
|
|
38
32
|
|
|
39
33
|
export default class BotonicPluginFlowBuilder implements Plugin {
|
|
40
34
|
private flowUrl: string
|
|
41
35
|
private flow: Promise<FlowBuilderData> | FlowBuilderData
|
|
42
36
|
private functions: Record<any, any>
|
|
43
37
|
private currentRequest: PluginPreRequest
|
|
44
|
-
private getAccessToken: () => string
|
|
38
|
+
private getAccessToken: (session: Session) => string
|
|
45
39
|
public getLocale: (session: Session) => string
|
|
46
40
|
|
|
47
41
|
constructor(readonly options: BotonicPluginFlowBuilderOptions) {
|
|
48
42
|
this.getLocale = options.getLocale
|
|
49
|
-
this.getAccessToken = options
|
|
43
|
+
this.getAccessToken = resolveGetAccessToken(options)
|
|
50
44
|
this.flowUrl = options.flowUrl
|
|
51
|
-
this.flow = options.flow
|
|
45
|
+
if (options.flow) this.flow = options.flow
|
|
52
46
|
const customFunctions = options.customFunctions || {}
|
|
53
47
|
this.functions = { ...DEFAULT_FUNCTIONS, ...customFunctions }
|
|
54
48
|
}
|
|
55
49
|
|
|
56
|
-
async readFlowContent(): Promise<FlowBuilderData> {
|
|
50
|
+
async readFlowContent(session: Session): Promise<FlowBuilderData> {
|
|
57
51
|
const { data } = await axios.get(this.flowUrl, {
|
|
58
|
-
headers: { Authorization: `Bearer ${this.getAccessToken()}` },
|
|
52
|
+
headers: { Authorization: `Bearer ${this.getAccessToken(session)}` },
|
|
59
53
|
})
|
|
60
54
|
return data
|
|
61
55
|
}
|
|
62
56
|
|
|
63
57
|
async pre(request: PluginPreRequest): Promise<void> {
|
|
64
58
|
this.currentRequest = request
|
|
65
|
-
this.flow = this.readFlowContent()
|
|
59
|
+
this.flow = await this.readFlowContent(this.currentRequest.session)
|
|
66
60
|
}
|
|
67
61
|
|
|
68
62
|
async post(_request: PluginPostRequest): Promise<void> {}
|
|
@@ -112,7 +106,7 @@ export default class BotonicPluginFlowBuilder implements Plugin {
|
|
|
112
106
|
return startNode.target.id
|
|
113
107
|
}
|
|
114
108
|
|
|
115
|
-
async getFallbackId(): Promise<string> {
|
|
109
|
+
async getFallbackId(alternate: boolean): Promise<string> {
|
|
116
110
|
const flow = await this.flow
|
|
117
111
|
const fallbackNode = flow.nodes.find(
|
|
118
112
|
node => node.type === NodeType.FALLBACK
|
|
@@ -121,8 +115,7 @@ export default class BotonicPluginFlowBuilder implements Plugin {
|
|
|
121
115
|
const fallbackFirstMessage = fallbackNode.content.first_message
|
|
122
116
|
const fallbackSecondMessage = fallbackNode.content.second_message
|
|
123
117
|
if (!fallbackSecondMessage) return fallbackFirstMessage.id
|
|
124
|
-
|
|
125
|
-
return fallbackIds[Math.floor(Math.random() * fallbackIds.length)]
|
|
118
|
+
return alternate ? fallbackFirstMessage.id : fallbackSecondMessage.id
|
|
126
119
|
}
|
|
127
120
|
async getContents(
|
|
128
121
|
id: string,
|
|
@@ -132,33 +125,9 @@ export default class BotonicPluginFlowBuilder implements Plugin {
|
|
|
132
125
|
const contents = prevContents || []
|
|
133
126
|
const hubtypeContent: any = await this.getContent(id)
|
|
134
127
|
const isHandoff = hubtypeContent.type === NodeType.HANDOFF
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
const button = hubtypeContent.content.elements[i].button
|
|
139
|
-
if (button.url) {
|
|
140
|
-
for (const j in button.url) {
|
|
141
|
-
button.url[j] = {
|
|
142
|
-
...button.url[j],
|
|
143
|
-
...(await this.getContent(button.url[j].id)),
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
if (hubtypeContent.content.buttons) {
|
|
150
|
-
for (const i in hubtypeContent.content.buttons) {
|
|
151
|
-
const button = hubtypeContent.content.buttons[i]
|
|
152
|
-
if (button.url) {
|
|
153
|
-
for (const j in button.url) {
|
|
154
|
-
button.url[j] = {
|
|
155
|
-
...button.url[j],
|
|
156
|
-
...(await this.getContent(button.url[j].id)),
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
}
|
|
128
|
+
// TODO: Create function to populate these buttons
|
|
129
|
+
await updateButtonUrls(hubtypeContent, 'elements', this.getContent)
|
|
130
|
+
await updateButtonUrls(hubtypeContent, 'buttons', this.getContent)
|
|
162
131
|
const content = await this.getFlowContent(hubtypeContent, locale)
|
|
163
132
|
if (hubtypeContent.type === NodeType.FUNCTION) {
|
|
164
133
|
const targetId = await this.callFunction(hubtypeContent, locale)
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Session } from '@botonic/core'
|
|
2
|
+
|
|
3
|
+
import { FlowBuilderData } from './flow-builder-models'
|
|
4
|
+
|
|
5
|
+
export interface BotonicPluginFlowBuilderOptions {
|
|
6
|
+
flowUrl: string
|
|
7
|
+
flow?: FlowBuilderData
|
|
8
|
+
customFunctions?: Record<any, any>
|
|
9
|
+
getLocale: (session: Session) => string
|
|
10
|
+
getAccessToken: () => string
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export enum ProcessEnvNodeEnvs {
|
|
14
|
+
PRODUCTION = 'production',
|
|
15
|
+
DEVELOPMENT = 'development',
|
|
16
|
+
}
|
package/src/utils.ts
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
import { Session } from '@botonic/core'
|
|
2
|
+
|
|
3
|
+
import { BotonicPluginFlowBuilderOptions, ProcessEnvNodeEnvs } from './types'
|
|
4
|
+
|
|
1
5
|
export function getWebpackEnvVar(
|
|
2
6
|
webpackEnvVar: string | false,
|
|
3
7
|
name: string,
|
|
@@ -9,3 +13,23 @@ export function getWebpackEnvVar(
|
|
|
9
13
|
defaultValue
|
|
10
14
|
)
|
|
11
15
|
}
|
|
16
|
+
|
|
17
|
+
function getAccessTokenFromSession(session: Session): string {
|
|
18
|
+
if (!session._access_token) {
|
|
19
|
+
throw new Error('No access token found in session')
|
|
20
|
+
}
|
|
21
|
+
return session._access_token
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function resolveGetAccessToken(
|
|
25
|
+
options: BotonicPluginFlowBuilderOptions
|
|
26
|
+
): (session: Session) => string {
|
|
27
|
+
switch (process.env.NODE_ENV) {
|
|
28
|
+
case ProcessEnvNodeEnvs.PRODUCTION:
|
|
29
|
+
return getAccessTokenFromSession
|
|
30
|
+
case ProcessEnvNodeEnvs.DEVELOPMENT:
|
|
31
|
+
return options.getAccessToken
|
|
32
|
+
default:
|
|
33
|
+
throw new Error('No method defined for getting access token')
|
|
34
|
+
}
|
|
35
|
+
}
|