cozy-client 49.8.0 → 50.1.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.
@@ -2997,7 +2997,7 @@ var CozyClient = /*#__PURE__*/function () {
2997
2997
  CozyClient.hooks = CozyClient.hooks || {};
2998
2998
  CozyClient.fetchPolicies = _policies.default; //COZY_CLIENT_VERSION_PACKAGE in replaced by babel. See babel config
2999
2999
 
3000
- CozyClient.version = "49.8.0";
3000
+ CozyClient.version = "50.1.0";
3001
3001
 
3002
3002
  _microee.default.mixin(CozyClient);
3003
3003
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cozy-client",
3
- "version": "49.8.0",
3
+ "version": "50.1.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": "^49.8.0",
21
+ "cozy-stack-client": "^50.1.0",
22
22
  "date-fns": "2.29.3",
23
23
  "json-stable-stringify": "^1.0.1",
24
24
  "lodash": "^4.17.13",
@@ -75,5 +75,5 @@
75
75
  "react-native-ios11-devicecheck": "https://github.com/cozy/react-native-devicecheck#app-attest-v0.1"
76
76
  },
77
77
  "sideEffects": false,
78
- "gitHead": "b4e2d123dfb2fc1b5e83b9fbb5224a7a04b5dc47"
78
+ "gitHead": "eabd164250195c878b7f40fb22c41e8b89503ea2"
79
79
  }
