@revenexx/sdk 0.0.2

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 (182) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +148 -0
  3. package/dist/cjs/package.json +3 -0
  4. package/dist/cjs/sdk.js +16340 -0
  5. package/dist/cjs/sdk.js.map +1 -0
  6. package/dist/esm/package.json +3 -0
  7. package/dist/esm/sdk.js +16250 -0
  8. package/dist/esm/sdk.js.map +1 -0
  9. package/dist/iife/sdk.js +20101 -0
  10. package/package.json +56 -0
  11. package/src/channel.ts +158 -0
  12. package/src/client.ts +950 -0
  13. package/src/enums/adapter.ts +4 -0
  14. package/src/enums/attribute-boolean-status.ts +7 -0
  15. package/src/enums/attribute-datetime-status.ts +7 -0
  16. package/src/enums/attribute-email-status.ts +7 -0
  17. package/src/enums/attribute-enum-status.ts +7 -0
  18. package/src/enums/attribute-float-status.ts +7 -0
  19. package/src/enums/attribute-integer-status.ts +7 -0
  20. package/src/enums/attribute-ip-status.ts +7 -0
  21. package/src/enums/attribute-line-status.ts +7 -0
  22. package/src/enums/attribute-longtext-status.ts +7 -0
  23. package/src/enums/attribute-mediumtext-status.ts +7 -0
  24. package/src/enums/attribute-point-status.ts +7 -0
  25. package/src/enums/attribute-polygon-status.ts +7 -0
  26. package/src/enums/attribute-relationship-status.ts +7 -0
  27. package/src/enums/attribute-string-status.ts +7 -0
  28. package/src/enums/attribute-text-status.ts +7 -0
  29. package/src/enums/attribute-url-status.ts +7 -0
  30. package/src/enums/attribute-varchar-status.ts +7 -0
  31. package/src/enums/build-runtime.ts +73 -0
  32. package/src/enums/code.ts +16 -0
  33. package/src/enums/collection.ts +4 -0
  34. package/src/enums/column-boolean-status.ts +7 -0
  35. package/src/enums/column-datetime-status.ts +7 -0
  36. package/src/enums/column-email-status.ts +7 -0
  37. package/src/enums/column-enum-status.ts +7 -0
  38. package/src/enums/column-float-status.ts +7 -0
  39. package/src/enums/column-integer-status.ts +7 -0
  40. package/src/enums/column-ip-status.ts +7 -0
  41. package/src/enums/column-line-status.ts +7 -0
  42. package/src/enums/column-longtext-status.ts +7 -0
  43. package/src/enums/column-mediumtext-status.ts +7 -0
  44. package/src/enums/column-point-status.ts +7 -0
  45. package/src/enums/column-polygon-status.ts +7 -0
  46. package/src/enums/column-relationship-status.ts +7 -0
  47. package/src/enums/column-string-status.ts +7 -0
  48. package/src/enums/column-text-status.ts +7 -0
  49. package/src/enums/column-url-status.ts +7 -0
  50. package/src/enums/column-varchar-status.ts +7 -0
  51. package/src/enums/compression.ts +5 -0
  52. package/src/enums/database-type.ts +4 -0
  53. package/src/enums/deployment-status.ts +8 -0
  54. package/src/enums/execution-status.ts +7 -0
  55. package/src/enums/execution-trigger.ts +5 -0
  56. package/src/enums/framework.ts +17 -0
  57. package/src/enums/gravity.ts +11 -0
  58. package/src/enums/health-antivirus-status.ts +5 -0
  59. package/src/enums/health-status-status.ts +4 -0
  60. package/src/enums/index-status.ts +7 -0
  61. package/src/enums/message-status.ts +7 -0
  62. package/src/enums/method.ts +9 -0
  63. package/src/enums/output.ts +9 -0
  64. package/src/enums/permissions.ts +22 -0
  65. package/src/enums/priority.ts +4 -0
  66. package/src/enums/range.ts +5 -0
  67. package/src/enums/runtime.ts +73 -0
  68. package/src/enums/runtimes.ts +73 -0
  69. package/src/enums/scopes.ts +57 -0
  70. package/src/enums/theme.ts +4 -0
  71. package/src/enums/timezone.ts +421 -0
  72. package/src/enums/type.ts +5 -0
  73. package/src/enums/use-cases.ts +9 -0
  74. package/src/id.ts +47 -0
  75. package/src/index.ts +92 -0
  76. package/src/models.ts +6013 -0
  77. package/src/operator.ts +308 -0
  78. package/src/permission.ts +57 -0
  79. package/src/query.ts +576 -0
  80. package/src/role.ts +100 -0
  81. package/src/service.ts +30 -0
  82. package/src/services/apps.ts +2473 -0
  83. package/src/services/avatars.ts +744 -0
  84. package/src/services/carts.ts +1057 -0
  85. package/src/services/channels.ts +227 -0
  86. package/src/services/customers.ts +729 -0
  87. package/src/services/greetings.ts +294 -0
  88. package/src/services/locale.ts +198 -0
  89. package/src/services/markets.ts +796 -0
  90. package/src/services/messaging.ts +3463 -0
  91. package/src/services/products.ts +3100 -0
  92. package/src/services/realtime.ts +537 -0
  93. package/src/services/search.ts +346 -0
  94. package/src/services/sites.ts +1847 -0
  95. package/src/services/storage.ts +1076 -0
  96. package/src/services/tokens.ts +314 -0
  97. package/types/channel.d.ts +74 -0
  98. package/types/client.d.ts +211 -0
  99. package/types/enums/adapter.d.ts +4 -0
  100. package/types/enums/attribute-boolean-status.d.ts +7 -0
  101. package/types/enums/attribute-datetime-status.d.ts +7 -0
  102. package/types/enums/attribute-email-status.d.ts +7 -0
  103. package/types/enums/attribute-enum-status.d.ts +7 -0
  104. package/types/enums/attribute-float-status.d.ts +7 -0
  105. package/types/enums/attribute-integer-status.d.ts +7 -0
  106. package/types/enums/attribute-ip-status.d.ts +7 -0
  107. package/types/enums/attribute-line-status.d.ts +7 -0
  108. package/types/enums/attribute-longtext-status.d.ts +7 -0
  109. package/types/enums/attribute-mediumtext-status.d.ts +7 -0
  110. package/types/enums/attribute-point-status.d.ts +7 -0
  111. package/types/enums/attribute-polygon-status.d.ts +7 -0
  112. package/types/enums/attribute-relationship-status.d.ts +7 -0
  113. package/types/enums/attribute-string-status.d.ts +7 -0
  114. package/types/enums/attribute-text-status.d.ts +7 -0
  115. package/types/enums/attribute-url-status.d.ts +7 -0
  116. package/types/enums/attribute-varchar-status.d.ts +7 -0
  117. package/types/enums/build-runtime.d.ts +73 -0
  118. package/types/enums/code.d.ts +16 -0
  119. package/types/enums/collection.d.ts +4 -0
  120. package/types/enums/column-boolean-status.d.ts +7 -0
  121. package/types/enums/column-datetime-status.d.ts +7 -0
  122. package/types/enums/column-email-status.d.ts +7 -0
  123. package/types/enums/column-enum-status.d.ts +7 -0
  124. package/types/enums/column-float-status.d.ts +7 -0
  125. package/types/enums/column-integer-status.d.ts +7 -0
  126. package/types/enums/column-ip-status.d.ts +7 -0
  127. package/types/enums/column-line-status.d.ts +7 -0
  128. package/types/enums/column-longtext-status.d.ts +7 -0
  129. package/types/enums/column-mediumtext-status.d.ts +7 -0
  130. package/types/enums/column-point-status.d.ts +7 -0
  131. package/types/enums/column-polygon-status.d.ts +7 -0
  132. package/types/enums/column-relationship-status.d.ts +7 -0
  133. package/types/enums/column-string-status.d.ts +7 -0
  134. package/types/enums/column-text-status.d.ts +7 -0
  135. package/types/enums/column-url-status.d.ts +7 -0
  136. package/types/enums/column-varchar-status.d.ts +7 -0
  137. package/types/enums/compression.d.ts +5 -0
  138. package/types/enums/database-type.d.ts +4 -0
  139. package/types/enums/deployment-status.d.ts +8 -0
  140. package/types/enums/execution-status.d.ts +7 -0
  141. package/types/enums/execution-trigger.d.ts +5 -0
  142. package/types/enums/framework.d.ts +17 -0
  143. package/types/enums/gravity.d.ts +11 -0
  144. package/types/enums/health-antivirus-status.d.ts +5 -0
  145. package/types/enums/health-status-status.d.ts +4 -0
  146. package/types/enums/index-status.d.ts +7 -0
  147. package/types/enums/message-status.d.ts +7 -0
  148. package/types/enums/method.d.ts +9 -0
  149. package/types/enums/output.d.ts +9 -0
  150. package/types/enums/permissions.d.ts +22 -0
  151. package/types/enums/priority.d.ts +4 -0
  152. package/types/enums/range.d.ts +5 -0
  153. package/types/enums/runtime.d.ts +73 -0
  154. package/types/enums/runtimes.d.ts +73 -0
  155. package/types/enums/scopes.d.ts +57 -0
  156. package/types/enums/theme.d.ts +4 -0
  157. package/types/enums/timezone.d.ts +421 -0
  158. package/types/enums/type.d.ts +5 -0
  159. package/types/enums/use-cases.d.ts +9 -0
  160. package/types/id.d.ts +20 -0
  161. package/types/index.d.ts +92 -0
  162. package/types/models.d.ts +5830 -0
  163. package/types/operator.d.ts +180 -0
  164. package/types/permission.d.ts +43 -0
  165. package/types/query.d.ts +442 -0
  166. package/types/role.d.ts +70 -0
  167. package/types/service.d.ts +11 -0
  168. package/types/services/apps.d.ts +932 -0
  169. package/types/services/avatars.d.ts +318 -0
  170. package/types/services/carts.d.ts +352 -0
  171. package/types/services/channels.d.ts +75 -0
  172. package/types/services/customers.d.ts +231 -0
  173. package/types/services/greetings.d.ts +101 -0
  174. package/types/services/locale.d.ts +64 -0
  175. package/types/services/markets.d.ts +274 -0
  176. package/types/services/messaging.d.ts +1324 -0
  177. package/types/services/products.d.ts +1014 -0
  178. package/types/services/realtime.d.ts +134 -0
  179. package/types/services/search.d.ts +131 -0
  180. package/types/services/sites.d.ts +689 -0
  181. package/types/services/storage.d.ts +421 -0
  182. package/types/services/tokens.d.ts +119 -0
