@panneau/core 1.0.1-alpha.0 → 1.0.3-alpha.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.
Files changed (80) hide show
  1. package/contexts.js +1 -1
  2. package/es/contexts.js +1740 -0
  3. package/es/hooks.js +1116 -0
  4. package/es/index.js +991 -9
  5. package/es/utils.js +329 -1
  6. package/hooks.js +1 -0
  7. package/lib/contexts.js +1871 -0
  8. package/lib/hooks.js +1167 -0
  9. package/lib/index.js +1003 -44
  10. package/lib/utils.js +371 -12
  11. package/package.json +79 -49
  12. package/utils.js +1 -1
  13. package/components.js +0 -1
  14. package/es/components/Button.js +0 -157
  15. package/es/components/ButtonGroup.js +0 -87
  16. package/es/components/Errors.js +0 -33
  17. package/es/components/Label.js +0 -32
  18. package/es/components/Loading.js +0 -56
  19. package/es/components/index.js +0 -6
  20. package/es/contexts/Components.js +0 -52
  21. package/es/contexts/Definition.js +0 -21
  22. package/es/contexts/UrlGenerator.js +0 -33
  23. package/es/contexts/index.js +0 -3
  24. package/es/lib/ComponentsCollection.js +0 -107
  25. package/es/lib/Definition.js +0 -134
  26. package/es/lib/PropTypes.js +0 -75
  27. package/es/lib/Resource.js +0 -93
  28. package/es/lib/ResourceApi.js +0 -139
  29. package/es/lib/UrlGenerator.js +0 -66
  30. package/es/lib/requests.js +0 -165
  31. package/es/lib/useResourceApi.js +0 -19
  32. package/es/lib/utils.js +0 -19
  33. package/es/requests.js +0 -1
  34. package/lib/components/Button.js +0 -182
  35. package/lib/components/ButtonGroup.js +0 -104
  36. package/lib/components/Errors.js +0 -49
  37. package/lib/components/Label.js +0 -47
  38. package/lib/components/Loading.js +0 -70
  39. package/lib/components/index.js +0 -47
  40. package/lib/contexts/Components.js +0 -78
  41. package/lib/contexts/Definition.js +0 -45
  42. package/lib/contexts/UrlGenerator.js +0 -53
  43. package/lib/contexts/index.js +0 -73
  44. package/lib/lib/ComponentsCollection.js +0 -117
  45. package/lib/lib/Definition.js +0 -145
  46. package/lib/lib/PropTypes.js +0 -152
  47. package/lib/lib/Resource.js +0 -104
  48. package/lib/lib/ResourceApi.js +0 -154
  49. package/lib/lib/UrlGenerator.js +0 -76
  50. package/lib/lib/requests.js +0 -206
  51. package/lib/lib/useResourceApi.js +0 -33
  52. package/lib/lib/utils.js +0 -35
  53. package/lib/requests.js +0 -17
  54. package/requests.js +0 -1
  55. package/src/components/Button.jsx +0 -186
  56. package/src/components/ButtonGroup.jsx +0 -98
  57. package/src/components/Errors.jsx +0 -46
  58. package/src/components/Label.jsx +0 -32
  59. package/src/components/Loading.jsx +0 -51
  60. package/src/components/index.js +0 -7
  61. package/src/contexts/Components.jsx +0 -49
  62. package/src/contexts/Definition.jsx +0 -21
  63. package/src/contexts/UrlGenerator.jsx +0 -35
  64. package/src/contexts/index.js +0 -12
  65. package/src/index.js +0 -8
  66. package/src/lib/ComponentsCollection.js +0 -81
  67. package/src/lib/Definition.js +0 -87
  68. package/src/lib/PropTypes.js +0 -117
  69. package/src/lib/Resource.js +0 -65
  70. package/src/lib/ResourceApi.js +0 -114
  71. package/src/lib/UrlGenerator.js +0 -55
  72. package/src/lib/requests.js +0 -120
  73. package/src/lib/useResourceApi.jsx +0 -20
  74. package/src/lib/utils.js +0 -13
  75. package/src/requests.js +0 -1
  76. package/src/styles/components/button-group.scss +0 -10
  77. package/src/styles/components/errors.scss +0 -5
  78. package/src/styles/components/loading.scss +0 -36
  79. package/src/styles/variables.scss +0 -5
  80. package/src/utils.js +0 -1
