cozy-client 24.6.0 → 24.8.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.
package/CHANGELOG.md CHANGED
@@ -3,6 +3,49 @@
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
+ # [24.8.0](https://github.com/cozy/cozy-client/compare/v24.7.0...v24.8.0) (2021-09-16)
7
+
8
+
9
+ ### Features
10
+
11
+ * Makes queryAll faster with many requests ([17a9e23](https://github.com/cozy/cozy-client/commit/17a9e2397f72f94daafd03b49f1cebae5bad0ec3))
12
+
13
+
14
+
15
+
16
+
17
+ # [24.7.0](https://github.com/cozy/cozy-client/compare/v24.6.2...v24.7.0) (2021-09-14)
18
+
19
+
20
+ ### Features
21
+
22
+ * Add deprecating warning for set/unset ([210a816](https://github.com/cozy/cozy-client/commit/210a8162df0816fa96b0beb6d3e910ef9ae5e616))
23
+ * Use add and remove API for both has-one and has-many ([8bc0a4b](https://github.com/cozy/cozy-client/commit/8bc0a4be3c70f2068556d3d0c169eb5d23ed5033))
24
+
25
+
26
+
27
+
28
+
29
+ ## [24.6.2](https://github.com/cozy/cozy-client/compare/v24.6.1...v24.6.2) (2021-09-14)
30
+
31
+
32
+ ### Bug Fixes
33
+
34
+ * **deps:** update dependency open to v8 ([8590ae3](https://github.com/cozy/cozy-client/commit/8590ae3ac00c1109c625316b4449d6e3f1d8b6ab))
35
+ * **deps:** update dependency url-search-params-polyfill to v8 ([7723a7d](https://github.com/cozy/cozy-client/commit/7723a7dbc9bab386cb9dbc9d0e7a751abb2e56fb))
36
+
37
+
38
+
39
+
40
+
41
+ ## [24.6.1](https://github.com/cozy/cozy-client/compare/v24.6.0...v24.6.1) (2021-09-13)
42
+
43
+ **Note:** Version bump only for package cozy-client
44
+
45
+
46
+
47
+
48
+
6
49
  # [24.6.0](https://github.com/cozy/cozy-client/compare/v24.5.0...v24.6.0) (2021-09-13)
7
50
 
8
51
  **Note:** Version bump only for package cozy-client
@@ -2386,7 +2386,7 @@ var CozyClient = /*#__PURE__*/function () {
2386
2386
  CozyClient.hooks = CozyClient.hooks || {};
2387
2387
  CozyClient.fetchPolicies = _policies.default; //COZY_CLIENT_VERSION_PACKAGE in replaced by babel. See babel config
2388
2388
 
2389
- CozyClient.version = "24.6.0";
2389
+ CozyClient.version = "24.8.0";
2390
2390
 
2391
2391
  _microee.default.mixin(CozyClient);
2392
2392
 
@@ -159,6 +159,38 @@ var HasMany = /*#__PURE__*/function (_Association) {
159
159
  value: function existsById(id) {
160
160
  return Boolean(this.containsById(id) && this.get(this.doctype, id));
161
161
  }
162
+ /**
163
+ * Add the relationships to the target document
164
+ *
165
+ * @param {CozyClientDocument[]} docsArg - Documents to add as relationships
166
+ * @returns {CozyClientDocument} The saved target document
167
+ */
168
+
169
+ }, {
170
+ key: "add",
171
+ value: function add(docsArg) {
172
+ var docs = Array.isArray(docsArg) ? docsArg : [docsArg];
173
+ var ids = docs.map(function (doc) {
174
+ return doc._id;
175
+ });
176
+ return this.addById(ids);
177
+ }
178
+ /**
179
+ * Remove the relationships from the target document
180
+ *
181
+ * @param {CozyClientDocument[]} docsArg - Documents to remove as relationships
182
+ * @returns {CozyClientDocument} The saved target document
183
+ */
184
+
185
+ }, {
186
+ key: "remove",
187
+ value: function remove(docsArg) {
188
+ var docs = Array.isArray(docsArg) ? docsArg : [docsArg];
189
+ var ids = docs.map(function (doc) {
190
+ return doc._id;
191
+ });
192
+ return this.removeById(ids);
193
+ }
162
194
  /**
163
195
  * Add a referenced document by id. You need to call save()
164
196
  * in order to synchronize your document with the store.
@@ -54,51 +54,87 @@ describe('HasMany', () => {
54
54
  }
55
55
  })
56
56
 
57
- it('adds', () => {
58
- hydrated.tasks.addById(4)
59
- expect(hydrated.tasks.data).toEqual([
60
- { doctype: 'io.cozy.tasks', id: 1 },
61
- { doctype: 'io.cozy.tasks', id: 2 },
62
- { doctype: 'io.cozy.tasks', id: 4 }
63
- ])
64
- expect(save).toHaveBeenCalled()
65
- })
57
+ describe('add', () => {
58
+ it('adds by id', () => {
59
+ hydrated.tasks.addById(4)
60
+ expect(hydrated.tasks.data).toEqual([
61
+ { doctype: 'io.cozy.tasks', id: 1 },
62
+ { doctype: 'io.cozy.tasks', id: 2 },
63
+ { doctype: 'io.cozy.tasks', id: 4 }
64
+ ])
65
+ expect(save).toHaveBeenCalled()
66
+ })
66
67
 
67
- it('doesnt add if already there', () => {
68
- hydrated.tasks.addById(1)
69
- expect(hydrated.tasks.data).toEqual([
70
- { doctype: 'io.cozy.tasks', id: 1 },
71
- { doctype: 'io.cozy.tasks', id: 2 }
72
- ])
73
- })
68
+ it('doesnt add if already there', () => {
69
+ hydrated.tasks.addById(1)
70
+ expect(hydrated.tasks.data).toEqual([
71
+ { doctype: 'io.cozy.tasks', id: 1 },
72
+ { doctype: 'io.cozy.tasks', id: 2 }
73
+ ])
74
+ })
74
75
 
75
- it('adds multiple ids', () => {
76
- hydrated.tasks.addById([4, 5])
77
- expect(hydrated.tasks.data).toEqual([
78
- { doctype: 'io.cozy.tasks', id: 1 },
79
- { doctype: 'io.cozy.tasks', id: 2 },
80
- { doctype: 'io.cozy.tasks', id: 4 },
81
- { doctype: 'io.cozy.tasks', id: 5 }
82
- ])
83
- })
76
+ it('adds multiple ids', () => {
77
+ hydrated.tasks.addById([4, 5])
84
78
 
85
- it('removes', () => {
86
- hydrated.tasks.removeById(2)
87
- expect(hydrated.tasks.data).toEqual([{ doctype: 'io.cozy.tasks', id: 1 }])
88
- expect(save).toHaveBeenCalled()
89
- })
79
+ expect(hydrated.tasks.data).toEqual([
80
+ { doctype: 'io.cozy.tasks', id: 1 },
81
+ { doctype: 'io.cozy.tasks', id: 2 },
82
+ { doctype: 'io.cozy.tasks', id: 4 },
83
+ { doctype: 'io.cozy.tasks', id: 5 }
84
+ ])
85
+ })
86
+
87
+ it('adds by doc', () => {
88
+ hydrated.tasks.add({ _id: 4 })
89
+ expect(hydrated.tasks.data).toEqual([
90
+ { doctype: 'io.cozy.tasks', id: 1 },
91
+ { doctype: 'io.cozy.tasks', id: 2 },
92
+ { doctype: 'io.cozy.tasks', id: 4 }
93
+ ])
94
+ })
90
95
 
91
- it('doesnt remove if not there', () => {
92
- hydrated.tasks.removeById(3)
93
- expect(hydrated.tasks.data).toEqual([
94
- { doctype: 'io.cozy.tasks', id: 1 },
95
- { doctype: 'io.cozy.tasks', id: 2 }
96
- ])
96
+ it('adds by multiple docs', () => {
97
+ hydrated.tasks.add([{ _id: 4 }, { _id: 5 }])
98
+ expect(hydrated.tasks.data).toEqual([
99
+ { doctype: 'io.cozy.tasks', id: 1 },
100
+ { doctype: 'io.cozy.tasks', id: 2 },
101
+ { doctype: 'io.cozy.tasks', id: 4 },
102
+ { doctype: 'io.cozy.tasks', id: 5 }
103
+ ])
104
+ })
97
105
  })
98
106
 
99
- it('can remove multiple docs from the relationship', () => {
100
- hydrated.tasks.removeById([1, 2])
101
- expect(hydrated.tasks.data).toEqual([])
107
+ describe('remove', () => {
108
+ it('removes by id', () => {
109
+ hydrated.tasks.removeById(2)
110
+ expect(hydrated.tasks.data).toEqual([{ doctype: 'io.cozy.tasks', id: 1 }])
111
+ expect(save).toHaveBeenCalled()
112
+ })
113
+
114
+ it('doesnt remove if not there', () => {
115
+ hydrated.tasks.removeById(3)
116
+ expect(hydrated.tasks.data).toEqual([
117
+ { doctype: 'io.cozy.tasks', id: 1 },
118
+ { doctype: 'io.cozy.tasks', id: 2 }
119
+ ])
120
+ })
121
+
122
+ it('can remove multiple docs from the relationship', () => {
123
+ hydrated.tasks.removeById([1, 2])
124
+ expect(hydrated.tasks.data).toEqual([])
125
+ })
126
+
127
+ it('remove by doc', () => {
128
+ hydrated.tasks.remove({ _id: 2 })
129
+ expect(hydrated.tasks.data).toEqual([{ doctype: 'io.cozy.tasks', id: 1 }])
130
+ expect(save).toHaveBeenCalled()
131
+ })
132
+
133
+ it('remove by docs', () => {
134
+ hydrated.tasks.remove([{ _id: 1 }, { _id: 2 }])
135
+ expect(hydrated.tasks.data).toEqual([])
136
+ expect(save).toHaveBeenCalled()
137
+ })
102
138
  })
103
139
 
104
140
  it('updates the count metadata', () => {
@@ -21,7 +21,7 @@ var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/ge
21
21
 
22
22
  var _get2 = _interopRequireDefault(require("lodash/get"));
23
23
 
24
- var _set2 = _interopRequireDefault(require("lodash/set"));
24
+ var _set = _interopRequireDefault(require("lodash/set"));
25
25
 
26
26
  var _dsl = require("../queries/dsl");
27
27
 
@@ -48,8 +48,33 @@ var HasOne = /*#__PURE__*/function (_Association) {
48
48
  }
49
49
 
50
50
  (0, _createClass2.default)(HasOne, [{
51
- key: "set",
52
- value: function set(doc) {
51
+ key: "add",
52
+
53
+ /**
54
+ * Add the relationship to the target document
55
+ *
56
+ * @param {CozyClientDocument} doc - Document to add as a relationship
57
+ * @returns {CozyClientDocument} The saved target document
58
+ */
59
+ value: function add(doc) {
60
+ this.setRelationship(doc);
61
+ return this.save(this.target);
62
+ }
63
+ /**
64
+ * Remove the relationship from the target document
65
+ *
66
+ * @returns {CozyClientDocument} The saved target document
67
+ */
68
+
69
+ }, {
70
+ key: "remove",
71
+ value: function remove() {
72
+ this.setRelationship(undefined);
73
+ return this.save(this.target);
74
+ }
75
+ }, {
76
+ key: "setRelationship",
77
+ value: function setRelationship(doc) {
53
78
  if (doc && doc._type !== this.doctype) {
54
79
  throw new Error("Tried to associate a ".concat(doc._type, " document to a HasOne relationship on ").concat(this.doctype, " document"));
55
80
  }
@@ -57,18 +82,25 @@ var HasOne = /*#__PURE__*/function (_Association) {
57
82
  var path = "relationships[".concat(this.name, "].data");
58
83
 
59
84
  if (doc) {
60
- (0, _set2.default)(this.target, path, {
85
+ (0, _set.default)(this.target, path, {
61
86
  _id: doc._id,
62
87
  _type: doc._type
63
88
  });
64
89
  } else {
65
- (0, _set2.default)(this.target, path, undefined);
90
+ (0, _set.default)(this.target, path, undefined);
66
91
  }
67
92
  }
93
+ }, {
94
+ key: "set",
95
+ value: function set(doc) {
96
+ console.warn('set is deprecated for has-one relationships. Use `add` instead.');
97
+ this.setRelationship(doc);
98
+ }
68
99
  }, {
69
100
  key: "unset",
70
101
  value: function unset() {
71
- this.set(undefined);
102
+ console.warn('unset is deprecated for has-one relationships. Use `remove` instead.');
103
+ this.setRelationship(undefined);
72
104
  }
73
105
  }, {
74
106
  key: "dehydrate",
@@ -28,14 +28,16 @@ const fixtures = {
28
28
  const hydratedMaster = {
29
29
  ...fixtures.jediMaster,
30
30
  padawan: new HasOne(fixtures.jediMaster, 'padawan', 'io.cozy.jedis', {
31
- get: jest.fn().mockReturnValue(fixtures.apprentice)
31
+ get: jest.fn().mockReturnValue(fixtures.apprentice),
32
+ save: jest.fn()
32
33
  })
33
34
  }
34
35
 
35
36
  const hydratedApprentice = {
36
37
  ...fixtures.apprentice,
37
38
  padawan: new HasOne(fixtures.apprentice, 'padawan', 'io.cozy.jedis', {
38
- get: jest.fn().mockReturnValue(undefined)
39
+ get: jest.fn().mockReturnValue(undefined),
40
+ save: jest.fn()
39
41
  })
40
42
  }
41
43
 
@@ -101,10 +103,13 @@ describe('HasOne', () => {
101
103
  })
102
104
  })
103
105
 
104
- describe('set', () => {
106
+ describe('setRelationship', () => {
105
107
  it("should throw if the document to be set's type is not the same as the association type", () => {
106
108
  expect(() =>
107
- hydratedMaster.padawan.set({ _id: '1', _type: 'io.cozy.stuff' })
109
+ hydratedMaster.padawan.setRelationship({
110
+ _id: '1',
111
+ _type: 'io.cozy.stuff'
112
+ })
108
113
  ).toThrow()
109
114
  })
110
115
 
@@ -115,7 +120,7 @@ describe('HasOne', () => {
115
120
  name: 'Bernard Nobody'
116
121
  }
117
122
 
118
- hydratedMaster.padawan.set(newPadawan)
123
+ hydratedMaster.padawan.setRelationship(newPadawan)
119
124
 
120
125
  expect(hydratedMaster.padawan.raw).toEqual({
121
126
  _id: newPadawan._id,
@@ -123,4 +128,23 @@ describe('HasOne', () => {
123
128
  })
124
129
  })
125
130
  })
131
+
132
+ describe('remove', () => {
133
+ it('should remove', () => {
134
+ hydratedMaster.padawan.remove()
135
+ expect(hydratedMaster.padawan.raw).toEqual(null)
136
+ })
137
+ })
138
+
139
+ describe('add', () => {
140
+ it('should add', () => {
141
+ hydratedMaster.padawan.remove()
142
+ hydratedMaster.padawan.add({ _id: 'luke', _type: 'io.cozy.jedis' })
143
+
144
+ expect(hydratedMaster.padawan.raw).toEqual({
145
+ _type: 'io.cozy.jedis',
146
+ _id: 'luke'
147
+ })
148
+ })
149
+ })
126
150
  })
@@ -184,21 +184,7 @@ var extractAndMergeDocument = function extractAndMergeDocument(data, updatedStat
184
184
  }
185
185
 
186
186
  var sortedData = (0, _keyBy.default)(data, _helpers.properId);
187
- var mergedData = {};
188
-
189
- if (updatedStateWithIncluded && updatedStateWithIncluded[doctype]) {
190
- Object.values(updatedStateWithIncluded[doctype]).map(function (dataState) {
191
- if (!mergedData[doctype]) mergedData[doctype] = {};
192
- var id = (0, _helpers.properId)(dataState);
193
-
194
- if (sortedData[id]) {
195
- mergedData[doctype][id] = _objectSpread(_objectSpread(_objectSpread({}, dataState), sortedData[id]), mergedData[doctype][id]);
196
- } else {
197
- mergedData[doctype][id] = _objectSpread(_objectSpread({}, dataState), mergedData[doctype][id]);
198
- }
199
- });
200
- }
201
-
187
+ var mergedData = Object.assign({}, updatedStateWithIncluded);
202
188
  Object.values(sortedData).map(function (data) {
203
189
  if (!mergedData[doctype]) mergedData[doctype] = {};
204
190
  var id = (0, _helpers.properId)(data);
@@ -209,7 +195,7 @@ var extractAndMergeDocument = function extractAndMergeDocument(data, updatedStat
209
195
  mergedData[doctype][id] = data;
210
196
  }
211
197
  });
212
- return _objectSpread(_objectSpread({}, updatedStateWithIncluded), mergedData);
198
+ return mergedData;
213
199
  };
214
200
 
215
201
  exports.extractAndMergeDocument = extractAndMergeDocument;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cozy-client",
3
- "version": "24.6.0",
3
+ "version": "24.8.0",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.node.js",
6
6
  "browser": "dist/index.js",
@@ -21,19 +21,19 @@
21
21
  "cozy-device-helper": "^1.12.0",
22
22
  "cozy-flags": "2.7.1",
23
23
  "cozy-logger": "^1.6.0",
24
- "cozy-stack-client": "^24.6.0",
24
+ "cozy-stack-client": "^24.6.1",
25
25
  "json-stable-stringify": "^1.0.1",
26
26
  "lodash": "^4.17.13",
27
27
  "microee": "^0.0.6",
28
28
  "node-fetch": "^2.6.1",
29
- "open": "^7.0.2",
29
+ "open": "^8.0.0",
30
30
  "prop-types": "^15.6.2",
31
31
  "react-redux": "^7.2.0",
32
32
  "redux": "3 || 4",
33
33
  "redux-thunk": "^2.3.0",
34
34
  "server-destroy": "^1.0.1",
35
35
  "sift": "^6.0.0",
36
- "url-search-params-polyfill": "^7.0.0"
36
+ "url-search-params-polyfill": "^8.0.0"
37
37
  },
38
38
  "scripts": {
39
39
  "build": "../../bin/build",
@@ -49,7 +49,7 @@
49
49
  "@testing-library/react-hooks": "^3.2.1",
50
50
  "babel-plugin-search-and-replace": "1.1.0",
51
51
  "btoa": "^1.2.1",
52
- "cozy-logger": "^1.6.0",
52
+ "cozy-logger": "1.7.0",
53
53
  "cozy-ui": "48.0.0",
54
54
  "date-fns": "2.19.0",
55
55
  "jsdoc-plugin-intersection": "^1.0.4",
@@ -57,16 +57,16 @@
57
57
  "react": "16.14.0",
58
58
  "react-inspector": "5.1.1",
59
59
  "react-redux": "7.2.4",
60
- "react-test-renderer": "^16.12.0",
60
+ "react-test-renderer": "16.14.0",
61
61
  "server-destroy": "^1.0.1",
62
62
  "typescript": "^4.1.5"
63
63
  },
64
64
  "peerDependencies": {
65
65
  "cozy-ui": ">34.0.0",
66
66
  "react": "^16.7.0",
67
- "react-native": "^0.64.1",
67
+ "react-native": "^0.65.0",
68
68
  "react-native-inappbrowser-reborn": "^3.5.1"
69
69
  },
70
70
  "sideEffects": false,
71
- "gitHead": "3a68634bf0b13cf4f4ae138681f47ddebe433cec"
71
+ "gitHead": "eb10dd8e450275b8ad3d10ae33f35c1e57e76bc4"
72
72
  }
@@ -87,6 +87,20 @@ declare class HasMany extends Association {
87
87
  exists(document: any): boolean;
88
88
  containsById(id: any): boolean;
89
89
  existsById(id: any): boolean;
90
+ /**
91
+ * Add the relationships to the target document
92
+ *
93
+ * @param {CozyClientDocument[]} docsArg - Documents to add as relationships
94
+ * @returns {CozyClientDocument} The saved target document
95
+ */
96
+ add(docsArg: CozyClientDocument[]): CozyClientDocument;
97
+ /**
98
+ * Remove the relationships from the target document
99
+ *
100
+ * @param {CozyClientDocument[]} docsArg - Documents to remove as relationships
101
+ * @returns {CozyClientDocument} The saved target document
102
+ */
103
+ remove(docsArg: CozyClientDocument[]): CozyClientDocument;
90
104
  /**
91
105
  * Add a referenced document by id. You need to call save()
92
106
  * in order to synchronize your document with the store.
@@ -106,3 +120,4 @@ declare class HasMany extends Association {
106
120
  dehydrate(doc: any): any;
107
121
  }
108
122
  import Association from "./Association";
123
+ import { CozyClientDocument } from "../types";
@@ -6,8 +6,23 @@ export default class HasOne extends Association {
6
6
  save: Function;
7
7
  dispatch: Function;
8
8
  });
9
+ /**
10
+ * Add the relationship to the target document
11
+ *
12
+ * @param {CozyClientDocument} doc - Document to add as a relationship
13
+ * @returns {CozyClientDocument} The saved target document
14
+ */
15
+ add(doc: CozyClientDocument): CozyClientDocument;
16
+ /**
17
+ * Remove the relationship from the target document
18
+ *
19
+ * @returns {CozyClientDocument} The saved target document
20
+ */
21
+ remove(): CozyClientDocument;
22
+ setRelationship(doc: any): void;
9
23
  set(doc: any): void;
10
24
  unset(): void;
11
25
  dehydrate(doc: any): any;
12
26
  }
13
27
  import Association from "./Association";
28
+ import { CozyClientDocument } from "../types";