@zoho_apptics/apptics-js-sdk 1.0.1 → 2.0.0-beta

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 (226) hide show
  1. package/CHANGELOG.md +87 -0
  2. package/LICENSE +13 -0
  3. package/README.md +747 -11
  4. package/dist/apptics.esm.min.js +3 -0
  5. package/dist/apptics.esm.min.js.map +1 -0
  6. package/dist/apptics.min.js +3 -0
  7. package/dist/apptics.min.js.map +1 -0
  8. package/dist/chunks/batch-BU_mTpm1.min.js +3 -0
  9. package/dist/chunks/batch-BU_mTpm1.min.js.map +1 -0
  10. package/dist/chunks/logs-Bea5zUW_.min.js +3 -0
  11. package/dist/chunks/logs-Bea5zUW_.min.js.map +1 -0
  12. package/dist/cjs/auto-init.d.ts +8 -0
  13. package/dist/cjs/auto-init.js +32 -0
  14. package/dist/cjs/core/attempts.d.ts +28 -0
  15. package/dist/cjs/core/attempts.js +113 -0
  16. package/dist/cjs/core/batch.d.ts +32 -0
  17. package/dist/cjs/core/batch.js +115 -0
  18. package/dist/cjs/core/callsite.d.ts +28 -0
  19. package/dist/cjs/core/callsite.js +93 -0
  20. package/dist/cjs/core/consent.d.ts +23 -0
  21. package/dist/cjs/core/consent.js +130 -0
  22. package/dist/cjs/core/constants.d.ts +212 -0
  23. package/dist/cjs/core/constants.js +299 -0
  24. package/dist/cjs/core/db.d.ts +40 -0
  25. package/dist/cjs/core/db.js +334 -0
  26. package/dist/cjs/core/debug.d.ts +22 -0
  27. package/dist/cjs/core/debug.js +88 -0
  28. package/dist/cjs/core/env.d.ts +28 -0
  29. package/dist/cjs/core/env.js +267 -0
  30. package/dist/cjs/core/framework-id.d.ts +1 -0
  31. package/dist/cjs/core/framework-id.js +9 -0
  32. package/dist/cjs/core/guard.d.ts +2 -0
  33. package/dist/cjs/core/guard.js +72 -0
  34. package/dist/cjs/core/hooks.d.ts +41 -0
  35. package/dist/cjs/core/hooks.js +23 -0
  36. package/dist/cjs/core/logger.d.ts +16 -0
  37. package/dist/cjs/core/logger.js +86 -0
  38. package/dist/cjs/core/state.d.ts +69 -0
  39. package/dist/cjs/core/state.js +78 -0
  40. package/dist/cjs/core/storage.d.ts +46 -0
  41. package/dist/cjs/core/storage.js +229 -0
  42. package/dist/cjs/core/transport.d.ts +83 -0
  43. package/dist/cjs/core/transport.js +441 -0
  44. package/dist/cjs/core/utils.d.ts +26 -0
  45. package/dist/cjs/core/utils.js +175 -0
  46. package/dist/cjs/core/validate.d.ts +18 -0
  47. package/dist/cjs/core/validate.js +233 -0
  48. package/dist/cjs/device/appversion.d.ts +20 -0
  49. package/dist/cjs/device/appversion.js +158 -0
  50. package/dist/cjs/device/index.d.ts +23 -0
  51. package/dist/cjs/device/index.js +372 -0
  52. package/dist/cjs/engagement/api-rules.d.ts +84 -0
  53. package/dist/cjs/engagement/api-rules.js +219 -0
  54. package/dist/cjs/engagement/auto-api.d.ts +7 -0
  55. package/dist/cjs/engagement/auto-api.js +200 -0
  56. package/dist/cjs/engagement/auto-screen.d.ts +16 -0
  57. package/dist/cjs/engagement/auto-screen.js +241 -0
  58. package/dist/cjs/engagement/defined-events.d.ts +23 -0
  59. package/dist/cjs/engagement/defined-events.js +58 -0
  60. package/dist/cjs/engagement/send.d.ts +6 -0
  61. package/dist/cjs/engagement/send.js +99 -0
  62. package/dist/cjs/engagement/session.d.ts +42 -0
  63. package/dist/cjs/engagement/session.js +327 -0
  64. package/dist/cjs/engagement/track.d.ts +56 -0
  65. package/dist/cjs/engagement/track.js +350 -0
  66. package/dist/cjs/errors/send.d.ts +6 -0
  67. package/dist/cjs/errors/send.js +73 -0
  68. package/dist/cjs/errors/track.d.ts +4 -0
  69. package/dist/cjs/errors/track.js +127 -0
  70. package/dist/cjs/feedback/controls.d.ts +31 -0
  71. package/dist/cjs/feedback/controls.js +286 -0
  72. package/dist/cjs/feedback/index.d.ts +8 -0
  73. package/dist/cjs/feedback/index.js +37 -0
  74. package/dist/cjs/feedback/logs.d.ts +40 -0
  75. package/dist/cjs/feedback/logs.js +202 -0
  76. package/dist/cjs/feedback/send.d.ts +25 -0
  77. package/dist/cjs/feedback/send.js +178 -0
  78. package/dist/cjs/feedback/template.d.ts +2 -0
  79. package/dist/cjs/feedback/template.js +162 -0
  80. package/dist/cjs/feedback/ui.d.ts +55 -0
  81. package/dist/cjs/feedback/ui.js +631 -0
  82. package/dist/cjs/heatmap/index.d.ts +16 -0
  83. package/dist/cjs/heatmap/index.js +22 -0
  84. package/dist/cjs/heatmap/send.d.ts +13 -0
  85. package/dist/cjs/heatmap/send.js +82 -0
  86. package/dist/cjs/heatmap/track.d.ts +4 -0
  87. package/dist/cjs/heatmap/track.js +377 -0
  88. package/dist/cjs/index.d.ts +68 -0
  89. package/dist/cjs/index.js +150 -0
  90. package/dist/cjs/lifecycle.d.ts +14 -0
  91. package/dist/cjs/lifecycle.js +538 -0
  92. package/dist/cjs/package.json +3 -0
  93. package/dist/cjs/remoteconfig/custom.d.ts +10 -0
  94. package/dist/cjs/remoteconfig/custom.js +78 -0
  95. package/dist/cjs/remoteconfig/evaluate.d.ts +24 -0
  96. package/dist/cjs/remoteconfig/evaluate.js +171 -0
  97. package/dist/cjs/remoteconfig/fetch.d.ts +24 -0
  98. package/dist/cjs/remoteconfig/fetch.js +214 -0
  99. package/dist/cjs/remoteconfig/index.d.ts +13 -0
  100. package/dist/cjs/remoteconfig/index.js +90 -0
  101. package/dist/cjs/remoteconfig/store.d.ts +18 -0
  102. package/dist/cjs/remoteconfig/store.js +98 -0
  103. package/dist/cjs/remotelog/gate.d.ts +11 -0
  104. package/dist/cjs/remotelog/gate.js +54 -0
  105. package/dist/cjs/remotelog/index.d.ts +25 -0
  106. package/dist/cjs/remotelog/index.js +55 -0
  107. package/dist/cjs/remotelog/logger.d.ts +28 -0
  108. package/dist/cjs/remotelog/logger.js +139 -0
  109. package/dist/cjs/remotelog/send.d.ts +13 -0
  110. package/dist/cjs/remotelog/send.js +153 -0
  111. package/dist/cjs/scheduler.d.ts +8 -0
  112. package/dist/cjs/scheduler.js +78 -0
  113. package/dist/cjs/types.d.ts +312 -0
  114. package/dist/cjs/user/index.d.ts +33 -0
  115. package/dist/cjs/user/index.js +178 -0
  116. package/dist/esm/auto-init.d.ts +8 -0
  117. package/dist/esm/auto-init.js +29 -0
  118. package/dist/esm/core/attempts.d.ts +28 -0
  119. package/dist/esm/core/attempts.js +106 -0
  120. package/dist/esm/core/batch.d.ts +32 -0
  121. package/dist/esm/core/batch.js +108 -0
  122. package/dist/esm/core/callsite.d.ts +28 -0
  123. package/dist/esm/core/callsite.js +89 -0
  124. package/dist/esm/core/consent.d.ts +23 -0
  125. package/dist/esm/core/consent.js +120 -0
  126. package/dist/esm/core/constants.d.ts +212 -0
  127. package/dist/esm/core/constants.js +277 -0
  128. package/dist/esm/core/db.d.ts +40 -0
  129. package/dist/esm/core/db.js +324 -0
  130. package/dist/esm/core/debug.d.ts +22 -0
  131. package/dist/esm/core/debug.js +81 -0
  132. package/dist/esm/core/env.d.ts +28 -0
  133. package/dist/esm/core/env.js +251 -0
  134. package/dist/esm/core/framework-id.d.ts +1 -0
  135. package/dist/esm/core/framework-id.js +7 -0
  136. package/dist/esm/core/guard.d.ts +2 -0
  137. package/dist/esm/core/guard.js +70 -0
  138. package/dist/esm/core/hooks.d.ts +41 -0
  139. package/dist/esm/core/hooks.js +21 -0
  140. package/dist/esm/core/logger.d.ts +16 -0
  141. package/dist/esm/core/logger.js +82 -0
  142. package/dist/esm/core/state.d.ts +69 -0
  143. package/dist/esm/core/state.js +73 -0
  144. package/dist/esm/core/storage.d.ts +46 -0
  145. package/dist/esm/core/storage.js +219 -0
  146. package/dist/esm/core/transport.d.ts +83 -0
  147. package/dist/esm/core/transport.js +425 -0
  148. package/dist/esm/core/utils.d.ts +26 -0
  149. package/dist/esm/core/utils.js +161 -0
  150. package/dist/esm/core/validate.d.ts +18 -0
  151. package/dist/esm/core/validate.js +226 -0
  152. package/dist/esm/device/appversion.d.ts +20 -0
  153. package/dist/esm/device/appversion.js +153 -0
  154. package/dist/esm/device/index.d.ts +23 -0
  155. package/dist/esm/device/index.js +362 -0
  156. package/dist/esm/engagement/api-rules.d.ts +84 -0
  157. package/dist/esm/engagement/api-rules.js +211 -0
  158. package/dist/esm/engagement/auto-api.d.ts +7 -0
  159. package/dist/esm/engagement/auto-api.js +198 -0
  160. package/dist/esm/engagement/auto-screen.d.ts +16 -0
  161. package/dist/esm/engagement/auto-screen.js +238 -0
  162. package/dist/esm/engagement/defined-events.d.ts +23 -0
  163. package/dist/esm/engagement/defined-events.js +55 -0
  164. package/dist/esm/engagement/send.d.ts +6 -0
  165. package/dist/esm/engagement/send.js +96 -0
  166. package/dist/esm/engagement/session.d.ts +42 -0
  167. package/dist/esm/engagement/session.js +315 -0
  168. package/dist/esm/engagement/track.d.ts +56 -0
  169. package/dist/esm/engagement/track.js +340 -0
  170. package/dist/esm/errors/send.d.ts +6 -0
  171. package/dist/esm/errors/send.js +70 -0
  172. package/dist/esm/errors/track.d.ts +4 -0
  173. package/dist/esm/errors/track.js +123 -0
  174. package/dist/esm/feedback/controls.d.ts +31 -0
  175. package/dist/esm/feedback/controls.js +280 -0
  176. package/dist/esm/feedback/index.d.ts +8 -0
  177. package/dist/esm/feedback/index.js +30 -0
  178. package/dist/esm/feedback/logs.d.ts +40 -0
  179. package/dist/esm/feedback/logs.js +188 -0
  180. package/dist/esm/feedback/send.d.ts +25 -0
  181. package/dist/esm/feedback/send.js +175 -0
  182. package/dist/esm/feedback/template.d.ts +2 -0
  183. package/dist/esm/feedback/template.js +159 -0
  184. package/dist/esm/feedback/ui.d.ts +55 -0
  185. package/dist/esm/feedback/ui.js +626 -0
  186. package/dist/esm/heatmap/index.d.ts +16 -0
  187. package/dist/esm/heatmap/index.js +17 -0
  188. package/dist/esm/heatmap/send.d.ts +13 -0
  189. package/dist/esm/heatmap/send.js +79 -0
  190. package/dist/esm/heatmap/track.d.ts +4 -0
  191. package/dist/esm/heatmap/track.js +374 -0
  192. package/dist/esm/index.d.ts +68 -0
  193. package/dist/esm/index.js +122 -0
  194. package/dist/esm/lifecycle.d.ts +14 -0
  195. package/dist/esm/lifecycle.js +534 -0
  196. package/dist/esm/package.json +3 -0
  197. package/dist/esm/remoteconfig/custom.d.ts +10 -0
  198. package/dist/esm/remoteconfig/custom.js +74 -0
  199. package/dist/esm/remoteconfig/evaluate.d.ts +24 -0
  200. package/dist/esm/remoteconfig/evaluate.js +166 -0
  201. package/dist/esm/remoteconfig/fetch.d.ts +24 -0
  202. package/dist/esm/remoteconfig/fetch.js +210 -0
  203. package/dist/esm/remoteconfig/index.d.ts +13 -0
  204. package/dist/esm/remoteconfig/index.js +85 -0
  205. package/dist/esm/remoteconfig/store.d.ts +18 -0
  206. package/dist/esm/remoteconfig/store.js +91 -0
  207. package/dist/esm/remotelog/gate.d.ts +11 -0
  208. package/dist/esm/remotelog/gate.js +48 -0
  209. package/dist/esm/remotelog/index.d.ts +25 -0
  210. package/dist/esm/remotelog/index.js +51 -0
  211. package/dist/esm/remotelog/logger.d.ts +28 -0
  212. package/dist/esm/remotelog/logger.js +136 -0
  213. package/dist/esm/remotelog/send.d.ts +13 -0
  214. package/dist/esm/remotelog/send.js +149 -0
  215. package/dist/esm/scheduler.d.ts +8 -0
  216. package/dist/esm/scheduler.js +74 -0
  217. package/dist/esm/types.d.ts +312 -0
  218. package/dist/esm/user/index.d.ts +33 -0
  219. package/dist/esm/user/index.js +170 -0
  220. package/dist/feedback.esm.min.js +3 -0
  221. package/dist/feedback.esm.min.js.map +1 -0
  222. package/dist/heatmap.esm.min.js +3 -0
  223. package/dist/heatmap.esm.min.js.map +1 -0
  224. package/dist/style.css +1 -0
  225. package/package.json +124 -7
  226. package/src/appticssdk.js +0 -3817