@@ -1,134 +0,0 @@
1
- import _defineProperty from "@babel/runtime/helpers/defineProperty";
2
- import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
3
- import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
4
- import _createClass from "@babel/runtime/helpers/createClass";
5
-
6
- 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; }
7
-
8
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
9
-
10
- import Resource from './Resource';
11
- import { getLocalizedName } from './utils';
12
-
13
- var Definition =
14
- /*#__PURE__*/
15
- function () {
16
- function Definition() {
17
- var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
18
-
19
- var _ref$resources = _ref.resources,
20
- resources = _ref$resources === void 0 ? [] : _ref$resources,
21
- definition = _objectWithoutProperties(_ref, ["resources"]);
22
-
23
- _classCallCheck(this, Definition);
24
-
25
- this.definition = _objectSpread({}, definition, {
26
- resources: resources.map(function (it) {
27
- return new Resource(it);
28
- })
29
- });
30
- }
31
-
32
- _createClass(Definition, [{
33
- key: "allRoutes",
34
- value: function allRoutes() {
35
- var _this$definition$rout = this.definition.routes,
36
- routes = _this$definition$rout === void 0 ? {} : _this$definition$rout;
37
- var resourcesRoutes = this.resources().filter(function (it) {
38
- return it.hasRoutes();
39
- }).reduce(function (routesMap, resource) {
40
- var resourceRoutes = resource.routes();
41
- return _objectSpread({}, routesMap, {}, Object.keys(resourceRoutes).reduce(function (mapRoutes, name) {
42
- return _objectSpread({}, mapRoutes, _defineProperty({}, "resource.".concat(resource.id(), ".").concat(name), resourceRoutes[name]));
43
- }, {}));
44
- }, {});
45
- return _objectSpread({}, routes, {}, resourcesRoutes);
46
- }
47
- }, {
48
- key: "localizedName",
49
- value: function localizedName() {
50
- var variant = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'default';
51
- return getLocalizedName(this.definition, variant);
52
- }
53
- }, {
54
- key: "apiEndpoint",
55
- value: function apiEndpoint() {
56
- var _this$definition$api = this.definition.api;
57
- _this$definition$api = _this$definition$api === void 0 ? {} : _this$definition$api;
58
- var _this$definition$api$ = _this$definition$api.endpoint,
59
- endpoint = _this$definition$api$ === void 0 ? null : _this$definition$api$;
60
- return endpoint;
61
- }
62
- }, {
63
- key: "apiUploadEndpoint",
64
- value: function apiUploadEndpoint() {
65
- var _this$definition$api2 = this.definition.api;
66
- _this$definition$api2 = _this$definition$api2 === void 0 ? {} : _this$definition$api2;
67
- var _this$definition$api3 = _this$definition$api2.uploadEndpoint,
68
- uploadEndpoint = _this$definition$api3 === void 0 ? null : _this$definition$api3;
69
- return uploadEndpoint;
70
- }
71
- }, {
72
- key: "layout",
73
- value: function layout() {
74
- var _this$definition$layo = this.definition.layout,
75
- layout = _this$definition$layo === void 0 ? {} : _this$definition$layo;
76
- return layout;
77
- }
78
- }, {
79
- key: "locales",
80
- value: function locales() {
81
- var _this$definition$loca = this.definition.localization;
82
- _this$definition$loca = _this$definition$loca === void 0 ? {} : _this$definition$loca;
83
- var _this$definition$loca2 = _this$definition$loca.locales,
84
- locales = _this$definition$loca2 === void 0 ? ['en'] : _this$definition$loca2;
85
- return locales;
86
- }
87
- }, {
88
- key: "resources",
89
- value: function resources() {
90
- var _this$definition$reso = this.definition.resources,
91
- resources = _this$definition$reso === void 0 ? [] : _this$definition$reso;
92
- return resources;
93
- }
94
- }, {
95
- key: "resource",
96
- value: function resource(id) {
97
- return this.resources().find(function (it) {
98
- return it.id() === id;
99
- }) || null;
100
- }
101
- }, {
102
- key: "fields",
103
- value: function fields() {
104
- var _this$definition$fiel = this.definition.fields,
105
- fields = _this$definition$fiel === void 0 ? [] : _this$definition$fiel;
106
- return fields;
107
- }
108
- }, {
109
- key: "field",
110
- value: function field(id) {
111
- return this.fields().find(function (it) {
112
- return it.id === id;
113
- }) || null;
114
- }
115
- }, {
116
- key: "blocks",
117
- value: function blocks() {
118
- var _this$definition$bloc = this.definition.blocks,
119
- blocks = _this$definition$bloc === void 0 ? [] : _this$definition$bloc;
120
- return blocks;
121
- }
122
- }, {
123
- key: "block",
124
- value: function block(id) {
125
- return this.blocks().find(function (it) {
126
- return it.id === id;
127
- }) || null;
128
- }
129
- }]);
130
-
131
- return Definition;
132
- }();
133
-
134
- export default Definition;
@@ -1,75 +0,0 @@
1
- import PropTypes from 'prop-types';
2
- import * as definitions from '@panneau/schemas/prop-types';
3
- import UrlGenerator from './UrlGenerator';
4
- import ResourceApi from './ResourceApi';
5
- import Definition from './Definition';
6
- import Resource from './Resource';
7
- import ComponentsCollection from './ComponentsCollection';
8
- /**
9
- * Core
10
- */
11
-
12
- export var urlGenerator = PropTypes.instanceOf(UrlGenerator);
13
- export var history = PropTypes.shape({
14
- push: PropTypes.func.isRequired
15
- });
16
- export var intl = PropTypes.shape({
17
- locale: PropTypes.string.isRequired,
18
- formatMessage: PropTypes.func.isRequired
19
- });
20
- export var intlMessage = PropTypes.shape({
21
- id: PropTypes.string,
22
- description: PropTypes.string,
23
- defaultMessage: PropTypes.string
24
- });
25
- export var componentsCollection = PropTypes.instanceOf(ComponentsCollection);
26
- export var components = PropTypes.oneOfType([PropTypes.objectOf(PropTypes.elementType), PropTypes.instanceOf(ComponentsCollection)]);
27
- export var resourceApi = PropTypes.instanceOf(ResourceApi);
28
- export var ref = PropTypes.oneOfType([PropTypes.func, PropTypes.shape({
29
- current: PropTypes.elementType
30
- })]);
31
- /**
32
- * UI
33
- */
34
-
35
- export var message = PropTypes.oneOfType([PropTypes.string, intlMessage]);
36
- export var label = PropTypes.oneOfType([PropTypes.node, intlMessage]);
37
- export var button = PropTypes.shape({
38
- id: PropTypes.string,
39
- type: PropTypes.string,
40
- href: PropTypes.string,
41
- external: PropTypes.bool,
42
- label: message,
43
- children: message,
44
- className: PropTypes.string,
45
- onClick: PropTypes.func
46
- });
47
- export var buttons = PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.node, button]));
48
- export var dropdownItem = PropTypes.shape({
49
- id: PropTypes.string,
50
- href: PropTypes.string,
51
- external: PropTypes.bool,
52
- label: message,
53
- className: PropTypes.string,
54
- onClick: PropTypes.func
55
- });
56
- export var dropdownItems = PropTypes.arrayOf(dropdownItem);
57
- export var errors = PropTypes.oneOfType([PropTypes.string, PropTypes.arrayOf(PropTypes.string)]);
58
- export var buttonSize = PropTypes.oneOf(['sm', 'md', 'lg']);
59
- export var buttonStyle = PropTypes.oneOf([null, 'primary', 'secondary', 'success', 'danger', 'warning', 'info', 'light', 'dark', 'link', 'outline-primary', 'outline-secondary', 'outline-success', 'outline-danger', 'outline-warning', 'outline-info', 'outline-light', 'outline-dark', 'outline-link']);
60
- /**
61
- * Models
62
- */
63
-
64
- export var user = PropTypes.shape({
65
- id: PropTypes.oneOfType([PropTypes.number, PropTypes.string]).isRequired,
66
- name: PropTypes.string,
67
- email: PropTypes.string
68
- });
69
- /**
70
- * Definitions
71
- */
72
-
73
- export var definition = PropTypes.instanceOf(Definition);
74
- export var resource = PropTypes.instanceOf(Resource);
75
- export { definitions };
@@ -1,93 +0,0 @@
1
- import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
2
- import _createClass from "@babel/runtime/helpers/createClass";
3
- import get from 'lodash/get';
4
- import { getLocalizedName } from './utils';
5
-
6
- var Resource =
7
- /*#__PURE__*/
8
- function () {
9
- function Resource(resource) {
10
- _classCallCheck(this, Resource);
11
-
12
- this.resource = resource || {};
13
- }
14
-
15
- _createClass(Resource, [{
16
- key: "id",
17
- value: function id() {
18
- return this.resource.id;
19
- }
20
- }, {
21
- key: "type",
22
- value: function type() {
23
- return this.resource.type || 'default';
24
- }
25
- }, {
26
- key: "types",
27
- value: function types() {
28
- return this.resource.types || [];
29
- }
30
- }, {
31
- key: "localizedName",
32
- value: function localizedName() {
33
- var variant = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'default';
34
- return getLocalizedName(this.resource, variant);
35
- }
36
- }, {
37
- key: "messages",
38
- value: function messages() {
39
- var _this$resource$messag = this.resource.messages,
40
- messages = _this$resource$messag === void 0 ? {} : _this$resource$messag;
41
- return messages;
42
- }
43
- }, {
44
- key: "message",
45
- value: function message(key, defaultValue) {
46
- return get(this.messages(), key, defaultValue);
47
- }
48
- }, {
49
- key: "routes",
50
- value: function routes() {
51
- var _this$resource$routes = this.resource.routes,
52
- routes = _this$resource$routes === void 0 ? {} : _this$resource$routes;
53
- return routes;
54
- }
55
- }, {
56
- key: "hasRoutes",
57
- value: function hasRoutes() {
58
- var _this$resource$routes2 = this.resource.routes,
59
- routes = _this$resource$routes2 === void 0 ? null : _this$resource$routes2;
60
- return routes !== null;
61
- }
62
- }, {
63
- key: "forms",
64
- value: function forms() {
65
- var _this$resource$forms = this.resource.forms,
66
- forms = _this$resource$forms === void 0 ? {} : _this$resource$forms;
67
- return forms;
68
- }
69
- }, {
70
- key: "form",
71
- value: function form(action) {
72
- var forms = this.forms();
73
- return forms[action] || forms;
74
- }
75
- }, {
76
- key: "lists",
77
- value: function lists() {
78
- var _this$resource$lists = this.resource.lists,
79
- lists = _this$resource$lists === void 0 ? {} : _this$resource$lists;
80
- return lists;
81
- }
82
- }, {
83
- key: "list",
84
- value: function list(action) {
85
- var lists = this.lists();
86
- return lists[action] || lists;
87
- }
88
- }]);
89
-
90
- return Resource;
91
- }();
92
-
93
- export default Resource;
@@ -1,139 +0,0 @@
1
- import _defineProperty from "@babel/runtime/helpers/defineProperty";
2
- import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
3
- import _createClass from "@babel/runtime/helpers/createClass";
4
-
5
- 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; }
6
-
7
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
8
-
9
- import trimEnd from 'lodash/trimEnd';
10
- import isEmpty from 'lodash/isEmpty';
11
- import isObject from 'lodash/isObject';
12
- import queryString from 'query-string';
13
- import 'whatwg-fetch';
14
- import { getResponseAndDataObject, throwResponseError, throwValidationError, getCSRFHeaders } from './requests';
15
-
16
- var ResourceApi =
17
- /*#__PURE__*/
18
- function () {
19
- function ResourceApi(resource, urlGenerator, opts) {
20
- _classCallCheck(this, ResourceApi);
21
-
22
- this.resource = resource;
23
- this.urlGenerator = urlGenerator;
24
- this.options = _objectSpread({
25
- idParamName: 'id',
26
- host: '/'
27
- }, opts);
28
- }
29
-
30
- _createClass(ResourceApi, [{
31
- key: "definition",
32
- value: function definition() {
33
- return this.callApi('definition', 'get');
34
- }
35
- }, {
36
- key: "index",
37
- value: function index() {
38
- var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
39
- var query = params !== null && isObject(params) ? queryString.stringify(params) : params || '';
40
- var path = "".concat(this.getActionPath('index')).concat(!isEmpty(query) ? "?".concat(query.replace(/^\?/, '')) : '');
41
- return this.callApi(path, 'get');
42
- }
43
- }, {
44
- key: "create",
45
- value: function create() {
46
- var path = this.getActionPath('create');
47
- return this.callApi(path, 'get');
48
- }
49
- }, {
50
- key: "store",
51
- value: function store(data) {
52
- var path = this.getActionPath('store');
53
- return this.callApi(path, 'post', data);
54
- }
55
- }, {
56
- key: "show",
57
- value: function show(id) {
58
- var path = this.getActionPath('show', id);
59
- return this.callApi(path, 'get');
60
- }
61
- }, {
62
- key: "edit",
63
- value: function edit(id) {
64
- var path = this.getActionPath('edit', id);
65
- return this.callApi(path, 'get');
66
- }
67
- }, {
68
- key: "update",
69
- value: function update(id, data) {
70
- var path = this.getActionPath('update', id);
71
- return this.callApi(path, 'put', data);
72
- }
73
- }, {
74
- key: "destroy",
75
- value: function destroy(id) {
76
- var path = this.getActionPath('destroy', id);
77
- return this.callApi(path, 'delete');
78
- } // eslint-disable-next-line class-methods-use-this
79
-
80
- }, {
81
- key: "callApi",
82
- value: function callApi(path, method) {
83
- var data = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : undefined;
84
- var body = data || null;
85
- var methodUpperCase = method.toUpperCase();
86
- var finalMethod;
87
- var finalBody;
88
-
89
- switch (methodUpperCase) {
90
- case 'PUT':
91
- case 'PATCH':
92
- case 'DELETE':
93
- finalMethod = 'POST';
94
- finalBody = _objectSpread({}, body, {
95
- _method: methodUpperCase // Laravel magic
96
-
97
- });
98
- break;
99
-
100
- default:
101
- finalMethod = methodUpperCase;
102
- finalBody = body;
103
- break;
104
- }
105
-
106
- return fetch(path, {
107
- credentials: 'include',
108
- method: finalMethod,
109
- headers: _objectSpread({}, getCSRFHeaders(), {
110
- 'Content-Type': 'application/json',
111
- Accept: 'application/json'
112
- }),
113
- body: finalBody !== null ? JSON.stringify(finalBody) : null
114
- }).then(getResponseAndDataObject).then(function (responseObject) {
115
- return throwResponseError(responseObject);
116
- })["catch"](function (error) {
117
- return throwValidationError(error);
118
- });
119
- }
120
- }, {
121
- key: "getActionPath",
122
- value: function getActionPath(action) {
123
- var id = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : undefined;
124
- var urlGenerator = this.urlGenerator,
125
- resource = this.resource;
126
- var _this$options = this.options,
127
- idParamName = _this$options.idParamName,
128
- host = _this$options.host;
129
- var path = urlGenerator.resource(resource, action, id);
130
- var idPattern = new RegExp(":".concat(idParamName, "([/](.*)?)?$"), 'i');
131
- var fullPath = trimEnd(host, '/') + path;
132
- return fullPath.replace(idPattern, id);
133
- }
134
- }]);
135
-
136
- return ResourceApi;
137
- }();
138
-
139
- export default ResourceApi;
@@ -1,66 +0,0 @@
1
- import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
2
- import _defineProperty from "@babel/runtime/helpers/defineProperty";
3
- import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
4
- import _createClass from "@babel/runtime/helpers/createClass";
5
-
6
- 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; }
7
-
8
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
9
-
10
- import reduce from 'lodash/reduce';
11
-
12
- var UrlGenerator =
13
- /*#__PURE__*/
14
- function () {
15
- function UrlGenerator(routes, opts) {
16
- _classCallCheck(this, UrlGenerator);
17
-
18
- this.routes = routes;
19
- this.options = _objectSpread({
20
- paramFormat: ':{key}(\\([^\\)]*\\))?'
21
- }, opts);
22
- }
23
-
24
- _createClass(UrlGenerator, [{
25
- key: "route",
26
- value: function route(key, opts) {
27
- var options = _objectSpread({
28
- withHost: false
29
- }, this.options, {}, opts);
30
-
31
- var withHost = options.withHost,
32
- paramFormat = options.paramFormat,
33
- params = _objectWithoutProperties(options, ["withHost", "paramFormat"]);
34
-
35
- var route = this.routes[key] || key;
36
- var url = reduce(params || {}, function (str, v, k) {
37
- return (v || null) !== null ? str.replace(new RegExp(paramFormat.replace(/\{\s*key\s*\}/gi, k), 'i'), v) : str;
38
- }, route);
39
- var host = typeof window !== 'undefined' ? "".concat(window.location.protocol, "//").concat(window.location.host) : '';
40
- var hostPattern = host !== null ? "^".concat(host) : '^$';
41
-
42
- if (typeof withHost !== 'undefined' && withHost === true) {
43
- return !url.match(hostPattern) ? "".concat(host).concat(url) : url;
44
- }
45
-
46
- var urlWithoutHost = url.replace(new RegExp(hostPattern, 'i'), '');
47
- return urlWithoutHost === '' ? '/' : urlWithoutHost;
48
- }
49
- }, {
50
- key: "resource",
51
- value: function resource(_resource, action) {
52
- var id = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
53
- var resourceId = _resource !== null ? _resource.id() : null;
54
- return _resource !== null && _resource.hasRoutes() ? this.route("resource.".concat(resourceId, ".").concat(action), {
55
- id: id
56
- }) : this.route("resource.".concat(action), {
57
- resource: resourceId,
58
- id: id
59
- });
60
- }
61
- }]);
62
-
63
- return UrlGenerator;
64
- }();
65
-
66
- export default UrlGenerator;
@@ -1,165 +0,0 @@
1
- import _defineProperty from "@babel/runtime/helpers/defineProperty";
2
- import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
3
- import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
4
- import _createClass from "@babel/runtime/helpers/createClass";
5
- import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
6
- import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
7
- import _inherits from "@babel/runtime/helpers/inherits";
8
- import _wrapNativeSuper from "@babel/runtime/helpers/wrapNativeSuper";
9
-
10
- 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; }
11
-
12
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
13
-
14
- /* eslint-disable max-classes-per-file */
15
- import Cookies from 'js-cookie';
16
- export var ResponseError =
17
- /*#__PURE__*/
18
- function (_Error) {
19
- _inherits(ResponseError, _Error);
20
-
21
- function ResponseError(message, responseData, status) {
22
- var _this;
23
-
24
- _classCallCheck(this, ResponseError);
25
-
26
- _this = _possibleConstructorReturn(this, _getPrototypeOf(ResponseError).call(this, message));
27
- _this.name = 'ResponseError';
28
- _this.responseData = responseData;
29
- _this.status = status;
30
- return _this;
31
- }
32
-
33
- _createClass(ResponseError, [{
34
- key: "getResponseData",
35
- value: function getResponseData() {
36
- return this.responseData;
37
- }
38
- }]);
39
-
40
- return ResponseError;
41
- }(_wrapNativeSuper(Error));
42
- export var ValidationError =
43
- /*#__PURE__*/
44
- function (_ResponseError) {
45
- _inherits(ValidationError, _ResponseError);
46
-
47
- function ValidationError(message, responseData, status) {
48
- var _this2;
49
-
50
- _classCallCheck(this, ValidationError);
51
-
52
- _this2 = _possibleConstructorReturn(this, _getPrototypeOf(ValidationError).call(this, message, responseData, status));
53
- _this2.name = 'ValidationError';
54
- return _this2;
55
- }
56
-
57
- _createClass(ValidationError, [{
58
- key: "getErrors",
59
- value: function getErrors() {
60
- return this.responseData;
61
- }
62
- }]);
63
-
64
- return ValidationError;
65
- }(ResponseError);
66
- export var getResponseAndDataObject = function getResponseAndDataObject(response) {
67
- return response.json().then(function (data) {
68
- return {
69
- data: data,
70
- response: response
71
- };
72
- });
73
- };
74
- export var getErrorsFromResponseError = function getErrorsFromResponseError(error) {
75
- var name = error.name,
76
- _error$responseData = error.responseData,
77
- responseData = _error$responseData === void 0 ? {} : _error$responseData,
78
- _error$message = error.message,
79
- message = _error$message === void 0 ? null : _error$message;
80
-
81
- if (name === 'ValidationError') {
82
- return responseData.errors || responseData;
83
- }
84
-
85
- if (name === 'ResponseError') {
86
- return responseData.error || responseData;
87
- }
88
-
89
- return message;
90
- };
91
- export var throwResponseError = function throwResponseError(responseObject) {
92
- var response = responseObject.response,
93
- data = responseObject.data;
94
-
95
- if (response.status >= 200 && response.status < 300) {
96
- return data;
97
- }
98
-
99
- throw new ResponseError(response.statusText, data, response.status);
100
- };
101
- export var throwValidationError = function throwValidationError(error) {
102
- if (error.name === 'ResponseError' && error.status === 422) {
103
- throw new ValidationError(error.message, error.responseData, error.status);
104
- }
105
-
106
- throw error;
107
- };
108
- export var getJSON = function getJSON(url, opts) {
109
- var _ref = opts || {},
110
- headers = _ref.headers,
111
- options = _objectWithoutProperties(_ref, ["headers"]);
112
-
113
- return fetch(url, _objectSpread({
114
- method: 'GET',
115
- headers: _objectSpread({
116
- Accept: 'application/json'
117
- }, headers || null)
118
- }, options || null)).then(getResponseAndDataObject).then(throwResponseError);
119
- };
120
- export var postJSON = function postJSON(url, data, opts) {
121
- var _ref2 = opts || {},
122
- headers = _ref2.headers,
123
- options = _objectWithoutProperties(_ref2, ["headers"]);
124
-
125
- return fetch(url, _objectSpread({
126
- method: 'POST',
127
- headers: _objectSpread({
128
- 'Content-Type': 'application/json',
129
- Accept: 'application/json'
130
- }, headers || null),
131
- body: JSON.stringify(data)
132
- }, options || null)).then(getResponseAndDataObject).then(throwResponseError)["catch"](throwValidationError);
133
- };
134
- export var getXSRFToken = function getXSRFToken() {
135
- var cookies = Cookies.get();
136
- return cookies['X-XSRF-TOKEN'] || null;
137
- };
138
-
139
- var getCsrfToken = function getCsrfToken(name) {
140
- var metaName = name || 'csrf-token';
141
- var metas = [].slice.call(document.getElementsByTagName('meta'));
142
- return metas.reduce(function (val, meta) {
143
- return meta.getAttribute('name') === metaName ? meta.getAttribute('content') : val;
144
- }, null);
145
- };
146
-
147
- export var getCSRFHeaders = function getCSRFHeaders(name) {
148
- var XSRF = getXSRFToken();
149
-
150
- if (XSRF !== null) {
151
- return {
152
- 'X-XSRF-TOKEN': XSRF
153
- };
154
- }
155
-
156
- var CSRF = getCsrfToken(name);
157
-
158
- if (CSRF !== null) {
159
- return {
160
- 'X-CSRF-TOKEN': CSRF
161
- };
162
- }
163
-
164
- return null;
165
- };
@@ -1,19 +0,0 @@
1
- import { useMemo } from 'react';
2
- import isObject from 'lodash/isObject';
3
- import ResourceApi from './ResourceApi';
4
- import { useDefinition } from '../contexts/Definition';
5
- import { useUrlGenerator } from '../contexts/UrlGenerator';
6
-
7
- var useResourceApi = function useResourceApi(resourceId) {
8
- var definition = useDefinition();
9
- var urlGenerator = useUrlGenerator();
10
- var api = useMemo(function () {
11
- var resource = isObject(resourceId) ? resourceId : definition.resource(resourceId);
12
- return new ResourceApi(resource, urlGenerator, {
13
- host: definition.apiEndpoint()
14
- });
15
- }, [resourceId, definition, urlGenerator]);
16
- return api;
17
- };
18
-
19
- export default useResourceApi;