@@ -1,183 +0,0 @@
1
- "use strict";
2
-
3
- var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
4
-
5
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
6
-
7
- Object.defineProperty(exports, "__esModule", {
8
- value: true
9
- });
10
- exports.default = useClientErrors;
11
-
12
- var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
13
-
14
- var _react = _interopRequireWildcard(require("react"));
15
-
16
- var _QuotaAlert = _interopRequireDefault(require("cozy-ui/transpiled/react/deprecated/QuotaAlert"));
17
-
18
- var _filter = _interopRequireDefault(require("lodash/filter"));
19
-
20
- var _cozyStackClient = require("cozy-stack-client");
21
-
22
- var _ = require("./");
23
-
24
- /**
25
- * Rendering functions for client fetch errors by status code
26
- *
27
- * @private
28
- * @type {object}
29
- */
30
- var byHttpStatus = {
31
- 413: QuotaError // <QuotaError />
32
-
33
- };
34
- /**
35
- * Display for a quota error from the client
36
- *
37
- * @see QuotaAlert
38
- * @private
39
- * @param {object} props - Props
40
- * @param {Function} props.dismiss - remove the error from the stack to display
41
- * @returns {React.ReactElement}
42
- */
43
-
44
- function QuotaError(_ref) {
45
- var dismiss = _ref.dismiss;
46
- return /*#__PURE__*/_react.default.createElement(_QuotaAlert.default, {
47
- onClose: dismiss
48
- });
49
- }
50
- /**
51
- * Returns the handler for an error
52
- *
53
- * @param {import("../types").ClientError} error - The error
54
- * @returns {Function|null} React Component
55
- */
56
-
57
-
58
- function getErrorComponent(error) {
59
- if (error instanceof Response || error instanceof _cozyStackClient.FetchError) {
60
- var status = error.status || '';
61
- return byHttpStatus[status] || null;
62
- }
63
-
64
- return null;
65
- }
66
- /**
67
- * Renders a stack of errors
68
- *
69
- * @private
70
- * @see ClientErrors
71
- * @param {import("../types").ClientError[]} errorStack - array of errors/exceptions
72
- * @param {Function} setErrorStack - mutates the array of errors
73
- * @returns {Array<React.ReactElement>} React rendering
74
- */
75
-
76
-
77
- function renderErrors(errorStack, setErrorStack) {
78
- var errors = errorStack.map(function (error, key) {
79
- var Component = getErrorComponent(error);
80
-
81
- if (Component) {
82
- var dismiss = function dismiss() {
83
- return setErrorStack(function (stack) {
84
- return (0, _filter.default)(stack, function (e) {
85
- return e !== error;
86
- });
87
- });
88
- };
89
-
90
- return /*#__PURE__*/_react.default.createElement(Component, {
91
- key: key,
92
- error: error,
93
- dismiss: dismiss
94
- });
95
- } else {
96
- return null;
97
- }
98
- });
99
- return errors;
100
- }
101
- /**
102
- * Manages the client errors and allows to display them
103
- *
104
- * Returns a `ClientErrors` React component that takes care
105
- * of how to display cozy-client errors (probably displaying a modal)
106
- *
107
- * Only Quota Errors are managed for now.
108
- *
109
- * @example
110
- * ```
111
- * const App = () => {
112
- * const { ClientErrors } = useClientErrors()
113
- *
114
- * return <Layout>
115
- * <h1>My app</h1>
116
- * <ClientErrors />
117
- * </Layout>
118
- * }
119
- * ```
120
- *
121
- * @param {object} [props] - Props
122
- * @param {boolean} [props.handleExceptions] - should cozy-client directly handle errors before forwarding them to the caller?
123
- * @returns {{ClientErrors: Function}} React component
124
- */
125
-
126
-
127
- function useClientErrors() {
128
- var _ref2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
129
- _ref2$handleException = _ref2.handleExceptions,
130
- handleExceptions = _ref2$handleException === void 0 ? true : _ref2$handleException;
131
-
132
- var client = (0, _.useClient)();
133
-
134
- var _useState = (0, _react.useState)([]),
135
- _useState2 = (0, _slicedToArray2.default)(_useState, 2),
136
- errorStack = _useState2[0],
137
- setErrorStack = _useState2[1];
138
- /**
139
- * Handle client errors, add them to the error stack
140
- *
141
- * @param {import("../types").ClientError} error -
142
- * @returns {boolean} true if the error was manager, false otherwise
143
- */
144
-
145
-
146
- var handleError = (0, _react.useCallback)(function (error) {
147
- // `isErrorManaged` is there to avoid the same error to be added twice
148
- // once the error has been added once, the `isErrorManaged`is set to true
149
- // and any future push is ignored
150
- if (error.isErrorManaged) return false;
151
- var isManageable = !!getErrorComponent(error);
152
-
153
- if (isManageable) {
154
- error.isErrorManaged = true;
155
- setErrorStack(function (stack) {
156
- return stack.concat(error);
157
- });
158
- return true;
159
- } else {
160
- error.isErrorManaged = false;
161
- return false;
162
- }
163
- }, [setErrorStack]);
164
- (0, _react.useEffect)(function () {
165
- if (handleExceptions) {
166
- client.on('error', handleError);
167
- return function () {
168
- return client.removeListener('error', handleError);
169
- };
170
- } else {
171
- return undefined;
172
- }
173
- }, [client, handleError, handleExceptions]); // @ts-ignore
174
-
175
- var ClientErrors = (0, _react.useCallback)(function () {
176
- return renderErrors(errorStack, setErrorStack);
177
- }, [errorStack, setErrorStack]); // @ts-ignore
178
-
179
- ClientErrors.displayName = 'ClientErrors';
180
- return {
181
- ClientErrors: ClientErrors
182
- };
183
- }
@@ -1,100 +0,0 @@
1
- import React from 'react'
2
- import useClientErrors from './useClientErrors'
3
- import { renderHook, act } from '@testing-library/react-hooks'
4
- import CozyProvider from '../Provider'
5
- import { shallow } from 'enzyme'
6
- import { FetchError } from 'cozy-stack-client'
7
-
8
- function createCozyClient() {
9
- return {
10
- on: jest.fn(),
11
- removeListener: jest.fn()
12
- }
13
- }
14
-
15
- function createWrapper(client = createCozyClient()) {
16
- function Wrapper({ children }) {
17
- return <CozyProvider client={client}>{children}</CozyProvider>
18
- }
19
- return Wrapper
20
- }
21
-
22
- function renderWrappedHook(client) {
23
- const wrapper = createWrapper(client)
24
- return renderHook(() => useClientErrors(), { wrapper })
25
- }
26
-
27
- function wrappedShallow(tree, client) {
28
- return shallow(tree, { wrappingComponent: createWrapper(client) })
29
- }
30
-
31
- describe('useClientErrors', () => {
32
- it('registers an `error` handler in client', done => {
33
- const client = createCozyClient()
34
- client.on.mockImplementation(name => name === 'error' && done())
35
- renderWrappedHook(client)
36
- })
37
-
38
- describe('renderErrors', () => {
39
- it('returns a function', () => {
40
- const { result } = renderWrappedHook()
41
- const { ClientErrors } = result.current
42
- expect(ClientErrors).toBeInstanceOf(Function)
43
- })
44
-
45
- it('displays nothing by default', () => {
46
- const { result } = renderWrappedHook()
47
- const { ClientErrors } = result.current
48
- const node = wrappedShallow(<ClientErrors />)
49
- expect(node).toHaveLength(0)
50
- })
51
-
52
- describe('for quota errors', () => {
53
- const findQuotaAlert = node => {
54
- return node.at(0).dive()
55
- }
56
- const setup = async () => {
57
- const client = createCozyClient()
58
- const response = new Response(null, {
59
- status: 413,
60
- statusText: 'Quota exceeded'
61
- })
62
- const error = new FetchError(
63
- response,
64
- `${response.status} ${response.statusText}`
65
- )
66
-
67
- const { result, waitForNextUpdate } = renderWrappedHook(client)
68
- const nextUpdate = waitForNextUpdate()
69
-
70
- act(() => {
71
- const handler = client.on.mock.calls[0][1]
72
- handler(error)
73
- })
74
-
75
- await nextUpdate
76
- const { ClientErrors } = result.current
77
- const node = wrappedShallow(<ClientErrors />, client)
78
- return { node, result, client }
79
- }
80
-
81
- it('displays a a QuotaAlert', async () => {
82
- const { node } = await setup()
83
- expect(node).toHaveLength(1)
84
- expect(findQuotaAlert(node).type().name).toEqual('QuotaAlert')
85
- })
86
-
87
- it('can be dismissed', async () => {
88
- const { node, result, client } = await setup()
89
- const quotaAlert = findQuotaAlert(node)
90
- const onClose = quotaAlert.props().onClose
91
- act(() => onClose())
92
-
93
- // re-render ClientErrors returned by the hook
94
- const { ClientErrors } = result.current
95
- const updatedNode = wrappedShallow(<ClientErrors />, client)
96
- expect(updatedNode.at(0).length).toBe(0)
97
- })
98
- })
99
- })
100
- })
@@ -1,29 +0,0 @@
1
- /**
2
- * Manages the client errors and allows to display them
3
- *
4
- * Returns a `ClientErrors` React component that takes care
5
- * of how to display cozy-client errors (probably displaying a modal)
6
- *
7
- * Only Quota Errors are managed for now.
8
- *
9
- * @example
10
- * ```
11
- * const App = () => {
12
- * const { ClientErrors } = useClientErrors()
13
- *
14
- * return <Layout>
15
- * <h1>My app</h1>
16
- * <ClientErrors />
17
- * </Layout>
18
- * }
19
- * ```
20
- *
21
- * @param {object} [props] - Props
22
- * @param {boolean} [props.handleExceptions] - should cozy-client directly handle errors before forwarding them to the caller?
23
- * @returns {{ClientErrors: Function}} React component
24
- */
25
- export default function useClientErrors({ handleExceptions }?: {
26
- handleExceptions: boolean;
27
- }): {
28
- ClientErrors: Function;
29
- };