-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathclient.ts
More file actions
758 lines (676 loc) · 24.3 KB
/
client.ts
File metadata and controls
758 lines (676 loc) · 24.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
import { Models } from './models';
import { Service } from './service';
import { Platform } from 'react-native';
import { Query } from './query';
import JSONbigModule from 'json-bigint';
const JSONbigParser = JSONbigModule({ storeAsString: false });
const JSONbigSerializer = JSONbigModule({ useNativeBigInt: true });
const MAX_SAFE = BigInt(Number.MAX_SAFE_INTEGER);
const MIN_SAFE = BigInt(Number.MIN_SAFE_INTEGER);
const MAX_INT64 = BigInt('9223372036854775807');
const MIN_INT64 = BigInt('-9223372036854775808');
function isBigNumber(value: any): boolean {
return value !== null
&& typeof value === 'object'
&& value._isBigNumber === true
&& typeof value.isInteger === 'function'
&& typeof value.toFixed === 'function'
&& typeof value.toNumber === 'function';
}
function reviver(_key: string, value: any): any {
if (isBigNumber(value)) {
if (value.isInteger()) {
const str = value.toFixed();
const bi = BigInt(str);
if (bi >= MIN_SAFE && bi <= MAX_SAFE) {
return Number(str);
}
if (bi >= MIN_INT64 && bi <= MAX_INT64) {
return bi;
}
return value.toNumber();
}
return value.toNumber();
}
return value;
}
const JSONbig = {
parse: (text: string) => JSONbigParser.parse(text, reviver),
stringify: JSONbigSerializer.stringify
};
type Payload = {
[key: string]: any;
}
type Headers = {
[key: string]: string;
}
type RealtimeResponse = {
type: 'error' | 'event' | 'connected' | 'response' | 'pong';
data: RealtimeResponseAuthenticated | RealtimeResponseConnected | RealtimeResponseError | RealtimeResponseEvent<unknown> | undefined;
}
type RealtimeRequest = {
type: 'authentication' | 'subscribe';
data: RealtimeRequestAuthenticate | RealtimeRequestSubscribe[];
}
type RealtimeRequestSubscribe = {
subscriptionId?: string;
channels: string[];
queries: string[];
}
type RealtimeResponseAction = {
to?: string;
success?: boolean;
subscriptions?: Array<{
subscriptionId?: string;
channels?: string[];
queries?: string[];
}>;
}
export type RealtimeResponseEvent<T extends unknown> = {
events: string[];
channels: string[];
timestamp: number;
payload: T;
subscriptions?: string[];
}
type RealtimeResponseError = {
code: number;
message: string;
}
type RealtimeResponseConnected = {
channels: string[];
user?: object;
}
type RealtimeResponseAuthenticated = {
to: string;
success: boolean;
user: object;
}
type RealtimeRequestAuthenticate = {
session: string;
}
type Realtime = {
socket?: WebSocket;
/**
* Timeout for reconnect operations.
*/
timeout?: number;
/**
* Heartbeat interval for the realtime connection.
*/
heartbeat?: number;
url?: string;
lastMessage?: RealtimeResponse;
subscriptions: Map<number, {
channels: string[];
queries: string[];
callback: (payload: RealtimeResponseEvent<any>) => void
}>;
slotToSubscriptionId: Map<number, string>;
subscriptionIdToSlot: Map<string, number>;
pendingSubscribeSlots: number[];
subscriptionsCounter: number;
reconnect: boolean;
reconnectAttempts: number;
getTimeout: () => number;
connect: () => void;
createSocket: () => void;
createHeartbeat: () => void;
sendSubscribeMessage: () => void;
onMessage: (event: MessageEvent) => void;
}
export type UploadProgress = {
$id: string;
progress: number;
sizeUploaded: number;
chunksTotal: number;
chunksUploaded: number;
}
class AppwriteException extends Error {
code: number;
response: string;
type: string;
constructor(message: string, code: number = 0, type: string = '', response: string = '') {
super(message);
this.name = 'AppwriteException';
this.message = message;
this.code = code;
this.type = type;
this.response = response;
}
}
class Client {
config = {
endpoint: 'https://cloud.appwrite.io/v1',
endpointRealtime: '',
project: '',
jwt: '',
locale: '',
session: '',
devkey: '',
impersonateuserid: '',
impersonateuseremail: '',
impersonateuserphone: '',
platform: '',
};
headers: Headers = {
'x-sdk-name': 'React Native',
'x-sdk-platform': 'client',
'x-sdk-language': 'reactnative',
'x-sdk-version': '0.29.0',
'X-Appwrite-Response-Format': '1.9.2',
};
/**
* Get Headers
*
* Returns a copy of the current request headers, including any
* authentication headers. Handle with care.
*
* @returns {Headers}
*/
getHeaders(): Headers {
return { ...this.headers };
}
/**
* Set Endpoint
*
* Your project endpoint
*
* @param {string} endpoint
*
* @returns {this}
*/
setEndpoint(endpoint: string): this {
if (!endpoint || typeof endpoint !== 'string') {
throw new AppwriteException('Endpoint must be a valid string');
}
if (!endpoint.startsWith('http://') && !endpoint.startsWith('https://')) {
throw new AppwriteException('Invalid endpoint URL: ' + endpoint);
}
this.config.endpoint = endpoint;
this.config.endpointRealtime = endpoint.replace('https://', 'wss://').replace('http://', 'ws://');
return this;
}
/**
* Set Realtime Endpoint
*
* @param {string} endpointRealtime
*
* @returns {this}
*/
setEndpointRealtime(endpointRealtime: string): this {
if (!endpointRealtime || typeof endpointRealtime !== 'string') {
throw new AppwriteException('Endpoint must be a valid string');
}
if (!endpointRealtime.startsWith('ws://') && !endpointRealtime.startsWith('wss://')) {
throw new AppwriteException('Invalid realtime endpoint URL: ' + endpointRealtime);
}
this.config.endpointRealtime = endpointRealtime;
return this;
}
/**
* Set platform
*
* Set platform. Will be used as origin for all requests.
*
* @param {string} platform
* @returns {this}
*/
setPlatform(platform: string): this {
this.config.platform = platform;
return this;
}
/**
* Set Project
*
* Your project ID
*
* @param value string
*
* @return {this}
*/
setProject(value: string): this {
this.headers['X-Appwrite-Project'] = value;
this.config.project = value;
return this;
}
/**
* Set JWT
*
* Your secret JSON Web Token
*
* @param value string
*
* @return {this}
*/
setJWT(value: string): this {
this.headers['X-Appwrite-JWT'] = value;
this.config.jwt = value;
return this;
}
/**
* Set Locale
*
* @param value string
*
* @return {this}
*/
setLocale(value: string): this {
this.headers['X-Appwrite-Locale'] = value;
this.config.locale = value;
return this;
}
/**
* Set Session
*
* The user session to authenticate with
*
* @param value string
*
* @return {this}
*/
setSession(value: string): this {
this.headers['X-Appwrite-Session'] = value;
this.config.session = value;
return this;
}
/**
* Set DevKey
*
* Your secret dev API key
*
* @param value string
*
* @return {this}
*/
setDevKey(value: string): this {
this.headers['X-Appwrite-Dev-Key'] = value;
this.config.devkey = value;
return this;
}
/**
* Set ImpersonateUserId
*
* Impersonate a user by ID on an already user-authenticated request. Requires
* the current request to be authenticated as a user with impersonator
* capability; X-Appwrite-Key alone is not sufficient. Impersonator users are
* intentionally granted users.read so they can discover a target before
* impersonation begins. Internal audit logs still attribute actions to the
* original impersonator and record the impersonated target only in internal
* audit payload data.
*
* @param value string
*
* @return {this}
*/
setImpersonateUserId(value: string): this {
this.headers['X-Appwrite-Impersonate-User-Id'] = value;
this.config.impersonateuserid = value;
return this;
}
/**
* Set ImpersonateUserEmail
*
* Impersonate a user by email on an already user-authenticated request.
* Requires the current request to be authenticated as a user with
* impersonator capability; X-Appwrite-Key alone is not sufficient.
* Impersonator users are intentionally granted users.read so they can
* discover a target before impersonation begins. Internal audit logs still
* attribute actions to the original impersonator and record the impersonated
* target only in internal audit payload data.
*
* @param value string
*
* @return {this}
*/
setImpersonateUserEmail(value: string): this {
this.headers['X-Appwrite-Impersonate-User-Email'] = value;
this.config.impersonateuseremail = value;
return this;
}
/**
* Set ImpersonateUserPhone
*
* Impersonate a user by phone on an already user-authenticated request.
* Requires the current request to be authenticated as a user with
* impersonator capability; X-Appwrite-Key alone is not sufficient.
* Impersonator users are intentionally granted users.read so they can
* discover a target before impersonation begins. Internal audit logs still
* attribute actions to the original impersonator and record the impersonated
* target only in internal audit payload data.
*
* @param value string
*
* @return {this}
*/
setImpersonateUserPhone(value: string): this {
this.headers['X-Appwrite-Impersonate-User-Phone'] = value;
this.config.impersonateuserphone = value;
return this;
}
private realtime: Realtime = {
socket: undefined,
timeout: undefined,
heartbeat: undefined,
url: '',
subscriptions: new Map(),
slotToSubscriptionId: new Map(),
subscriptionIdToSlot: new Map(),
pendingSubscribeSlots: [],
subscriptionsCounter: 0,
reconnect: true,
reconnectAttempts: 0,
lastMessage: undefined,
connect: () => {
clearTimeout(this.realtime.timeout);
this.realtime.timeout = window?.setTimeout(() => {
this.realtime.createSocket();
}, 50);
},
getTimeout: () => {
switch (true) {
case this.realtime.reconnectAttempts < 5:
return 1000;
case this.realtime.reconnectAttempts < 15:
return 5000;
case this.realtime.reconnectAttempts < 100:
return 10_000;
default:
return 60_000;
}
},
createHeartbeat: () => {
if (this.realtime.heartbeat) {
clearTimeout(this.realtime.heartbeat);
}
this.realtime.heartbeat = window?.setInterval(() => {
this.realtime.socket?.send(JSONbig.stringify({
type: 'ping'
}));
}, 20_000);
},
createSocket: () => {
if (this.realtime.subscriptions.size < 1) {
this.realtime.reconnect = false;
this.realtime.socket?.close();
return;
}
const channels = new URLSearchParams();
channels.set('project', this.config.project);
const url = this.config.endpointRealtime + '/realtime?' + channels.toString();
if (
url !== this.realtime.url || // Check if URL is present
!this.realtime.socket || // Check if WebSocket has not been created
this.realtime.socket?.readyState > WebSocket.OPEN // Check if WebSocket is CLOSING (3) or CLOSED (4)
) {
if (
this.realtime.socket &&
this.realtime.socket?.readyState < WebSocket.CLOSING // Close WebSocket if it is CONNECTING (0) or OPEN (1)
) {
this.realtime.reconnect = false;
this.realtime.socket.close();
}
this.realtime.url = url;
// @ts-ignore
this.realtime.socket = new WebSocket(url, undefined, {
headers: {
Origin: `appwrite-${Platform.OS}://${this.config.platform}`
}
});
this.realtime.socket.addEventListener('message', this.realtime.onMessage);
this.realtime.socket.addEventListener('open', _event => {
this.realtime.reconnectAttempts = 0;
this.realtime.createHeartbeat();
});
this.realtime.socket.addEventListener('close', event => {
if (
!this.realtime.reconnect ||
(
this.realtime?.lastMessage?.type === 'error' && // Check if last message was of type error
(<RealtimeResponseError>this.realtime?.lastMessage.data).code === 1008 // Check for policy violation 1008
)
) {
this.realtime.reconnect = true;
return;
}
const timeout = this.realtime.getTimeout();
console.error(`Realtime got disconnected. Reconnect will be attempted in ${timeout / 1000} seconds.`, event.reason);
setTimeout(() => {
this.realtime.reconnectAttempts++;
this.realtime.createSocket();
}, timeout);
})
} else if (this.realtime.socket?.readyState === WebSocket.OPEN) {
// URL is unchanged; re-send subscribe message to apply updated queries.
this.realtime.sendSubscribeMessage();
}
},
sendSubscribeMessage: () => {
if (!this.realtime.socket || this.realtime.socket.readyState !== WebSocket.OPEN) {
return;
}
const rows: RealtimeRequestSubscribe[] = [];
this.realtime.pendingSubscribeSlots = [];
this.realtime.subscriptions.forEach((sub, slot) => {
const queries = sub.queries ?? [];
const row: RealtimeRequestSubscribe = {
channels: sub.channels,
queries
};
const knownSubscriptionId = this.realtime.slotToSubscriptionId.get(slot);
if (knownSubscriptionId) {
row.subscriptionId = knownSubscriptionId;
}
rows.push(row);
this.realtime.pendingSubscribeSlots.push(slot);
});
if (rows.length < 1) {
return;
}
this.realtime.socket.send(JSONbig.stringify(<RealtimeRequest>{
type: 'subscribe',
data: rows
}));
},
onMessage: (event) => {
try {
const message: RealtimeResponse = JSONbig.parse(event.data);
this.realtime.lastMessage = message;
switch (message.type) {
case 'connected': {
const messageData = <RealtimeResponseConnected>message.data;
let session = this.config.session;
if (!session) {
const cookie = JSONbig.parse(window.localStorage.getItem('cookieFallback') ?? '{}');
session = cookie?.[`a_session_${this.config.project}`];
}
if (session && !messageData?.user) {
this.realtime.socket?.send(JSONbig.stringify(<RealtimeRequest>{
type: 'authentication',
data: {
session
}
}));
}
this.realtime.sendSubscribeMessage();
break;
}
case 'response': {
const action = message.data as RealtimeResponseAction;
if (action?.to !== 'subscribe' || !Array.isArray(action.subscriptions)) {
break;
}
action.subscriptions.forEach((subscription, index) => {
const subscriptionId = subscription?.subscriptionId;
const slot = this.realtime.pendingSubscribeSlots[index];
if (!subscriptionId || slot === undefined) {
return;
}
this.realtime.slotToSubscriptionId.set(slot, subscriptionId);
this.realtime.subscriptionIdToSlot.set(subscriptionId, slot);
});
break;
}
case 'event':
const data = <RealtimeResponseEvent<unknown>>message.data;
if (data?.channels) {
if (data.subscriptions && data.subscriptions.length > 0) {
data.subscriptions.forEach((subscriptionId) => {
const slot = this.realtime.subscriptionIdToSlot.get(subscriptionId);
if (slot !== undefined) {
const subscription = this.realtime.subscriptions.get(slot);
if (subscription) {
setTimeout(() => subscription.callback(data));
}
}
});
} else {
this.realtime.subscriptions.forEach(subscription => {
if (data.channels.some(channel => subscription.channels.includes(channel))) {
setTimeout(() => subscription.callback(data));
}
});
}
}
break;
case 'pong':
break; // Handle pong response if needed
case 'error':
throw message.data;
default:
break;
}
} catch (e) {
console.error(e);
}
}
}
/**
* Subscribes to Appwrite events and passes you the payload in realtime.
*
* @param {string|string[]} channels
* Channel to subscribe - pass a single channel as a string or multiple with an array of strings.
*
* Possible channels are:
* - account
* - collections
* - collections.[ID]
* - collections.[ID].documents
* - documents
* - documents.[ID]
* - files
* - files.[ID]
* - executions
* - executions.[ID]
* - functions.[ID]
* - teams
* - teams.[ID]
* - memberships
* - memberships.[ID]
* @param {(payload: RealtimeMessage) => void} callback Is called on every realtime update.
* @returns {() => void} Unsubscribes from events.
*/
subscribe<T extends unknown>(
channels: string | string[],
callback: (payload: RealtimeResponseEvent<T>) => void,
queries: (string | Query)[] = []
): () => void {
let channelArray = typeof channels === 'string' ? [channels] : channels;
const queryStrings = (queries ?? []).map(q => typeof q === 'string' ? q : q.toString());
const counter = this.realtime.subscriptionsCounter++;
this.realtime.subscriptions.set(counter, {
channels: channelArray,
queries: queryStrings,
callback
});
this.realtime.connect();
return () => {
const subscriptionId = this.realtime.slotToSubscriptionId.get(counter);
this.realtime.subscriptions.delete(counter);
this.realtime.slotToSubscriptionId.delete(counter);
if (subscriptionId) {
this.realtime.subscriptionIdToSlot.delete(subscriptionId);
}
this.realtime.connect();
}
}
async call(method: string, url: URL, headers: Headers = {}, params: Payload = {}, responseType = 'json'): Promise<any> {
method = method.toUpperCase();
headers = Object.assign({}, this.headers, headers);
headers.Origin = `appwrite-${Platform.OS}://${this.config.platform}`
let options: RequestInit = {
method,
headers,
};
if (headers['X-Appwrite-Dev-Key'] === undefined) {
options.credentials = 'include';
}
if (method === 'GET') {
for (const [key, value] of Object.entries(Service.flatten(params))) {
url.searchParams.append(key, value);
}
} else {
switch (headers['content-type']) {
case 'application/json':
options.body = JSONbig.stringify(params);
break;
case 'multipart/form-data':
let formData = new FormData();
for (const key in params) {
if (Array.isArray(params[key])) {
params[key].forEach((value: any) => {
formData.append(key + '[]', value);
})
} else {
formData.append(key, params[key]);
}
}
options.body = formData;
delete headers['content-type'];
break;
}
}
try {
let data = null;
const response = await fetch(url.toString(), options);
const warnings = response.headers.get('x-appwrite-warning');
if (warnings) {
warnings.split(';').forEach((warning: string) => console.warn('Warning: ' + warning));
}
if (response.headers.get('content-type')?.includes('application/json')) {
data = JSONbig.parse(await response.text());
} else if (responseType === 'arrayBuffer') {
data = await response.arrayBuffer();
} else {
data = {
message: await response.text()
};
}
if (400 <= response.status) {
let responseText = '';
if (response.headers.get('content-type')?.includes('application/json')) {
responseText = JSONbig.stringify(data);
} else {
responseText = data?.message;
}
throw new AppwriteException(data?.message, response.status, data?.type, responseText);
}
const cookieFallback = response.headers.get('X-Fallback-Cookies');
if (typeof window !== 'undefined' && window.localStorage && cookieFallback) {
window.console.warn('Appwrite is using localStorage for session management. Increase your security by adding a custom domain as your API endpoint.');
window.localStorage.setItem('cookieFallback', cookieFallback);
}
if (data && typeof data === 'object') {
data.toString = () => JSONbig.stringify(data);
}
return data;
} catch (e) {
if (e instanceof AppwriteException) {
throw e;
}
throw new AppwriteException((<Error>e).message);
}
}
}
export { Client, AppwriteException };
export type { Models, Payload };