@panneau/data 1.0.0-alpha.15 → 1.0.0-alpha.170

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.
Files changed (3) hide show
  1. package/es/index.js +63 -15
  2. package/lib/index.js +72 -24
  3. package/package.json +3 -3
package/es/index.js CHANGED
@@ -3,8 +3,8 @@ import React, { useState, useCallback, useEffect, useRef, useMemo, useContext }
3
3
  import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
4
4
  import _objectSpread from '@babel/runtime/helpers/objectSpread2';
5
5
  import _objectWithoutProperties from '@babel/runtime/helpers/objectWithoutProperties';
6
- import PropTypes from 'prop-types';
7
6
  import { useUrlGenerator } from '@panneau/core/contexts';
7
+ import PropTypes from 'prop-types';
8
8
  import _classCallCheck from '@babel/runtime/helpers/classCallCheck';
9
9
  import _assertThisInitialized from '@babel/runtime/helpers/assertThisInitialized';
10
10
  import _inherits from '@babel/runtime/helpers/inherits';
@@ -94,15 +94,17 @@ var useItems = function useItems(_ref) {
94
94
  initialPages = _ref$pages === void 0 ? null : _ref$pages,
95
95
  _ref$getPageFromRespo = _ref.getPageFromResponse,
96
96
  getPageFromResponse = _ref$getPageFromRespo === void 0 ? function (_ref2) {
97
- var _ref2$meta = _ref2.meta,
98
- currentPage = _ref2$meta.current_page,
99
- lastPage = _ref2$meta.last_page,
100
- total = _ref2$meta.total,
97
+ var _ref2$pagination = _ref2.pagination,
98
+ currentPage = _ref2$pagination.page,
99
+ lastPage = _ref2$pagination.last_page,
100
+ total = _ref2$pagination.total,
101
+ perPage = _ref2$pagination.per_page,
101
102
  items = _ref2.data;
102
103
  return {
103
104
  page: parseInt(currentPage, 10),
104
105
  lastPage: parseInt(lastPage, 10),
105
106
  total: parseInt(total, 10),
107
+ count: parseInt(perPage, 10),
106
108
  items: items
107
109
  };
108
110
  } : _ref$getPageFromRespo,
@@ -326,13 +328,16 @@ var Base = /*#__PURE__*/function () {
326
328
  routes: {},
327
329
  generateUrl: null
328
330
  }, opts), {}, {
329
- baseUrl: opts.baseUrl
331
+ baseUrl: opts.baseUrl,
332
+ onUnauthorized: opts.onUnauthorized || null
330
333
  });
331
334
  }
332
335
 
