@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.
Files changed (74) hide show
  1. package/lib/cjs/action.js +4 -2
  2. package/lib/cjs/action.js.map +1 -1
  3. package/lib/cjs/content-fields/content-fields-base.d.ts +1 -1
  4. package/lib/cjs/content-fields/content-fields-base.js.map +1 -1
  5. package/lib/cjs/content-fields/flow-button.d.ts +1 -1
  6. package/lib/cjs/content-fields/flow-button.js +3 -3
  7. package/lib/cjs/content-fields/flow-button.js.map +1 -1
  8. package/lib/cjs/content-fields/flow-carousel.d.ts +1 -1
  9. package/lib/cjs/content-fields/flow-carousel.js +4 -5
  10. package/lib/cjs/content-fields/flow-carousel.js.map +1 -1
  11. package/lib/cjs/content-fields/flow-image.d.ts +1 -1
  12. package/lib/cjs/content-fields/flow-image.js +2 -2
  13. package/lib/cjs/content-fields/flow-image.js.map +1 -1
  14. package/lib/cjs/content-fields/flow-text.d.ts +1 -1
  15. package/lib/cjs/content-fields/flow-text.js +3 -3
  16. package/lib/cjs/content-fields/flow-text.js.map +1 -1
  17. package/lib/cjs/content-fields/flow-video.d.ts +1 -1
  18. package/lib/cjs/content-fields/flow-video.js +2 -2
  19. package/lib/cjs/content-fields/flow-video.js.map +1 -1
  20. package/lib/cjs/helpers.d.ts +1 -0
  21. package/lib/cjs/helpers.js +14 -1
  22. package/lib/cjs/helpers.js.map +1 -1
  23. package/lib/cjs/index.d.ts +3 -9
  24. package/lib/cjs/index.js +13 -28
  25. package/lib/cjs/index.js.map +1 -1
  26. package/lib/cjs/types.d.ts +13 -0
  27. package/lib/cjs/types.js +9 -0
  28. package/lib/cjs/types.js.map +1 -0
  29. package/lib/cjs/utils.d.ts +3 -0
  30. package/lib/cjs/utils.js +19 -1
  31. package/lib/cjs/utils.js.map +1 -1
  32. package/lib/esm/action.js +4 -2
  33. package/lib/esm/action.js.map +1 -1
  34. package/lib/esm/content-fields/content-fields-base.d.ts +1 -1
  35. package/lib/esm/content-fields/content-fields-base.js.map +1 -1
  36. package/lib/esm/content-fields/flow-button.d.ts +1 -1
  37. package/lib/esm/content-fields/flow-button.js +3 -3
  38. package/lib/esm/content-fields/flow-button.js.map +1 -1
  39. package/lib/esm/content-fields/flow-carousel.d.ts +1 -1
  40. package/lib/esm/content-fields/flow-carousel.js +4 -5
  41. package/lib/esm/content-fields/flow-carousel.js.map +1 -1
  42. package/lib/esm/content-fields/flow-image.d.ts +1 -1
  43. package/lib/esm/content-fields/flow-image.js +2 -2
  44. package/lib/esm/content-fields/flow-image.js.map +1 -1
  45. package/lib/esm/content-fields/flow-text.d.ts +1 -1
  46. package/lib/esm/content-fields/flow-text.js +3 -3
  47. package/lib/esm/content-fields/flow-text.js.map +1 -1
  48. package/lib/esm/content-fields/flow-video.d.ts +1 -1
  49. package/lib/esm/content-fields/flow-video.js +2 -2
  50. package/lib/esm/content-fields/flow-video.js.map +1 -1
  51. package/lib/esm/helpers.d.ts +1 -0
  52. package/lib/esm/helpers.js +12 -0
  53. package/lib/esm/helpers.js.map +1 -1
  54. package/lib/esm/index.d.ts +3 -9
  55. package/lib/esm/index.js +13 -28
  56. package/lib/esm/index.js.map +1 -1
  57. package/lib/esm/types.d.ts +13 -0
  58. package/lib/esm/types.js +6 -0
  59. package/lib/esm/types.js.map +1 -0
  60. package/lib/esm/utils.d.ts +3 -0
  61. package/lib/esm/utils.js +17 -0
  62. package/lib/esm/utils.js.map +1 -1
  63. package/package.json +8 -7
  64. package/src/action.tsx +6 -2
  65. package/src/content-fields/content-fields-base.ts +1 -1
  66. package/src/content-fields/flow-button.tsx +3 -3
  67. package/src/content-fields/flow-carousel.tsx +4 -5
  68. package/src/content-fields/flow-image.tsx +2 -2
  69. package/src/content-fields/flow-text.tsx +4 -4
  70. package/src/content-fields/flow-video.tsx +2 -2
  71. package/src/helpers.ts +21 -0
  72. package/src/index.ts +14 -45
  73. package/src/types.ts +16 -0
  74. 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
- export type BotonicPluginFlowBuilderOptions = {
32
- flowUrl: string
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.getAccessToken
43
+ this.getAccessToken = resolveGetAccessToken(options)
50
44
  this.flowUrl = options.flowUrl
51
- this.flow = options.flow || this.readFlowContent()
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
- const fallbackIds = [fallbackFirstMessage.id, fallbackSecondMessage.id]
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
- if (hubtypeContent.content.elements) {
137
- for (const i in hubtypeContent.content.elements) {
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
+ }