@botonic/core 0.26.0-beta.0 → 0.26.1-alpha.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.
Files changed (102) hide show
  1. package/lib/cjs/core-bot.d.ts +1 -3
  2. package/lib/cjs/core-bot.js +3 -33
  3. package/lib/cjs/core-bot.js.map +1 -1
  4. package/lib/cjs/handoff.d.ts +1 -1
  5. package/lib/cjs/index.d.ts +0 -1
  6. package/lib/cjs/index.js +0 -3
  7. package/lib/cjs/index.js.map +1 -1
  8. package/lib/cjs/models/legacy-types.d.ts +44 -40
  9. package/lib/cjs/models/legacy-types.js +16 -32
  10. package/lib/cjs/models/legacy-types.js.map +1 -1
  11. package/lib/cjs/plugins.d.ts +2 -3
  12. package/lib/cjs/plugins.js +2 -4
  13. package/lib/cjs/plugins.js.map +1 -1
  14. package/lib/cjs/utils.d.ts +8 -0
  15. package/lib/cjs/utils.js +32 -1
  16. package/lib/cjs/utils.js.map +1 -1
  17. package/lib/esm/core-bot.d.ts +1 -3
  18. package/lib/esm/core-bot.js +4 -34
  19. package/lib/esm/core-bot.js.map +1 -1
  20. package/lib/esm/handoff.d.ts +1 -1
  21. package/lib/esm/index.d.ts +0 -1
  22. package/lib/esm/index.js +0 -3
  23. package/lib/esm/index.js.map +1 -1
  24. package/lib/esm/models/legacy-types.d.ts +44 -40
  25. package/lib/esm/models/legacy-types.js +15 -31
  26. package/lib/esm/models/legacy-types.js.map +1 -1
  27. package/lib/esm/plugins.d.ts +2 -3
  28. package/lib/esm/plugins.js +2 -4
  29. package/lib/esm/plugins.js.map +1 -1
  30. package/lib/esm/utils.d.ts +8 -0
  31. package/lib/esm/utils.js +24 -0
  32. package/lib/esm/utils.js.map +1 -1
  33. package/package.json +1 -6
  34. package/src/core-bot.ts +2 -56
  35. package/src/handoff.ts +1 -1
  36. package/src/index.ts +0 -3
  37. package/src/models/legacy-types.ts +77 -75
  38. package/src/plugins.ts +2 -8
  39. package/src/utils.ts +32 -0
  40. package/lib/cjs/data-provider/dynamodb-data-provider.d.ts +0 -26
  41. package/lib/cjs/data-provider/dynamodb-data-provider.js +0 -107
  42. package/lib/cjs/data-provider/dynamodb-data-provider.js.map +0 -1
  43. package/lib/cjs/data-provider/dynamodb-utils.d.ts +0 -11
  44. package/lib/cjs/data-provider/dynamodb-utils.js +0 -121
  45. package/lib/cjs/data-provider/dynamodb-utils.js.map +0 -1
  46. package/lib/cjs/data-provider/factory.d.ts +0 -25
  47. package/lib/cjs/data-provider/factory.js +0 -31
  48. package/lib/cjs/data-provider/factory.js.map +0 -1
  49. package/lib/cjs/data-provider/index.d.ts +0 -4
  50. package/lib/cjs/data-provider/index.js +0 -8
  51. package/lib/cjs/data-provider/index.js.map +0 -1
  52. package/lib/cjs/data-provider/local-data-provider.d.ts +0 -26
  53. package/lib/cjs/data-provider/local-data-provider.js +0 -137
  54. package/lib/cjs/data-provider/local-data-provider.js.map +0 -1
  55. package/lib/cjs/output-parser/botonic-output-parser.d.ts +0 -16
  56. package/lib/cjs/output-parser/botonic-output-parser.js +0 -37
  57. package/lib/cjs/output-parser/botonic-output-parser.js.map +0 -1
  58. package/lib/cjs/output-parser/factory.d.ts +0 -4
  59. package/lib/cjs/output-parser/factory.js +0 -36
  60. package/lib/cjs/output-parser/factory.js.map +0 -1
  61. package/lib/cjs/output-parser/index.d.ts +0 -3
  62. package/lib/cjs/output-parser/index.js +0 -7
  63. package/lib/cjs/output-parser/index.js.map +0 -1
  64. package/lib/cjs/output-parser/parsers.d.ts +0 -26
  65. package/lib/cjs/output-parser/parsers.js +0 -171
  66. package/lib/cjs/output-parser/parsers.js.map +0 -1
  67. package/lib/esm/data-provider/dynamodb-data-provider.d.ts +0 -26
  68. package/lib/esm/data-provider/dynamodb-data-provider.js +0 -103
  69. package/lib/esm/data-provider/dynamodb-data-provider.js.map +0 -1
  70. package/lib/esm/data-provider/dynamodb-utils.d.ts +0 -11
  71. package/lib/esm/data-provider/dynamodb-utils.js +0 -113
  72. package/lib/esm/data-provider/dynamodb-utils.js.map +0 -1
  73. package/lib/esm/data-provider/factory.d.ts +0 -25
  74. package/lib/esm/data-provider/factory.js +0 -27
  75. package/lib/esm/data-provider/factory.js.map +0 -1
  76. package/lib/esm/data-provider/index.d.ts +0 -4
  77. package/lib/esm/data-provider/index.js +0 -5
  78. package/lib/esm/data-provider/index.js.map +0 -1
  79. package/lib/esm/data-provider/local-data-provider.d.ts +0 -26
  80. package/lib/esm/data-provider/local-data-provider.js +0 -133
  81. package/lib/esm/data-provider/local-data-provider.js.map +0 -1
  82. package/lib/esm/output-parser/botonic-output-parser.d.ts +0 -16
  83. package/lib/esm/output-parser/botonic-output-parser.js +0 -33
  84. package/lib/esm/output-parser/botonic-output-parser.js.map +0 -1
  85. package/lib/esm/output-parser/factory.d.ts +0 -4
  86. package/lib/esm/output-parser/factory.js +0 -32
  87. package/lib/esm/output-parser/factory.js.map +0 -1
  88. package/lib/esm/output-parser/index.d.ts +0 -3
  89. package/lib/esm/output-parser/index.js +0 -4
  90. package/lib/esm/output-parser/index.js.map +0 -1
  91. package/lib/esm/output-parser/parsers.d.ts +0 -26
  92. package/lib/esm/output-parser/parsers.js +0 -151
  93. package/lib/esm/output-parser/parsers.js.map +0 -1
  94. package/src/data-provider/dynamodb-data-provider.ts +0 -105
  95. package/src/data-provider/dynamodb-utils.ts +0 -149
  96. package/src/data-provider/factory.ts +0 -60
  97. package/src/data-provider/index.ts +0 -4
  98. package/src/data-provider/local-data-provider.ts +0 -154
  99. package/src/output-parser/botonic-output-parser.ts +0 -38
  100. package/src/output-parser/factory.ts +0 -45
  101. package/src/output-parser/index.ts +0 -3
  102. package/src/output-parser/parsers.ts +0 -237
