cozy-client 38.11.1 → 39.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.
@@ -2677,7 +2677,7 @@ var CozyClient = /*#__PURE__*/function () {
2677
2677
  CozyClient.hooks = CozyClient.hooks || {};
2678
2678
  CozyClient.fetchPolicies = _policies.default; //COZY_CLIENT_VERSION_PACKAGE in replaced by babel. See babel config
2679
2679
 
2680
- CozyClient.version = "38.11.1";
2680
+ CozyClient.version = "39.0.0";
2681
2681
 
2682
2682
  _microee.default.mixin(CozyClient);
2683
2683
 
@@ -51,6 +51,12 @@ Object.defineProperty(exports, "InvalidProtocolError", {
51
51
  return _urlHelper.InvalidProtocolError;
52
52
  }
53
53
  });
54
+ Object.defineProperty(exports, "BlockedCozyError", {
55
+ enumerable: true,
56
+ get: function get() {
57
+ return _urlHelper.BlockedCozyError;
58
+ }
59
+ });
54
60
  Object.defineProperty(exports, "dehydrate", {
55
61
  enumerable: true,
56
62
  get: function get() {
@@ -5,7 +5,7 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefau
5
5
  Object.defineProperty(exports, "__esModule", {
6
6
  value: true
7
7
  });
8
- exports.rootCozyUrl = exports.InvalidCozyUrlError = exports.InvalidProtocolError = exports.InvalidRedirectLinkError = exports.deconstructRedirectLink = exports.deconstructCozyWebLinkWithSlug = exports.generateWebLink = exports.ensureFirstSlash = void 0;
8
+ exports.rootCozyUrl = exports.InvalidCozyUrlError = exports.BlockedCozyError = exports.InvalidProtocolError = exports.InvalidRedirectLinkError = exports.deconstructRedirectLink = exports.deconstructCozyWebLinkWithSlug = exports.generateWebLink = exports.ensureFirstSlash = void 0;
9
9
 
10
10
  var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
11
11
 
@@ -206,20 +206,39 @@ var InvalidProtocolError = /*#__PURE__*/function (_Error2) {
206
206
 
207
207
  exports.InvalidProtocolError = InvalidProtocolError;
208
208
 
209
- var InvalidCozyUrlError = /*#__PURE__*/function (_Error3) {
210
- (0, _inherits2.default)(InvalidCozyUrlError, _Error3);
209
+ var BlockedCozyError = /*#__PURE__*/function (_Error3) {
210
+ (0, _inherits2.default)(BlockedCozyError, _Error3);
211
211
 
212
- var _super3 = _createSuper(InvalidCozyUrlError);
212
+ var _super3 = _createSuper(BlockedCozyError);
213
213
 
214
- function InvalidCozyUrlError(url) {
214
+ function BlockedCozyError(url) {
215
215
  var _this3;
216
216
 
217
- (0, _classCallCheck2.default)(this, InvalidCozyUrlError);
218
- _this3 = _super3.call(this, "URL ".concat(url.toString(), " does not seem to be a valid Cozy URL"));
217
+ (0, _classCallCheck2.default)(this, BlockedCozyError);
218
+ _this3 = _super3.call(this, "Blocked cozy ".concat(url.toString()));
219
219
  _this3.url = url;
220
220
  return _this3;
221
221
  }
222
222
 
223
+ return BlockedCozyError;
224
+ }( /*#__PURE__*/(0, _wrapNativeSuper2.default)(Error));
225
+
226
+ exports.BlockedCozyError = BlockedCozyError;
227
+
228
+ var InvalidCozyUrlError = /*#__PURE__*/function (_Error4) {
229
+ (0, _inherits2.default)(InvalidCozyUrlError, _Error4);
230
+
231
+ var _super4 = _createSuper(InvalidCozyUrlError);
232
+
233
+ function InvalidCozyUrlError(url) {
234
+ var _this4;
235
+
236
+ (0, _classCallCheck2.default)(this, InvalidCozyUrlError);
237
+ _this4 = _super4.call(this, "URL ".concat(url.toString(), " does not seem to be a valid Cozy URL"));
238
+ _this4.url = url;
239
+ return _this4;
240
+ }
241
+
223
242
  return InvalidCozyUrlError;
224
243
  }( /*#__PURE__*/(0, _wrapNativeSuper2.default)(Error));
225
244
  /* uri - Returns a well formed URL origin from a protocol, a hostname and a port
@@ -270,7 +289,12 @@ var wellKnownUrl = function wellKnownUrl(url) {
270
289
  * page accessible from the given origin so we suppose it's a valid Cozy
271
290
  * origin (i.e. it could be another site altogether though)
272
291
  * - a 401 response status means the pointed page requires authentication so the
273
- * origin is probably pointing to a Cozy app
292
+ * origin is probably pointing to a cozy-app. In that case we should consider this
293
+ * URL to be invalid
294
+ * - a 503 response status with a "Blocked" reason means the pointed page is a Cozy
295
+ * but it is blocked. In that case we consider that the url is a valid Cozy origin
296
+ * but we want the method to throw as we cannot verify if the URL points to the
297
+ * Cozy's root or to a specifc slug. The caller is responsible to handle that exception
274
298
  * - another status means there aren't any Cozy behind to the given origin
275
299
  *
276
300
  * @param {object} url Object of URL elements
@@ -280,13 +304,13 @@ var wellKnownUrl = function wellKnownUrl(url) {
280
304
  *
281
305
  * @returns {Promise<boolean>} True if we believe there's a Cozy behind the given origin
282
306
  * @throws {InvalidCozyUrlError} Thrown when we know for sure there aren't any Cozy behind the given origin
307
+ * @throws {BlockedCozyError} Thrown when we know for sure there is Cozy behind the given origin but it is in a "Blocked" state
283
308
  */
284
309
 
285
310
 
286
311
  var isValidOrigin = /*#__PURE__*/function () {
287
312
  var _ref3 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(url) {
288
- var _yield$fetch, status;
289
-
313
+ var response, status;
290
314
  return _regenerator.default.wrap(function _callee$(_context) {
291
315
  while (1) {
292
316
  switch (_context.prev = _context.next) {
@@ -295,8 +319,8 @@ var isValidOrigin = /*#__PURE__*/function () {
295
319
  return fetch(wellKnownUrl(url));
296
320
 
297
321
  case 2:
298
- _yield$fetch = _context.sent;
299
- status = _yield$fetch.status;
322
+ response = _context.sent;
323
+ status = response.status;
300
324
 
301
325
  if (!(status === 404)) {
302
326
  _context.next = 6;
@@ -306,9 +330,21 @@ var isValidOrigin = /*#__PURE__*/function () {
306
330
  throw new InvalidCozyUrlError(url);
307
331
 
308
332
  case 6:
333
+ _context.next = 8;
334
+ return isResponseAboutBlockedCozy(response);
335
+
336
+ case 8:
337
+ if (!_context.sent) {
338
+ _context.next = 10;
339
+ break;
340
+ }
341
+
342
+ throw new BlockedCozyError(url);
343
+
344
+ case 10:
309
345
  return _context.abrupt("return", status === 200);
310
346
 
311
- case 7:
347
+ case 11:
312
348
  case "end":
313
349
  return _context.stop();
314
350
  }
@@ -455,5 +491,62 @@ var rootCozyUrl = /*#__PURE__*/function () {
455
491
  return _ref4.apply(this, arguments);
456
492
  };
457
493
  }();
494
+ /**
495
+ * Check if the given response is about a Cozy being blocked
496
+ *
497
+ * @param {Response} response - Fetch API response
498
+ * @returns {Promise<boolean>} true if the response is about a Cozy being blocked, false otherwize
499
+ */
500
+
458
501
 
459
- exports.rootCozyUrl = rootCozyUrl;
502
+ exports.rootCozyUrl = rootCozyUrl;
503
+
504
+ var isResponseAboutBlockedCozy = /*#__PURE__*/function () {
505
+ var _ref5 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee3(response) {
506
+ var _data$some;
507
+
508
+ var contentType, isJson, data;
509
+ return _regenerator.default.wrap(function _callee3$(_context3) {
510
+ while (1) {
511
+ switch (_context3.prev = _context3.next) {
512
+ case 0:
513
+ if (!(response.status !== 503)) {
514
+ _context3.next = 2;
515
+ break;
516
+ }
517
+
518
+ return _context3.abrupt("return", false);
519
+
520
+ case 2:
521
+ contentType = response.headers.get('content-type');
522
+ isJson = contentType && contentType.indexOf('json') >= 0;
523
+ _context3.next = 6;
524
+ return isJson ? response.json() : response.text();
525
+
526
+ case 6:
527
+ data = _context3.sent;
528
+
529
+ if (!(data !== null && data !== void 0 && (_data$some = data.some) !== null && _data$some !== void 0 && _data$some.call(data, function (reason) {
530
+ return reason.title === 'Blocked';
531
+ }))) {
532
+ _context3.next = 9;
533
+ break;
534
+ }
535
+
536
+ return _context3.abrupt("return", true);
537
+
538
+ case 9:
539
+ return _context3.abrupt("return", false);
540
+
541
+ case 10:
542
+ case "end":
543
+ return _context3.stop();
544
+ }
545
+ }
546
+ }, _callee3);
547
+ }));
548
+
549
+ return function isResponseAboutBlockedCozy(_x3) {
550
+ return _ref5.apply(this, arguments);
551
+ };
552
+ }();
@@ -7,7 +7,8 @@ import {
7
7
  rootCozyUrl,
8
8
  InvalidRedirectLinkError,
9
9
  InvalidCozyUrlError,
10
- InvalidProtocolError
10
+ InvalidProtocolError,
11
+ BlockedCozyError
11
12
  } from './urlHelper'
12
13
 
13
14
  describe('generateWebLink', () => {
@@ -420,4 +421,60 @@ describe('rootCozyUrl', () => {
420
421
  rootCozyUrl(new URL('https://missing.mycozy.cloud'))
421
422
  ).rejects.toBeInstanceOf(InvalidCozyUrlError)
422
423
  })
424
+
425
+ it('should reject if Blocked cozy', async () => {
426
+ fetch.mockResponse(() => {
427
+ return Promise.resolve({
428
+ headers: {
429
+ 'content-type': 'application/json; charset=UTF-8'
430
+ },
431
+ body: JSON.stringify([
432
+ {
433
+ status: '503',
434
+ title: 'Blocked',
435
+ code: 'UNKNOWN',
436
+ detail: 'The Cozy is blocked for an unknown reason',
437
+ source: {}
438
+ }
439
+ ]),
440
+ ok: false,
441
+ status: 503,
442
+ statusText: '',
443
+ type: 'default',
444
+ url: 'https://camillenimbus.com/.well-known/change-password'
445
+ })
446
+ })
447
+
448
+ await expect(
449
+ rootCozyUrl(new URL('https://camillenimbus.com'))
450
+ ).rejects.toBeInstanceOf(BlockedCozyError)
451
+ })
452
+
453
+ it('should reject on 503 error (if not Blocked)', async () => {
454
+ fetch.mockResponse(() => {
455
+ return Promise.resolve({
456
+ headers: {
457
+ 'content-type': 'application/json; charset=UTF-8'
458
+ },
459
+ body: JSON.stringify([
460
+ {
461
+ status: '503',
462
+ title: 'Some error',
463
+ code: 'UNKNOWN',
464
+ detail: 'Some error description',
465
+ source: {}
466
+ }
467
+ ]),
468
+ ok: false,
469
+ status: 503,
470
+ statusText: '',
471
+ type: 'default',
472
+ url: 'https://camillenimbus.com/.well-known/change-password'
473
+ })
474
+ })
475
+
476
+ await expect(
477
+ rootCozyUrl(new URL('https://camillenimbus.com'))
478
+ ).rejects.toBeInstanceOf(InvalidCozyUrlError)
479
+ })
423
480
  })
package/dist/index.js CHANGED
@@ -35,6 +35,7 @@ var _exportNames = {
35
35
  InvalidRedirectLinkError: true,
36
36
  InvalidCozyUrlError: true,
37
37
  InvalidProtocolError: true,
38
+ BlockedCozyError: true,
38
39
  cancelable: true,
39
40
  isQueryLoading: true,
40
41
  hasQueryBeenLoaded: true,
@@ -221,6 +222,12 @@ Object.defineProperty(exports, "InvalidProtocolError", {
221
222
  return _helpers2.InvalidProtocolError;
222
223
  }
223
224
  });
225
+ Object.defineProperty(exports, "BlockedCozyError", {
226
+ enumerable: true,
227
+ get: function get() {
228
+ return _helpers2.BlockedCozyError;
229
+ }
230
+ });
224
231
  Object.defineProperty(exports, "cancelable", {
225
232
  enumerable: true,
226
233
  get: function get() {
@@ -30,6 +30,7 @@ var _exportNames = {
30
30
  rootCozyUrl: true,
31
31
  InvalidCozyUrlError: true,
32
32
  InvalidProtocolError: true,
33
+ BlockedCozyError: true,
33
34
  cancelable: true,
34
35
  getQueryFromState: true,
35
36
  Registry: true,
@@ -176,6 +177,12 @@ Object.defineProperty(exports, "InvalidProtocolError", {
176
177
  return _helpers.InvalidProtocolError;
177
178
  }
178
179
  });
180
+ Object.defineProperty(exports, "BlockedCozyError", {
181
+ enumerable: true,
182
+ get: function get() {
183
+ return _helpers.BlockedCozyError;
184
+ }
185
+ });
179
186
  Object.defineProperty(exports, "cancelable", {
180
187
  enumerable: true,
181
188
  get: function get() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cozy-client",
3
- "version": "38.11.1",
3
+ "version": "39.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": "^38.9.2",
21
+ "cozy-stack-client": "^39.0.0",
22
22
  "date-fns": "2.29.3",
23
23
  "json-stable-stringify": "^1.0.1",
24
24
  "lodash": "^4.17.13",
@@ -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": "fb1e1e8e207406a8c88193eab81d4a3120c0cc30"
76
+ "gitHead": "4b431bf2b7ee4fd4c55ebc0228f39872596e8c2b"
77
77
  }
@@ -1,2 +1,2 @@
1
1
  export { dehydrate } from "./dehydrateHelper";
2
- export { deconstructCozyWebLinkWithSlug, deconstructRedirectLink, generateWebLink, ensureFirstSlash, rootCozyUrl, InvalidRedirectLinkError, InvalidCozyUrlError, InvalidProtocolError } from "./urlHelper";
2
+ export { deconstructCozyWebLinkWithSlug, deconstructRedirectLink, generateWebLink, ensureFirstSlash, rootCozyUrl, InvalidRedirectLinkError, InvalidCozyUrlError, InvalidProtocolError, BlockedCozyError } from "./urlHelper";
@@ -17,6 +17,10 @@ export class InvalidProtocolError extends Error {
17
17
  constructor(url: any);
18
18
  url: any;
19
19
  }
20
+ export class BlockedCozyError extends Error {
21
+ constructor(url: any);
22
+ url: any;
23
+ }
20
24
  export class InvalidCozyUrlError extends Error {
21
25
  constructor(url: any);
22
26
  url: any;
package/types/index.d.ts CHANGED
@@ -19,6 +19,6 @@ export { manifest, models };
19
19
  export { QueryDefinition, Q, Mutations, MutationTypes, getDoctypeFromOperation } from "./queries/dsl";
20
20
  export { Association, HasMany, HasOne, HasOneInPlace, HasManyInPlace, HasManyTriggers } from "./associations";
21
21
  export { isReferencedBy, isReferencedById, getReferencedBy, getReferencedById } from "./associations/helpers";
22
- export { deconstructCozyWebLinkWithSlug, deconstructRedirectLink, dehydrate, generateWebLink, ensureFirstSlash, rootCozyUrl, InvalidRedirectLinkError, InvalidCozyUrlError, InvalidProtocolError } from "./helpers";
22
+ export { deconstructCozyWebLinkWithSlug, deconstructRedirectLink, dehydrate, generateWebLink, ensureFirstSlash, rootCozyUrl, InvalidRedirectLinkError, InvalidCozyUrlError, InvalidProtocolError, BlockedCozyError } from "./helpers";
23
23
  export { cancelable, isQueryLoading, hasQueryBeenLoaded } from "./utils";
24
24
  export { queryConnect, queryConnectFlat, withClient } from "./hoc";
@@ -14,4 +14,4 @@ import * as models from "./models";
14
14
  export { manifest, models };
15
15
  export { QueryDefinition, Mutations, MutationTypes, getDoctypeFromOperation, Q } from "./queries/dsl";
16
16
  export { Association, HasMany, HasOne, HasOneInPlace, HasManyInPlace, HasManyTriggers } from "./associations";
17
- export { deconstructCozyWebLinkWithSlug, deconstructRedirectLink, dehydrate, generateWebLink, ensureFirstSlash, rootCozyUrl, InvalidCozyUrlError, InvalidProtocolError } from "./helpers";
17
+ export { deconstructCozyWebLinkWithSlug, deconstructRedirectLink, dehydrate, generateWebLink, ensureFirstSlash, rootCozyUrl, InvalidCozyUrlError, InvalidProtocolError, BlockedCozyError } from "./helpers";