cozy-client 60.45.0 → 61.0.1

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.
@@ -87,20 +87,26 @@ var _flagshipCertification = require("./flagship-certification/flagship-certific
87
87
 
88
88
  var _storage = require("./storage");
89
89
 
90
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
91
-
92
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
93
-
94
90
  function _createForOfIteratorHelper(o, allowArrayLike) { var it; if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = o[Symbol.iterator](); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
95
91
 
96
92
  function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
97
93
 
98
94
  function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
99
95
 
96
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
97
+
98
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
99
+
100
100
  var ensureArray = function ensureArray(arr) {
101
101
  return Array.isArray(arr) ? arr : [arr];
102
102
  };
103
103
 
104
+ function getDriveScopedQueryDefinition(definition, driveId) {
105
+ return definition.doctype === _const.DOCTYPE_FILES && driveId && !definition.sharingId ? new _dsl.QueryDefinition(_objectSpread(_objectSpread({}, definition), {}, {
106
+ sharingId: driveId
107
+ })) : definition;
108
+ }
109
+
104
110
  var deprecatedHandler = function deprecatedHandler(msg) {
105
111
  return {
106
112
  get: function get(target, prop) {
@@ -781,7 +787,7 @@ var CozyClient = /*#__PURE__*/function () {
781
787
  * @param {import("./types").ReferenceMap} [references] - References are a special kind of relationship
782
788
  * that is not stored inside the referencer document, they are used for example between a photo
783
789
  * and its album. You should not need to use it normally.
784
- * @param {object} options - Mutation options
790
+ * @param {Partial<import('./types').MutationOptions>} [options] - Mutation options
785
791
  *
786
792
  * @example
787
793
  * ```js
@@ -807,6 +813,7 @@ var CozyClient = /*#__PURE__*/function () {
807
813
  attributes,
808
814
  normalizedDoc,
809
815
  ret,
816
+ mutation,
810
817
  _args4 = arguments;
811
818
 
812
819
  return _regenerator.default.wrap(function _callee4$(_context4) {
@@ -832,9 +839,19 @@ var CozyClient = /*#__PURE__*/function () {
832
839
  throw new Error('Validation failed');
833
840
 
834
841
  case 8:
835
- return _context4.abrupt("return", this.mutate(this.getDocumentSavePlan(normalizedDoc, references), options));
842
+ mutation = this.getDocumentSavePlan(normalizedDoc, references); // References produce a multi-step save plan, represented as an array.
836
843
 
837
- case 9:
844
+ if (!(type === _const.DOCTYPE_FILES && options.driveId && Array.isArray(mutation))) {
845
+ _context4.next = 11;
846
+ break;
847
+ }
848
+
849
+ throw new Error('References are not supported when creating files in a shared drive');
850
+
851
+ case 11:
852
+ return _context4.abrupt("return", this.mutate(mutation, options));
853
+
854
+ case 12:
838
855
  case "end":
839
856
  return _context4.stop();
840
857
  }
@@ -1406,6 +1423,9 @@ var CozyClient = /*#__PURE__*/function () {
1406
1423
  update,
1407
1424
  executeFromStore,
1408
1425
  options,
1426
+ definition,
1427
+ driveId,
1428
+ linkOptions,
1409
1429
  markQuery,
1410
1430
  queryId,
1411
1431
  existingQuery,
@@ -1421,67 +1441,72 @@ var CozyClient = /*#__PURE__*/function () {
1421
1441
  case 0:
1422
1442
  _ref5 = _args11.length > 1 && _args11[1] !== undefined ? _args11[1] : {};
1423
1443
  update = _ref5.update, executeFromStore = _ref5.executeFromStore, options = (0, _objectWithoutProperties2.default)(_ref5, ["update", "executeFromStore"]);
1424
- markQuery = this.performanceApi.mark("client.query(".concat(queryDefinition.doctype, ")"));
1444
+ definition = getDriveScopedQueryDefinition(queryDefinition, options.driveId);
1445
+ driveId = definition.doctype === _const.DOCTYPE_FILES ? definition.sharingId : null;
1446
+ linkOptions = driveId ? _objectSpread(_objectSpread({}, options), {}, {
1447
+ driveId: driveId
1448
+ }) : options;
1449
+ markQuery = this.performanceApi.mark("client.query(".concat(definition.doctype, ")"));
1425
1450
  this.ensureStore();
1426
- queryId = options.as || this.queryIdGenerator.generateId(queryDefinition);
1451
+ queryId = options.as || this.queryIdGenerator.generateId(definition);
1427
1452
  existingQuery = this.disableStoreForQueries ? null : this.getQueryFromState(queryId, options);
1428
1453
 
1429
1454
  if (!(options.enabled !== undefined)) {
1430
- _context11.next = 9;
1455
+ _context11.next = 12;
1431
1456
  break;
1432
1457
  }
1433
1458
 
1434
1459
  if (!('boolean' !== typeof options.enabled)) {
1435
- _context11.next = 9;
1460
+ _context11.next = 12;
1436
1461
  break;
1437
1462
  }
1438
1463
 
1439
- throw new Error("option.enabled should be a boolean for this query: ".concat(JSON.stringify(queryDefinition)));
1464
+ throw new Error("option.enabled should be a boolean for this query: ".concat(JSON.stringify(definition)));
1440
1465
 
1441
- case 9:
1466
+ case 12:
1442
1467
  if (!options.fetchPolicy) {
1443
- _context11.next = 15;
1468
+ _context11.next = 18;
1444
1469
  break;
1445
1470
  }
1446
1471
 
1447
1472
  if (options.as) {
1448
- _context11.next = 12;
1473
+ _context11.next = 15;
1449
1474
  break;
1450
1475
  }
1451
1476
 
1452
1477
  throw new Error('Cannot use `fetchPolicy` without naming the query, please use `as` to name the query');
1453
1478
 
1454
- case 12:
1479
+ case 15:
1455
1480
  shouldFetch = options.fetchPolicy(existingQuery);
1456
1481
 
1457
1482
  if (shouldFetch) {
1458
- _context11.next = 15;
1483
+ _context11.next = 18;
1459
1484
  break;
1460
1485
  }
1461
1486
 
1462
1487
  return _context11.abrupt("return");
1463
1488
 
1464
- case 15:
1489
+ case 18:
1465
1490
  if (!(existingQuery && Object.keys(existingQuery).length > 0)) {
1466
- _context11.next = 18;
1491
+ _context11.next = 21;
1467
1492
  break;
1468
1493
  }
1469
1494
 
1470
1495
  if (!(existingQuery.fetchStatus === 'loading')) {
1471
- _context11.next = 18;
1496
+ _context11.next = 21;
1472
1497
  break;
1473
1498
  }
1474
1499
 
1475
1500
  return _context11.abrupt("return", this._promiseCache.get(function () {
1476
- return (0, _jsonStableStringify.default)(queryDefinition);
1501
+ return (0, _jsonStableStringify.default)(definition);
1477
1502
  }));
1478
1503
 
1479
- case 18:
1504
+ case 21:
1480
1505
  if (!this.disableStoreForQueries) {
1481
- this.ensureQueryExists(queryId, queryDefinition, options);
1506
+ this.ensureQueryExists(queryId, definition, options);
1482
1507
  }
1483
1508
 
1484
- _context11.prev = 19;
1509
+ _context11.prev = 22;
1485
1510
  backgroundFetching = options.backgroundFetching !== undefined ? options.backgroundFetching : this.options.backgroundFetching;
1486
1511
 
1487
1512
  if (!this.disableStoreForQueries) {
@@ -1491,26 +1516,26 @@ var CozyClient = /*#__PURE__*/function () {
1491
1516
  }
1492
1517
 
1493
1518
  requestFn = executeFromStore ? function () {
1494
- return Promise.resolve((0, _store.executeQueryFromState)(_this4.store.getState(), queryDefinition));
1519
+ return Promise.resolve((0, _store.executeQueryFromState)(_this4.store.getState(), definition));
1495
1520
  } : function () {
1496
- return _this4.requestQuery(queryDefinition, options);
1521
+ return _this4.requestQuery(definition, linkOptions);
1497
1522
  };
1498
- _context11.next = 25;
1523
+ _context11.next = 28;
1499
1524
  return this._promiseCache.exec(requestFn, function () {
1500
- return (0, _jsonStableStringify.default)(queryDefinition);
1525
+ return (0, _jsonStableStringify.default)(definition);
1501
1526
  });
1502
1527
 
1503
- case 25:
1528
+ case 28:
1504
1529
  response = _context11.sent;
1505
1530
 
1506
1531
  if (!this.disableStoreForQueries) {
1507
- _context11.next = 28;
1532
+ _context11.next = 31;
1508
1533
  break;
1509
1534
  }
1510
1535
 
1511
1536
  return _context11.abrupt("return", response);
1512
1537
 
1513
- case 28:
1538
+ case 31:
1514
1539
  this.dispatch((0, _store.receiveQueryResult)(queryId, response, {
1515
1540
  update: update,
1516
1541
  backgroundFetching: backgroundFetching
@@ -1522,9 +1547,9 @@ var CozyClient = /*#__PURE__*/function () {
1522
1547
  });
1523
1548
  return _context11.abrupt("return", response);
1524
1549
 
1525
- case 33:
1526
- _context11.prev = 33;
1527
- _context11.t0 = _context11["catch"](19);
1550
+ case 36:
1551
+ _context11.prev = 36;
1552
+ _context11.t0 = _context11["catch"](22);
1528
1553
  this.performanceApi.measure({
1529
1554
  markName: markQuery,
1530
1555
  measureName: "".concat(markQuery, " error"),
@@ -1533,34 +1558,34 @@ var CozyClient = /*#__PURE__*/function () {
1533
1558
  this.dispatch((0, _store.receiveQueryError)(queryId, _context11.t0)); // specific onError
1534
1559
 
1535
1560
  if (!options.onError) {
1536
- _context11.next = 41;
1561
+ _context11.next = 44;
1537
1562
  break;
1538
1563
  }
1539
1564
 
1540
1565
  options.onError(_context11.t0); // defaulted onError
1541
1566
 
1542
- _context11.next = 46;
1567
+ _context11.next = 49;
1543
1568
  break;
1544
1569
 
1545
- case 41:
1570
+ case 44:
1546
1571
  if (!this.options.onError) {
1547
- _context11.next = 45;
1572
+ _context11.next = 48;
1548
1573
  break;
1549
1574
  }
1550
1575
 
1551
1576
  this.options.onError(_context11.t0);
1552
- _context11.next = 46;
1577
+ _context11.next = 49;
1553
1578
  break;
1554
1579
 
1555
- case 45:
1580
+ case 48:
1556
1581
  throw _context11.t0;
1557
1582
 
1558
- case 46:
1583
+ case 49:
1559
1584
  case "end":
1560
1585
  return _context11.stop();
1561
1586
  }
1562
1587
  }
1563
- }, _callee11, this, [[19, 33]]);
1588
+ }, _callee11, this, [[22, 36]]);
1564
1589
  }));
1565
1590
 
1566
1591
  function query(_x12) {
@@ -1676,19 +1701,16 @@ var CozyClient = /*#__PURE__*/function () {
1676
1701
  value: function makeObservableQuery(queryDefinition) {
1677
1702
  var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
1678
1703
  this.ensureStore();
1679
- var queryId = options.as || this.queryIdGenerator.generateId(queryDefinition);
1680
- this.ensureQueryExists(queryId, queryDefinition);
1681
- return new _ObservableQuery.default(queryId, queryDefinition, this, options);
1704
+ var definition = getDriveScopedQueryDefinition(queryDefinition, options.driveId);
1705
+ var queryId = options.as || this.queryIdGenerator.generateId(definition);
1706
+ this.ensureQueryExists(queryId, definition);
1707
+ return new _ObservableQuery.default(queryId, definition, this, options);
1682
1708
  }
1683
1709
  /**
1684
1710
  * Mutate a document
1685
1711
  *
1686
1712
  * @param {object} mutationDefinition - Describe the mutation
1687
- * @param {object} [params] The mutation params
1688
- * @param {function} [params.update] - update function from MutationOptions
1689
- * @param {function} [params.updateQueries] - updateQueries function from MutationOptions
1690
- * @param {string} [params.as] - Mutation id
1691
- * @param {Object} [params.options={}] - Additional options
1713
+ * @param {Partial<import('./types').MutationOptions> & { options?: object }} [params] - Mutation options
1692
1714
  * @returns {Promise}
1693
1715
  */
1694
1716
 
@@ -3127,7 +3149,7 @@ var CozyClient = /*#__PURE__*/function () {
3127
3149
  CozyClient.hooks = CozyClient.hooks || {};
3128
3150
  CozyClient.fetchPolicies = _policies.default; //COZY_CLIENT_VERSION_PACKAGE in replaced by babel. See babel config
3129
3151
 
3130
- CozyClient.version = "60.45.0";
3152
+ CozyClient.version = "61.0.1";
3131
3153
 
3132
3154
  _microee.default.mixin(CozyClient);
3133
3155
 
@@ -0,0 +1,84 @@
1
+ import path from 'path'
2
+ import ts from 'typescript'
3
+
4
+ it('accepts supported create and mutate options in the published types', () => {
5
+ const filename = path.resolve(__dirname, '../types/CozyClient.consumer.js')
6
+ const source = `
7
+ import CozyClient from './CozyClient'
8
+ import { Q } from './queries/dsl'
9
+ import { dispatchCreate } from './store/realtime'
10
+
11
+ const client = new CozyClient({})
12
+ client.create('io.cozy.todos', {}, null, {
13
+ updateQueries: {
14
+ todos: (documents, response) => documents.concat(response.data)
15
+ }
16
+ })
17
+ client.create('io.cozy.files', {}, null, { forceStack: true })
18
+ client.create('io.cozy.files', {}, null, {
19
+ driveId: 'shared-drive', forceLink: 'stack', as: 'create-directory'
20
+ })
21
+ client.create('io.cozy.todos', {})
22
+
23
+ const mutation = {
24
+ mutationType: 'CREATE_DOCUMENT',
25
+ document: { _type: 'io.cozy.todos', title: 'A task' }
26
+ }
27
+ client.mutate(mutation)
28
+ client.mutate(mutation, { driveId: 'shared-drive' })
29
+ client.mutate(mutation, { forceLink: 'stack' })
30
+ client.mutate(mutation, { forceStack: true })
31
+ client.query(Q('io.cozy.files'), { driveId: 'shared-drive' })
32
+ dispatchCreate(client, 'io.cozy.files', { _id: 'file-id', _rev: '1-created' }, { driveId: 'shared-drive' })
33
+ // @ts-expect-error Query drive identifiers must be strings.
34
+ client.query(Q('io.cozy.files'), { driveId: false })
35
+ // @ts-expect-error Realtime drive identifiers must be strings.
36
+ dispatchCreate(client, 'io.cozy.files', { _id: 'file-id', _rev: '1-created' }, { driveId: false })
37
+ client.mutate(mutation, {
38
+ as: 'create-task',
39
+ update: document => document,
40
+ updateQueries: {
41
+ todos: (documents, response) => documents.concat(response.data)
42
+ },
43
+ options: {}
44
+ })
45
+
46
+ // @ts-expect-error Drive identifiers must be strings.
47
+ client.mutate(mutation, { driveId: false })
48
+ // @ts-expect-error Link names must be strings.
49
+ client.mutate(mutation, { forceLink: false })
50
+ // @ts-expect-error The legacy routing flag must be a boolean.
51
+ client.mutate(mutation, { forceStack: 'stack' })
52
+ // @ts-expect-error Each query updater must be a function.
53
+ client.mutate(mutation, { updateQueries: { todos: 'invalid' } })
54
+
55
+ // @ts-expect-error Drive identifiers must be strings.
56
+ client.create('io.cozy.files', {}, null, { driveId: false })
57
+ // @ts-expect-error Each query updater must be a function.
58
+ client.create('io.cozy.todos', {}, null, { updateQueries: { todos: 'invalid' } })
59
+ `
60
+ const options = {
61
+ allowJs: true,
62
+ checkJs: true,
63
+ noEmit: true,
64
+ skipLibCheck: true,
65
+ esModuleInterop: true,
66
+ target: ts.ScriptTarget.ES2018,
67
+ moduleResolution: ts.ModuleResolutionKind.NodeJs,
68
+ types: []
69
+ }
70
+ const host = ts.createCompilerHost(options)
71
+ const getSourceFile = host.getSourceFile.bind(host)
72
+ host.getSourceFile = (name, languageVersion, ...args) =>
73
+ name === filename
74
+ ? ts.createSourceFile(name, source, languageVersion)
75
+ : getSourceFile(name, languageVersion, ...args)
76
+ const program = ts.createProgram([filename], options, host)
77
+ const diagnostics = ts.getPreEmitDiagnostics(program)
78
+
79
+ expect(
80
+ diagnostics.map(diagnostic =>
81
+ ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n')
82
+ )
83
+ ).toEqual([])
84
+ })
@@ -21,18 +21,20 @@ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (O
21
21
 
22
22
  function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
23
23
 
24
- var buildFileByIdQuery = function buildFileByIdQuery(id) {
24
+ function buildFileByIdQuery(id, driveId) {
25
+ var _definition = (0, _dsl.Q)('io.cozy.files').getById(id);
26
+
25
27
  return {
26
28
  definition: function definition() {
27
- return (0, _dsl.Q)('io.cozy.files').getById(id);
29
+ return driveId ? _definition.sharingById(driveId) : _definition;
28
30
  },
29
31
  options: {
30
- as: "io.cozy.files/".concat(id),
32
+ as: driveId ? "io.cozy.files/drive/".concat(driveId, "/").concat(id) : "io.cozy.files/".concat(id),
31
33
  singleDocData: true,
32
34
  fetchPolicy: _CozyClient.default.fetchPolicies.olderThan(30 * 1000)
33
35
  }
34
36
  };
35
- };
37
+ }
36
38
  /**
37
39
  * Ensures existence of `path` inside the io.cozy.files document
38
40
  *
@@ -67,7 +69,7 @@ var ensureFilePath = /*#__PURE__*/function () {
67
69
  return _context.abrupt("return", couchDBDoc);
68
70
 
69
71
  case 3:
70
- parentQuery = buildFileByIdQuery(couchDBDoc.dir_id);
72
+ parentQuery = buildFileByIdQuery(couchDBDoc.dir_id, couchDBDoc.driveId);
71
73
  _context.next = 6;
72
74
  return options.client.fetchQueryAndGetFromState({
73
75
  definition: parentQuery.definition(),
@@ -178,7 +178,8 @@ var StackLink = /*#__PURE__*/function (_CozyLink) {
178
178
  key: "request",
179
179
  value: function () {
180
180
  var _request = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee2(operation, options, result, forward) {
181
- var forceLink;
181
+ var forceLink, _operation$document, response, driveId, doctype, addDriveId;
182
+
182
183
  return _regenerator.default.wrap(function _callee2$(_context2) {
183
184
  while (1) {
184
185
  switch (_context2.prev = _context2.next) {
@@ -226,7 +227,7 @@ var StackLink = /*#__PURE__*/function (_CozyLink) {
226
227
  _context2.prev = 12;
227
228
 
228
229
  if (!operation.mutationType) {
229
- _context2.next = 17;
230
+ _context2.next = 19;
230
231
  break;
231
232
  }
232
233
 
@@ -234,43 +235,70 @@ var StackLink = /*#__PURE__*/function (_CozyLink) {
234
235
  return this.executeMutation(operation, options, result, forward);
235
236
 
236
237
  case 16:
237
- return _context2.abrupt("return", _context2.sent);
238
+ _context2.t1 = _context2.sent;
239
+ _context2.next = 22;
240
+ break;
238
241
 
239
- case 17:
240
- _context2.next = 19;
242
+ case 19:
243
+ _context2.next = 21;
241
244
  return this.executeQuery(operation);
242
245
 
243
- case 19:
244
- return _context2.abrupt("return", _context2.sent);
246
+ case 21:
247
+ _context2.t1 = _context2.sent;
245
248
 
246
249
  case 22:
247
- _context2.prev = 22;
248
- _context2.t1 = _context2["catch"](12);
250
+ response = _context2.t1;
251
+ driveId = operation.mutationType ? operation.mutationType === _dsl.MutationTypes.CREATE_DOCUMENT && (options === null || options === void 0 ? void 0 : options.driveId) : operation.sharingId;
252
+ doctype = operation.doctype || ((_operation$document = operation.document) === null || _operation$document === void 0 ? void 0 : _operation$document._type);
249
253
 
250
- if (!(forceLink !== 'stack' && (0, _utils.isReactNativeOfflineError)(_context2.t1))) {
254
+ if (!(doctype === _const.DOCTYPE_FILES && driveId)) {
251
255
  _context2.next = 28;
252
256
  break;
253
257
  }
254
258
 
259
+ addDriveId = function addDriveId(document) {
260
+ return document ? _objectSpread(_objectSpread({}, document), {}, {
261
+ driveId: driveId
262
+ }) : document;
263
+ };
264
+
265
+ return _context2.abrupt("return", _objectSpread(_objectSpread({}, response), {}, {
266
+ data: Array.isArray(response.data) ? response.data.map(addDriveId) : addDriveId(response.data)
267
+ }, response.included && {
268
+ included: response.included.map(addDriveId)
269
+ }));
270
+
271
+ case 28:
272
+ return _context2.abrupt("return", response);
273
+
274
+ case 31:
275
+ _context2.prev = 31;
276
+ _context2.t2 = _context2["catch"](12);
277
+
278
+ if (!(forceLink !== 'stack' && (0, _utils.isReactNativeOfflineError)(_context2.t2))) {
279
+ _context2.next = 37;
280
+ break;
281
+ }
282
+
255
283
  if (!(operation.doctype === _const.DOCTYPE_SHARING_RECIPIENTS)) {
256
- _context2.next = 27;
284
+ _context2.next = 36;
257
285
  break;
258
286
  }
259
287
 
260
- throw _context2.t1;
288
+ throw _context2.t2;
261
289
 
262
- case 27:
290
+ case 36:
263
291
  return _context2.abrupt("return", forward(operation, options));
264
292
 
265
- case 28:
266
- throw _context2.t1;
293
+ case 37:
294
+ throw _context2.t2;
267
295
 
268
- case 29:
296
+ case 38:
269
297
  case "end":
270
298
  return _context2.stop();
271
299
  }
272
300
  }
273
- }, _callee2, this, [[12, 22]]);
301
+ }, _callee2, this, [[12, 31]]);
274
302
  }));
275
303
 
276
304
  function request(_x, _x2, _x3, _x4) {
@@ -313,7 +341,7 @@ var StackLink = /*#__PURE__*/function (_CozyLink) {
313
341
  key: "executeQuery",
314
342
  value: function () {
315
343
  var _executeQuery = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee4(query) {
316
- var doctype, selector, id, ids, referenced, sharingId, rawOptions, options, _selector$drive_id, fileId, driveId, response, collection;
344
+ var doctype, selector, id, ids, referenced, sharingId, rawOptions, options, _selector$drive_id, fileId, _driveId, response, collection;
317
345
 
318
346
  return _regenerator.default.wrap(function _callee4$(_context4) {
319
347
  while (1) {
@@ -346,7 +374,7 @@ var StackLink = /*#__PURE__*/function (_CozyLink) {
346
374
  }
347
375
 
348
376
  fileId = selector === null || selector === void 0 ? void 0 : selector.file_id;
349
- driveId = (_selector$drive_id = selector === null || selector === void 0 ? void 0 : selector.drive_id) !== null && _selector$drive_id !== void 0 ? _selector$drive_id : null;
377
+ _driveId = (_selector$drive_id = selector === null || selector === void 0 ? void 0 : selector.drive_id) !== null && _selector$drive_id !== void 0 ? _selector$drive_id : null;
350
378
 
351
379
  if (fileId) {
352
380
  _context4.next = 11;
@@ -358,7 +386,7 @@ var StackLink = /*#__PURE__*/function (_CozyLink) {
358
386
  case 11:
359
387
  _context4.next = 13;
360
388
  return this.stackClient.collection('io.cozy.sharings').fetchEffectiveRecipients(fileId, {
361
- driveId: driveId
389
+ driveId: _driveId
362
390
  });
363
391
 
364
392
  case 13:
@@ -368,7 +396,7 @@ var StackLink = /*#__PURE__*/function (_CozyLink) {
368
396
  var recipientId = recipient.id,
369
397
  attributes = (0, _objectWithoutProperties2.default)(recipient, ["id"]); // Recipient attributes vary by target, so IDs include the query scope.
370
398
 
371
- var scope = driveId ? "drive/".concat(encodeURIComponent(driveId)) : 'personal';
399
+ var scope = _driveId ? "drive/".concat(encodeURIComponent(_driveId)) : 'personal';
372
400
  var id = "".concat(scope, "/file/").concat(encodeURIComponent(fileId), "/recipient/").concat(encodeURIComponent(recipientId));
373
401
  return _objectSpread({
374
402
  id: id,
@@ -376,7 +404,7 @@ var StackLink = /*#__PURE__*/function (_CozyLink) {
376
404
  _type: _const.DOCTYPE_SHARING_RECIPIENTS,
377
405
  recipient_id: recipientId,
378
406
  file_id: fileId,
379
- drive_id: driveId
407
+ drive_id: _driveId
380
408
  }, attributes);
381
409
  })
382
410
  }));
@@ -443,64 +471,74 @@ var StackLink = /*#__PURE__*/function (_CozyLink) {
443
471
  case 0:
444
472
  mutationType = mutation.mutationType, doc = mutation.document, docs = mutation.documents, props = (0, _objectWithoutProperties2.default)(mutation, ["mutationType", "document", "documents"]);
445
473
  _context5.t0 = mutationType;
446
- _context5.next = _context5.t0 === _dsl.MutationTypes.CREATE_DOCUMENT ? 4 : _context5.t0 === _dsl.MutationTypes.UPDATE_DOCUMENTS ? 5 : _context5.t0 === _dsl.MutationTypes.UPDATE_DOCUMENT ? 9 : _context5.t0 === _dsl.MutationTypes.DELETE_DOCUMENT ? 10 : _context5.t0 === _dsl.MutationTypes.DELETE_DOCUMENTS ? 11 : _context5.t0 === _dsl.MutationTypes.ADD_REFERENCES_TO ? 12 : _context5.t0 === _dsl.MutationTypes.REMOVE_REFERENCES_TO ? 13 : _context5.t0 === _dsl.MutationTypes.ADD_REFERENCED_BY ? 14 : _context5.t0 === _dsl.MutationTypes.REMOVE_REFERENCED_BY ? 19 : _context5.t0 === _dsl.MutationTypes.UPLOAD_FILE ? 24 : 25;
474
+ _context5.next = _context5.t0 === _dsl.MutationTypes.CREATE_DOCUMENT ? 4 : _context5.t0 === _dsl.MutationTypes.UPDATE_DOCUMENTS ? 7 : _context5.t0 === _dsl.MutationTypes.UPDATE_DOCUMENT ? 11 : _context5.t0 === _dsl.MutationTypes.DELETE_DOCUMENT ? 12 : _context5.t0 === _dsl.MutationTypes.DELETE_DOCUMENTS ? 13 : _context5.t0 === _dsl.MutationTypes.ADD_REFERENCES_TO ? 14 : _context5.t0 === _dsl.MutationTypes.REMOVE_REFERENCES_TO ? 15 : _context5.t0 === _dsl.MutationTypes.ADD_REFERENCED_BY ? 16 : _context5.t0 === _dsl.MutationTypes.REMOVE_REFERENCED_BY ? 21 : _context5.t0 === _dsl.MutationTypes.UPLOAD_FILE ? 26 : 27;
447
475
  break;
448
476
 
449
477
  case 4:
478
+ if (!(doc._type === _const.DOCTYPE_FILES && options !== null && options !== void 0 && options.driveId)) {
479
+ _context5.next = 6;
480
+ break;
481
+ }
482
+
483
+ return _context5.abrupt("return", this.stackClient.collection(doc._type, {
484
+ driveId: options.driveId
485
+ }).create(doc));
486
+
487
+ case 6:
450
488
  return _context5.abrupt("return", this.stackClient.collection(doc._type).create(doc));
451
489
 
452
- case 5:
453
- _context5.next = 7;
490
+ case 7:
491
+ _context5.next = 9;
454
492
  return this.stackClient.collection(docs[0]._type).updateAll(docs);
455
493
 
456
- case 7:
494
+ case 9:
457
495
  updateAllResp = _context5.sent;
458
496
  return _context5.abrupt("return", transformBulkDocsResponse(updateAllResp, docs));
459
497
 
460
- case 9:
498
+ case 11:
461
499
  return _context5.abrupt("return", this.stackClient.collection(doc._type).update(doc));
462
500
 
463
- case 10:
501
+ case 12:
464
502
  return _context5.abrupt("return", this.stackClient.collection(doc._type).destroy(doc));
465
503
 
466
- case 11:
504
+ case 13:
467
505
  return _context5.abrupt("return", this.stackClient.collection(doc._type).destroyAll(docs));
468
506
 
469
- case 12:
507
+ case 14:
470
508
  return _context5.abrupt("return", this.stackClient.collection(props.referencedDocuments[0]._type).addReferencesTo(doc, props.referencedDocuments));
471
509
 
472
- case 13:
510
+ case 15:
473
511
  return _context5.abrupt("return", this.stackClient.collection(props.referencedDocuments[0]._type).removeReferencesTo(doc, props.referencedDocuments));
474
512
 
475
- case 14:
513
+ case 16:
476
514
  if (!(doc._type === _const.DOCTYPE_FILES)) {
477
- _context5.next = 18;
515
+ _context5.next = 20;
478
516
  break;
479
517
  }
480
518
 
481
519
  return _context5.abrupt("return", this.stackClient.collection(_const.DOCTYPE_FILES).addReferencedBy(doc, props.referencedDocuments));
482
520
 
483
- case 18:
521
+ case 20:
484
522
  throw new Error('The document type should be io.cozy.files');
485
523
 
486
- case 19:
524
+ case 21:
487
525
  if (!(doc._type === _const.DOCTYPE_FILES)) {
488
- _context5.next = 23;
526
+ _context5.next = 25;
489
527
  break;
490
528
  }
491
529
 
492
530
  return _context5.abrupt("return", this.stackClient.collection(_const.DOCTYPE_FILES).removeReferencedBy(doc, props.referencedDocuments));
493
531
 
494
- case 23:
532
+ case 25:
495
533
  throw new Error('The document type should be io.cozy.files');
496
534
 
497
- case 24:
535
+ case 26:
498
536
  return _context5.abrupt("return", this.stackClient.collection(_const.DOCTYPE_FILES).upload(props.file, props.dirPath));
499
537
 
500
- case 25:
538
+ case 27:
501
539
  return _context5.abrupt("return", forward(mutation, options, result));
502
540
 
503
- case 26:
541
+ case 28:
504
542
  case "end":
505
543
  return _context5.stop();
506
544
  }