cozy-client 46.7.1 → 46.9.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/dist/CozyClient.js +90 -13
- package/dist/__tests__/mocks.js +2 -1
- package/dist/helpers/index.js +33 -1
- package/dist/helpers/settings.js +311 -0
- package/dist/helpers/settings.spec.js +493 -0
- package/dist/hooks/index.js +9 -1
- package/dist/hooks/useSetting.js +64 -0
- package/dist/models/document/locales/fr.json +1 -1
- package/dist/types.js +44 -4
- package/package.json +2 -2
- package/types/CozyClient.d.ts +29 -0
- package/types/__tests__/mocks.d.ts +1 -0
- package/types/helpers/index.d.ts +1 -0
- package/types/helpers/settings.d.ts +7 -0
- package/types/hooks/index.d.ts +1 -0
- package/types/hooks/useSetting.d.ts +1 -0
- package/types/types.d.ts +40 -8
package/dist/CozyClient.js
CHANGED
|
@@ -1245,6 +1245,10 @@ var CozyClient = /*#__PURE__*/function () {
|
|
|
1245
1245
|
* `getQueryFromState` or directly using `<Query />`. `<Query />` automatically
|
|
1246
1246
|
* executes its query when mounted if no fetch policy has been indicated.
|
|
1247
1247
|
*
|
|
1248
|
+
* If the query is called under the fetch policy's delay, then the query
|
|
1249
|
+
* is not executed and nothing is returned. If you need a result anyway,
|
|
1250
|
+
* please use `fetchQueryAndGetFromState` instead
|
|
1251
|
+
*
|
|
1248
1252
|
* @param {QueryDefinition} queryDefinition - Definition that will be executed
|
|
1249
1253
|
* @param {import("./types").QueryOptions} [options] - Options
|
|
1250
1254
|
* @returns {Promise<import("./types").QueryResult>}
|
|
@@ -2543,6 +2547,79 @@ var CozyClient = /*#__PURE__*/function () {
|
|
|
2543
2547
|
value: function setAppMetadata(newAppMetadata) {
|
|
2544
2548
|
this.appMetadata = _objectSpread(_objectSpread({}, this.appMetadata), newAppMetadata);
|
|
2545
2549
|
}
|
|
2550
|
+
/**
|
|
2551
|
+
* Query the cozy-app settings corresponding to the given slug and
|
|
2552
|
+
* extract the value corresponding to the given `key`
|
|
2553
|
+
*
|
|
2554
|
+
* @template {string} T
|
|
2555
|
+
*
|
|
2556
|
+
* @param {string} slug - the cozy-app's slug containing the setting (can be 'instance' for global settings)
|
|
2557
|
+
* @param {T[]} keys - The names of the settings to retrieve
|
|
2558
|
+
* @returns {Promise<any>} - The value of the requested setting
|
|
2559
|
+
*/
|
|
2560
|
+
|
|
2561
|
+
}, {
|
|
2562
|
+
key: "getSettings",
|
|
2563
|
+
value: function () {
|
|
2564
|
+
var _getSettings2 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee19(slug, keys) {
|
|
2565
|
+
return _regenerator.default.wrap(function _callee19$(_context19) {
|
|
2566
|
+
while (1) {
|
|
2567
|
+
switch (_context19.prev = _context19.next) {
|
|
2568
|
+
case 0:
|
|
2569
|
+
return _context19.abrupt("return", (0, _helpers2.getSettings)(this, slug, keys));
|
|
2570
|
+
|
|
2571
|
+
case 1:
|
|
2572
|
+
case "end":
|
|
2573
|
+
return _context19.stop();
|
|
2574
|
+
}
|
|
2575
|
+
}
|
|
2576
|
+
}, _callee19, this);
|
|
2577
|
+
}));
|
|
2578
|
+
|
|
2579
|
+
function getSettings(_x17, _x18) {
|
|
2580
|
+
return _getSettings2.apply(this, arguments);
|
|
2581
|
+
}
|
|
2582
|
+
|
|
2583
|
+
return getSettings;
|
|
2584
|
+
}()
|
|
2585
|
+
/**
|
|
2586
|
+
* Save the given value into the corresponding cozy-app setting
|
|
2587
|
+
*
|
|
2588
|
+
* This methods will first query the cozy-app's settings before injecting the new value and then
|
|
2589
|
+
* save the new resulting settings into database
|
|
2590
|
+
*
|
|
2591
|
+
* @template {string} T
|
|
2592
|
+
*
|
|
2593
|
+
* @param {string} slug - the cozy-app's slug containing the setting (can be 'instance' for global settings)
|
|
2594
|
+
* @param {Record<string, any> | ((oldValue) => Record<T, any>)} itemsOrSetter - The new values of the settings to save. It can be a raw dictionnary, or a callback that should return a new dictionnary
|
|
2595
|
+
* @param {T[]=} setterKeys - The new values of the settings to save. It can be a raw dictionnary, or a callback that should return a new dictionnary
|
|
2596
|
+
* @returns {Promise<any>} - The result of the `client.save()` call
|
|
2597
|
+
*/
|
|
2598
|
+
|
|
2599
|
+
}, {
|
|
2600
|
+
key: "saveAfterFetchSettings",
|
|
2601
|
+
value: function () {
|
|
2602
|
+
var _saveAfterFetchSettings2 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee20(slug, itemsOrSetter, setterKeys) {
|
|
2603
|
+
return _regenerator.default.wrap(function _callee20$(_context20) {
|
|
2604
|
+
while (1) {
|
|
2605
|
+
switch (_context20.prev = _context20.next) {
|
|
2606
|
+
case 0:
|
|
2607
|
+
return _context20.abrupt("return", (0, _helpers2.saveAfterFetchSettings)(this, slug, itemsOrSetter, setterKeys));
|
|
2608
|
+
|
|
2609
|
+
case 1:
|
|
2610
|
+
case "end":
|
|
2611
|
+
return _context20.stop();
|
|
2612
|
+
}
|
|
2613
|
+
}
|
|
2614
|
+
}, _callee20, this);
|
|
2615
|
+
}));
|
|
2616
|
+
|
|
2617
|
+
function saveAfterFetchSettings(_x19, _x20, _x21) {
|
|
2618
|
+
return _saveAfterFetchSettings2.apply(this, arguments);
|
|
2619
|
+
}
|
|
2620
|
+
|
|
2621
|
+
return saveAfterFetchSettings;
|
|
2622
|
+
}()
|
|
2546
2623
|
}], [{
|
|
2547
2624
|
key: "fromOldClient",
|
|
2548
2625
|
value: function fromOldClient(oldClient, options) {
|
|
@@ -2565,22 +2642,22 @@ var CozyClient = /*#__PURE__*/function () {
|
|
|
2565
2642
|
}, {
|
|
2566
2643
|
key: "fromOldOAuthClient",
|
|
2567
2644
|
value: function () {
|
|
2568
|
-
var _fromOldOAuthClient = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function
|
|
2645
|
+
var _fromOldOAuthClient = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee21(oldClient, options) {
|
|
2569
2646
|
var credentials, oauthOptions, client;
|
|
2570
|
-
return _regenerator.default.wrap(function
|
|
2647
|
+
return _regenerator.default.wrap(function _callee21$(_context21) {
|
|
2571
2648
|
while (1) {
|
|
2572
|
-
switch (
|
|
2649
|
+
switch (_context21.prev = _context21.next) {
|
|
2573
2650
|
case 0:
|
|
2574
2651
|
if (!oldClient._oauth) {
|
|
2575
|
-
|
|
2652
|
+
_context21.next = 12;
|
|
2576
2653
|
break;
|
|
2577
2654
|
}
|
|
2578
2655
|
|
|
2579
|
-
|
|
2656
|
+
_context21.next = 3;
|
|
2580
2657
|
return oldClient.authorize();
|
|
2581
2658
|
|
|
2582
2659
|
case 3:
|
|
2583
|
-
credentials =
|
|
2660
|
+
credentials = _context21.sent;
|
|
2584
2661
|
oauthOptions = {
|
|
2585
2662
|
oauth: credentials.client,
|
|
2586
2663
|
token: credentials.token,
|
|
@@ -2591,28 +2668,28 @@ var CozyClient = /*#__PURE__*/function () {
|
|
|
2591
2668
|
}, oauthOptions), options));
|
|
2592
2669
|
|
|
2593
2670
|
if (!client.loginPromise) {
|
|
2594
|
-
|
|
2671
|
+
_context21.next = 9;
|
|
2595
2672
|
break;
|
|
2596
2673
|
}
|
|
2597
2674
|
|
|
2598
|
-
|
|
2675
|
+
_context21.next = 9;
|
|
2599
2676
|
return client.loginPromise;
|
|
2600
2677
|
|
|
2601
2678
|
case 9:
|
|
2602
|
-
return
|
|
2679
|
+
return _context21.abrupt("return", client);
|
|
2603
2680
|
|
|
2604
2681
|
case 12:
|
|
2605
2682
|
throw new Error('Cannot instantiate a new client: old client is not an OAuth client. CozyClient.fromOldClient might be more suitable.');
|
|
2606
2683
|
|
|
2607
2684
|
case 13:
|
|
2608
2685
|
case "end":
|
|
2609
|
-
return
|
|
2686
|
+
return _context21.stop();
|
|
2610
2687
|
}
|
|
2611
2688
|
}
|
|
2612
|
-
},
|
|
2689
|
+
}, _callee21);
|
|
2613
2690
|
}));
|
|
2614
2691
|
|
|
2615
|
-
function fromOldOAuthClient(
|
|
2692
|
+
function fromOldOAuthClient(_x22, _x23) {
|
|
2616
2693
|
return _fromOldOAuthClient.apply(this, arguments);
|
|
2617
2694
|
}
|
|
2618
2695
|
|
|
@@ -2705,7 +2782,7 @@ var CozyClient = /*#__PURE__*/function () {
|
|
|
2705
2782
|
CozyClient.hooks = CozyClient.hooks || {};
|
|
2706
2783
|
CozyClient.fetchPolicies = _policies.default; //COZY_CLIENT_VERSION_PACKAGE in replaced by babel. See babel config
|
|
2707
2784
|
|
|
2708
|
-
CozyClient.version = "46.
|
|
2785
|
+
CozyClient.version = "46.9.0";
|
|
2709
2786
|
|
|
2710
2787
|
_microee.default.mixin(CozyClient);
|
|
2711
2788
|
|
package/dist/__tests__/mocks.js
CHANGED
|
@@ -38,7 +38,8 @@ var client = function client(implementations) {
|
|
|
38
38
|
makeObservableQuery: jest.fn(),
|
|
39
39
|
requestQuery: jest.fn(),
|
|
40
40
|
all: jest.fn(),
|
|
41
|
-
setStore: jest.fn()
|
|
41
|
+
setStore: jest.fn(),
|
|
42
|
+
fetchQueryAndGetFromState: jest.fn()
|
|
42
43
|
};
|
|
43
44
|
mockImplementations(base, implementations);
|
|
44
45
|
return base;
|
package/dist/helpers/index.js
CHANGED
|
@@ -63,7 +63,39 @@ Object.defineProperty(exports, "dehydrate", {
|
|
|
63
63
|
return _dehydrateHelper.dehydrate;
|
|
64
64
|
}
|
|
65
65
|
});
|
|
66
|
+
Object.defineProperty(exports, "editSettings", {
|
|
67
|
+
enumerable: true,
|
|
68
|
+
get: function get() {
|
|
69
|
+
return _settings.editSettings;
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
Object.defineProperty(exports, "getQuery", {
|
|
73
|
+
enumerable: true,
|
|
74
|
+
get: function get() {
|
|
75
|
+
return _settings.getQuery;
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
Object.defineProperty(exports, "getSettings", {
|
|
79
|
+
enumerable: true,
|
|
80
|
+
get: function get() {
|
|
81
|
+
return _settings.getSettings;
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
Object.defineProperty(exports, "normalizeSettings", {
|
|
85
|
+
enumerable: true,
|
|
86
|
+
get: function get() {
|
|
87
|
+
return _settings.normalizeSettings;
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
Object.defineProperty(exports, "saveAfterFetchSettings", {
|
|
91
|
+
enumerable: true,
|
|
92
|
+
get: function get() {
|
|
93
|
+
return _settings.saveAfterFetchSettings;
|
|
94
|
+
}
|
|
95
|
+
});
|
|
66
96
|
|
|
67
97
|
var _urlHelper = require("./urlHelper");
|
|
68
98
|
|
|
69
|
-
var _dehydrateHelper = require("./dehydrateHelper");
|
|
99
|
+
var _dehydrateHelper = require("./dehydrateHelper");
|
|
100
|
+
|
|
101
|
+
var _settings = require("./settings");
|
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
exports.getQuery = exports.extractKeys = exports.editSettings = exports.normalizeSettings = exports.saveAfterFetchSettings = exports.getSettings = void 0;
|
|
9
|
+
|
|
10
|
+
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
|
|
11
|
+
|
|
12
|
+
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
13
|
+
|
|
14
|
+
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
|
|
15
|
+
|
|
16
|
+
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
|
|
17
|
+
|
|
18
|
+
var _CozyClient = _interopRequireDefault(require("../CozyClient"));
|
|
19
|
+
|
|
20
|
+
var _policies = _interopRequireDefault(require("../policies"));
|
|
21
|
+
|
|
22
|
+
var _dsl = require("../queries/dsl");
|
|
23
|
+
|
|
24
|
+
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; } } }; }
|
|
25
|
+
|
|
26
|
+
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); }
|
|
27
|
+
|
|
28
|
+
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; }
|
|
29
|
+
|
|
30
|
+
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; }
|
|
31
|
+
|
|
32
|
+
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; }
|
|
33
|
+
|
|
34
|
+
var defaultFetchPolicy = _policies.default.olderThan(60 * 60 * 1000);
|
|
35
|
+
/**
|
|
36
|
+
* Query the cozy-app settings corresponding to the given slug and
|
|
37
|
+
* extract the values corresponding to the given `keys`
|
|
38
|
+
*
|
|
39
|
+
* @template {string} T
|
|
40
|
+
*
|
|
41
|
+
* @param {CozyClient} client - Cozy client instance
|
|
42
|
+
* @param {string} slug - the cozy-app's slug containing the setting (can be 'instance' for global settings)
|
|
43
|
+
* @param {T[]} keys - The names of the settings to retrieve
|
|
44
|
+
* @returns {Promise<Record<T, any>>} - The values of the requested settings
|
|
45
|
+
*/
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
var getSettings = /*#__PURE__*/function () {
|
|
49
|
+
var _ref = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(client, slug, keys) {
|
|
50
|
+
var query, currentSettingsResult, currentSettings;
|
|
51
|
+
return _regenerator.default.wrap(function _callee$(_context) {
|
|
52
|
+
while (1) {
|
|
53
|
+
switch (_context.prev = _context.next) {
|
|
54
|
+
case 0:
|
|
55
|
+
query = getQuery(slug);
|
|
56
|
+
_context.next = 3;
|
|
57
|
+
return client.fetchQueryAndGetFromState({
|
|
58
|
+
definition: query.definition,
|
|
59
|
+
options: query.options
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
case 3:
|
|
63
|
+
currentSettingsResult = _context.sent;
|
|
64
|
+
currentSettings = normalizeSettings(currentSettingsResult.data); // @ts-ignore
|
|
65
|
+
|
|
66
|
+
return _context.abrupt("return", extractKeys(currentSettings, keys));
|
|
67
|
+
|
|
68
|
+
case 6:
|
|
69
|
+
case "end":
|
|
70
|
+
return _context.stop();
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}, _callee);
|
|
74
|
+
}));
|
|
75
|
+
|
|
76
|
+
return function getSettings(_x, _x2, _x3) {
|
|
77
|
+
return _ref.apply(this, arguments);
|
|
78
|
+
};
|
|
79
|
+
}();
|
|
80
|
+
/**
|
|
81
|
+
* Save the given value into the corresponding cozy-app setting
|
|
82
|
+
*
|
|
83
|
+
* This methods will first query the cozy-app's settings before injecting the new value and then
|
|
84
|
+
* save the new resulting settings into database
|
|
85
|
+
*
|
|
86
|
+
* @template {string} T
|
|
87
|
+
*
|
|
88
|
+
* @param {CozyClient} client - Cozy client instance
|
|
89
|
+
* @param {string} slug - the cozy-app's slug containing the setting (special cases are: 'instance' for global settings and 'bitwarden' for cozy-pass)
|
|
90
|
+
* @param {Record<string, any> | ((oldValue) => Record<T, any>)} itemsOrSetter - The new values of the settings to save. It can be a raw dictionnary, or a callback that should return a new dictionnary
|
|
91
|
+
* @param {T[]=} setterKeys - The new values of the settings to save. It can be a raw dictionnary, or a callback that should return a new dictionnary
|
|
92
|
+
* @returns {Promise<any>} - The result of the `client.save()` call
|
|
93
|
+
*/
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
exports.getSettings = getSettings;
|
|
97
|
+
|
|
98
|
+
var saveAfterFetchSettings = /*#__PURE__*/function () {
|
|
99
|
+
var _ref2 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee2(client, slug, itemsOrSetter, setterKeys) {
|
|
100
|
+
var query, currentSettingsResult, currentSettings, items, currentItems, _currentItems, newSettings;
|
|
101
|
+
|
|
102
|
+
return _regenerator.default.wrap(function _callee2$(_context2) {
|
|
103
|
+
while (1) {
|
|
104
|
+
switch (_context2.prev = _context2.next) {
|
|
105
|
+
case 0:
|
|
106
|
+
query = getQuery(slug);
|
|
107
|
+
_context2.next = 3;
|
|
108
|
+
return client.fetchQueryAndGetFromState({
|
|
109
|
+
definition: query.definition,
|
|
110
|
+
options: query.options
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
case 3:
|
|
114
|
+
currentSettingsResult = _context2.sent;
|
|
115
|
+
currentSettings = normalizeSettings(currentSettingsResult.data);
|
|
116
|
+
items = undefined;
|
|
117
|
+
|
|
118
|
+
if (typeof itemsOrSetter === 'function') {
|
|
119
|
+
currentItems = extractKeys(currentSettings, setterKeys);
|
|
120
|
+
items = itemsOrSetter(currentItems);
|
|
121
|
+
} else {
|
|
122
|
+
_currentItems = extractKeys(currentSettings, Object.keys(itemsOrSetter));
|
|
123
|
+
items = _objectSpread(_objectSpread({}, _currentItems), itemsOrSetter);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
newSettings = editSettings(slug, currentSettings, items);
|
|
127
|
+
_context2.next = 10;
|
|
128
|
+
return client.save(newSettings);
|
|
129
|
+
|
|
130
|
+
case 10:
|
|
131
|
+
return _context2.abrupt("return", _context2.sent);
|
|
132
|
+
|
|
133
|
+
case 11:
|
|
134
|
+
case "end":
|
|
135
|
+
return _context2.stop();
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}, _callee2);
|
|
139
|
+
}));
|
|
140
|
+
|
|
141
|
+
return function saveAfterFetchSettings(_x4, _x5, _x6, _x7) {
|
|
142
|
+
return _ref2.apply(this, arguments);
|
|
143
|
+
};
|
|
144
|
+
}();
|
|
145
|
+
/**
|
|
146
|
+
* Convert a result from a `client.query()` or a `useQuery()` that can be
|
|
147
|
+
* an object or an array of objects into a single object
|
|
148
|
+
*
|
|
149
|
+
* @param {Array | Object} data - Result from a client.query or a useQuery
|
|
150
|
+
* @returns {Object} A single object containing the setting data
|
|
151
|
+
*/
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
exports.saveAfterFetchSettings = saveAfterFetchSettings;
|
|
155
|
+
|
|
156
|
+
var normalizeSettings = function normalizeSettings(data) {
|
|
157
|
+
var settingsData = Array.isArray(data) ? data[0] : data;
|
|
158
|
+
return settingsData || {};
|
|
159
|
+
};
|
|
160
|
+
/**
|
|
161
|
+
* Edit the given settings by injecting `value` into the `key` entry
|
|
162
|
+
* This methods takes care of the kind of settings that is edited as there are
|
|
163
|
+
* some exceptions in settings formats (i.e. `io.cozy.settings.instance`)
|
|
164
|
+
*
|
|
165
|
+
* @param {string} slug - the cozy-app's slug containing the setting (can be 'instance' for global settings)
|
|
166
|
+
* @param {Object} currentSettings - the Setting object (ideally from a `client.query()` or a `useQuery()` and normalized using `normalizeSettings`)
|
|
167
|
+
* @param {Record<string, unknown>} items - The new values for the settings
|
|
168
|
+
* @returns {Object} a new Setting object containing the new value
|
|
169
|
+
*/
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
exports.normalizeSettings = normalizeSettings;
|
|
173
|
+
|
|
174
|
+
var editSettings = function editSettings(slug, currentSettings, items) {
|
|
175
|
+
var type = getDoctype(slug);
|
|
176
|
+
var newSettings = slug === 'instance' ? mergeInstance(currentSettings, items) : mergeSettings(currentSettings, type, items);
|
|
177
|
+
return newSettings;
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
exports.editSettings = editSettings;
|
|
181
|
+
|
|
182
|
+
var mergeInstance = function mergeInstance(currentSettings, items) {
|
|
183
|
+
var newSettings = _objectSpread(_objectSpread({
|
|
184
|
+
_id: currentSettings._id,
|
|
185
|
+
_type: currentSettings._type,
|
|
186
|
+
_rev: currentSettings.meta.rev
|
|
187
|
+
}, currentSettings), {}, {
|
|
188
|
+
attributes: _objectSpread({}, currentSettings.attributes)
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
for (var _i = 0, _Object$entries = Object.entries(items); _i < _Object$entries.length; _i++) {
|
|
192
|
+
var _Object$entries$_i = (0, _slicedToArray2.default)(_Object$entries[_i], 2),
|
|
193
|
+
key = _Object$entries$_i[0],
|
|
194
|
+
value = _Object$entries$_i[1];
|
|
195
|
+
|
|
196
|
+
newSettings[key] = value;
|
|
197
|
+
newSettings.attributes[key] = value;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
return newSettings;
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
var mergeSettings = function mergeSettings(currentSettings, type, items) {
|
|
204
|
+
var newSettings = _objectSpread({
|
|
205
|
+
_type: type
|
|
206
|
+
}, currentSettings);
|
|
207
|
+
|
|
208
|
+
for (var _i2 = 0, _Object$entries2 = Object.entries(items); _i2 < _Object$entries2.length; _i2++) {
|
|
209
|
+
var _Object$entries2$_i = (0, _slicedToArray2.default)(_Object$entries2[_i2], 2),
|
|
210
|
+
key = _Object$entries2$_i[0],
|
|
211
|
+
value = _Object$entries2$_i[1];
|
|
212
|
+
|
|
213
|
+
newSettings[key] = value;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
return newSettings;
|
|
217
|
+
};
|
|
218
|
+
/**
|
|
219
|
+
* Extract values from given settings for requested keys
|
|
220
|
+
*
|
|
221
|
+
* @template {string} T
|
|
222
|
+
*
|
|
223
|
+
* @param {Record<T, any>} settings - the Setting object (ideally from a `client.query()` or a `useQuery()` and normalized using `normalizeSettings`)
|
|
224
|
+
* @param {T[]} keys - The names of the settings to extract
|
|
225
|
+
* @returns {Record<T, any>} - Dictionnary containing the values for the requested keys
|
|
226
|
+
*/
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
var extractKeys = function extractKeys(settings, keys) {
|
|
230
|
+
var result = {};
|
|
231
|
+
|
|
232
|
+
var _iterator = _createForOfIteratorHelper(keys),
|
|
233
|
+
_step;
|
|
234
|
+
|
|
235
|
+
try {
|
|
236
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
237
|
+
var key = _step.value;
|
|
238
|
+
// @ts-ignore
|
|
239
|
+
result[key] = settings[key];
|
|
240
|
+
} // @ts-ignore
|
|
241
|
+
|
|
242
|
+
} catch (err) {
|
|
243
|
+
_iterator.e(err);
|
|
244
|
+
} finally {
|
|
245
|
+
_iterator.f();
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
return result;
|
|
249
|
+
};
|
|
250
|
+
/**
|
|
251
|
+
* Create a Query that can be used to fetch the cozy-app settings for the given slug
|
|
252
|
+
*
|
|
253
|
+
* @param {string} slug - the cozy-app's slug containing the setting (special cases are: 'instance' for global settings and 'passwords' for bitwarden settings)
|
|
254
|
+
* @returns {import('../types').Query} - the Query that can be used to fetch the cozy-app settings
|
|
255
|
+
*/
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
exports.extractKeys = extractKeys;
|
|
259
|
+
|
|
260
|
+
var getQuery = function getQuery(slug) {
|
|
261
|
+
if (slug === 'instance') {
|
|
262
|
+
return getNestedSettings(slug);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
if (slug === 'passwords') {
|
|
266
|
+
return getNestedSettings('bitwarden');
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
return getRootSettings(slug);
|
|
270
|
+
};
|
|
271
|
+
|
|
272
|
+
exports.getQuery = getQuery;
|
|
273
|
+
|
|
274
|
+
var getRootSettings = function getRootSettings(slug) {
|
|
275
|
+
var settingsDoctype = getDoctype(slug);
|
|
276
|
+
var query = {
|
|
277
|
+
definition: (0, _dsl.Q)(settingsDoctype).limitBy(1),
|
|
278
|
+
options: {
|
|
279
|
+
as: settingsDoctype,
|
|
280
|
+
fetchPolicy: defaultFetchPolicy,
|
|
281
|
+
singleDocData: true
|
|
282
|
+
}
|
|
283
|
+
};
|
|
284
|
+
return query;
|
|
285
|
+
};
|
|
286
|
+
|
|
287
|
+
var getNestedSettings = function getNestedSettings(slug) {
|
|
288
|
+
var doctype = "io.cozy.settings";
|
|
289
|
+
var subDoctype = getDoctype(slug);
|
|
290
|
+
var query = {
|
|
291
|
+
definition: (0, _dsl.Q)(doctype).getById(subDoctype),
|
|
292
|
+
options: {
|
|
293
|
+
as: "".concat(doctype, "/").concat(subDoctype),
|
|
294
|
+
fetchPolicy: defaultFetchPolicy,
|
|
295
|
+
singleDocData: true
|
|
296
|
+
}
|
|
297
|
+
};
|
|
298
|
+
return query;
|
|
299
|
+
};
|
|
300
|
+
|
|
301
|
+
var getDoctype = function getDoctype(slug) {
|
|
302
|
+
if (['instance', 'bitwarden'].includes(slug)) {
|
|
303
|
+
return "io.cozy.settings.".concat(slug);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
if (slug === 'passwords') {
|
|
307
|
+
return 'io.cozy.settings.bitwarden';
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
return "io.cozy.".concat(slug, ".settings");
|
|
311
|
+
};
|