node-appwrite 4.0.0 → 5.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. package/README.md +2 -2
  2. package/docs/examples/account/update-session.md +20 -0
  3. package/docs/examples/functions/{create-tag.md → create-deployment.md} +1 -1
  4. package/docs/examples/functions/{delete-tag.md → delete-deployment.md} +1 -1
  5. package/docs/examples/functions/{update-tag.md → get-deployment.md} +1 -1
  6. package/docs/examples/functions/{list-tags.md → list-deployments.md} +1 -1
  7. package/docs/examples/functions/retry-build.md +20 -0
  8. package/docs/examples/functions/{get-tag.md → update-deployment.md} +1 -1
  9. package/docs/examples/storage/create-bucket.md +20 -0
  10. package/docs/examples/storage/create-file.md +1 -1
  11. package/docs/examples/storage/delete-bucket.md +20 -0
  12. package/docs/examples/storage/delete-file.md +1 -1
  13. package/docs/examples/storage/get-bucket.md +20 -0
  14. package/docs/examples/storage/get-file-download.md +1 -1
  15. package/docs/examples/storage/get-file-preview.md +1 -1
  16. package/docs/examples/storage/get-file-view.md +1 -1
  17. package/docs/examples/storage/get-file.md +1 -1
  18. package/docs/examples/storage/list-buckets.md +20 -0
  19. package/docs/examples/storage/list-files.md +1 -1
  20. package/docs/examples/storage/update-bucket.md +20 -0
  21. package/docs/examples/storage/update-file.md +1 -1
  22. package/index.d.ts +411 -151
  23. package/index.js +3 -1
  24. package/lib/client.js +7 -5
  25. package/lib/exception.js +2 -1
  26. package/lib/query.js +2 -0
  27. package/lib/services/account.js +28 -3
  28. package/lib/services/avatars.js +3 -0
  29. package/lib/services/database.js +6 -3
  30. package/lib/services/functions.js +207 -116
  31. package/lib/services/health.js +3 -0
  32. package/lib/services/locale.js +3 -0
  33. package/lib/services/storage.js +379 -33
  34. package/lib/services/teams.js +7 -0
  35. package/lib/services/users.js +6 -2
  36. package/package.json +2 -2
@@ -1,5 +1,8 @@
1
1
  const Service = require('../service.js');
2
2
  const AppwriteException = require('../exception.js');
3
+ const client = require('../client.js');
4
+ const { promisify } = require('util');
5
+ const fs = require('fs');
3
6
 