@@ -0,0 +1,233 @@
1
+ /*! @zoho_apptics/apptics-js-sdk v2.0.0-beta | (c) Zoho Corporation Private Limited | see LICENSE */
2
+ 'use strict';
3
+
4
+ var logger = require('./logger.js');
5
+ var utils = require('./utils.js');
6
+
7
+ /**
8
+ * `ap_` / `AP_` begins the names of Apptics' own defined events and properties (DefinedEvents), so
9
+ * an app's event, group or property key may not, as on Android: one that does is not logged.
10
+ */
11
+ const RESERVED_PREFIX = /^ap_/i;
12
+ function validateEventOrGroup(str) {
13
+ if (!str || str === '') { // no i18n
14
+ logger.Console.warn('validateEventOrGroup: Invalid event or group name'); // no i18n
15
+ return false;
16
+ }
17
+ // a letter, then up to 99 letters, digits or _, and not the reserved ap_ prefix
18
+ const pattern = /^[a-zA-Z][a-zA-Z0-9_]{0,99}$/;
19
+ if (RESERVED_PREFIX.test(str)) {
20
+ logger.Console.warn('validateEventOrGroup: "' + str + '" starts with ap_, which is reserved for defined events'); // no i18n
21
+ return false;
22
+ }
23
+ return pattern.test(str);
24
+ }
25
+ /** A property key: a letter, then up to 49 letters, digits or _, and not the reserved ap_ prefix. */
26
+ const PROPERTY_KEY = /^[a-zA-Z][a-zA-Z0-9_]{0,49}$/;
27
+ /** At most this many properties on one event, screen or error. */
28
+ const MAX_PROPERTIES = 24;
29
+ /** At most this many characters of properties in all, as the payload carries them. */
30
+ const MAX_PROPERTIES_LENGTH = 7666;
31
+ /** At most this many characters in one value. */
32
+ const MAX_VALUE_LENGTH = 250;
33
+ /**
34
+ * A value as the payload carries it, with its type. Numbers are Number and booleans Boolean, sent
35
+ * as 1 and 0 — the form the server has always received a Boolean in. Anything else (an object, an
36
+ * array, null) is turned into text, as on Android, rather than dropped.
37
+ */
38
+ function typedValue(value) {
39
+ if (typeof value === 'number' && isFinite(value)) { // no i18n
40
+ return { value: value, type: 'Number' }; // no i18n
41
+ }
42
+ if (typeof value === 'boolean') { // no i18n
43
+ return { value: value ? 1 : 0, type: 'Boolean' }; // no i18n
44
+ }
45
+ if (typeof value === 'string') { // no i18n
46
+ return { value: value, type: 'String' }; // no i18n
47
+ }
48
+ var text;
49
+ try {
50
+ text = value === undefined ? 'undefined' : JSON.stringify(value); // no i18n
51
+ }
52
+ catch (err) {
53
+ text = String(value);
54
+ }
55
+ return { value: text === undefined ? String(value) : text, type: 'String' }; // no i18n
56
+ }
57
+ /**
58
+ * Event, screen and error properties as the payload carries them: `{ data, datatype, isvalid }`.
59
+ *
60
+ * Each property is judged on its own, as on Android: a bad key (wrong characters, too long, the
61
+ * reserved ap_ prefix) or a value over 250 characters drops that property, and the rest are kept
62
+ * and the event is still logged. Past 24 properties, or 7,666 characters in all, the later ones are
63
+ * dropped. When nothing valid is left the result says why, in the shape the server has always taken
64
+ * for a refused set: `{ isvalid: false, message: { code, keys, valuekeys } }`.
65
+ */
66
+ function validateJSON(jsonObj) {
67
+ if (utils.isEmpty(jsonObj) || utils.isEmptyObj(jsonObj)) {
68
+ return {};
69
+ }
70
+ var obj = jsonObj;
71
+ var data = {};
72
+ var datatype = {};
73
+ var codes = [];
74
+ var badKeys = [];
75
+ var longKeys = [];
76
+ function note(code) {
77
+ if (codes.indexOf(code) === -1) {
78
+ codes.push(code);
79
+ }
80
+ }
81
+ Object.keys(obj).forEach(function (key) {
82
+ if (!PROPERTY_KEY.test(key) || RESERVED_PREFIX.test(key)) {
83
+ logger.Console.warn('validateJSON: dropped the property \'' + key + '\': a key is a letter, then up to 49 letters, digits or _, and not ap_'); // no i18n
84
+ badKeys.push(key.substring(0, 50));
85
+ note(103);
86
+ return;
87
+ }
88
+ var typed = typedValue(obj[key]);
89
+ if (String(typed.value).length > MAX_VALUE_LENGTH) {
90
+ logger.Console.warn('validateJSON: dropped the property \'' + key + '\': its value is longer than ' + MAX_VALUE_LENGTH + ' characters'); // no i18n
91
+ longKeys.push(key);
92
+ note(104);
93
+ return;
94
+ }
95
+ if (Object.keys(data).length >= MAX_PROPERTIES) {
96
+ logger.Console.warn('validateJSON: dropped the property \'' + key + '\': at most ' + MAX_PROPERTIES + ' properties'); // no i18n
97
+ note(102);
98
+ return;
99
+ }
100
+ var withIt = Object.assign({}, data);
101
+ withIt[key] = typed.value;
102
+ if (JSON.stringify(withIt).length > MAX_PROPERTIES_LENGTH) {
103
+ logger.Console.warn('validateJSON: dropped the property \'' + key + '\': properties are at most ' + MAX_PROPERTIES_LENGTH + ' characters in all'); // no i18n
104
+ note(101);
105
+ return;
106
+ }
107
+ data[key] = typed.value;
108
+ datatype[key] = typed.type;
109
+ });
110
+ if (Object.keys(data).length > 0) {
111
+ return { data: data, datatype: datatype, isvalid: true };
112
+ }
113
+ if (codes.length === 0) {
114
+ return {};
115
+ }
116
+ return { isvalid: false, message: { code: codes, keys: badKeys, valuekeys: longKeys } };
117
+ }
118
+ /** user/register accepts at most 10000 characters of body (max-len on its inputstream). */
119
+ const USER_PROPERTIES_MAX_LENGTH = 10000;
120
+ /** user/register accepts at most this many user properties. */
121
+ const USER_PROPERTIES_MAX_COUNT = 45;
122
+ function userValidateJSON(obj) {
123
+ const data = {};
124
+ const datatype = {};
125
+ const message = [];
126
+ const errorMap = { 101: [], 102: [], 103: [], 104: [] };
127
+ const keyRegex = /^[a-zA-Z][a-zA-Z0-9_]{0,49}$/;
128
+ const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
129
+ let propCount = 0;
130
+ // Main fields and their validation
131
+ const stringFields = [
132
+ 'first_name', 'last_name', 'company_name', 'contact_number', 'country', 'region', 'city', 'geo_location', 'gender', 'plan_type', 'timezone', 'language'
133
+ ]; // no i18n
134
+ const emailField = 'email_address'; // no i18n
135
+ const numberFields = ['engagement_score', 'date_of_birth']; // no i18n
136
+ for (const [key, value] of Object.entries(obj)) {
137
+ propCount++;
138
+ // Key validation
139
+ if (typeof key !== 'string' || key.length > 50 || !keyRegex.test(key)) { // no i18n
140
+ errorMap[103].push(key);
141
+ continue;
142
+ }
143
+ // String fields
144
+ if (stringFields.includes(key)) {
145
+ if (typeof value !== 'string' || value.length > 255) { // no i18n
146
+ errorMap[103].push(key);
147
+ continue;
148
+ }
149
+ data[key] = value;
150
+ datatype[key] = 'String'; // no i18n
151
+ continue;
152
+ }
153
+ // Email field
154
+ if (key === emailField) {
155
+ if (typeof value !== 'string' || value.length > 255 || !emailRegex.test(value)) { // no i18n
156
+ errorMap[103].push(key);
157
+ continue;
158
+ }
159
+ data[key] = value;
160
+ datatype[key] = 'String'; // no i18n
161
+ continue;
162
+ }
163
+ // Number fields (engagement_score)
164
+ if (numberFields.includes(key)) {
165
+ if (typeof value !== 'number' || !Number.isFinite(value)) { // no i18n
166
+ errorMap[103].push(key);
167
+ continue;
168
+ }
169
+ data[key] = value;
170
+ datatype[key] = 'Number'; // no i18n
171
+ continue;
172
+ }
173
+ // Custom properties: key must be string, value must be Boolean, String (≤255), or Number (finite)
174
+ if (typeof value === 'boolean') { // no i18n
175
+ data[key] = value;
176
+ datatype[key] = 'Boolean'; // no i18n
177
+ }
178
+ else if (typeof value === 'string') { // no i18n
179
+ if (value.length > 255) {
180
+ errorMap[103].push(key);
181
+ continue;
182
+ }
183
+ data[key] = value;
184
+ datatype[key] = 'String'; // no i18n
185
+ }
186
+ else if (typeof value === 'number') { // no i18n
187
+ if (!Number.isFinite(value)) {
188
+ errorMap[104].push(key);
189
+ continue;
190
+ }
191
+ data[key] = value;
192
+ datatype[key] = 'Number'; // no i18n
193
+ }
194
+ else {
195
+ errorMap[104].push(key);
196
+ continue;
197
+ }
198
+ }
199
+ // Property count validation
200
+ if (propCount > USER_PROPERTIES_MAX_COUNT) {
201
+ errorMap[102] = Object.keys(obj);
202
+ }
203
+ // Total length validation: the payload carries data and datatype, so both are counted
204
+ const jsonLength = JSON.stringify(data).length + JSON.stringify(datatype).length;
205
+ if (jsonLength > USER_PROPERTIES_MAX_LENGTH) {
206
+ errorMap[101] = Object.keys(obj);
207
+ }
208
+ // Prepare message array with error_type
209
+ for (const code of [101, 102, 103, 104]) {
210
+ if (errorMap[code].length > 0) {
211
+ message.push({ code, keys: errorMap[code] });
212
+ }
213
+ }
214
+ return { data, datatype, message };
215
+ }
216
+ function validateScreenName(str) {
217
+ if (!str || str === '') { // no i18n
218
+ logger.Console.warn('validateScreenName: Invalid screenName'); // no i18n
219
+ return false;
220
+ }
221
+ // Define the regular expression pattern
222
+ //^[a-zA-Z][\\w\\s]{1,100}$
223
+ const pattern = /^[\w\s.\-_&/:]{0,99}$/;
224
+ // Test if the string matches the pattern
225
+ return pattern.test(str);
226
+ }
227
+
228
+ exports.USER_PROPERTIES_MAX_COUNT = USER_PROPERTIES_MAX_COUNT;
229
+ exports.USER_PROPERTIES_MAX_LENGTH = USER_PROPERTIES_MAX_LENGTH;
230
+ exports.userValidateJSON = userValidateJSON;
231
+ exports.validateEventOrGroup = validateEventOrGroup;
232
+ exports.validateJSON = validateJSON;
233
+ exports.validateScreenName = validateScreenName;
@@ -0,0 +1,20 @@
1
+ /** Whether we already hold usable ids for the app and version running now. */
2
+ export declare function hasAppVersion(): boolean;
3
+ /**
4
+ * Registers this build, or reuses the stored answer.
5
+ *
6
+ * Resolves true when the ids are in place, false when they are not — the caller stops there
7
+ * rather than carrying on to calls that would be refused for missing them.
8
+ */
9
+ export declare function registerAppVersion(): Promise<boolean>;
10
+ /**
11
+ * Puts the stored ids back on `_ZAP._device_info`.
12
+ *
13
+ * checkAndUpdateDeviceInfo rebuilds that object from scratch on every start, which would drop
14
+ * what registration put there. Called from inside that rebuild, so the ids survive it.
15
+ */
16
+ export declare function applyStoredAppVersion(): void;
17
+ /** Forgets the stored ids. reset() calls it, so the next start registers from scratch. */
18
+ export declare function clearAppVersion(): void;
19
+ /** The url this call goes to, for logging and tests. */
20
+ export declare function appVersionUrl(): string;
@@ -0,0 +1,158 @@
1
+ /*! @zoho_apptics/apptics-js-sdk v2.0.0-beta | (c) Zoho Corporation Private Limited | see LICENSE */
2
+ 'use strict';
3
+
4
+ var state = require('../core/state.js');
5
+ var logger = require('../core/logger.js');
6
+ var utils = require('../core/utils.js');
7
+ var storage = require('../core/storage.js');
8
+ var transport = require('../core/transport.js');
9
+
10
+ /**
11
+ * app/addversion — the first call the SDK makes, and the one everything else waits for.
12
+ *
13
+ * It is called with only what init() was given — zak, identifier and appversion — and registers
14
+ * this build with the platform. It answers with the ids every later call needs: aaid, the portal
15
+ * and project ids that form every other url, apid and mapid that authenticate the requests, and
16
+ * appversionid and appreleaseversionid that go in every payload's `meta`. metaWebSDKV2 declares all ten of its fields mandatory, so without this
17
+ * call v2 engagement, exception and log requests are refused outright.
18
+ *
19
+ * It is throttled at ten calls a minute per identifier+platform+version, so the answer is stored
20
+ * and reused: a returning visitor on the same build does not call it again. A changed appversion
21
+ * is a different build and does call it again, which is the point of the endpoint.
22
+ */
23
+ const LABEL = 'registerAppVersion'; // no i18n
24
+ /** Where the answer is kept between visits. */
25
+ const STORE_KEY = 'app_version_info'; // no i18n
26
+ /** The ids this call is the only source of. Everything else flows from them. */
27
+ const RESULT_KEYS = ['apid', 'mapid', 'aaid', 'appversionid', 'appreleaseversionid', 'platformid', 'projectid', 'portalid']; // no i18n
28
+ var inFlight = null;
29
+ /** The stored answer, if this exact build already registered. */
30
+ function stored() {
31
+ var saved = storage.user_defaults.item(STORE_KEY);
32
+ return saved && typeof saved === 'object' ? saved : {};
33
+ }
34
+ /**
35
+ * The ids without which no later url can be built or authenticated. portalid and projectid are
36
+ * the two path segments of every v2 url but this one; aaid names the app.
37
+ */
38
+ const REQUIRED_KEYS = ['aaid', 'portalid', 'projectid', 'apid', 'mapid', 'appversionid']; // no i18n
39
+ function missingKeys(data) {
40
+ return REQUIRED_KEYS.filter(function (key) { return utils.isEmpty(data[key]); });
41
+ }
42
+ /** Whether we already hold usable ids for the app and version running now. */
43
+ function hasAppVersion() {
44
+ var saved = stored();
45
+ return missingKeys(saved).length === 0
46
+ && saved.appversion === state._ZAP._defaultoptions.appversion
47
+ && saved.identifier === state._ZAP._defaultoptions.identifier
48
+ && saved.serviceUrl === state._ZAP._defaultoptions.serviceUrl;
49
+ }
50
+ /** Puts the ids where the rest of the SDK reads them from. */
51
+ function apply(data) {
52
+ RESULT_KEYS.forEach(function (key) {
53
+ if (!utils.isEmpty(data[key])) {
54
+ state._ZAP._device_info[key] = data[key];
55
+ }
56
+ });
57
+ // the app and the org/project segments of every url come from here, not from init().
58
+ // The server sends portalid as a string and projectid as a number; both become url text
59
+ if (!utils.isEmpty(data.aaid)) {
60
+ state._ZAP._defaultoptions.aaid = data.aaid;
61
+ }
62
+ if (!utils.isEmpty(data.portalid)) {
63
+ state._ZAP._defaultoptions.zsoid = String(data.portalid);
64
+ }
65
+ if (!utils.isEmpty(data.projectid)) {
66
+ state._ZAP._defaultoptions.projectID = String(data.projectid);
67
+ }
68
+ // apid and mapid are also read straight off state for the request headers. The server
69
+ // answers with numbers, and a header value is a string
70
+ if (!utils.isEmpty(data.apid)) {
71
+ state._ZAP._apid = String(data.apid);
72
+ }
73
+ if (!utils.isEmpty(data.mapid)) {
74
+ state._ZAP._mapid = String(data.mapid);
75
+ }
76
+ }
77
+ /**
78
+ * Registers this build, or reuses the stored answer.
79
+ *
80
+ * Resolves true when the ids are in place, false when they are not — the caller stops there
81
+ * rather than carrying on to calls that would be refused for missing them.
82
+ */
83
+ function registerAppVersion() {
84
+ if (hasAppVersion()) {
85
+ apply(stored());
86
+ logger.Console.log(LABEL + ': reusing the ids stored for ' + state._ZAP._defaultoptions.appversion); // no i18n
87
+ return Promise.resolve(true);
88
+ }
89
+ // a second caller joins the first rather than spending another of the ten calls a minute
90
+ if (inFlight !== null) {
91
+ return inFlight;
92
+ }
93
+ var frameworkid = !utils.isEmpty(state._ZAP._device_info.frameworkid) ? state._ZAP._device_info.frameworkid : state._ZAP._frameworkID;
94
+ if (frameworkid === 0) {
95
+ // a build made without npm pack: it still runs, but the server has no record of this SDK
96
+ logger.warnAlways(LABEL + ': this build has no registered framework id; build the package with npm pack'); // no i18n
97
+ }
98
+ if (utils.isEmpty(frameworkid) || utils.isEmpty(state._ZAP._defaultoptions.identifier) || utils.isEmpty(state._ZAP._defaultoptions.zak)) {
99
+ logger.warnAlways(LABEL + ': need zak, an identifier and a frameworkid to register this app version'); // no i18n
100
+ return Promise.resolve(false);
101
+ }
102
+ inFlight = transport.postToApptics('addversion', '', { zak: transport.getHeaderParams('zak') }, LABEL) // no i18n
103
+ .then(function (result) {
104
+ if (!utils.isSuccess(result) || !result.data) {
105
+ logger.warnAlways(LABEL + ': the server did not return the app ids'); // no i18n
106
+ return false;
107
+ }
108
+ var data = result.data;
109
+ var missing = missingKeys(data);
110
+ if (missing.length > 0) {
111
+ logger.warnAlways(LABEL + ': the answer has no ' + missing.join(', ')); // no i18n
112
+ return false;
113
+ }
114
+ apply(data);
115
+ // stored against the app and version it describes, so a new build registers again
116
+ var keep = {
117
+ appversion: state._ZAP._defaultoptions.appversion,
118
+ identifier: state._ZAP._defaultoptions.identifier,
119
+ serviceUrl: state._ZAP._defaultoptions.serviceUrl,
120
+ };
121
+ RESULT_KEYS.forEach(function (key) {
122
+ if (!utils.isEmpty(data[key])) {
123
+ keep[key] = data[key];
124
+ }
125
+ });
126
+ storage.user_defaults.add(STORE_KEY, keep);
127
+ logger.Console.log(LABEL + ': registered ' + state._ZAP._defaultoptions.appversion + ' as apid ' + data.apid); // no i18n
128
+ return true;
129
+ })
130
+ .catch(function (err) {
131
+ logger.warnAlways(LABEL + ': could not register this app version: ' + String(err && err.message ? err.message : err)); // no i18n
132
+ return false;
133
+ })
134
+ .then(function (ok) {
135
+ inFlight = null;
136
+ return ok;
137
+ });
138
+ return inFlight;
139
+ }
140
+ /**
141
+ * Puts the stored ids back on `_ZAP._device_info`.
142
+ *
143
+ * checkAndUpdateDeviceInfo rebuilds that object from scratch on every start, which would drop
144
+ * what registration put there. Called from inside that rebuild, so the ids survive it.
145
+ */
146
+ function applyStoredAppVersion() {
147
+ apply(stored());
148
+ }
149
+ /** Forgets the stored ids. reset() calls it, so the next start registers from scratch. */
150
+ function clearAppVersion() {
151
+ inFlight = null;
152
+ storage.user_defaults.add(STORE_KEY, undefined);
153
+ }
154
+
155
+ exports.applyStoredAppVersion = applyStoredAppVersion;
156
+ exports.clearAppVersion = clearAppVersion;
157
+ exports.hasAppVersion = hasAppVersion;
158
+ exports.registerAppVersion = registerAppVersion;
@@ -0,0 +1,23 @@
1
+ import type { DeviceInfo, Dict, ServerResult } from '../types.js';
2
+ export declare function checkAndUpdateDeviceInfo(): void;
3
+ export declare function addOrUpdateDeviceID(device_info: DeviceInfo, previous?: DeviceInfo): void;
4
+ /** True while the device has no server ids, so nothing can be sent yet. */
5
+ export declare function needsDevice(): boolean;
6
+ /**
7
+ * Resolves once a registration that is in flight has settled, succeeded or not, and right away
8
+ * when none is running. Callers use it to wait for the ids rather than poll for them.
9
+ */
10
+ export declare function deviceReady(): Promise<void>;
11
+ export declare function registerDeviceWithApptics(): Promise<ServerResult | DeviceInfo>;
12
+ export declare function updateDeviceWithApptics(device_info: DeviceInfo): Promise<ServerResult>;
13
+ export declare function getUpdatesFromServer(): Promise<Dict>;
14
+ /**
15
+ * Start-up: applies the server settings saved by the last getupdates, so a "stop tracking"
16
+ * answer holds across page loads instead of resetting to "on" until the next check.
17
+ */
18
+ export declare function restoreServerSettings(): void;
19
+ /**
20
+ * Fetches the server settings now if the device is registered. On a first visit the ids don't
21
+ * exist yet; registerDeviceWithApptics calls this as soon as device/add succeeds.
22
+ */
23
+ export declare function refreshServerSettings(): void;