333
336
  _createClass(Base, [{
334
337
  key: "requestGet",
335
338
  value: function requestGet(path) {
339
+ var _this = this;
340
+
336
341
  var query = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
337
342
  var queryString = query !== null ? stringify(query, {
338
343
  arrayFormat: 'bracket'
@@ -340,41 +345,59 @@ var Base = /*#__PURE__*/function () {
340
345
  return getJSON("".concat(this.getFullUrl(path)).concat(queryString !== null && queryString.length > 0 ? "?".concat(queryString) : ''), {
341
346
  credentials: 'include',
342
347
  headers: getCSRFHeaders()
348
+ })["catch"](function (error) {
349
+ return _this.onError(error);
343
350
  });
344
351
  }
345
352
  }, {
346
353
  key: "requestPost",
347
354
  value: function requestPost(path, data) {
355
+ var _this2 = this;
356
+
348
357
  return postJSON(this.getFullUrl(path), data, {
349
358
  credentials: 'include',
350
359
  headers: getCSRFHeaders()
360
+ })["catch"](function (error) {
361
+ return _this2.onError(error);
351
362
  });
352
363
  }
353
364
  }, {
354
365
  key: "requestPut",
355
366
  value: function requestPut(path, data) {
367
+ var _this3 = this;
368
+
356
369
  return postJSON(this.getFullUrl(path), _objectSpread({
357
370
  _method: 'PUT'
358
371
  }, data), {
359
372
  credentials: 'include',
360
373
  headers: getCSRFHeaders()
374
+ })["catch"](function (error) {
375
+ return _this3.onError(error);
361
376
  });
362
377
  }
363
378
  }, {
364
379
  key: "requestPatch",
365
380
  value: function requestPatch(path, data) {
381
+ var _this4 = this;
382
+
366
383
  return postJSON(this.getFullUrl(path), _objectSpread({
367
384
  _method: 'PATCH'
368
385
  }, data), {
369
386
  credentials: 'include',
370
387
  headers: getCSRFHeaders()
388
+ })["catch"](function (error) {
389
+ return _this4.onError(error);
371
390
  });
372
391
  }
373
392
  }, {
374
393
  key: "requestDelete",
375
394
  value: function requestDelete(path) {
395
+ var _this5 = this;
396
+
376
397
  return this.requestPost(path, {
377
398
  _method: 'DELETE'
399
+ })["catch"](function (error) {
400
+ return _this5.onError(error);
378
401
  });
379
402
  }
380
403
  }, {
@@ -398,6 +421,23 @@ var Base = /*#__PURE__*/function () {
398
421
  baseUrl = _this$options$baseUrl === void 0 ? null : _this$options$baseUrl;
399
422
  return baseUrl !== null ? "".concat(baseUrl.replace(/\/$/, ''), "/").concat(path.replace(/^\//, '')) : path;
400
423
  }
424
+ }, {
425
+ key: "onError",
426
+ value: function onError(err) {
427
+ var onUnauthorized = this.options.onUnauthorized;
428
+
429
+ var _ref = err || {},
430
+ _ref$status = _ref.status,
431
+ status = _ref$status === void 0 ? null : _ref$status;
432
+
433
+ var statusCode = parseInt(status, 10); // If status is refused and callback exists
434
+
435
+ if ((statusCode === 401 || statusCode === 419) && onUnauthorized !== null) {
436
+ return onUnauthorized();
437
+ }
438
+
439
+ throw err;
440
+ }
401
441
  }]);
402
442
 
403
443
  return Base;
@@ -446,10 +486,13 @@ var AuthApi = /*#__PURE__*/function () {
446
486
  }, {
447
487
  key: "logout",
448
488
  value: function logout() {
449
- var withCredentials = this.options.withCredentials;
450
- return this.api.fetchPost(this.api.route('auth.logout'), null, {
451
- withSession: !withCredentials,
452
- withCredentials: withCredentials
489
+ var url = this.api.getFullUrl(this.api.route('auth.logout'));
490
+ return fetch(url, {
491
+ method: 'POST',
492
+ headers: _objectSpread({}, getCSRFHeaders()),
493
+ credentials: 'include'
494
+ })["catch"](function (err) {
495
+ console.log('logout err', err); // eslint-disable-line
453
496
  });
454
497
  }
455
498
  }, {
@@ -727,22 +770,26 @@ var useApi = function useApi() {
727
770
  var propTypes$1 = {
728
771
  api: PropTypes.instanceOf(Api),
729
772
  baseUrl: PropTypes.string,
773
+ onUnauthorized: PropTypes.func,
730
774
  children: PropTypes.node.isRequired
731
775
  };
732
776
  var defaultProps$1 = {
733
777
  api: null,
778
+ onUnauthorized: null,
734
779
  baseUrl: undefined
735
780
  };
736
781
  var ApiProvider = function ApiProvider(_ref) {
737
782
  var initialApi = _ref.api,
738
783
  baseUrl = _ref.baseUrl,
784
+ onUnauthorized = _ref.onUnauthorized,
739
785
  children = _ref.children;
740
786
  var generateUrl = useUrlGenerator();
741
787
  var previousApi = useApi();
742
788
  var api = useMemo(function () {
743
789
  return initialApi || previousApi || new Api({
744
790
  baseUrl: baseUrl,
745
- generateUrl: generateUrl
791
+ generateUrl: generateUrl,
792
+ onUnauthorized: onUnauthorized
746
793
  });
747
794
  }, [previousApi, initialApi, baseUrl]);
748
795
  return /*#__PURE__*/React.createElement(ApiContext.Provider, {
@@ -856,8 +903,9 @@ var useMediaTags = function useMediaTags() {
856
903
  var _useItems = useItems(_objectSpread({
857
904
  getItems: getItems
858
905
  }, opts)),
859
- items = _useItems.items,
860
- request = _objectWithoutProperties(_useItems, ["items", "pageItems"]);
906
+ items = _useItems.items;
907
+ _useItems.pageItems;
908
+ var request = _objectWithoutProperties(_useItems, ["items", "pageItems"]);
861
909
 
862
910
  return _objectSpread({
863
911
  tags: items
@@ -884,7 +932,7 @@ var useMediaUpdate = function useMediaUpdate() {
884
932
  };
885
933
  };
886
934
 
887
- var useResourceUpdate$1 = function useResourceUpdate(resource) {
935
+ var useResourceDestroy = function useResourceDestroy(resource) {
888
936
  var id = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
889
937
 
890
938
  var _useState = useState(false),
@@ -1021,4 +1069,4 @@ var DataProvider = function DataProvider(_ref) {
1021
1069
  DataProvider.propTypes = propTypes;
1022
1070
  DataProvider.defaultProps = defaultProps;
1023
1071
 
1024
- export { Api, ApiProvider, Base as BaseApi, DataProvider, useApi, useData, useItems, useMedia, useMediaCreate, useMediaTags, useMediaUpdate, useMedias, useMediasRecent as useMediasRecentSearches, useResourceUpdate$1 as useResourceDestroy, useResourceItem, useResourceItems, useResourceCreate as useResourceStore, useResourceUpdate };
1072
+ export { Api, ApiProvider, Base as BaseApi, DataProvider, useApi, useData, useItems, useMedia, useMediaCreate, useMediaTags, useMediaUpdate, useMedias, useMediasRecent as useMediasRecentSearches, useResourceDestroy, useResourceItem, useResourceItems, useResourceCreate as useResourceStore, useResourceUpdate };
package/lib/index.js CHANGED
@@ -7,14 +7,14 @@ var React = require('react');
7
7
  var _toConsumableArray = require('@babel/runtime/helpers/toConsumableArray');
8
8
  var _objectSpread = require('@babel/runtime/helpers/objectSpread2');
9
9
  var _objectWithoutProperties = require('@babel/runtime/helpers/objectWithoutProperties');
10
- var PropTypes = require('prop-types');
11
10
  var contexts = require('@panneau/core/contexts');
11
+ var PropTypes = require('prop-types');
12
12
  var _classCallCheck = require('@babel/runtime/helpers/classCallCheck');
13
13
  var _assertThisInitialized = require('@babel/runtime/helpers/assertThisInitialized');
14
14
  var _inherits = require('@babel/runtime/helpers/inherits');
15
15
  var _createSuper = require('@babel/runtime/helpers/createSuper');
16
16
  var _createClass = require('@babel/runtime/helpers/createClass');
17
- var fetch = require('@folklore/fetch');
17
+ var fetch$1 = require('@folklore/fetch');
18
18
  var reactRouter = require('react-router');
19
19
  var queryString = require('query-string');
20
20
 
@@ -112,15 +112,17 @@ var useItems = function useItems(_ref) {
112
112
  initialPages = _ref$pages === void 0 ? null : _ref$pages,
113
113
  _ref$getPageFromRespo = _ref.getPageFromResponse,
114
114
  getPageFromResponse = _ref$getPageFromRespo === void 0 ? function (_ref2) {
115
- var _ref2$meta = _ref2.meta,
116
- currentPage = _ref2$meta.current_page,
117
- lastPage = _ref2$meta.last_page,
118
- total = _ref2$meta.total,
115
+ var _ref2$pagination = _ref2.pagination,
116
+ currentPage = _ref2$pagination.page,
117
+ lastPage = _ref2$pagination.last_page,
118
+ total = _ref2$pagination.total,
119
+ perPage = _ref2$pagination.per_page,
119
120
  items = _ref2.data;
120
121
  return {
121
122
  page: parseInt(currentPage, 10),
122
123
  lastPage: parseInt(lastPage, 10),
123
124
  total: parseInt(total, 10),
125
+ count: parseInt(perPage, 10),
124
126
  items: items
125
127
  };
126
128
  } : _ref$getPageFromRespo,
@@ -344,55 +346,76 @@ var Base = /*#__PURE__*/function () {
344
346
  routes: {},
345
347
  generateUrl: null
346
348
  }, opts), {}, {
347
- baseUrl: opts.baseUrl
349
+ baseUrl: opts.baseUrl,
350
+ onUnauthorized: opts.onUnauthorized || null
348
351
  });
349
352
  }
350
353
 
351
354
  _createClass__default['default'](Base, [{
352
355
  key: "requestGet",
353
356
  value: function requestGet(path) {
357
+ var _this = this;
358
+
354
359
  var query = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
355
360
  var queryString$1 = query !== null ? queryString.stringify(query, {
356
361
  arrayFormat: 'bracket'
357
362
  }) : null;
358
- return fetch.getJSON("".concat(this.getFullUrl(path)).concat(queryString$1 !== null && queryString$1.length > 0 ? "?".concat(queryString$1) : ''), {
363
+ return fetch$1.getJSON("".concat(this.getFullUrl(path)).concat(queryString$1 !== null && queryString$1.length > 0 ? "?".concat(queryString$1) : ''), {
359
364
  credentials: 'include',
360
- headers: fetch.getCSRFHeaders()
365
+ headers: fetch$1.getCSRFHeaders()
366
+ })["catch"](function (error) {
367
+ return _this.onError(error);
361
368
  });
362
369
  }
363
370
  }, {
364
371
  key: "requestPost",
365
372
  value: function requestPost(path, data) {
366
- return fetch.postJSON(this.getFullUrl(path), data, {
373
+ var _this2 = this;
374
+
375
+ return fetch$1.postJSON(this.getFullUrl(path), data, {
367
376
  credentials: 'include',
368
- headers: fetch.getCSRFHeaders()
377
+ headers: fetch$1.getCSRFHeaders()
378
+ })["catch"](function (error) {
379
+ return _this2.onError(error);
369
380
  });
370
381
  }
371
382
  }, {
372
383
  key: "requestPut",
373
384
  value: function requestPut(path, data) {
374
- return fetch.postJSON(this.getFullUrl(path), _objectSpread__default['default']({
385
+ var _this3 = this;
386
+
387
+ return fetch$1.postJSON(this.getFullUrl(path), _objectSpread__default['default']({
375
388
  _method: 'PUT'
376
389
  }, data), {
377
390
  credentials: 'include',
378
- headers: fetch.getCSRFHeaders()
391
+ headers: fetch$1.getCSRFHeaders()
392
+ })["catch"](function (error) {
393
+ return _this3.onError(error);
379
394
  });
380
395
  }
381
396
  }, {
382
397
  key: "requestPatch",
383
398
  value: function requestPatch(path, data) {
384
- return fetch.postJSON(this.getFullUrl(path), _objectSpread__default['default']({
399
+ var _this4 = this;
400
+
401
+ return fetch$1.postJSON(this.getFullUrl(path), _objectSpread__default['default']({
385
402
  _method: 'PATCH'
386
403
  }, data), {
387
404
  credentials: 'include',
388
- headers: fetch.getCSRFHeaders()
405
+ headers: fetch$1.getCSRFHeaders()
406
+ })["catch"](function (error) {
407
+ return _this4.onError(error);
389
408
  });
390
409
  }
391
410
  }, {
392
411
  key: "requestDelete",
393
412
  value: function requestDelete(path) {
413
+ var _this5 = this;
414
+
394
415
  return this.requestPost(path, {
395
416
  _method: 'DELETE'
417
+ })["catch"](function (error) {
418
+ return _this5.onError(error);
396
419
  });
397
420
  }
398
421
  }, {
@@ -416,6 +439,23 @@ var Base = /*#__PURE__*/function () {
416
439
  baseUrl = _this$options$baseUrl === void 0 ? null : _this$options$baseUrl;
417
440
  return baseUrl !== null ? "".concat(baseUrl.replace(/\/$/, ''), "/").concat(path.replace(/^\//, '')) : path;
418
441
  }
442
+ }, {
443
+ key: "onError",
444
+ value: function onError(err) {
445
+ var onUnauthorized = this.options.onUnauthorized;
446
+
447
+ var _ref = err || {},
448
+ _ref$status = _ref.status,
449
+ status = _ref$status === void 0 ? null : _ref$status;
450
+
451
+ var statusCode = parseInt(status, 10); // If status is refused and callback exists
452
+
453
+ if ((statusCode === 401 || statusCode === 419) && onUnauthorized !== null) {
454
+ return onUnauthorized();
455
+ }
456
+
457
+ throw err;
458
+ }
419
459
  }]);
420
460
 
421
461
  return Base;
@@ -464,10 +504,13 @@ var AuthApi = /*#__PURE__*/function () {
464
504
  }, {
465
505
  key: "logout",
466
506
  value: function logout() {
467
- var withCredentials = this.options.withCredentials;
468
- return this.api.fetchPost(this.api.route('auth.logout'), null, {
469
- withSession: !withCredentials,
470
- withCredentials: withCredentials
507
+ var url = this.api.getFullUrl(this.api.route('auth.logout'));
508
+ return fetch(url, {
509
+ method: 'POST',
510
+ headers: _objectSpread__default['default']({}, fetch$1.getCSRFHeaders()),
511
+ credentials: 'include'
512
+ })["catch"](function (err) {
513
+ console.log('logout err', err); // eslint-disable-line
471
514
  });
472
515
  }
473
516
  }, {
@@ -745,22 +788,26 @@ var useApi = function useApi() {
745
788
  var propTypes$1 = {
746
789
  api: PropTypes__default['default'].instanceOf(Api),
747
790
  baseUrl: PropTypes__default['default'].string,
791
+ onUnauthorized: PropTypes__default['default'].func,
748
792
  children: PropTypes__default['default'].node.isRequired
749
793
  };
750
794
  var defaultProps$1 = {
751
795
  api: null,
796
+ onUnauthorized: null,
752
797
  baseUrl: undefined
753
798
  };
754
799
  var ApiProvider = function ApiProvider(_ref) {
755
800
  var initialApi = _ref.api,
756
801
  baseUrl = _ref.baseUrl,
802
+ onUnauthorized = _ref.onUnauthorized,
757
803
  children = _ref.children;
758
804
  var generateUrl = contexts.useUrlGenerator();
759
805
  var previousApi = useApi();
760
806
  var api = React.useMemo(function () {
761
807
  return initialApi || previousApi || new Api({
762
808
  baseUrl: baseUrl,
763
- generateUrl: generateUrl
809
+ generateUrl: generateUrl,
810
+ onUnauthorized: onUnauthorized
764
811
  });
765
812
  }, [previousApi, initialApi, baseUrl]);
766
813
  return /*#__PURE__*/React__default['default'].createElement(ApiContext.Provider, {
@@ -874,8 +921,9 @@ var useMediaTags = function useMediaTags() {
874
921
  var _useItems = useItems(_objectSpread__default['default']({
875
922
  getItems: getItems
876
923
  }, opts)),
877
- items = _useItems.items,
878
- request = _objectWithoutProperties__default['default'](_useItems, ["items", "pageItems"]);
924
+ items = _useItems.items;
925
+ _useItems.pageItems;
926
+ var request = _objectWithoutProperties__default['default'](_useItems, ["items", "pageItems"]);
879
927
 
880
928
  return _objectSpread__default['default']({
881
929
  tags: items
@@ -902,7 +950,7 @@ var useMediaUpdate = function useMediaUpdate() {
902
950
  };
903
951
  };
904
952
 
905
- var useResourceUpdate$1 = function useResourceUpdate(resource) {
953
+ var useResourceDestroy = function useResourceDestroy(resource) {
906
954
  var id = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
907
955
 
908
956
  var _useState = React.useState(false),
@@ -1052,7 +1100,7 @@ exports.useMediaTags = useMediaTags;
1052
1100
  exports.useMediaUpdate = useMediaUpdate;
1053
1101
  exports.useMedias = useMedias;
1054
1102
  exports.useMediasRecentSearches = useMediasRecent;
1055
- exports.useResourceDestroy = useResourceUpdate$1;
1103
+ exports.useResourceDestroy = useResourceDestroy;
1056
1104
  exports.useResourceItem = useResourceItem;
1057
1105
  exports.useResourceItems = useResourceItems;
1058
1106
  exports.useResourceStore = useResourceCreate;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@panneau/data",
3
- "version": "1.0.0-alpha.15",
3
+ "version": "1.0.0-alpha.170",
4
4
  "description": "",
5
5
  "keywords": [
6
6
  "javascript"
@@ -54,7 +54,7 @@
54
54
  "dependencies": {
55
55
  "@babel/runtime": "^7.12.5",
56
56
  "@folklore/fetch": "^0.1.10",
57
- "@panneau/core": "^1.0.0-alpha.14",
57
+ "@panneau/core": "^1.0.0-alpha.170",
58
58
  "prop-types": "^15.7.2",
59
59
  "query-string": "^7.0.0",
60
60
  "react-router": "^5.2.0"
@@ -62,5 +62,5 @@
62
62
  "publishConfig": {
63
63
  "access": "public"
64
64
  },
65
- "gitHead": "63c2685d7c5763c3296355b13fb56c81bb8d8aca"
65
+ "gitHead": "bba760fc2b560b9a0467b6e22d9bd8ca8618abb2"
66
66
  }