cozy-client 24.10.2 → 25.0.3

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.
package/CHANGELOG.md CHANGED
@@ -3,6 +3,53 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ ## [25.0.3](https://github.com/cozy/cozy-client/compare/v25.0.2...v25.0.3) (2021-10-21)
7
+
8
+
9
+ ### Bug Fixes
10
+
11
+ * Add id in CozyClientDocument for RealTime ([5e242d6](https://github.com/cozy/cozy-client/commit/5e242d6d5fcb34e9f09c8b5661133b63aa35f93e))
12
+
13
+
14
+
15
+
16
+
17
+ ## [25.0.2](https://github.com/cozy/cozy-client/compare/v25.0.1...v25.0.2) (2021-10-14)
18
+
19
+ **Note:** Version bump only for package cozy-client
20
+
21
+
22
+
23
+
24
+
25
+ ## [25.0.1](https://github.com/cozy/cozy-client/compare/v25.0.0...v25.0.1) (2021-10-13)
26
+
27
+
28
+ ### Bug Fixes
29
+
30
+ * Do not save target document after add/remove references ([62902ce](https://github.com/cozy/cozy-client/commit/62902ce158f1b20da241ae6c42e5cc63ca0205bc))
31
+ * Normalize file relationship response ([f5f465f](https://github.com/cozy/cozy-client/commit/f5f465f05a4885981f7bb1f5c2a4c1d968e29449))
32
+
33
+
34
+
35
+
36
+
37
+ # [25.0.0](https://github.com/cozy/cozy-client/compare/v24.10.2...v25.0.0) (2021-10-12)
38
+
39
+
40
+ ### Bug Fixes
41
+
42
+ * Doc created by Save should use _type to define _type ([61f9559](https://github.com/cozy/cozy-client/commit/61f95599359f9bbf02f0f2a689fa4b5aa5577508))
43
+
44
+
45
+ ### BREAKING CHANGES
46
+
47
+ * save() must use argument with `_type` property
48
+
49
+
50
+
51
+
52
+
6
53
  ## [24.10.2](https://github.com/cozy/cozy-client/compare/v24.10.1...v24.10.2) (2021-10-11)
7
54
 
8
55
 
@@ -785,39 +785,60 @@ var CozyClient = /*#__PURE__*/function () {
785
785
  value: function validate(document) {
786
786
  return this.schema.validate(document);
787
787
  }
788
+ /**
789
+ * Create or update a document on the server
790
+ *
791
+ * @param {object} doc - Document to save
792
+ * @param {object} mutationOptions - Mutation options
793
+ * @returns {Promise}
794
+ */
795
+
788
796
  }, {
789
797
  key: "save",
790
798
  value: function () {
791
799
  var _save = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee5(doc) {
792
800
  var mutationOptions,
801
+ _type,
802
+ attributes,
793
803
  normalizedDoc,
794
804
  ret,
795
805
  _args5 = arguments;
806
+
796
807
  return _regenerator.default.wrap(function _callee5$(_context5) {
797
808
  while (1) {
798
809
  switch (_context5.prev = _context5.next) {
799
810
  case 0:
800
811
  mutationOptions = _args5.length > 1 && _args5[1] !== undefined ? _args5[1] : {};
812
+ _type = doc._type, attributes = (0, _objectWithoutProperties2.default)(doc, ["_type"]);
813
+
814
+ if (_type) {
815
+ _context5.next = 4;
816
+ break;
817
+ }
818
+
819
+ throw new Error('The document must have a `_type` property');
820
+
821
+ case 4:
801
822
  normalizedDoc = _objectSpread({
802
- _type: doc.type
803
- }, doc);
804
- _context5.next = 4;
823
+ _type: _type
824
+ }, attributes);
825
+ _context5.next = 7;
805
826
  return this.schema.validate(normalizedDoc);
806
827
 
807
- case 4:
828
+ case 7:
808
829
  ret = _context5.sent;
809
830
 
810
831
  if (!(ret !== true)) {
811
- _context5.next = 7;
832
+ _context5.next = 10;
812
833
  break;
813
834
  }
814
835
 
815
836
  throw new Error('Validation failed');
816
837
 
817
- case 7:
838
+ case 10:
818
839
  return _context5.abrupt("return", this.mutate(this.getDocumentSavePlan(normalizedDoc), mutationOptions));
819
840
 
820
- case 8:
841
+ case 11:
821
842
  case "end":
822
843
  return _context5.stop();
823
844
  }
@@ -2390,7 +2411,7 @@ var CozyClient = /*#__PURE__*/function () {
2390
2411
  CozyClient.hooks = CozyClient.hooks || {};
2391
2412
  CozyClient.fetchPolicies = _policies.default; //COZY_CLIENT_VERSION_PACKAGE in replaced by babel. See babel config
2392
2413
 
2393
- CozyClient.version = "24.10.2";
2414
+ CozyClient.version = "25.0.3";
2394
2415
 
2395
2416
  _microee.default.mixin(CozyClient);
2396
2417
 
@@ -677,7 +677,7 @@ describe('CozyClient', () => {
677
677
 
678
678
  describe('save', () => {
679
679
  it('should add the _type if not present', async () => {
680
- const todo = { type: 'io.cozy.todos', label: 'Buy croissants' }
680
+ const todo = { _type: 'io.cozy.todos', label: 'Buy croissants' }
681
681
  client.store.dispatch.mockReset()
682
682
  await client.save(todo)
683
683
  const dispatchCalls = client.store.dispatch.mock.calls
@@ -1754,6 +1754,16 @@ describe('file creation', () => {
1754
1754
  it('should support creating a file with references', async () => {
1755
1755
  const { client } = setup()
1756
1756
  jest.spyOn(client, 'requestMutation')
1757
+ client.stackClient.fetchJSON = jest
1758
+ .fn()
1759
+ .mockResolvedValueOnce({
1760
+ data: {
1761
+ _id: '1337'
1762
+ }
1763
+ })
1764
+ .mockResolvedValueOnce({
1765
+ data: [{ id: 1, type: 'io.cozy.files' }]
1766
+ })
1757
1767
  await client.create(
1758
1768
  'io.cozy.files',
1759
1769
  {
@@ -1766,6 +1776,9 @@ describe('file creation', () => {
1766
1776
  icons: [{ _id: 1, _type: 'io.cozy.files' }]
1767
1777
  }
1768
1778
  )
1779
+ client.stackClient.fetchJSON = jest.fn().mockResolvedValue({
1780
+ data: [{ id: '1337', type: 'io.cozy.files' }]
1781
+ })
1769
1782
  const requestMutationCalls = client.requestMutation.mock.calls
1770
1783
  const lastCall = requestMutationCalls[requestMutationCalls.length - 1]
1771
1784
  expect(lastCall[0]).toEqual(
@@ -40,6 +40,7 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
40
40
  */
41
41
  var normalizeDoc = function normalizeDoc(couchDBDoc, doctype) {
42
42
  return _objectSpread({
43
+ id: couchDBDoc._id,
43
44
  _type: doctype
44
45
  }, couchDBDoc);
45
46
  };
@@ -38,6 +38,7 @@ describe('RealTimeQueries', () => {
38
38
  definition: {
39
39
  document: {
40
40
  _id: 'mock-created',
41
+ id: 'mock-created',
41
42
  _type: 'io.cozy.files',
42
43
  type: 'file'
43
44
  },
@@ -47,6 +48,7 @@ describe('RealTimeQueries', () => {
47
48
  response: {
48
49
  data: {
49
50
  _id: 'mock-created',
51
+ id: 'mock-created',
50
52
  _type: 'io.cozy.files',
51
53
  type: 'file'
52
54
  }
@@ -60,6 +62,7 @@ describe('RealTimeQueries', () => {
60
62
  definition: {
61
63
  document: {
62
64
  _id: 'mock-updated',
65
+ id: 'mock-updated',
63
66
  _type: 'io.cozy.files',
64
67
  type: 'file'
65
68
  },
@@ -69,6 +72,7 @@ describe('RealTimeQueries', () => {
69
72
  response: {
70
73
  data: {
71
74
  _id: 'mock-updated',
75
+ id: 'mock-updated',
72
76
  _type: 'io.cozy.files',
73
77
  type: 'file'
74
78
  }
@@ -81,6 +85,7 @@ describe('RealTimeQueries', () => {
81
85
  definition: {
82
86
  document: {
83
87
  _id: 'mock-deleted',
88
+ id: 'mock-deleted',
84
89
  _type: 'io.cozy.files',
85
90
  type: 'file',
86
91
  _deleted: true
@@ -91,6 +96,7 @@ describe('RealTimeQueries', () => {
91
96
  response: {
92
97
  data: {
93
98
  _id: 'mock-deleted',
99
+ id: 'mock-deleted',
94
100
  _type: 'io.cozy.files',
95
101
  type: 'file',
96
102
  _deleted: true
@@ -118,6 +124,7 @@ describe('RealTimeQueries', () => {
118
124
  definition: {
119
125
  document: {
120
126
  _id: 'mock-created',
127
+ id: 'mock-created',
121
128
  _type: 'io.cozy.oauth.clients',
122
129
  client_kind: 'desktop',
123
130
  client_name: 'Cozy Drive (hostname)'
@@ -128,6 +135,7 @@ describe('RealTimeQueries', () => {
128
135
  response: {
129
136
  data: {
130
137
  _id: 'mock-created',
138
+ id: 'mock-created',
131
139
  _type: 'io.cozy.oauth.clients',
132
140
  client_kind: 'desktop',
133
141
  client_name: 'Cozy Drive (hostname)'
@@ -192,18 +192,14 @@ var HasMany = /*#__PURE__*/function (_Association) {
192
192
  return this.removeById(ids);
193
193
  }
194
194
  /**
195
- * Add a referenced document by id. You need to call save()
196
- * in order to synchronize your document with the store.
197
- *
198
- * @todo We shouldn't create the array of relationship manually since
199
- * it'll not be present in the store as well.
200
- * We certainly should use something like `updateRelationship`
195
+ * Update target document with relationships
201
196
  *
197
+ * @param {string[]} idsArg - The ids to add as a relationship
202
198
  */
203
199
 
204
200
  }, {
205
- key: "addById",
206
- value: function addById(idsArg) {
201
+ key: "addTargetRelationships",
202
+ value: function addTargetRelationships(idsArg) {
207
203
  var _this2 = this,
208
204
  _this$target$relation;
209
205
 
@@ -228,17 +224,43 @@ var HasMany = /*#__PURE__*/function (_Association) {
228
224
  (_this$target$relation = this.target.relationships[this.name].data).push.apply(_this$target$relation, (0, _toConsumableArray2.default)(newRelations));
229
225
 
230
226
  this.updateMetaCount();
227
+ }
228
+ /**
229
+ * Add a referenced document by id. You need to call save()
230
+ * in order to synchronize your document with the store.
231
+ *
232
+ * @todo We shouldn't create the array of relationship manually since
233
+ * it'll not be present in the store as well.
234
+ * We certainly should use something like `updateRelationship`
235
+ *
236
+ */
237
+
238
+ }, {
239
+ key: "addById",
240
+ value: function addById(idsArg) {
241
+ this.addTargetRelationships(idsArg);
231
242
  return this.save(this.target);
232
243
  }
244
+ /**
245
+ * Remove relationships from target document
246
+ *
247
+ * @param {string[]} idsArg - The ids to remove from the target relationships
248
+ */
249
+
233
250
  }, {
234
- key: "removeById",
235
- value: function removeById(idsArg) {
251
+ key: "removeTargetRelationships",
252
+ value: function removeTargetRelationships(idsArg) {
236
253
  var ids = Array.isArray(idsArg) ? idsArg : [idsArg];
237
254
  this.target.relationships[this.name].data = this.target.relationships[this.name].data.filter(function (_ref2) {
238
255
  var _id = _ref2._id;
239
256
  return !ids.includes(_id);
240
257
  });
241
258
  this.updateMetaCount();
259
+ }
260
+ }, {
261
+ key: "removeById",
262
+ value: function removeById(idsArg) {
263
+ this.removeTargetRelationships(idsArg);
242
264
  return this.save(this.target);
243
265
  }
244
266
  }, {
@@ -164,10 +164,9 @@ var HasManyFiles = /*#__PURE__*/function (_HasMany) {
164
164
  return this.mutate(this.addReferences(relations));
165
165
 
166
166
  case 4:
167
- _context3.next = 6;
168
- return (0, _get3.default)((0, _getPrototypeOf2.default)(HasManyFiles.prototype), "addById", this).call(this, ids);
167
+ this.addTargetRelationships(ids);
169
168
 
170
- case 6:
169
+ case 5:
171
170
  case "end":
172
171
  return _context3.stop();
173
172
  }
@@ -203,10 +202,9 @@ var HasManyFiles = /*#__PURE__*/function (_HasMany) {
203
202
  return this.mutate(this.removeReferences(references));
204
203
 
205
204
  case 4:
206
- _context4.next = 6;
207
- return (0, _get3.default)((0, _getPrototypeOf2.default)(HasManyFiles.prototype), "removeById", this).call(this, ids);
205
+ this.removeTargetRelationships(ids);
208
206
 
209
- case 6:
207
+ case 5:
210
208
  case "end":
211
209
  return _context4.stop();
212
210
  }
@@ -73,7 +73,6 @@ describe('HasManyFiles', () => {
73
73
  mutationType: 'ADD_REFERENCES_TO',
74
74
  referencedDocuments: [{ _type: DOCTYPE_FILES, _id: 4 }]
75
75
  })
76
- expect(save).toHaveBeenCalled()
77
76
  })
78
77
 
79
78
  it('adds multiple relations', async () => {
@@ -92,7 +91,6 @@ describe('HasManyFiles', () => {
92
91
  { _type: DOCTYPE_FILES, _id: 5 }
93
92
  ]
94
93
  })
95
- expect(save).toHaveBeenCalled()
96
94
  })
97
95
 
98
96
  it('removes relations', async () => {
@@ -103,7 +101,6 @@ describe('HasManyFiles', () => {
103
101
  mutationType: 'REMOVE_REFERENCES_TO',
104
102
  referencedDocuments: [{ _type: DOCTYPE_FILES, _id: 2 }]
105
103
  })
106
- expect(save).toHaveBeenCalled()
107
104
  })
108
105
 
109
106
  it('adds multiple relations', async () => {
@@ -117,7 +114,6 @@ describe('HasManyFiles', () => {
117
114
  { _type: DOCTYPE_FILES, _id: 2 }
118
115
  ]
119
116
  })
120
- expect(save).toHaveBeenCalled()
121
117
  })
122
118
 
123
119
  it('add wrong file relations', () => {