cozy-client 32.7.4 → 33.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.
@@ -266,9 +266,11 @@ var CozyClient = /*#__PURE__*/function () {
266
266
 
267
267
  this.plugins = {};
268
268
 
269
- try {
270
- this.loadInstanceOptionsFromDOM();
271
- } catch (err) {// not a critical error, we may be in node or the instance options are not on the default HTML element
269
+ if (!(this.stackClient instanceof _cozyStackClient.OAuthClient)) {
270
+ try {
271
+ this.loadInstanceOptionsFromDOM();
272
+ } catch (err) {// not a critical error, we may be in node or the instance options are not on the default HTML element
273
+ }
272
274
  }
273
275
 
274
276
  if (_options.uri && _options.token) {
@@ -513,9 +515,19 @@ var CozyClient = /*#__PURE__*/function () {
513
515
  case 21:
514
516
  this.isLogged = true;
515
517
  this.isRevoked = false;
518
+
519
+ if (!(this.stackClient instanceof _cozyStackClient.OAuthClient)) {
520
+ _context2.next = 26;
521
+ break;
522
+ }
523
+
524
+ _context2.next = 26;
525
+ return this.loadInstanceOptionsFromStack();
526
+
527
+ case 26:
516
528
  this.emit('login');
517
529
 
518
- case 24:
530
+ case 27:
519
531
  case "end":
520
532
  return _context2.stop();
521
533
  }
@@ -2382,6 +2394,49 @@ var CozyClient = /*#__PURE__*/function () {
2382
2394
  this.instanceOptions = _objectSpread(_objectSpread({}, JSON.parse(cozy)), dataset);
2383
2395
  this.capabilities = this.instanceOptions.capabilities || null;
2384
2396
  }
2397
+ /**
2398
+ * loadInstanceOptionsFromStack - Loads the instance options from cozy-stack and exposes it through getInstanceOptions
2399
+ *
2400
+ * For now only retrieving capabilities is supported
2401
+ *
2402
+ * @returns {Promise<void>}
2403
+ */
2404
+
2405
+ }, {
2406
+ key: "loadInstanceOptionsFromStack",
2407
+ value: function () {
2408
+ var _loadInstanceOptionsFromStack = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee18() {
2409
+ var _yield$this$query, data;
2410
+
2411
+ return _regenerator.default.wrap(function _callee18$(_context18) {
2412
+ while (1) {
2413
+ switch (_context18.prev = _context18.next) {
2414
+ case 0:
2415
+ _context18.next = 2;
2416
+ return this.query((0, _dsl.Q)('io.cozy.settings').getById('capabilities'));
2417
+
2418
+ case 2:
2419
+ _yield$this$query = _context18.sent;
2420
+ data = _yield$this$query.data;
2421
+ this.instanceOptions = {
2422
+ capabilities: data.attributes
2423
+ };
2424
+ this.capabilities = this.instanceOptions.capabilities || null;
2425
+
2426
+ case 6:
2427
+ case "end":
2428
+ return _context18.stop();
2429
+ }
2430
+ }
2431
+ }, _callee18, this);
2432
+ }));
2433
+
2434
+ function loadInstanceOptionsFromStack() {
2435
+ return _loadInstanceOptionsFromStack.apply(this, arguments);
2436
+ }
2437
+
2438
+ return loadInstanceOptionsFromStack;
2439
+ }()
2385
2440
  /**
2386
2441
  * Directly set the data in the store, without using a query
2387
2442
  * This is useful for cases like Pouch replication, which wants to
@@ -2451,28 +2506,28 @@ var CozyClient = /*#__PURE__*/function () {
2451
2506
  }, {
2452
2507
  key: "fromOldOAuthClient",
2453
2508
  value: function () {
2454
- var _fromOldOAuthClient = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee18(oldClient, options) {
2509
+ var _fromOldOAuthClient = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee19(oldClient, options) {
2455
2510
  var credentials, oauthOptions;
2456
- return _regenerator.default.wrap(function _callee18$(_context18) {
2511
+ return _regenerator.default.wrap(function _callee19$(_context19) {
2457
2512
  while (1) {
2458
- switch (_context18.prev = _context18.next) {
2513
+ switch (_context19.prev = _context19.next) {
2459
2514
  case 0:
2460
2515
  if (!oldClient._oauth) {
2461
- _context18.next = 8;
2516
+ _context19.next = 8;
2462
2517
  break;
2463
2518
  }
2464
2519
 
2465
- _context18.next = 3;
2520
+ _context19.next = 3;
2466
2521
  return oldClient.authorize();
2467
2522
 
2468
2523
  case 3:
2469
- credentials = _context18.sent;
2524
+ credentials = _context19.sent;
2470
2525
  oauthOptions = {
2471
2526
  oauth: credentials.client,
2472
2527
  token: credentials.token,
2473
2528
  scope: credentials.token.scope
2474
2529
  };
2475
- return _context18.abrupt("return", new CozyClient(_objectSpread(_objectSpread({
2530
+ return _context19.abrupt("return", new CozyClient(_objectSpread(_objectSpread({
2476
2531
  uri: oldClient._url
2477
2532
  }, oauthOptions), options)));
2478
2533
 
@@ -2481,10 +2536,10 @@ var CozyClient = /*#__PURE__*/function () {
2481
2536
 
2482
2537
  case 9:
2483
2538
  case "end":
2484
- return _context18.stop();
2539
+ return _context19.stop();
2485
2540
  }
2486
2541
  }
2487
- }, _callee18);
2542
+ }, _callee19);
2488
2543
  }));
2489
2544
 
2490
2545
  function fromOldOAuthClient(_x17, _x18) {
@@ -2581,7 +2636,7 @@ var CozyClient = /*#__PURE__*/function () {
2581
2636
  CozyClient.hooks = CozyClient.hooks || {};
2582
2637
  CozyClient.fetchPolicies = _policies.default; //COZY_CLIENT_VERSION_PACKAGE in replaced by babel. See babel config
2583
2638
 
2584
- CozyClient.version = "32.7.4";
2639
+ CozyClient.version = "33.0.0";
2585
2640
 
2586
2641
  _microee.default.mixin(CozyClient);
2587
2642
 
@@ -493,6 +493,9 @@ describe('CozyClient logout', () => {
493
493
  })
494
494
 
495
495
  it('should call reset on each link that can be reset', async () => {
496
+ client.query = jest.fn().mockResolvedValueOnce({
497
+ data: { attributes: { file_versioning: true, flat_subdomains: true } }
498
+ })
496
499
  links[0].reset = jest.fn()
497
500
  links[2].reset = jest.fn()
498
501
  await client.login()
@@ -510,6 +513,9 @@ describe('CozyClient logout', () => {
510
513
  })
511
514
 
512
515
  it('should call all reset even if a reset throws an error', async () => {
516
+ client.query = jest.fn().mockResolvedValueOnce({
517
+ data: { attributes: { file_versioning: true, flat_subdomains: true } }
518
+ })
513
519
  links[0].reset = jest.fn().mockRejectedValue(new Error('Async error'))
514
520
  links[2].reset = jest.fn()
515
521
  await client.login()
@@ -519,6 +525,9 @@ describe('CozyClient logout', () => {
519
525
  })
520
526
 
521
527
  it('should emit events', async () => {
528
+ client.query = jest.fn().mockResolvedValueOnce({
529
+ data: { attributes: { file_versioning: true, flat_subdomains: true } }
530
+ })
522
531
  const originalLogout = client.logout
523
532
  links[0].reset = jest.fn()
524
533
  links[2].reset = jest.fn()
@@ -537,6 +546,9 @@ describe('CozyClient logout', () => {
537
546
  })
538
547
 
539
548
  it('should unregister an oauth client', async () => {
549
+ client.query = jest.fn().mockResolvedValueOnce({
550
+ data: { attributes: { file_versioning: true, flat_subdomains: true } }
551
+ })
540
552
  await client.login()
541
553
  stackClient.isRegistered.mockReturnValue(true)
542
554
 
@@ -321,6 +321,11 @@
321
321
  "purpose": "invoice",
322
322
  "sourceCategory": "water"
323
323
  },
324
+ {
325
+ "label": "energy_contract",
326
+ "purpose": "contract",
327
+ "sourceCategory": "energy"
328
+ },
324
329
  {
325
330
  "label": "appliance_invoice",
326
331
  "purpose": "invoice",
@@ -15,6 +15,8 @@ var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/sli
15
15
 
16
16
  var _react = require("react");
17
17
 
18
+ var _get = _interopRequireDefault(require("lodash/get"));
19
+
18
20
  var _dsl = require("../queries/dsl");
19
21
 
20
22
  var useCapabilities = function useCapabilities(client) {
@@ -43,7 +45,7 @@ var useCapabilities = function useCapabilities(client) {
43
45
 
44
46
  case 4:
45
47
  capabilitiesResult = _context.sent;
46
- setCapabilities(capabilitiesResult);
48
+ setCapabilities((0, _get.default)(capabilitiesResult, 'data.attributes', {}));
47
49
  setFetchStatus('loaded');
48
50
  _context.next = 12;
49
51
  break;
@@ -66,7 +68,12 @@ var useCapabilities = function useCapabilities(client) {
66
68
  };
67
69
  }();
68
70
 
69
- fetchData();
71
+ if (client.capabilities) {
72
+ setCapabilities(client.capabilities);
73
+ setFetchStatus('loaded');
74
+ } else {
75
+ fetchData();
76
+ }
70
77
  }, [client]);
71
78
  return {
72
79
  capabilities: capabilities,
@@ -3,6 +3,10 @@ import useCapabilities from './useCapabilities'
3
3
  describe('useCapabilities', () => {
4
4
  const mockClient = { query: jest.fn(), get: jest.fn() }
5
5
 
6
+ beforeEach(() => {
7
+ jest.clearAllMocks()
8
+ })
9
+
6
10
  it('should change loading status', async () => {
7
11
  mockClient.query.mockResolvedValue({ data: [] })
8
12
  const { result, waitForNextUpdate } = renderHook(() =>
@@ -40,13 +44,22 @@ describe('useCapabilities', () => {
40
44
 
41
45
  await waitForNextUpdate()
42
46
  expect(result.current.capabilities).toEqual({
43
- data: {
44
- type: 'io.cozy.settings',
45
- id: 'io.cozy.settings.capabilities',
46
- attributes: { file_versioning: true, flat_subdomains: true },
47
- meta: {},
48
- links: { self: '/settings/capabilities' }
49
- }
47
+ file_versioning: true,
48
+ flat_subdomains: true
49
+ })
50
+ })
51
+
52
+ it(`should return client's capabilities if defined`, async () => {
53
+ mockClient.capabilities = {
54
+ file_versioning: false,
55
+ flat_subdomains: false
56
+ }
57
+ const { result } = renderHook(() => useCapabilities(mockClient))
58
+
59
+ expect(result.current.capabilities).toEqual({
60
+ file_versioning: false,
61
+ flat_subdomains: false
50
62
  })
63
+ expect(mockClient.query).not.toHaveBeenCalled()
51
64
  })
52
65
  })
@@ -43,6 +43,7 @@
43
43
  "other_family_document": "Other family document",
44
44
  "diploma": "Diploma",
45
45
  "work_contract": "Work contract",
46
+ "energy_contract": "Energy provider contract",
46
47
  "pay_sheet": "Pay sheet",
47
48
  "unemployment_benefit": "Unemployment benefit",
48
49
  "pension": "Pension",
@@ -43,6 +43,7 @@
43
43
  "other_family_document": "Autre document de famille",
44
44
  "diploma": "Diplôme",
45
45
  "work_contract": "Contrat de travail",
46
+ "energy_contract": "Contrat de fournisseur d'énergies",
46
47
  "pay_sheet": "Fiche de paie",
47
48
  "unemployment_benefit": "Allocations chômage",
48
49
  "pension": "Retraite",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cozy-client",
3
- "version": "32.7.4",
3
+ "version": "33.0.0",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.node.js",
6
6
  "browser": "dist/index.js",
@@ -18,7 +18,7 @@
18
18
  "@types/jest": "^26.0.20",
19
19
  "@types/lodash": "^4.14.170",
20
20
  "btoa": "^1.2.1",
21
- "cozy-stack-client": "^32.7.3",
21
+ "cozy-stack-client": "^33.0.0",
22
22
  "json-stable-stringify": "^1.0.1",
23
23
  "lodash": "^4.17.13",
24
24
  "microee": "^0.0.6",
@@ -73,5 +73,5 @@
73
73
  "react-native-ios11-devicecheck": "https://github.com/cozy/react-native-devicecheck#app-attest-v0.1"
74
74
  },
75
75
  "sideEffects": false,
76
- "gitHead": "c98083b269380482d69615bb8c3fd59aeb3b1085"
76
+ "gitHead": "df8ccd2d574926f6ea9a476f0ae607429fe2afb8"
77
77
  }
@@ -684,6 +684,14 @@ declare class CozyClient {
684
684
  * @returns {void}
685
685
  */
686
686
  loadInstanceOptionsFromDOM(selector?: string): void;
687
+ /**
688
+ * loadInstanceOptionsFromStack - Loads the instance options from cozy-stack and exposes it through getInstanceOptions
689
+ *
690
+ * For now only retrieving capabilities is supported
691
+ *
692
+ * @returns {Promise<void>}
693
+ */
694
+ loadInstanceOptionsFromStack(): Promise<void>;
687
695
  /**
688
696
  * Directly set the data in the store, without using a query
689
697
  * This is useful for cases like Pouch replication, which wants to