10
10
* OR CONDITIONS OF ANY KIND, express or implied. See the License for the specific language governing permissions
11
11
* and limitations under the License.
12
12
*/
13
- import { ConsoleLogger as Logger } from '@aws-amplify/core' ;
14
- import { EventError } from '../types' ;
15
-
16
- const logger = new Logger ( 'ClickstreamProvider' ) ;
17
-
18
13
export class Event {
19
- static checkEventName ( eventName : string ) : EventError {
20
- const { EVENT_NAME_INVALID , EVENT_NAME_LENGTH_EXCEED , NO_ERROR } =
21
- Event . ErrorCode ;
22
- const { MAX_EVENT_TYPE_LENGTH } = Event . Limit ;
23
- if ( ! Event . isValidName ( eventName ) ) {
24
- return {
25
- error_code : EVENT_NAME_INVALID ,
26
- error_message :
27
- `Event name can only contains uppercase and lowercase letters, ` +
28
- `underscores, number, and is not start with a number. event name: ${ eventName } ` ,
29
- } ;
30
- } else if ( eventName . length > MAX_EVENT_TYPE_LENGTH ) {
31
- return {
32
- error_code : EVENT_NAME_LENGTH_EXCEED ,
33
- error_message :
34
- `Event name is too long, the max event type length is ` +
35
- `${ MAX_EVENT_TYPE_LENGTH } characters. event name: ${ eventName } ` ,
36
- } ;
37
- }
38
- return {
39
- error_code : NO_ERROR ,
40
- } ;
41
- }
42
-
43
- static isValidName ( name : string ) : boolean {
44
- const regex = / ^ (? ! [ 0 - 9 ] ) [ 0 - 9 a - z A - Z _ ] + $ / ;
45
- return regex . test ( name ) ;
46
- }
47
-
48
- static checkAttributes (
49
- currentNumber : number ,
50
- key : string ,
51
- value : string | number | boolean
52
- ) : EventError {
53
- const { MAX_NUM_OF_ATTRIBUTES , MAX_LENGTH_OF_NAME , MAX_LENGTH_OF_VALUE } =
54
- Event . Limit ;
55
- const {
56
- NO_ERROR ,
57
- ATTRIBUTE_SIZE_EXCEED ,
58
- ATTRIBUTE_NAME_INVALID ,
59
- ATTRIBUTE_NAME_LENGTH_EXCEED ,
60
- ATTRIBUTE_VALUE_LENGTH_EXCEED ,
61
- } = Event . ErrorCode ;
62
- if ( currentNumber >= MAX_NUM_OF_ATTRIBUTES ) {
63
- const errorMsg =
64
- `reached the max number of user attributes limit ${ MAX_NUM_OF_ATTRIBUTES } . ` +
65
- `and the user attribute: ${ key } will not be recorded` ;
66
- logger . error ( errorMsg ) ;
67
- const errorString = `attribute name: ${ key } ` ;
68
- return {
69
- error_message : Event . getLimitString ( errorString ) ,
70
- error_code : ATTRIBUTE_SIZE_EXCEED ,
71
- } ;
72
- }
73
- if ( key . length > MAX_LENGTH_OF_NAME ) {
74
- const errorMsg =
75
- `attribute : ${ key } , reached the max length of attributes name ` +
76
- `limit(${ MAX_LENGTH_OF_NAME } ). current length is: (${ key . length } ) ` +
77
- `and the attribute will not be recorded` ;
78
- logger . error ( errorMsg ) ;
79
- const errorString = `attribute name length is: (${ key . length } ) name is: ${ key } ` ;
80
- return {
81
- error_message : Event . getLimitString ( errorString ) ,
82
- error_code : ATTRIBUTE_NAME_LENGTH_EXCEED ,
83
- } ;
84
- }
85
- if ( ! Event . isValidName ( key ) ) {
86
- const errorMsg =
87
- `attribute : ${ key } , was not valid, attribute name can only ` +
88
- `contains uppercase and lowercase letters, underscores, number, and is not ` +
89
- `start with a number, so the attribute will not be recorded` ;
90
- logger . error ( errorMsg ) ;
91
- return {
92
- error_message : Event . getLimitString ( key ) ,
93
- error_code : ATTRIBUTE_NAME_INVALID ,
94
- } ;
95
- }
96
- const valueLength = String ( value ) . length ;
97
- if ( valueLength > MAX_LENGTH_OF_VALUE ) {
98
- const errorMsg =
99
- `attribute : ${ key } , reached the max length of attributes value limit ` +
100
- `(${ MAX_LENGTH_OF_VALUE } ). current length is: (${ valueLength } ). ` +
101
- `and the attribute will not be recorded, attribute value: ${ value } ` ;
102
- logger . error ( errorMsg ) ;
103
- const errorString = `attribute name: ${ key } , attribute value: ${ value } ` ;
104
- return {
105
- error_message : Event . getLimitString ( errorString ) ,
106
- error_code : ATTRIBUTE_VALUE_LENGTH_EXCEED ,
107
- } ;
108
- }
109
- return {
110
- error_code : NO_ERROR ,
111
- } ;
112
- }
113
-
114
- static getLimitString ( str : string ) : string {
115
- return str . substring ( 0 , Event . Limit . MAX_LENGTH_OF_ERROR_VALUE ) ;
116
- }
117
-
118
- static checkUserAttribute (
119
- currentNumber : number ,
120
- key : string ,
121
- value : string | number | boolean
122
- ) : EventError {
123
- const {
124
- MAX_NUM_OF_USER_ATTRIBUTES ,
125
- MAX_LENGTH_OF_NAME ,
126
- MAX_LENGTH_OF_USER_VALUE ,
127
- } = Event . Limit ;
128
- const {
129
- NO_ERROR ,
130
- USER_ATTRIBUTE_SIZE_EXCEED ,
131
- USER_ATTRIBUTE_NAME_LENGTH_EXCEED ,
132
- USER_ATTRIBUTE_NAME_INVALID ,
133
- USER_ATTRIBUTE_VALUE_LENGTH_EXCEED ,
134
- } = Event . ErrorCode ;
135
- if ( currentNumber >= MAX_NUM_OF_USER_ATTRIBUTES ) {
136
- const errorMsg =
137
- `reached the max number of user attributes limit (${ MAX_NUM_OF_USER_ATTRIBUTES } ). ` +
138
- `and the user attribute: ${ key } will not be recorded` ;
139
- logger . error ( errorMsg ) ;
140
- const errorString = `attribute name:${ key } ` ;
141
- return {
142
- error_message : Event . getLimitString ( errorString ) ,
143
- error_code : USER_ATTRIBUTE_SIZE_EXCEED ,
144
- } ;
145
- }
146
- if ( key . length > MAX_LENGTH_OF_NAME ) {
147
- const errorMsg =
148
- `user attribute : ${ key } , reached the max length of attributes name limit ` +
149
- `(${ MAX_LENGTH_OF_NAME } ). current length is: (${ key . length } ) ` +
150
- `and the attribute will not be recorded` ;
151
- logger . error ( errorMsg ) ;
152
- const errorString = `user attribute name length is: (${ key . length } ) name is: ${ key } ` ;
153
- return {
154
- error_message : Event . getLimitString ( errorString ) ,
155
- error_code : USER_ATTRIBUTE_NAME_LENGTH_EXCEED ,
156
- } ;
157
- }
158
- if ( ! Event . isValidName ( key ) ) {
159
- const errorMsg =
160
- `user attribute : ${ key } , was not valid, user attribute name can only ` +
161
- `contains uppercase and lowercase letters, underscores, number, and is not ` +
162
- `start with a number. so the attribute will not be recorded` ;
163
- logger . error ( errorMsg ) ;
164
- return {
165
- error_message : Event . getLimitString ( key ) ,
166
- error_code : USER_ATTRIBUTE_NAME_INVALID ,
167
- } ;
168
- }
169
- const valueLength = String ( value ) . length ;
170
- if ( valueLength > MAX_LENGTH_OF_USER_VALUE ) {
171
- const errorMsg =
172
- `user attribute : ${ key } , reached the max length of attributes value limit ` +
173
- `(${ MAX_LENGTH_OF_USER_VALUE } ). current length is: (${ valueLength } ). ` +
174
- `and the attribute will not be recorded, attribute value: ${ value } ` ;
175
- logger . error ( errorMsg ) ;
176
- const errorString = `attribute name: ${ key } , attribute value: ${ value } ` ;
177
- return {
178
- error_message : Event . getLimitString ( errorString ) ,
179
- error_code : USER_ATTRIBUTE_VALUE_LENGTH_EXCEED ,
180
- } ;
181
- }
182
- return {
183
- error_code : NO_ERROR ,
184
- } ;
185
- }
186
-
187
14
static readonly Limit = {
188
15
MAX_EVENT_TYPE_LENGTH : 50 ,
189
16
MAX_NUM_OF_ATTRIBUTES : 500 ,
@@ -193,6 +20,8 @@ export class Event {
193
20
MAX_LENGTH_OF_USER_VALUE : 256 ,
194
21
MAX_EVENT_NUMBER_OF_BATCH : 100 ,
195
22
MAX_LENGTH_OF_ERROR_VALUE : 256 ,
23
+ MAX_NUM_OF_ITEMS : 100 ,
24
+ MAX_LENGTH_OF_ITEM_VALUE : 256 ,
196
25
} ;
197
26
198
27
static readonly ErrorCode = {
@@ -207,6 +36,8 @@ export class Event {
207
36
USER_ATTRIBUTE_NAME_LENGTH_EXCEED : 3002 ,
208
37
USER_ATTRIBUTE_NAME_INVALID : 3003 ,
209
38
USER_ATTRIBUTE_VALUE_LENGTH_EXCEED : 3004 ,
39
+ ITEM_SIZE_EXCEED : 4001 ,
40
+ ITEM_VALUE_LENGTH_EXCEED : 4002 ,
210
41
} ;
211
42
212
43
static readonly ReservedAttribute = {
0 commit comments