@@ -1,151 +0,0 @@
1
- import { EventTypes, } from '../models';
2
- import { TEXT_NODE_NAME } from './botonic-output-parser';
3
- export function parseNumber(strNumber) {
4
- return parseInt(strNumber);
5
- }
6
- export function parseBoolean(strNumber) {
7
- if (strNumber === '0')
8
- return false;
9
- return true;
10
- }
11
- // COMMON
12
- export const parseMessage = args => {
13
- const typingAndDelay = {};
14
- if (args.toParse.delay !== undefined) {
15
- typingAndDelay['delay'] = parseNumber(args.toParse.delay);
16
- }
17
- if (args.toParse.typing !== undefined) {
18
- typingAndDelay['typing'] = parseNumber(args.toParse.typing);
19
- }
20
- return {
21
- toParse: args.toParse,
22
- parsed: Object.assign({
23
- // Following properties added later before saving event: eventId, userId, createdAt, from, ack
24
- eventType: EventTypes.MESSAGE, type: args.toParse.type, ack: args.toParse.ack, from: args.toParse.from }, typingAndDelay),
25
- };
26
- };
27
- // BUTTONS
28
- export const parseButton = (button) => {
29
- const title = button[TEXT_NODE_NAME];
30
- if ('payload' in button) {
31
- return { title, payload: button.payload };
32
- }
33
- if ('url' in button) {
34
- const b = { title, url: button.url, target: button.target };
35
- if (button.target)
36
- b.target = button.target;
37
- return b;
38
- }
39
- if ('webview' in button) {
40
- return {
41
- title,
42
- webview: button.webview,
43
- params: button.params,
44
- };
45
- }
46
- throw new Error('Invalid parsed Button');
47
- };
48
- export const parseButtons = args => {
49
- var _a, _b;
50
- const hasButtons = ((_b = (_a = args.toParse) === null || _a === void 0 ? void 0 : _a.button) === null || _b === void 0 ? void 0 : _b.length) > 0;
51
- let buttons = [];
52
- if (hasButtons) {
53
- buttons = args.toParse.button.map(button => parseButton(button));
54
- }
55
- return {
56
- toParse: args.toParse,
57
- parsed: Object.assign(Object.assign({}, args.parsed), { buttons }),
58
- };
59
- };
60
- // REPLIES
61
- export const parseReply = (reply) => ({
62
- title: reply[TEXT_NODE_NAME],
63
- payload: reply.payload,
64
- });
65
- export const parseReplies = args => {
66
- var _a, _b;
67
- const hasReplies = ((_b = (_a = args.toParse) === null || _a === void 0 ? void 0 : _a.reply) === null || _b === void 0 ? void 0 : _b.length) > 0;
68
- let replies = [];
69
- if (hasReplies) {
70
- replies = args.toParse.reply.map(reply => parseReply(reply));
71
- }
72
- return {
73
- toParse: args.toParse,
74
- parsed: Object.assign(Object.assign({}, args.parsed), { replies }),
75
- };
76
- };
77
- // TEXT
78
- export const parseText = args => {
79
- return {
80
- toParse: args.toParse,
81
- parsed: Object.assign(Object.assign({}, args.parsed), { text: args.toParse[TEXT_NODE_NAME], markdown: parseBoolean(args.toParse.markdown) }),
82
- };
83
- };
84
- // POSTBACK
85
- export const parsePostback = args => {
86
- return {
87
- toParse: args.toParse,
88
- parsed: Object.assign(Object.assign({}, args.parsed), { payload: args.toParse.payload }),
89
- };
90
- };
91
- // MEDIA
92
- export const parseMedia = args => {
93
- return {
94
- toParse: args.toParse,
95
- parsed: Object.assign(Object.assign({}, args.parsed), { src: args.toParse.src }),
96
- };
97
- };
98
- // LOCATION
99
- export const parseLocation = args => {
100
- return {
101
- toParse: args.toParse,
102
- parsed: Object.assign(Object.assign({}, args.parsed), { lat: args.toParse.lat, long: args.toParse.long }),
103
- };
104
- };
105
- // CAROUSEL
106
- export const parseElement = (element) => {
107
- const e = {
108
- pic: element.pic,
109
- title: element.title,
110
- subtitle: element.desc,
111
- };
112
- const hasButtons = element.button !== undefined;
113
- if (hasButtons)
114
- e.buttons = element.button.map(b => parseButton(b));
115
- return e;
116
- };
117
- export const parseCarousel = args => {
118
- return {
119
- toParse: args.toParse,
120
- parsed: Object.assign(Object.assign({}, args.parsed), { elements: args.toParse.element.map(element => parseElement(element)) }),
121
- };
122
- };
123
- // CUSTOM
124
- export const parseCustom = args => {
125
- return {
126
- toParse: args.toParse,
127
- parsed: Object.assign(Object.assign({}, args.parsed), { json: JSON.parse(args.toParse.json) }),
128
- };
129
- };
130
- // MISSED
131
- export const parseMissed = args => {
132
- return {
133
- toParse: args.toParse,
134
- parsed: Object.assign(Object.assign({}, args.parsed), { reason: args.toParse.reason, media_type: args.toParse.media_type }),
135
- };
136
- };
137
- // FORM
138
- export const parseForm = args => {
139
- return {
140
- toParse: args.toParse,
141
- parsed: Object.assign(Object.assign({}, args.parsed), { form_title: args.toParse.form_title, form_answers: args.toParse.form_answers }),
142
- };
143
- };
144
- // CONTACT
145
- export const parseContact = args => {
146
- return {
147
- toParse: args.toParse,
148
- parsed: Object.assign(Object.assign({}, args.parsed), { phone_number: args.toParse.phone_number, first_name: args.toParse.first_name, last_name: args.toParse.last_name, vcard: args.toParse.vcard }),
149
- };
150
- };
151
- //# sourceMappingURL=parsers.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"parsers.js","sourceRoot":"","sources":["../../../src/output-parser/parsers.ts"],"names":[],"mappings":"AAAA,OAAO,EAQL,UAAU,GAWX,MAAM,WAAW,CAAA;AAElB,OAAO,EAAE,cAAc,EAAE,MAAM,yBAAyB,CAAA;AAExD,MAAM,UAAU,WAAW,CAAC,SAAiB;IAC3C,OAAO,QAAQ,CAAC,SAAS,CAAC,CAAA;AAC5B,CAAC;AAED,MAAM,UAAU,YAAY,CAAC,SAAiB;IAC5C,IAAI,SAAS,KAAK,GAAG;QAAE,OAAO,KAAK,CAAA;IACnC,OAAO,IAAI,CAAA;AACb,CAAC;AAOD,SAAS;AACT,MAAM,CAAC,MAAM,YAAY,GAAuC,IAAI,CAAC,EAAE;IACrE,MAAM,cAAc,GAAG,EAAE,CAAA;IACzB,IAAI,IAAI,CAAC,OAAO,CAAC,KAAK,KAAK,SAAS,EAAE;QACpC,cAAc,CAAC,OAAO,CAAC,GAAG,WAAW,CAAC,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;KAC1D;IACD,IAAI,IAAI,CAAC,OAAO,CAAC,MAAM,KAAK,SAAS,EAAE;QACrC,cAAc,CAAC,QAAQ,CAAC,GAAG,WAAW,CAAC,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,CAAA;KAC5D;IACD,OAAO;QACL,OAAO,EAAE,IAAI,CAAC,OAAO;QACrB,MAAM;YACJ,8FAA8F;YAC9F,SAAS,EAAE,UAAU,CAAC,OAAO,EAC7B,IAAI,EAAE,IAAI,CAAC,OAAO,CAAC,IAAI,EACvB,GAAG,EAAE,IAAI,CAAC,OAAO,CAAC,GAAG,EACrB,IAAI,EA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
@@ -1,105 +0,0 @@
1
- import { Entity, Table } from 'dynamodb-toolbox'
2
-
3
- import { BotonicEvent, EventTypes, User } from '../models'
4
- import {
5
- getConnectionEventEntity,
6
- getMessageEventEntities,
7
- getUserEntity,
8
- getUserEventsTable,
9
- GLOBAL_SECONDARY_INDEX_NAME,
10
- SORT_KEY_NAME,
11
- USER_PREFIX,
12
- } from './dynamodb-utils'
13
- import { DataProvider } from './factory'
14
-
15
- export class DynamoDBDataProvider implements DataProvider {
16
- region: string
17
- tableName: string
18
- userEventsTable: Table
19
- userEntity: Entity<any>
20
- eventEntity: Entity<any>
21
- messageEventEntities: Record<string, Entity<any>>
22
- textMessageEventEntity: Entity<any>
23
- connectionEventEntity: Entity<any>
24
- constructor(url: string) {
25
- try {
26
- const urlParts = url.split('://')[1].split('.')
27
- this.tableName = urlParts[0]
28
- this.region = urlParts[1]
29
- this.userEventsTable = getUserEventsTable(this.tableName, this.region)
30
- this.userEntity = getUserEntity(this.userEventsTable)
31
- this.connectionEventEntity = getConnectionEventEntity(
32
- this.userEventsTable
33
- )
34
- this.messageEventEntities = getMessageEventEntities(this.userEventsTable)
35
- } catch (e) {
36
- console.log({ e })
37
- }
38
- }
39
-
40
- // @ts-ignore
41
- async getUsers(limit = 10, offset = 0): Promise<User[]> {
42
- // TODO: finish to implement with offset
43
- const result = await this.userEventsTable.scan({
44
- filters: { attr: SORT_KEY_NAME, beginsWith: USER_PREFIX },
45
- limit,
46
- })
47
- return result.Items
48
- }
49
-
50
- async getUser(id: string): Promise<User | undefined> {
51
- const userById = {
52
- id: id,
53
- SK: id,
54
- }
55
- const result = await this.userEntity.get(userById)
56
- if (Object.keys(result).length === 0) return undefined
57
- return result.Item
58
- }
59
-
60
- async getUserByWebsocketId(websocketId: string): Promise<User | undefined> {
61
- const result = await this.userEventsTable.query(websocketId, {
62
- index: GLOBAL_SECONDARY_INDEX_NAME,
63
- })
64
- if (result.Count === 0) return undefined
65
- return result.Items[0]
66
- }
67
- // @ts-ignore
68
- async getUserByField(field: string, value: any): Promise<User | undefined> {} //TODO: Implement
69
-
70
- async saveUser(user: User): Promise<User> {
71
- const putUser = { ...user, id: user.id, userId: user.id }
72
- await this.userEntity.put(putUser)
73
- return user
74
- }
75
- async updateUser(user: User): Promise<User> {
76
- const updateUser = { ...user, id: user.id, userId: user.id }
77
- const res = await this.userEntity.update(updateUser)
78
- return user
79
- }
80
- // @ts-ignore
81
- async deleteUser(id: string): Promise<User | undefined> {} // TODO: Implement
82
-
83
- // @ts-ignore
84
- async getEvents(limit = 10, offset = 0): Promise<BotonicEvent[]> {} // TODO: Implement
85
-
86
- // @ts-ignore
87
- async getEvent(id: string): Promise<BotonicEvent | undefined> {} // TODO: Implement
88
-
89
- async saveEvent(event: BotonicEvent): Promise<BotonicEvent> {
90
- if (event.eventType === EventTypes.CONNECTION) {
91
- await this.connectionEventEntity.put(event)
92
- }
93
- if (event.eventType === EventTypes.MESSAGE) {
94
- await this.messageEventEntities[event.type].put(event)
95
- }
96
- // TODO: Implementation of other event types
97
- return event
98
- }
99
-
100
- // @ts-ignore
101
- async updateEvent(event: BotonicEvent): Promise<BotonicEvent> {} // TODO: Implement
102
-
103
- // @ts-ignore
104
- async deleteEvent(id: string): Promise<BotonicEvent | undefined> {} // TODO: Implement
105
- }
@@ -1,149 +0,0 @@
1
- import DynamoDB from 'aws-sdk/clients/dynamodb'
2
- import { Entity, Table } from 'dynamodb-toolbox'
3
- import { EntityAttributes } from 'dynamodb-toolbox/dist/classes/Entity'
4
-
5
- import { MessageEventTypes } from '../models'
6
-
7
- // Table Definitions
8
- export const GLOBAL_SECONDARY_INDEX_NAME = 'GSI1'
9
- export const GLOBAL_SECONDARY_INDEX_ATTR = 'websocketId'
10
- export const PARTITION_KEY_NAME = 'PK'
11
- export const SORT_KEY_NAME = 'SK'
12
-
13
- // Entities Definitions
14
- export const USER_PREFIX = 'USR#'
15
- export const EVENT_PREFIX = 'EVT#'
16
-
17
- function capitalize(string: string) {
18
- return string.charAt(0).toUpperCase() + string.slice(1)
19
- }
20
-
21
- export function getUserEventsTable(tableName: string, region: string): Table {
22
- const documentClient = new DynamoDB.DocumentClient({
23
- apiVersion: '2012-08-10',
24
- region: region,
25
- })
26
- return new Table({
27
- name: tableName,
28
- partitionKey: PARTITION_KEY_NAME,
29
- sortKey: SORT_KEY_NAME,
30
- entityField: true,
31
- indexes: {
32
- GSI1: {
33
- partitionKey: GLOBAL_SECONDARY_INDEX_ATTR,
34
- },
35
- },
36
- DocumentClient: documentClient,
37
- })
38
- }
39
-
40
- export function getUserEntity(table: Table): Entity<any> {
41
- return new Entity({
42
- name: 'User',
43
- attributes: {
44
- id: { partitionKey: true, prefix: USER_PREFIX },
45
- [`${SORT_KEY_NAME}`]: {
46
- hidden: true,
47
- sortKey: true,
48
- prefix: USER_PREFIX,
49
- },
50
- userId: [SORT_KEY_NAME, 0],
51
- websocketId: 'string',
52
- isOnline: 'boolean',
53
- route: 'string',
54
- session: 'string',
55
- },
56
- table,
57
- })
58
- }
59
-
60
- export function getConnectionEventEntity(table: Table): Entity<any> {
61
- return new Entity({
62
- name: 'Connection Event',
63
- attributes: {
64
- userId: { partitionKey: true, prefix: USER_PREFIX },
65
- [`${SORT_KEY_NAME}`]: {
66
- hidden: true,
67
- sortKey: true,
68
- prefix: EVENT_PREFIX,
69
- },
70
- eventId: [SORT_KEY_NAME, 0],
71
- eventType: 'string',
72
- createdAt: 'string',
73
- modifiedAt: 'string',
74
- status: 'string',
75
- },
76
- table,
77
- })
78
- }
79
-
80
- export function getMessageEventEntities(
81
- table: Table
82
- ): Record<string, Entity<any>> {
83
- const textAttributes: EntityAttributes = {
84
- text: 'string',
85
- markdown: 'boolean',
86
- buttons: 'list',
87
- replies: 'list',
88
- }
89
-
90
- const mediaAttributes: EntityAttributes = {
91
- src: 'string',
92
- buttons: 'list',
93
- }
94
-
95
- const locationAttributes: EntityAttributes = {
96
- lat: 'number',
97
- long: 'number',
98
- }
99
-
100
- const carouselAttributes: EntityAttributes = {
101
- elements: 'list',
102
- }
103
-
104
- const customAttributes: EntityAttributes = {
105
- json: 'map',
106
- replies: 'list',
107
- }
108
-
109
- const entityWithAttributes = (type: string, attributes: EntityAttributes) =>
110
- new Entity({
111
- name: `${capitalize(type)} Message Event`, // Entity Names must be unique
112
- attributes: {
113
- userId: { partitionKey: true, prefix: USER_PREFIX },
114
- [`${SORT_KEY_NAME}`]: {
115
- hidden: true,
116
- sortKey: true,
117
- prefix: EVENT_PREFIX,
118
- },
119
- eventId: [SORT_KEY_NAME, 0],
120
- eventType: 'string',
121
- createdAt: 'string',
122
- modifiedAt: 'string',
123
- from: 'string',
124
- ack: 'string',
125
- typing: 'number',
126
- delay: 'number',
127
- type: 'string',
128
- ...attributes,
129
- },
130
- table,
131
- })
132
-
133
- return {
134
- text: entityWithAttributes(MessageEventTypes.TEXT, textAttributes),
135
- audio: entityWithAttributes(MessageEventTypes.AUDIO, mediaAttributes),
136
- image: entityWithAttributes(MessageEventTypes.IMAGE, mediaAttributes),
137
- document: entityWithAttributes(MessageEventTypes.DOCUMENT, mediaAttributes),
138
- video: entityWithAttributes(MessageEventTypes.VIDEO, mediaAttributes),
139
- location: entityWithAttributes(
140
- MessageEventTypes.LOCATION,
141
- locationAttributes
142
- ),
143
- carousel: entityWithAttributes(
144
- MessageEventTypes.CAROUSEL,
145
- carouselAttributes
146
- ),
147
- custom: entityWithAttributes(MessageEventTypes.CUSTOM, customAttributes),
148
- }
149
- }
@@ -1,60 +0,0 @@
1
- import { BotonicEvent, User } from '../models'
2
- import { DynamoDBDataProvider } from './dynamodb-data-provider'
3
- import { LocalDevDataProvider } from './local-data-provider'
4
-
5
- export const dataProviderProtocols = {
6
- DYNAMO_DB: 'dynamodb',
7
- FILE: 'file',
8
- }
9
-
10
- export interface DataProvider {
11
- getUsers(limit?: number, offset?: number): User[] | Promise<User[]>
12
- getUser(id: string): User | Promise<User | undefined> | undefined
13
- //TODO: replace getUserByWebsocketId to getUserByField?
14
- getUserByWebsocketId(
15
- websocketId: string
16
- ): User | Promise<User | undefined> | undefined
17
- getUserByField(
18
- field: string,
19
- value: any
20
- ): User | Promise<User | undefined> | undefined
21
- saveUser(user: User): User | Promise<User>
22
- updateUser(user: User): User | Promise<User>
23
- deleteUser(id: string): User | Promise<User | undefined> | undefined
24
- getEvents(
25
- limit?: number,
26
- offset?: number
27
- ): BotonicEvent[] | Promise<BotonicEvent[]>
28
- getEvent(
29
- id: string
30
- ): BotonicEvent | Promise<BotonicEvent | undefined> | undefined
31
- saveEvent(event: BotonicEvent): BotonicEvent | Promise<BotonicEvent>
32
- updateEvent(event: BotonicEvent): BotonicEvent | Promise<BotonicEvent>
33
- deleteEvent(
34
- id: string
35
- ): BotonicEvent | Promise<BotonicEvent | undefined> | undefined
36
- }
37
-
38
- /** URL examples:
39
- * dynamodb://my-table.my-region.aws.com
40
- * postgresql://my-database.my-db-provider.com
41
- * file://path/to/local/db.json
42
- */
43
- export function dataProviderFactory(url?: string): DataProvider {
44
- if (!url) {
45
- return new LocalDevDataProvider()
46
- }
47
-
48
- const protocol = url.split('://')[0]
49
- switch (protocol) {
50
- case dataProviderProtocols.DYNAMO_DB:
51
- return new DynamoDBDataProvider(url)
52
- case dataProviderProtocols.FILE:
53
- return new LocalDevDataProvider(url)
54
- default:
55
- console.error(
56
- `Protocol ${protocol} not implemented, using local DB instead.`
57
- )
58
- return new LocalDevDataProvider()
59
- }
60
- }
@@ -1,4 +0,0 @@
1
- export * from './dynamodb-data-provider'
2
- export * from './dynamodb-utils'
3
- export * from './factory'
4
- export * from './local-data-provider'
@@ -1,154 +0,0 @@
1
- import { JsonDB } from 'node-json-db'
2
- import { Config } from 'node-json-db/dist/lib/JsonDBConfig'
3
-
4
- import { BotonicEvent, User } from '../models'
5
- import { DataProvider } from './factory'
6
-
7
- export class LocalDevDataProvider implements DataProvider {
8
- private readonly DB_PATH = 'tmp/localDb'
9
- private readonly SEPARATOR = '/'
10
- private readonly paths = {
11
- USERS: '/users',
12
- EVENTS: '/events',
13
- CONNECTIONS: '/connections',
14
- }
15
- db: JsonDB
16
-
17
- constructor(url?: string) {
18
- const dbPath = url ? url.split('://')[1] : this.DB_PATH
19
- this.db = new JsonDB(new Config(dbPath, true, false, this.SEPARATOR))
20
- }
21
-
22
- getUsers(limit = 10, offset = 0): User[] {
23
- const path = this.paths.USERS
24
- this.db.reload()
25
- const userList = this.db.getObject<Record<string, User>>(path)
26
- const users = Object.values(userList)
27
- const to = limit !== 0 ? offset + limit : undefined
28
- return users.slice(offset, to)
29
- }
30
-
31
- getUser(userId: string): User | undefined {
32
- const path = this.createPath([this.paths.USERS, userId])
33
- this.db.reload()
34
- return this.db.exists(path) ? this.db.getObject<User>(path) : undefined
35
- }
36
-
37
- getUserByWebsocketId(websocketId: string): User | undefined {
38
- return this.getUserByField('websocketId', websocketId)
39
- }
40
-
41
- getUserByField(field: string, value: any): User | undefined {
42
- const path = this.paths.USERS
43
- this.db.reload()
44
- const dbUser = this.db.exists(path)
45
- ? this.db.find<User>(path, (user: User) => user[field] === value)
46
- : undefined
47
- return dbUser
48
- }
49
-
50
- saveUser(user: User): User {
51
- const path = this.createPath([this.paths.USERS, user.id])
52
- this.db.reload()
53
- if (!this.db.exists(path)) {
54
- this.db.push(path, user)
55
- }
56
- return user
57
- }
58
-
59
- updateUser(user: User): User {
60
- const path = this.createPath([this.paths.USERS, user.id])
61
- this.db.reload()
62
- if (this.db.exists(path)) {
63
- this.db.push(path, user, true)
64
- }
65
- return user
66
- }
67
-
68
- deleteUser(id: string): User | undefined {
69
- const path = this.createPath([this.paths.USERS, id])
70
- this.db.reload()
71
- const user = this.db.exists(path)
72
- ? this.db.getObject<User>(path)
73
- : undefined
74
- if (user) {
75
- this.db.delete(path)
76
- return user
77
- }
78
- return undefined
79
- }
80
-
81
- getEvents(limit = 10, offset = 0): BotonicEvent[] {
82
- const path = this.paths.EVENTS
83
- this.db.reload()
84
- const eventList = this.db.getObject<Record<string, BotonicEvent>>(path)
85
- const events = Object.values(eventList)
86
- const to = limit !== 0 ? offset + limit : undefined
87
- return events.slice(offset, to)
88
- }
89
-
90
- getEvent(id: string): BotonicEvent | undefined {
91
- const path = this.createPath([this.paths.EVENTS, id])
92
- this.db.reload()
93
- return this.db.exists(path)
94
- ? this.db.getObject<BotonicEvent>(path)
95
- : undefined
96
- }
97
-
98
- saveEvent(event: BotonicEvent): BotonicEvent {
99
- const path = this.createPath([this.paths.EVENTS, event.eventId])
100
- this.db.reload()
101
- if (!this.db.exists(path)) {
102
- this.db.push(path, event)
103
- }
104
- return event
105
- }
106
-
107
- updateEvent(event: BotonicEvent): BotonicEvent {
108
- const path = this.createPath([this.paths.EVENTS, event.eventId])
109
- this.db.reload()
110
- if (this.db.exists(path)) {
111
- this.db.push(path, event, true)
112
- }
113
- return event
114
- }
115
-
116
- deleteEvent(id: string): BotonicEvent | undefined {
117
- const path = this.createPath([this.paths.EVENTS, id])
118
- this.db.reload()
119
- const event = this.db.exists(path)
120
- ? this.db.getObject<BotonicEvent>(path)
121
- : undefined
122
- if (event) {
123
- this.db.delete(path)
124
- return event
125
- }
126
- return undefined
127
- }
128
-
129
- addConnection(websocketId: string) {
130
- const path = this.createPath([this.paths.CONNECTIONS, websocketId])
131
- this.db.reload()
132
- if (!this.db.exists(path)) {
133
- this.db.push(path, '', false)
134
- }
135
- }
136
-
137
- updateConnection(websocketId: string, userId: string) {
138
- const path = this.createPath([this.paths.CONNECTIONS, websocketId])
139
- this.db.reload()
140
- if (this.db.exists(path)) {
141
- this.db.push(path, userId, true)
142
- }
143
- }
144
-
145
- deleteConnection(websocketId: string) {
146
- const path = this.createPath([this.paths.CONNECTIONS, websocketId])
147
- this.db.reload()
148
- this.db.delete(path)
149
- }
150
-
151
- private createPath(urlChunks: string[]): string {
152
- return urlChunks.join(this.SEPARATOR)
153
- }
154
- }
@@ -1,38 +0,0 @@
1
- // eslint-disable-next-line import/named
2
- import { parse, X2jOptions } from 'fast-xml-parser'
3
- import { decode } from 'html-entities'
4
-
5
- import { BotonicEvent } from '../models'
6
- import { MessageParsingFactory } from './factory'
7
-
8
- export const TEXT_NODE_NAME = 'text'
9
-
10
- export class BotonicOutputParser {
11
- private static OPTIONS: Partial<X2jOptions> = {
12
- ignoreAttributes: false,
13
- attributeNamePrefix: '',
14
- textNodeName: TEXT_NODE_NAME,
15
- arrayMode: true,
16
- attrValueProcessor: a => decode(a),
17
- }
18
-
19
- constructor(private readonly factory = new MessageParsingFactory()) {}
20
-
21
- xmlToMessageEvents(html: string): Partial<BotonicEvent>[] {
22
- const jsonMessages = this.xmlToJSONMessages(html)
23
- return jsonMessages.map(msgToParse => this.factory.parse(msgToParse))
24
- }
25
-
26
- /**
27
- * Right now, when saving BotonicEvents to DataProvider, we need the standarized Botonic event
28
- * to be saved. This is, converting a botonic input like: '{id: 'msgId', data: 'rawData', payload: 'somePayload'}'
29
- * into a BotonicEvent with the expected properties.
30
- */
31
- parseFromUserInput(input: any): Partial<BotonicEvent> {
32
- return this.factory.parse(input)
33
- }
34
-
35
- private xmlToJSONMessages(html: any): any {
36
- return parse(html, BotonicOutputParser.OPTIONS).message
37
- }
38
- }