package/src/client.ts ADDED
@@ -0,0 +1,950 @@
1
+ import { Models } from './models';
2
+ import { Channel, ActionableChannel, ResolvedChannel } from './channel';
3
+ import { Query } from './query';
4
+ import JSONbigModule from 'json-bigint';
5
+ const JSONbigParser = JSONbigModule({ storeAsString: false });
6
+ const JSONbigSerializer = JSONbigModule({ useNativeBigInt: true });
7
+
8
+ const MAX_SAFE = BigInt(Number.MAX_SAFE_INTEGER);
9
+ const MIN_SAFE = BigInt(Number.MIN_SAFE_INTEGER);
10
+ const MAX_INT64 = BigInt('9223372036854775807');
11
+ const MIN_INT64 = BigInt('-9223372036854775808');
12
+
13
+ function isBigNumber(value: any): boolean {
14
+ return value !== null
15
+ && typeof value === 'object'
16
+ && value._isBigNumber === true
17
+ && typeof value.isInteger === 'function'
18
+ && typeof value.toFixed === 'function'
19
+ && typeof value.toNumber === 'function';
20
+ }
21
+
22
+ function reviver(_key: string, value: any): any {
23
+ if (isBigNumber(value)) {
24
+ if (value.isInteger()) {
25
+ const str = value.toFixed();
26
+ const bi = BigInt(str);
27
+ if (bi >= MIN_SAFE && bi <= MAX_SAFE) {
28
+ return Number(str);
29
+ }
30
+ if (bi >= MIN_INT64 && bi <= MAX_INT64) {
31
+ return bi;
32
+ }
33
+ return value.toNumber();
34
+ }
35
+ return value.toNumber();
36
+ }
37
+ return value;
38
+ }
39
+
40
+ const JSONbig = {
41
+ parse: (text: string) => JSONbigParser.parse(text, reviver),
42
+ stringify: JSONbigSerializer.stringify
43
+ };
44
+
45
+ /**
46
+ * Payload type representing a key-value pair with string keys and any values.
47
+ */
48
+ type Payload = {
49
+ [key: string]: any;
50
+ }
51
+
52
+ /**
53
+ * Headers type representing a key-value pair with string keys and string values.
54
+ */
55
+ type Headers = {
56
+ [key: string]: string;
57
+ }
58
+
59
+ /**
60
+ * Realtime response structure with different types.
61
+ */
62
+ type RealtimeResponse = {
63
+ /**
64
+ * Type of the response: 'error', 'event', 'connected', 'response' or 'pong'.
65
+ */
66
+ type: 'error' | 'event' | 'connected' | 'response' | 'pong';
67
+
68
+ /**
69
+ * Data associated with the response based on the response type.
70
+ */
71
+ data: RealtimeResponseAuthenticated | RealtimeResponseConnected | RealtimeResponseError | RealtimeResponseEvent<unknown> | undefined;
72
+ }
73
+
74
+ /**
75
+ * Realtime request structure for authentication.
76
+ */
77
+ type RealtimeRequest = {
78
+ /**
79
+ * Type of the request: 'authentication'.
80
+ */
81
+ type: 'authentication';
82
+
83
+ /**
84
+ * Data required for authentication.
85
+ */
86
+ data: RealtimeRequestAuthenticate;
87
+ }
88
+
89
+ /**
90
+ * Realtime event response structure with generic payload type.
91
+ */
92
+ type RealtimeResponseEvent<T extends unknown> = {
93
+ /**
94
+ * List of event names associated with the response.
95
+ */
96
+ events: string[];
97
+
98
+ /**
99
+ * List of channel names associated with the response.
100
+ */
101
+ channels: string[];
102
+
103
+ /**
104
+ * Timestamp indicating the time of the event.
105
+ */
106
+ timestamp: string;
107
+
108
+ /**
109
+ * Payload containing event-specific data.
110
+ */
111
+ payload: T;
112
+
113
+ /**
114
+ * Subscription IDs this event matches (from backend, optional).
115
+ */
116
+ subscriptions?: string[];
117
+ }
118
+
119
+ /**
120
+ * Realtime response structure for errors.
121
+ */
122
+ type RealtimeResponseError = {
123
+ /**
124
+ * Numeric error code indicating the type of error.
125
+ */
126
+ code: number;
127
+
128
+ /**
129
+ * Error message describing the encountered error.
130
+ */
131
+ message: string;
132
+ }
133
+
134
+ /**
135
+ * Realtime response structure for a successful connection.
136
+ */
137
+ type RealtimeResponseConnected = {
138
+ /**
139
+ * List of channels the user is connected to.
140
+ */
141
+ channels: string[];
142
+
143
+ /**
144
+ * User object representing the connected user (optional).
145
+ */
146
+ user?: object;
147
+
148
+ /**
149
+ * Map slot index -> subscription ID from backend (optional).
150
+ */
151
+ subscriptions?: Record<string, string>;
152
+ }
153
+
154
+ /**
155
+ * Realtime response structure for authenticated connections.
156
+ */
157
+ type RealtimeResponseAuthenticated = {
158
+ /**
159
+ * Destination channel for the response.
160
+ */
161
+ to: string;
162
+
163
+ /**
164
+ * Boolean indicating the success of the authentication process.
165
+ */
166
+ success: boolean;
167
+
168
+ /**
169
+ * User object representing the authenticated user.
170
+ */
171
+ user: object;
172
+ }
173
+
174
+ /**
175
+ * Realtime request structure for authentication.
176
+ */
177
+ type RealtimeRequestAuthenticate = {
178
+ /**
179
+ * Session identifier for authentication.
180
+ */
181
+ session: string;
182
+ }
183
+
184
+ type TimeoutHandle = ReturnType<typeof setTimeout> | number;
185
+
186
+ /**
187
+ * Realtime interface representing the structure of a realtime communication object.
188
+ */
189
+ type Realtime = {
190
+ /**
191
+ * WebSocket instance for realtime communication.
192
+ */
193
+ socket?: WebSocket;
194
+
195
+ /**
196
+ * Timeout for reconnect operations.
197
+ */
198
+ timeout?: TimeoutHandle;
199
+
200
+ /**
201
+ * Heartbeat interval for the realtime connection.
202
+ */
203
+ heartbeat?: TimeoutHandle;
204
+
205
+ /**
206
+ * URL for establishing the WebSocket connection.
207
+ */
208
+ url?: string;
209
+
210
+ /**
211
+ * Last received message from the realtime server.
212
+ */
213
+ lastMessage?: RealtimeResponse;
214
+
215
+ /**
216
+ * Set of channel names the client is subscribed to.
217
+ */
218
+ channels: Set<string>;
219
+
220
+ /**
221
+ * Set of query strings the client is subscribed to.
222
+ */
223
+ queries: Set<string>;
224
+
225
+ /**
226
+ * Map of subscriptions containing channel names and corresponding callback functions.
227
+ */
228
+ subscriptions: Map<number, {
229
+ channels: string[];
230
+ queries: string[];
231
+ callback: (payload: RealtimeResponseEvent<any>) => void
232
+ }>;
233
+
234
+ /**
235
+ * Map slot index -> subscription ID (from backend, set on 'connected').
236
+ */
237
+ slotToSubscriptionId: Map<number, string>;
238
+
239
+ /**
240
+ * Map subscription ID -> slot index (for O(1) event dispatch).
241
+ */
242
+ subscriptionIdToSlot: Map<string, number>;
243
+
244
+ /**
245
+ * Counter for managing subscriptions.
246
+ */
247
+ subscriptionsCounter: number;
248
+
249
+ /**
250
+ * Boolean indicating whether automatic reconnection is enabled.
251
+ */
252
+ reconnect: boolean;
253
+
254
+ /**
255
+ * Number of reconnection attempts made.
256
+ */
257
+ reconnectAttempts: number;
258
+
259
+ /**
260
+ * Function to get the timeout duration for communication operations.
261
+ */
262
+ getTimeout: () => number;
263
+
264
+ /**
265
+ * Function to establish a WebSocket connection.
266
+ */
267
+ connect: () => void;
268
+
269
+ /**
270
+ * Function to create a new WebSocket instance.
271
+ */
272
+ createSocket: () => void;
273
+
274
+ /**
275
+ * Function to create a new heartbeat interval.
276
+ */
277
+ createHeartbeat: () => void;
278
+
279
+ /**
280
+ * Function to clean up resources associated with specified channels.
281
+ *
282
+ * @param {string[]} channels - List of channel names to clean up.
283
+ */
284
+ cleanUp: (channels: string[], queries: string[]) => void;
285
+
286
+ /**
287
+ * Function to handle incoming messages from the WebSocket connection.
288
+ *
289
+ * @param {MessageEvent} event - Event containing the received message.
290
+ */
291
+ onMessage: (event: MessageEvent) => void;
292
+ }
293
+
294
+ /**
295
+ * Type representing upload progress information.
296
+ */
297
+ type UploadProgress = {
298
+ /**
299
+ * Identifier for the upload progress.
300
+ */
301
+ $id: string;
302
+
303
+ /**
304
+ * Current progress of the upload (in percentage).
305
+ */
306
+ progress: number;
307
+
308
+ /**
309
+ * Total size uploaded (in bytes) during the upload process.
310
+ */
311
+ sizeUploaded: number;
312
+
313
+ /**
314
+ * Total number of chunks that need to be uploaded.
315
+ */
316
+ chunksTotal: number;
317
+
318
+ /**
319
+ * Number of chunks that have been successfully uploaded.
320
+ */
321
+ chunksUploaded: number;
322
+ }
323
+
324
+ /**
325
+ * Exception thrown by the package
326
+ */
327
+ class RevenexxException extends Error {
328
+ /**
329
+ * The error code associated with the exception.
330
+ */
331
+ code: number;
332
+
333
+ /**
334
+ * The response string associated with the exception.
335
+ */
336
+ response: string;
337
+
338
+ /**
339
+ * Error type.
340
+ * See [Error Types](https://revenexx.com/docs/response-codes#errorTypes) for more information.
341
+ */
342
+ type: string;
343
+
344
+ /**
345
+ * Initializes a Revenexx Exception.
346
+ *
347
+ * @param {string} message - The error message.
348
+ * @param {number} code - The error code. Default is 0.
349
+ * @param {string} type - The error type. Default is an empty string.
350
+ * @param {string} response - The response string. Default is an empty string.
351
+ */
352
+ constructor(message: string, code: number = 0, type: string = '', response: string = '') {
353
+ super(message);
354
+ this.name = 'RevenexxException';
355
+ this.message = message;
356
+ this.code = code;
357
+ this.type = type;
358
+ this.response = response;
359
+ }
360
+ }
361
+
362
+ /**
363
+ * Client that handles requests to Revenexx
364
+ */
365
+ class Client {
366
+ static CHUNK_SIZE = 1024 * 1024 * 5;
367
+
368
+ /**
369
+ * Holds configuration such as project.
370
+ */
371
+ config: {
372
+ endpoint: string;
373
+ endpointRealtime: string;
374
+ tenant: string;
375
+ apikeyauth: string;
376
+ bearerauth: string;
377
+ } = {
378
+ endpoint: 'https://api.revenexx.com',
379
+ endpointRealtime: '',
380
+ tenant: '',
381
+ apikeyauth: '',
382
+ bearerauth: '',
383
+ };
384
+ /**
385
+ * Custom headers for API requests.
386
+ */
387
+ headers: Headers = {
388
+ 'x-sdk-name': 'Revenexx Web',
389
+ 'x-sdk-platform': '',
390
+ 'x-sdk-language': 'web',
391
+ 'x-sdk-version': '0.0.1',
392
+ };
393
+
394
+ /**
395
+ * Set Endpoint
396
+ *
397
+ * Your project endpoint
398
+ *
399
+ * @param {string} endpoint
400
+ *
401
+ * @returns {this}
402
+ */
403
+ setEndpoint(endpoint: string): this {
404
+ if (!endpoint || typeof endpoint !== 'string') {
405
+ throw new RevenexxException('Endpoint must be a valid string');
406
+ }
407
+
408
+ if (!endpoint.startsWith('http://') && !endpoint.startsWith('https://')) {
409
+ throw new RevenexxException('Invalid endpoint URL: ' + endpoint);
410
+ }
411
+
412
+ this.config.endpoint = endpoint;
413
+ this.config.endpointRealtime = endpoint.replace('https://', 'wss://').replace('http://', 'ws://');
414
+
415
+ return this;
416
+ }
417
+
418
+ /**
419
+ * Set Realtime Endpoint
420
+ *
421
+ * @param {string} endpointRealtime
422
+ *
423
+ * @returns {this}
424
+ */
425
+ setEndpointRealtime(endpointRealtime: string): this {
426
+ if (!endpointRealtime || typeof endpointRealtime !== 'string') {
427
+ throw new RevenexxException('Endpoint must be a valid string');
428
+ }
429
+
430
+ if (!endpointRealtime.startsWith('ws://') && !endpointRealtime.startsWith('wss://')) {
431
+ throw new RevenexxException('Invalid realtime endpoint URL: ' + endpointRealtime);
432
+ }
433
+
434
+ this.config.endpointRealtime = endpointRealtime;
435
+ return this;
436
+ }
437
+
438
+ /**
439
+ * Set Tenant
440
+ *
441
+ * The tenant slug your requests are scoped to, sent as the
442
+ * X-Revenexx-Tenant header on every request.
443
+ *
444
+ * @param value string
445
+ *
446
+ * @return {this}
447
+ */
448
+ setTenant(value: string): this {
449
+ this.headers['X-Revenexx-Tenant'] = value;
450
+ this.config.tenant = value;
451
+ return this;
452
+ }
453
+
454
+ /**
455
+ * Set ApiKeyAuth
456
+ *
457
+ * A gateway-managed scoped API key (rvxk_…).
458
+ *
459
+ * @param value string
460
+ *
461
+ * @return {this}
462
+ */
463
+ setApiKeyAuth(value: string): this {
464
+ this.headers['X-Revenexx-Api-Key'] = value;
465
+ this.config.apikeyauth = value;
466
+ return this;
467
+ }
468
+ /**
469
+ * Set BearerAuth
470
+ *
471
+ * A Zitadel-issued JWT (Cockpit / interactive callers).
472
+ *
473
+ * @param value string
474
+ *
475
+ * @return {this}
476
+ */
477
+ setBearerAuth(value: string): this {
478
+ this.headers['Authorization'] = value;
479
+ this.config.bearerauth = value;
480
+ return this;
481
+ }
482
+
483
+ private realtime: Realtime = {
484
+ socket: undefined,
485
+ timeout: undefined,
486
+ heartbeat: undefined,
487
+ url: '',
488
+ channels: new Set(),
489
+ queries: new Set(),
490
+ subscriptions: new Map(),
491
+ slotToSubscriptionId: new Map(),
492
+ subscriptionIdToSlot: new Map(),
493
+ subscriptionsCounter: 0,
494
+ reconnect: true,
495
+ reconnectAttempts: 0,
496
+ lastMessage: undefined,
497
+ connect: () => {
498
+ clearTimeout(this.realtime.timeout);
499
+ this.realtime.timeout = window?.setTimeout(() => {
500
+ this.realtime.createSocket();
501
+ }, 50);
502
+ },
503
+ getTimeout: () => {
504
+ switch (true) {
505
+ case this.realtime.reconnectAttempts < 5:
506
+ return 1000;
507
+ case this.realtime.reconnectAttempts < 15:
508
+ return 5000;
509
+ case this.realtime.reconnectAttempts < 100:
510
+ return 10_000;
511
+ default:
512
+ return 60_000;
513
+ }
514
+ },
515
+ createHeartbeat: () => {
516
+ if (this.realtime.heartbeat) {
517
+ clearTimeout(this.realtime.heartbeat);
518
+ }
519
+
520
+ this.realtime.heartbeat = window?.setInterval(() => {
521
+ this.realtime.socket?.send(JSONbig.stringify({
522
+ type: 'ping'
523
+ }));
524
+ }, 20_000);
525
+ },
526
+ createSocket: () => {
527
+ if (this.realtime.subscriptions.size < 1) {
528
+ this.realtime.reconnect = false;
529
+ this.realtime.socket?.close();
530
+ return;
531
+ }
532
+
533
+ const encodedProject = encodeURIComponent(this.config.tenant ?? '');
534
+ let queryParams = 'project=' + encodedProject;
535
+
536
+ this.realtime.channels.forEach(channel => {
537
+ queryParams += '&channels[]=' + encodeURIComponent(channel);
538
+ });
539
+
540
+ // Per-subscription queries: channel[slot][]=query so server can route events by subscription
541
+ const selectAllQuery = Query.select(['*']).toString();
542
+ this.realtime.subscriptions.forEach((sub, slot) => {
543
+ const queries = sub.queries.length > 0 ? sub.queries : [selectAllQuery];
544
+ sub.channels.forEach(channel => {
545
+ queries.forEach(query => {
546
+ queryParams += '&' + encodeURIComponent(channel) + '[' + slot + '][]=' + encodeURIComponent(query);
547
+ });
548
+ });
549
+ });
550
+
551
+ const url = this.config.endpointRealtime + '/realtime?' + queryParams;
552
+
553
+ if (
554
+ url !== this.realtime.url || // Check if URL is present
555
+ !this.realtime.socket || // Check if WebSocket has not been created
556
+ this.realtime.socket?.readyState > WebSocket.OPEN // Check if WebSocket is CLOSING (3) or CLOSED (4)
557
+ ) {
558
+ if (
559
+ this.realtime.socket &&
560
+ this.realtime.socket?.readyState < WebSocket.CLOSING // Close WebSocket if it is CONNECTING (0) or OPEN (1)
561
+ ) {
562
+ this.realtime.reconnect = false;
563
+ this.realtime.socket.close();
564
+ }
565
+
566
+ this.realtime.url = url;
567
+ this.realtime.socket = new WebSocket(url);
568
+ this.realtime.socket.addEventListener('message', this.realtime.onMessage);
569
+ this.realtime.socket.addEventListener('open', _event => {
570
+ this.realtime.reconnectAttempts = 0;
571
+ this.realtime.createHeartbeat();
572
+ });
573
+ this.realtime.socket.addEventListener('close', event => {
574
+ if (
575
+ !this.realtime.reconnect ||
576
+ (
577
+ this.realtime?.lastMessage?.type === 'error' && // Check if last message was of type error
578
+ (<RealtimeResponseError>this.realtime?.lastMessage.data).code === 1008 // Check for policy violation 1008
579
+ )
580
+ ) {
581
+ this.realtime.reconnect = true;
582
+ return;
583
+ }
584
+
585
+ const timeout = this.realtime.getTimeout();
586
+ console.error(`Realtime got disconnected. Reconnect will be attempted in ${timeout / 1000} seconds.`, event.reason);
587
+
588
+ setTimeout(() => {
589
+ this.realtime.reconnectAttempts++;
590
+ this.realtime.createSocket();
591
+ }, timeout);
592
+ })
593
+ }
594
+ },
595
+ onMessage: (event) => {
596
+ try {
597
+ const message: RealtimeResponse = JSONbig.parse(event.data);
598
+ this.realtime.lastMessage = message;
599
+ switch (message.type) {
600
+ case 'connected': {
601
+ const messageData = <RealtimeResponseConnected>message.data;
602
+ if (messageData?.subscriptions) {
603
+ this.realtime.slotToSubscriptionId.clear();
604
+ this.realtime.subscriptionIdToSlot.clear();
605
+ for (const [slotStr, subscriptionId] of Object.entries(messageData.subscriptions)) {
606
+ const slot = Number(slotStr);
607
+ if (!isNaN(slot) && typeof subscriptionId === 'string') {
608
+ this.realtime.slotToSubscriptionId.set(slot, subscriptionId);
609
+ this.realtime.subscriptionIdToSlot.set(subscriptionId, slot);
610
+ }
611
+ }
612
+ }
613
+
614
+ let session;
615
+ if (!session) {
616
+ const cookie = JSONbig.parse(window.localStorage.getItem('cookieFallback') ?? '{}');
617
+ session = cookie?.[`a_session_${this.config.tenant}`];
618
+ }
619
+ if (session && !messageData?.user) {
620
+ this.realtime.socket?.send(JSONbig.stringify(<RealtimeRequest>{
621
+ type: 'authentication',
622
+ data: {
623
+ session
624
+ }
625
+ }));
626
+ }
627
+ break;
628
+ }
629
+ case 'event': {
630
+ const data = <RealtimeResponseEvent<unknown>>message.data;
631
+ if (!data?.channels) break;
632
+
633
+ const eventSubIds = data.subscriptions;
634
+ if (eventSubIds && eventSubIds.length > 0) {
635
+ for (const subscriptionId of eventSubIds) {
636
+ const slot = this.realtime.subscriptionIdToSlot.get(subscriptionId);
637
+ if (slot !== undefined) {
638
+ const subscription = this.realtime.subscriptions.get(slot);
639
+ if (subscription) {
640
+ setTimeout(() => subscription.callback(data));
641
+ }
642
+ }
643
+ }
644
+ } else {
645
+ const isSubscribed = data.channels.some(channel => this.realtime.channels.has(channel));
646
+ if (!isSubscribed) break;
647
+ this.realtime.subscriptions.forEach(subscription => {
648
+ if (data.channels.some(channel => subscription.channels.includes(channel))) {
649
+ setTimeout(() => subscription.callback(data));
650
+ }
651
+ });
652
+ }
653
+ break;
654
+ }
655
+ case 'pong':
656
+ break; // Handle pong response if needed
657
+ case 'error':
658
+ throw message.data;
659
+ default:
660
+ break;
661
+ }
662
+ } catch (e) {
663
+ console.error(e);
664
+ }
665
+ },
666
+ cleanUp: (channels, queries) => {
667
+ this.realtime.channels.forEach(channel => {
668
+ if (channels.includes(channel)) {
669
+ let found = Array.from(this.realtime.subscriptions).some(([_key, subscription] )=> {
670
+ return subscription.channels.includes(channel);
671
+ })
672
+
673
+ if (!found) {
674
+ this.realtime.channels.delete(channel);
675
+ }
676
+ }
677
+ })
678
+
679
+ this.realtime.queries.forEach(query => {
680
+ if (queries.includes(query)) {
681
+ let found = Array.from(this.realtime.subscriptions).some(([_key, subscription]) => {
682
+ return subscription.queries?.includes(query);
683
+ });
684
+
685
+ if (!found) {
686
+ this.realtime.queries.delete(query);
687
+ }
688
+ }
689
+ })
690
+ }
691
+ }
692
+
693
+ /**
694
+ * Subscribes to Revenexx events and passes you the payload in realtime.
695
+ *
696
+ * @deprecated Use the Realtime service instead.
697
+ * @see Realtime
698
+ *
699
+ * @param {string|string[]|Channel<any>|ActionableChannel|ResolvedChannel|(Channel<any>|ActionableChannel|ResolvedChannel)[]} channels
700
+ * Channel to subscribe - pass a single channel as a string or Channel builder instance, or multiple with an array.
701
+ *
702
+ * Possible channels are:
703
+ * - account
704
+ * - collections
705
+ * - collections.[ID]
706
+ * - collections.[ID].documents
707
+ * - documents
708
+ * - documents.[ID]
709
+ * - files
710
+ * - files.[ID]
711
+ * - executions
712
+ * - executions.[ID]
713
+ * - functions.[ID]
714
+ * - teams
715
+ * - teams.[ID]
716
+ * - memberships
717
+ * - memberships.[ID]
718
+ *
719
+ * You can also use Channel builders:
720
+ * - Channel.database('db').collection('col').document('doc').create()
721
+ * - Channel.bucket('bucket').file('file').update()
722
+ * - Channel.function('func').execution('exec').delete()
723
+ * - Channel.team('team').create()
724
+ * - Channel.membership('membership').update()
725
+ * @param {(payload: RealtimeMessage) => void} callback Is called on every realtime update.
726
+ * @returns {() => void} Unsubscribes from events.
727
+ */
728
+ subscribe<T extends unknown>(
729
+ channels: string | string[] | Channel<any> | ActionableChannel | ResolvedChannel | (Channel<any> | ActionableChannel | ResolvedChannel)[],
730
+ callback: (payload: RealtimeResponseEvent<T>) => void,
731
+ queries: (string | Query)[] = []
732
+ ): () => void {
733
+ const channelArray = Array.isArray(channels) ? channels : [channels];
734
+ // Convert Channel instances to strings
735
+ const channelStrings = channelArray.map(ch => {
736
+ if (typeof ch === 'string') {
737
+ return ch;
738
+ }
739
+ // All Channel instances have toString() method
740
+ if (ch && typeof (ch as Channel<any>).toString === 'function') {
741
+ return (ch as Channel<any>).toString();
742
+ }
743
+ // Fallback to generic string conversion
744
+ return String(ch);
745
+ });
746
+ channelStrings.forEach(channel => this.realtime.channels.add(channel));
747
+
748
+ const queryStrings = (queries ?? []).map(q => typeof q === 'string' ? q : q.toString());
749
+ queryStrings.forEach(query => this.realtime.queries.add(query));
750
+
751
+ const counter = this.realtime.subscriptionsCounter++;
752
+ this.realtime.subscriptions.set(counter, {
753
+ channels: channelStrings,
754
+ queries: queryStrings,
755
+ callback
756
+ });
757
+
758
+ this.realtime.connect();
759
+
760
+ return () => {
761
+ this.realtime.subscriptions.delete(counter);
762
+ this.realtime.cleanUp(channelStrings, queryStrings);
763
+ this.realtime.connect();
764
+ }
765
+ }
766
+
767
+ prepareRequest(method: string, url: URL, headers: Headers = {}, params: Payload = {}): { uri: string, options: RequestInit } {
768
+ method = method.toUpperCase();
769
+
770
+ headers = Object.assign({}, this.headers, headers);
771
+
772
+ if (typeof window !== 'undefined' && window.localStorage) {
773
+ const cookieFallback = window.localStorage.getItem('cookieFallback');
774
+ if (cookieFallback) {
775
+ headers['X-Fallback-Cookies'] = cookieFallback;
776
+ }
777
+ }
778
+
779
+ let options: RequestInit = {
780
+ method,
781
+ headers,
782
+ };
783
+
784
+ if (headers['X-Revenexx-Dev-Key'] === undefined) {
785
+ options.credentials = 'include';
786
+ }
787
+
788
+ if (method === 'GET') {
789
+ for (const [key, value] of Object.entries(Client.flatten(params))) {
790
+ url.searchParams.append(key, value);
791
+ }
792
+ } else {
793
+ switch (headers['content-type']) {
794
+ case 'application/json':
795
+ options.body = JSONbig.stringify(params);
796
+ break;
797
+
798
+ case 'multipart/form-data':
799
+ const formData = new FormData();
800
+
801
+ for (const [key, value] of Object.entries(params)) {
802
+ if (value instanceof File) {
803
+ formData.append(key, value, value.name);
804
+ } else if (Array.isArray(value)) {
805
+ for (const nestedValue of value) {
806
+ formData.append(`${key}[]`, nestedValue);
807
+ }
808
+ } else {
809
+ formData.append(key, value);
810
+ }
811
+ }
812
+
813
+ options.body = formData;
814
+ delete headers['content-type'];
815
+ break;
816
+ }
817
+ }
818
+
819
+ return { uri: url.toString(), options };
820
+ }
821
+
822
+ async chunkedUpload(method: string, url: URL, headers: Headers = {}, originalPayload: Payload = {}, onProgress: (progress: UploadProgress) => void) {
823
+ const [fileParam, file] = Object.entries(originalPayload).find(([_, value]) => value instanceof File) ?? [];
824
+
825
+ if (!file || !fileParam) {
826
+ throw new Error('File not found in payload');
827
+ }
828
+
829
+ if (file.size <= Client.CHUNK_SIZE) {
830
+ return await this.call(method, url, headers, originalPayload);
831
+ }
832
+
833
+ let start = 0;
834
+ let response = null;
835
+
836
+ while (start < file.size) {
837
+ let end = start + Client.CHUNK_SIZE; // Prepare end for the next chunk
838
+ if (end >= file.size) {
839
+ end = file.size; // Adjust for the last chunk to include the last byte
840
+ }
841
+
842
+ headers['content-range'] = `bytes ${start}-${end-1}/${file.size}`;
843
+ const chunk = file.slice(start, end);
844
+
845
+ let payload = { ...originalPayload };
846
+ payload[fileParam] = new File([chunk], file.name);
847
+
848
+ response = await this.call(method, url, headers, payload);
849
+
850
+ if (onProgress && typeof onProgress === 'function') {
851
+ onProgress({
852
+ $id: response.$id,
853
+ progress: Math.round((end / file.size) * 100),
854
+ sizeUploaded: end,
855
+ chunksTotal: Math.ceil(file.size / Client.CHUNK_SIZE),
856
+ chunksUploaded: Math.ceil(end / Client.CHUNK_SIZE)
857
+ });
858
+ }
859
+
860
+ if (response && response.$id) {
861
+ headers['x-revenexx api — revenexx-id'] = response.$id;
862
+ }
863
+
864
+ start = end;
865
+ }
866
+
867
+ return response;
868
+ }
869
+
870
+ async ping(): Promise<string> {
871
+ return this.call('GET', new URL(this.config.endpoint + '/ping'));
872
+ }
873
+
874
+ async call(method: string, url: URL, headers: Headers = {}, params: Payload = {}, responseType = 'json'): Promise<any> {
875
+ const { uri, options } = this.prepareRequest(method, url, headers, params);
876
+
877
+ let data: any = null;
878
+
879
+ const response = await fetch(uri, options);
880
+
881
+ // type opaque: No-CORS, different-origin response (CORS-issue)
882
+ if (response.type === 'opaque') {
883
+ throw new RevenexxException(
884
+ `Invalid Origin. Register your new client (${window.location.host}) as a new Web platform on your project console dashboard`,
885
+ 403,
886
+ "forbidden",
887
+ ""
888
+ );
889
+ }
890
+
891
+ const warnings = response.headers.get('x-revenexx-warning');
892
+ if (warnings) {
893
+ warnings.split(';').forEach((warning: string) => console.warn('Warning: ' + warning));
894
+ }
895
+
896
+ if (response.headers.get('content-type')?.includes('application/json')) {
897
+ data = JSONbig.parse(await response.text());
898
+ } else if (responseType === 'arrayBuffer') {
899
+ data = await response.arrayBuffer();
900
+ } else {
901
+ data = {
902
+ message: await response.text()
903
+ };
904
+ }
905
+
906
+ if (400 <= response.status) {
907
+ let responseText = '';
908
+ if (response.headers.get('content-type')?.includes('application/json') || responseType === 'arrayBuffer') {
909
+ responseText = JSONbig.stringify(data);
910
+ } else {
911
+ responseText = data?.message;
912
+ }
913
+ throw new RevenexxException(data?.message, response.status, data?.type, responseText);
914
+ }
915
+
916
+ const cookieFallback = response.headers.get('X-Fallback-Cookies');
917
+
918
+ if (typeof window !== 'undefined' && window.localStorage && cookieFallback) {
919
+ window.console.warn('Revenexx is using localStorage for session management. Increase your security by adding a custom domain as your API endpoint.');
920
+ window.localStorage.setItem('cookieFallback', cookieFallback);
921
+ }
922
+
923
+ if (data && typeof data === 'object') {
924
+ data.toString = () => JSONbig.stringify(data);
925
+ }
926
+
927
+ return data;
928
+ }
929
+
930
+ static flatten(data: Payload, prefix = ''): Payload {
931
+ let output: Payload = {};
932
+
933
+ for (const [key, value] of Object.entries(data)) {
934
+ let finalKey = prefix ? prefix + '[' + key +']' : key;
935
+ if (Array.isArray(value)) {
936
+ output = { ...output, ...Client.flatten(value, finalKey) };
937
+ } else {
938
+ output[finalKey] = value;
939
+ }
940
+ }
941
+
942
+ return output;
943
+ }
944
+ }
945
+
946
+ export { Client, RevenexxException };
947
+ export { Query } from './query';
948
+ export type { Models, Payload, UploadProgress };
949
+ export type { RealtimeResponseEvent };
950
+ export type { QueryTypes, QueryTypesList } from './query';