4
7
  class Functions extends Service {
5
8
 
@@ -248,30 +251,33 @@ class Functions extends Service {
248
251
  }
249
252
 
250
253
  /**
251
- * List Executions
254
+ * List Deployments
252
255
  *
253
- * Get a list of all the current user function execution logs. You can use the
254
- * query params to filter your results. On admin mode, this endpoint will
255
- * return a list of all of the project's executions. [Learn more about
256
- * different API modes](/docs/admin).
256
+ * Get a list of all the project's code deployments. You can use the query
257
+ * params to filter your results.
257
258
  *
258
259
  * @param {string} functionId
260
+ * @param {string} search
259
261
  * @param {number} limit
260
262
  * @param {number} offset
261
- * @param {string} search
262
263
  * @param {string} cursor
263
264
  * @param {string} cursorDirection
265
+ * @param {string} orderType
264
266
  * @throws {AppwriteException}
265
267
  * @returns {Promise}
266
268
  */
267
- async listExecutions(functionId, limit, offset, search, cursor, cursorDirection) {
269
+ async listDeployments(functionId, search, limit, offset, cursor, cursorDirection, orderType) {
268
270
  if (typeof functionId === 'undefined') {
269
271
  throw new AppwriteException('Missing required parameter: "functionId"');
270
272
  }
271
273
 
272
- let path = '/functions/{functionId}/executions'.replace('{functionId}', functionId);
274
+ let path = '/functions/{functionId}/deployments'.replace('{functionId}', functionId);
273
275
  let payload = {};
274
276
 
277
+ if (typeof search !== 'undefined') {
278
+ payload['search'] = search;
279
+ }
280
+
275
281
  if (typeof limit !== 'undefined') {
276
282
  payload['limit'] = limit;
277
283
  }
@@ -280,10 +286,6 @@ class Functions extends Service {
280
286
  payload['offset'] = offset;
281
287
  }
282
288
 
283
- if (typeof search !== 'undefined') {
284
- payload['search'] = search;
285
- }
286
-
287
289
  if (typeof cursor !== 'undefined') {
288
290
  payload['cursor'] = cursor;
289
291
  }
@@ -292,61 +294,141 @@ class Functions extends Service {
292
294
  payload['cursorDirection'] = cursorDirection;
293
295
  }
294
296
 
297
+ if (typeof orderType !== 'undefined') {
298
+ payload['orderType'] = orderType;
299
+ }
300
+
295
301
  return await this.client.call('get', path, {
296
302
  'content-type': 'application/json',
297
303
  }, payload);
298
304
  }
299
305
 
300
306
  /**
301
- * Create Execution
307
+ * Create Deployment
302
308
  *
303
- * Trigger a function execution. The returned object will return you the
304
- * current execution status. You can ping the `Get Execution` endpoint to get
305
- * updates on the current execution status. Once this endpoint is called, your
306
- * function execution process will start asynchronously.
309
+ * Create a new function code deployment. Use this endpoint to upload a new
310
+ * version of your code function. To execute your newly uploaded code, you'll
311
+ * need to update the function's deployment to use your new deployment UID.
312
+ *
313
+ * This endpoint accepts a tar.gz file compressed with your code. Make sure to
314
+ * include any dependencies your code has within the compressed file. You can
315
+ * learn more about code packaging in the [Appwrite Cloud Functions
316
+ * tutorial](/docs/functions).
317
+ *
318
+ * Use the "command" param to set the entry point used to execute your code.
307
319
  *
308
320
  * @param {string} functionId
309
- * @param {string} data
321
+ * @param {string} entrypoint
322
+ * @param {string} code
323
+ * @param {boolean} activate
310
324
  * @throws {AppwriteException}
311
325
  * @returns {Promise}
312
326
  */
313
- async createExecution(functionId, data) {
327
+ async createDeployment(functionId, entrypoint, code, activate, onProgress = () => {}) {
314
328
  if (typeof functionId === 'undefined') {
315
329
  throw new AppwriteException('Missing required parameter: "functionId"');
316
330
  }
317
331
 
318
- let path = '/functions/{functionId}/executions'.replace('{functionId}', functionId);
332
+ if (typeof entrypoint === 'undefined') {
333
+ throw new AppwriteException('Missing required parameter: "entrypoint"');
334
+ }
335
+
336
+ if (typeof code === 'undefined') {
337
+ throw new AppwriteException('Missing required parameter: "code"');
338
+ }
339
+
340
+ if (typeof activate === 'undefined') {
341
+ throw new AppwriteException('Missing required parameter: "activate"');
342
+ }
343
+
344
+ let path = '/functions/{functionId}/deployments'.replace('{functionId}', functionId);
319
345
  let payload = {};
320
346
 
321
- if (typeof data !== 'undefined') {
322
- payload['data'] = data;
347
+ if (typeof entrypoint !== 'undefined') {
348
+ payload['entrypoint'] = entrypoint;
323
349
  }
324
350
 
325
- return await this.client.call('post', path, {
326
- 'content-type': 'application/json',
327
- }, payload);
351
+ if (typeof code !== 'undefined') {
352
+ payload['code'] = code.toString();
353
+ }
354
+
355
+ if (typeof activate !== 'undefined') {
356
+ payload['activate'] = activate.toString();
357
+ }
358
+
359
+ const { size: size } = await promisify(fs.stat)(code);
360
+
361
+ if (size <= client.CHUNK_SIZE) {
362
+ payload['code'] = fs.createReadStream(code);
363
+
364
+ return await this.client.call('post', path, {
365
+ 'content-type': 'multipart/form-data',
366
+ }, payload);
367
+ } else {
368
+ let id = undefined;
369
+ let response = undefined;
370
+
371
+ const totalCounters = Math.ceil(size / client.CHUNK_SIZE);
372
+
373
+ for (let counter = 0; counter < totalCounters; counter++) {
374
+ const start = (counter * client.CHUNK_SIZE);
375
+ const end = Math.min((((counter * client.CHUNK_SIZE) + client.CHUNK_SIZE) - 1), size);
376
+ const headers = {
377
+ 'content-type': 'multipart/form-data',
378
+ 'content-range': 'bytes ' + start + '-' + end + '/' + size
379
+ };
380
+
381
+ if (id) {
382
+ headers['x-appwrite-id'] = id;
383
+ }
384
+
385
+ const stream = fs.createReadStream(code, {
386
+ start,
387
+ end
388
+ });
389
+ payload['code'] = stream;
390
+
391
+ response = await this.client.call('post', path, headers, payload);
392
+
393
+ if (!id) {
394
+ id = response['$id'];
395
+ }
396
+
397
+ if (onProgress !== null) {
398
+ onProgress({
399
+ $id: response['$id'],
400
+ progress: Math.min((counter+1) * client.CHUNK_SIZE, size) / size * 100,
401
+ sizeUploaded: end+1,
402
+ chunksTotal: response['chunksTotal'],
403
+ chunksUploaded: response['chunksUploaded']
404
+ });
405
+ }
406
+ }
407
+
408
+ return response;
409
+ }
328
410
  }
329
411
 
330
412
  /**
331
- * Get Execution
413
+ * Get Deployment
332
414
  *
333
- * Get a function execution log by its unique ID.
415
+ * Get a code deployment by its unique ID.
334
416
  *
335
417
  * @param {string} functionId
336
- * @param {string} executionId
418
+ * @param {string} deploymentId
337
419
  * @throws {AppwriteException}
338
420
  * @returns {Promise}
339
421
  */
340
- async getExecution(functionId, executionId) {
422
+ async getDeployment(functionId, deploymentId) {
341
423
  if (typeof functionId === 'undefined') {
342
424
  throw new AppwriteException('Missing required parameter: "functionId"');
343
425
  }
344
426
 
345
- if (typeof executionId === 'undefined') {
346
- throw new AppwriteException('Missing required parameter: "executionId"');
427
+ if (typeof deploymentId === 'undefined') {
428
+ throw new AppwriteException('Missing required parameter: "deploymentId"');
347
429
  }
348
430
 
349
- let path = '/functions/{functionId}/executions/{executionId}'.replace('{functionId}', functionId).replace('{executionId}', executionId);
431
+ let path = '/functions/{functionId}/deployments/{deploymentId}'.replace('{functionId}', functionId).replace('{deploymentId}', deploymentId);
350
432
  let payload = {};
351
433
 
352
434
  return await this.client.call('get', path, {
@@ -355,190 +437,199 @@ class Functions extends Service {
355
437
  }
356
438
 
357
439
  /**
358
- * Update Function Tag
440
+ * Update Function Deployment
359
441
  *
360
- * Update the function code tag ID using the unique function ID. Use this
361
- * endpoint to switch the code tag that should be executed by the execution
362
- * endpoint.
442
+ * Update the function code deployment ID using the unique function ID. Use
443
+ * this endpoint to switch the code deployment that should be executed by the
444
+ * execution endpoint.
363
445
  *
364
446
  * @param {string} functionId
365
- * @param {string} tag
447
+ * @param {string} deploymentId
366
448
  * @throws {AppwriteException}
367
449
  * @returns {Promise}
368
450
  */
369
- async updateTag(functionId, tag) {
451
+ async updateDeployment(functionId, deploymentId) {
370
452
  if (typeof functionId === 'undefined') {
371
453
  throw new AppwriteException('Missing required parameter: "functionId"');
372
454
  }
373
455
 
374
- if (typeof tag === 'undefined') {
375
- throw new AppwriteException('Missing required parameter: "tag"');
456
+ if (typeof deploymentId === 'undefined') {
457
+ throw new AppwriteException('Missing required parameter: "deploymentId"');
376
458
  }
377
459
 
378
- let path = '/functions/{functionId}/tag'.replace('{functionId}', functionId);
460
+ let path = '/functions/{functionId}/deployments/{deploymentId}'.replace('{functionId}', functionId).replace('{deploymentId}', deploymentId);
379
461
  let payload = {};
380
462
 
381
- if (typeof tag !== 'undefined') {
382
- payload['tag'] = tag;
383
- }
384
-
385
463
  return await this.client.call('patch', path, {
386
464
  'content-type': 'application/json',
387
465
  }, payload);
388
466
  }
389
467
 
390
468
  /**
391
- * List Tags
469
+ * Delete Deployment
392
470
  *
393
- * Get a list of all the project's code tags. You can use the query params to
394
- * filter your results.
471
+ * Delete a code deployment by its unique ID.
395
472
  *
396
473
  * @param {string} functionId
397
- * @param {string} search
398
- * @param {number} limit
399
- * @param {number} offset
400
- * @param {string} cursor
401
- * @param {string} cursorDirection
402
- * @param {string} orderType
474
+ * @param {string} deploymentId
403
475
  * @throws {AppwriteException}
404
476
  * @returns {Promise}
405
477
  */
406
- async listTags(functionId, search, limit, offset, cursor, cursorDirection, orderType) {
478
+ async deleteDeployment(functionId, deploymentId) {
407
479
  if (typeof functionId === 'undefined') {
408
480
  throw new AppwriteException('Missing required parameter: "functionId"');
409
481
  }
410
482
 
411
- let path = '/functions/{functionId}/tags'.replace('{functionId}', functionId);
412
- let payload = {};
413
-
414
- if (typeof search !== 'undefined') {
415
- payload['search'] = search;
483
+ if (typeof deploymentId === 'undefined') {
484
+ throw new AppwriteException('Missing required parameter: "deploymentId"');
416
485
  }
417
486
 
418
- if (typeof limit !== 'undefined') {
419
- payload['limit'] = limit;
420
- }
487
+ let path = '/functions/{functionId}/deployments/{deploymentId}'.replace('{functionId}', functionId).replace('{deploymentId}', deploymentId);
488
+ let payload = {};
421
489
 
422
- if (typeof offset !== 'undefined') {
423
- payload['offset'] = offset;
424
- }
490
+ return await this.client.call('delete', path, {
491
+ 'content-type': 'application/json',
492
+ }, payload);
493
+ }
425
494
 
426
- if (typeof cursor !== 'undefined') {
427
- payload['cursor'] = cursor;
495
+ /**
496
+ * Retry Build
497
+ *
498
+ * @param {string} functionId
499
+ * @param {string} deploymentId
500
+ * @param {string} buildId
501
+ * @throws {AppwriteException}
502
+ * @returns {Promise}
503
+ */
504
+ async retryBuild(functionId, deploymentId, buildId) {
505
+ if (typeof functionId === 'undefined') {
506
+ throw new AppwriteException('Missing required parameter: "functionId"');
428
507
  }
429
508
 
430
- if (typeof cursorDirection !== 'undefined') {
431
- payload['cursorDirection'] = cursorDirection;
509
+ if (typeof deploymentId === 'undefined') {
510
+ throw new AppwriteException('Missing required parameter: "deploymentId"');
432
511
  }
433
512
 
434
- if (typeof orderType !== 'undefined') {
435
- payload['orderType'] = orderType;
513
+ if (typeof buildId === 'undefined') {
514
+ throw new AppwriteException('Missing required parameter: "buildId"');
436
515
  }
437
516
 
438
- return await this.client.call('get', path, {
517
+ let path = '/functions/{functionId}/deployments/{deploymentId}/builds/{buildId}'.replace('{functionId}', functionId).replace('{deploymentId}', deploymentId).replace('{buildId}', buildId);
518
+ let payload = {};
519
+
520
+ return await this.client.call('post', path, {
439
521
  'content-type': 'application/json',
440
522
  }, payload);
441
523
  }
442
524
 
443
525
  /**
444
- * Create Tag
526
+ * List Executions
445
527
  *
446
- * Create a new function code tag. Use this endpoint to upload a new version
447
- * of your code function. To execute your newly uploaded code, you'll need to
448
- * update the function's tag to use your new tag UID.
449
- *
450
- * This endpoint accepts a tar.gz file compressed with your code. Make sure to
451
- * include any dependencies your code has within the compressed file. You can
452
- * learn more about code packaging in the [Appwrite Cloud Functions
453
- * tutorial](/docs/functions).
454
- *
455
- * Use the "command" param to set the entry point used to execute your code.
528
+ * Get a list of all the current user function execution logs. You can use the
529
+ * query params to filter your results. On admin mode, this endpoint will
530
+ * return a list of all of the project's executions. [Learn more about
531
+ * different API modes](/docs/admin).
456
532
  *
457
533
  * @param {string} functionId
458
- * @param {string} command
459
- * @param {File} code
534
+ * @param {number} limit
535
+ * @param {number} offset
536
+ * @param {string} search
537
+ * @param {string} cursor
538
+ * @param {string} cursorDirection
460
539
  * @throws {AppwriteException}
461
540
  * @returns {Promise}
462
541
  */
463
- async createTag(functionId, command, code) {
542
+ async listExecutions(functionId, limit, offset, search, cursor, cursorDirection) {
464
543
  if (typeof functionId === 'undefined') {
465
544
  throw new AppwriteException('Missing required parameter: "functionId"');
466
545
  }
467
546
 
468
- if (typeof command === 'undefined') {
469
- throw new AppwriteException('Missing required parameter: "command"');
547
+ let path = '/functions/{functionId}/executions'.replace('{functionId}', functionId);
548
+ let payload = {};
549
+
550
+ if (typeof limit !== 'undefined') {
551
+ payload['limit'] = limit;
470
552
  }
471
553
 
472
- if (typeof code === 'undefined') {
473
- throw new AppwriteException('Missing required parameter: "code"');
554
+ if (typeof offset !== 'undefined') {
555
+ payload['offset'] = offset;
474
556
  }
475
557
 
476
- let path = '/functions/{functionId}/tags'.replace('{functionId}', functionId);
477
- let payload = {};
558
+ if (typeof search !== 'undefined') {
559
+ payload['search'] = search;
560
+ }
478
561
 
479
- if (typeof command !== 'undefined') {
480
- payload['command'] = command;
562
+ if (typeof cursor !== 'undefined') {
563
+ payload['cursor'] = cursor;
481
564
  }
482
565
 
483
- if (typeof code !== 'undefined') {
484
- payload['code'] = code;
566
+ if (typeof cursorDirection !== 'undefined') {
567
+ payload['cursorDirection'] = cursorDirection;
485
568
  }
486
569
 
487
- return await this.client.call('post', path, {
488
- 'content-type': 'multipart/form-data',
570
+ return await this.client.call('get', path, {
571
+ 'content-type': 'application/json',
489
572
  }, payload);
490
573
  }
491
574
 
492
575
  /**
493
- * Get Tag
576
+ * Create Execution
494
577
  *
495
- * Get a code tag by its unique ID.
578
+ * Trigger a function execution. The returned object will return you the
579
+ * current execution status. You can ping the `Get Execution` endpoint to get
580
+ * updates on the current execution status. Once this endpoint is called, your
581
+ * function execution process will start asynchronously.
496
582
  *
497
583
  * @param {string} functionId
498
- * @param {string} tagId
584
+ * @param {string} data
585
+ * @param {boolean} async
499
586
  * @throws {AppwriteException}
500
587
  * @returns {Promise}
501
588
  */
502
- async getTag(functionId, tagId) {
589
+ async createExecution(functionId, data, async) {
503
590
  if (typeof functionId === 'undefined') {
504
591
  throw new AppwriteException('Missing required parameter: "functionId"');
505
592
  }
506
593
 
507
- if (typeof tagId === 'undefined') {
508
- throw new AppwriteException('Missing required parameter: "tagId"');
594
+ let path = '/functions/{functionId}/executions'.replace('{functionId}', functionId);
595
+ let payload = {};
596
+
597
+ if (typeof data !== 'undefined') {
598
+ payload['data'] = data;
509
599
  }
510
600
 
511
- let path = '/functions/{functionId}/tags/{tagId}'.replace('{functionId}', functionId).replace('{tagId}', tagId);
512
- let payload = {};
601
+ if (typeof async !== 'undefined') {
602
+ payload['async'] = async;
603
+ }
513
604
 
514
- return await this.client.call('get', path, {
605
+ return await this.client.call('post', path, {
515
606
  'content-type': 'application/json',
516
607
  }, payload);
517
608
  }
518
609
 
519
610
  /**
520
- * Delete Tag
611
+ * Get Execution
521
612
  *
522
- * Delete a code tag by its unique ID.
613
+ * Get a function execution log by its unique ID.
523
614
  *
524
615
  * @param {string} functionId
525
- * @param {string} tagId
616
+ * @param {string} executionId
526
617
  * @throws {AppwriteException}
527
618
  * @returns {Promise}
528
619
  */
529
- async deleteTag(functionId, tagId) {
620
+ async getExecution(functionId, executionId) {
530
621
  if (typeof functionId === 'undefined') {
531
622
  throw new AppwriteException('Missing required parameter: "functionId"');
532
623
  }
533
624
 
534
- if (typeof tagId === 'undefined') {
535
- throw new AppwriteException('Missing required parameter: "tagId"');
625
+ if (typeof executionId === 'undefined') {
626
+ throw new AppwriteException('Missing required parameter: "executionId"');
536
627
  }
537
628
 
538
- let path = '/functions/{functionId}/tags/{tagId}'.replace('{functionId}', functionId).replace('{tagId}', tagId);
629
+ let path = '/functions/{functionId}/executions/{executionId}'.replace('{functionId}', functionId).replace('{executionId}', executionId);
539
630
  let payload = {};
540
631
 
541
- return await this.client.call('delete', path, {
632
+ return await this.client.call('get', path, {
542
633
  'content-type': 'application/json',
543
634
  }, payload);
544
635
  }
@@ -1,5 +1,8 @@
1
1
  const Service = require('../service.js');
2
2
  const AppwriteException = require('../exception.js');
3
+ const client = require('../client.js');
4
+ const { promisify } = require('util');
5
+ const fs = require('fs');
3
6
 
4
7
  class Health extends Service {
5
8
 
@@ -1,5 +1,8 @@
1
1
  const Service = require('../service.js');
2
2
  const AppwriteException = require('../exception.js');
3
+ const client = require('../client.js');
4
+ const { promisify } = require('util');
5
+ const fs = require('fs');
3
6
 
4
7
  class Locale extends Service {
5
8