@faasjs/react 0.0.3-beta.107 → 0.0.3-beta.108

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.
package/README.md CHANGED
@@ -166,7 +166,9 @@ ___
166
166
  ### Options
167
167
 
168
168
  Ƭ **Options**: `RequestInit` & { `beforeRequest?`: (`{
169
- action, params, options
169
+ action,
170
+ params,
171
+ options,
170
172
  }`: { `action`: `string` ; `options`: [`Options`](#options) ; `params`: `Record`<`string`, `any`\> }) => `Promise`<`void`\> \| `void` ; `headers?`: { `[key: string]`: `string`; } ; `request?`: <PathOrData\>(`url`: `string`, `options`: [`Options`](#options)) => `Promise`<[`Response`](classes/Response.md)<[`FaasData`](#faasdata)<`PathOrData`\>\>\> }
171
173
 
172
174
  ___
package/dist/index.d.mts CHANGED
@@ -14,9 +14,11 @@ type FaasReactClientInstance = {
14
14
  useFaas: <PathOrData extends FaasAction>(
15
15
  action: string | PathOrData,
16
16
  defaultParams: FaasParams<PathOrData>,
17
- options?: useFaasOptions<PathOrData>,
17
+ options?: useFaasOptions<PathOrData>
18
18
  ) => FaasDataInjection<FaasData<PathOrData>>
19
- FaasDataWrapper<PathOrData extends FaasAction>(props: FaasDataWrapperProps<PathOrData>): JSX.Element
19
+ FaasDataWrapper<PathOrData extends FaasAction>(
20
+ props: FaasDataWrapperProps<PathOrData>
21
+ ): JSX.Element
20
22
  }
21
23
 
22
24
  /**
@@ -30,7 +32,7 @@ type FaasDataInjection<Data = any> = {
30
32
  data: Data
31
33
  error: any
32
34
  promise: Promise<Response<Data>>
33
- reload(params?: Record<string, any>): Promise<Response<Data>>,
35
+ reload(params?: Record<string, any>): Promise<Response<Data>>
34
36
  setData: React.Dispatch<React.SetStateAction<Data>>
35
37
  setLoading: React.Dispatch<React.SetStateAction<boolean>>
36
38
  setPromise: React.Dispatch<React.SetStateAction<Promise<Response<Data>>>>
@@ -38,7 +40,9 @@ type FaasDataInjection<Data = any> = {
38
40
  }
39
41
 
40
42
  type FaasDataWrapperProps<PathOrData extends FaasAction> = {
41
- render?(args: FaasDataInjection<FaasData<PathOrData>>): JSX.Element | JSX.Element[]
43
+ render?(
44
+ args: FaasDataInjection<FaasData<PathOrData>>
45
+ ): JSX.Element | JSX.Element[]
42
46
  children?: React.ReactElement<Partial<FaasDataInjection>>
43
47
  fallback?: JSX.Element | false
44
48
  action: string
@@ -72,7 +76,7 @@ type useFaasOptions<PathOrData extends FaasAction> = {
72
76
  * })
73
77
  * ```
74
78
  */
75
- declare function FaasReactClient({ domain, options, onError }: {
79
+ declare function FaasReactClient({ domain, options, onError, }: {
76
80
  domain: string;
77
81
  options?: Options;
78
82
  onError?: (action: string, params: Record<string, any>) => (res: ResponseError) => Promise<void>;
package/dist/index.d.ts CHANGED
@@ -14,9 +14,11 @@ type FaasReactClientInstance = {
14
14
  useFaas: <PathOrData extends FaasAction>(
15
15
  action: string | PathOrData,
16
16
  defaultParams: FaasParams<PathOrData>,
17
- options?: useFaasOptions<PathOrData>,
17
+ options?: useFaasOptions<PathOrData>
18
18
  ) => FaasDataInjection<FaasData<PathOrData>>
19
- FaasDataWrapper<PathOrData extends FaasAction>(props: FaasDataWrapperProps<PathOrData>): JSX.Element
19
+ FaasDataWrapper<PathOrData extends FaasAction>(
20
+ props: FaasDataWrapperProps<PathOrData>
21
+ ): JSX.Element
20
22
  }
21
23
 
22
24
  /**
@@ -30,7 +32,7 @@ type FaasDataInjection<Data = any> = {
30
32
  data: Data
31
33
  error: any
32
34
  promise: Promise<Response<Data>>
33
- reload(params?: Record<string, any>): Promise<Response<Data>>,
35
+ reload(params?: Record<string, any>): Promise<Response<Data>>
34
36
  setData: React.Dispatch<React.SetStateAction<Data>>
35
37
  setLoading: React.Dispatch<React.SetStateAction<boolean>>
36
38
  setPromise: React.Dispatch<React.SetStateAction<Promise<Response<Data>>>>
@@ -38,7 +40,9 @@ type FaasDataInjection<Data = any> = {
38
40
  }
39
41
 
40
42
  type FaasDataWrapperProps<PathOrData extends FaasAction> = {
41
- render?(args: FaasDataInjection<FaasData<PathOrData>>): JSX.Element | JSX.Element[]
43
+ render?(
44
+ args: FaasDataInjection<FaasData<PathOrData>>
45
+ ): JSX.Element | JSX.Element[]
42
46
  children?: React.ReactElement<Partial<FaasDataInjection>>
43
47
  fallback?: JSX.Element | false
44
48
  action: string
@@ -72,7 +76,7 @@ type useFaasOptions<PathOrData extends FaasAction> = {
72
76
  * })
73
77
  * ```
74
78
  */
75
- declare function FaasReactClient({ domain, options, onError }: {
79
+ declare function FaasReactClient({ domain, options, onError, }: {
76
80
  domain: string;
77
81
  options?: Options;
78
82
  onError?: (action: string, params: Record<string, any>) => (res: ResponseError) => Promise<void>;
package/dist/index.js CHANGED
@@ -30,59 +30,71 @@ function FaasReactClient({
30
30
  const [params, setParams] = react.useState(defaultParams);
31
31
  const [reloadTimes, setReloadTimes] = react.useState(0);
32
32
  const [fails, setFails] = react.useState(0);
33
- react.useEffect(function() {
34
- if (JSON.stringify(defaultParams) !== JSON.stringify(params)) {
35
- setParams(defaultParams);
36
- }
37
- }, [JSON.stringify(defaultParams)]);
38
- react.useEffect(function() {
39
- if (!action || (options2 == null ? void 0 : options2.skip)) {
40
- setLoading(false);
41
- return;
42
- }
43
- setLoading(true);
44
- const controller = new AbortController();
45
- function send() {
46
- const request = client.action(action, options2.params || params, { signal: controller.signal });
47
- setPromise(request);
48
- request.then((r) => (options2 == null ? void 0 : options2.setData) ? options2.setData(r.data) : setData(r.data)).catch(async (e) => {
49
- if ((e == null ? void 0 : e.message) === "The user aborted a request." || (e == null ? void 0 : e.message) === "Aborted")
50
- return;
51
- if (!fails && typeof (e == null ? void 0 : e.message) === "string" && e.message.indexOf("Failed to fetch") >= 0) {
52
- console.warn(`FaasReactClient: ${e.message} retry...`);
53
- setFails(1);
54
- return send();
55
- }
56
- if (onError)
57
- try {
58
- await onError(action, params)(e);
59
- } catch (error2) {
60
- setError(error2);
33
+ react.useEffect(
34
+ function() {
35
+ if (JSON.stringify(defaultParams) !== JSON.stringify(params)) {
36
+ setParams(defaultParams);
37
+ }
38
+ },
39
+ [JSON.stringify(defaultParams)]
40
+ );
41
+ react.useEffect(
42
+ function() {
43
+ if (!action || (options2 == null ? void 0 : options2.skip)) {
44
+ setLoading(false);
45
+ return;
46
+ }
47
+ setLoading(true);
48
+ const controller = new AbortController();
49
+ function send() {
50
+ const request = client.action(
51
+ action,
52
+ options2.params || params,
53
+ { signal: controller.signal }
54
+ );
55
+ setPromise(request);
56
+ request.then(
57
+ (r) => (options2 == null ? void 0 : options2.setData) ? options2.setData(r.data) : setData(r.data)
58
+ ).catch(async (e) => {
59
+ if ((e == null ? void 0 : e.message) === "The user aborted a request." || (e == null ? void 0 : e.message) === "Aborted")
60
+ return;
61
+ if (!fails && typeof (e == null ? void 0 : e.message) === "string" && e.message.indexOf("Failed to fetch") >= 0) {
62
+ console.warn(`FaasReactClient: ${e.message} retry...`);
63
+ setFails(1);
64
+ return send();
61
65
  }
62
- else
63
- setError(e);
64
- return Promise.reject(e);
65
- }).finally(() => setLoading(false));
66
- }
67
- if (options2 == null ? void 0 : options2.debounce) {
68
- const timeout = setTimeout(send, options2.debounce);
66
+ if (onError)
67
+ try {
68
+ await onError(action, params)(e);
69
+ } catch (error2) {
70
+ setError(error2);
71
+ }
72
+ else
73
+ setError(e);
74
+ return Promise.reject(e);
75
+ }).finally(() => setLoading(false));
76
+ }
77
+ if (options2 == null ? void 0 : options2.debounce) {
78
+ const timeout = setTimeout(send, options2.debounce);
79
+ return () => {
80
+ clearTimeout(timeout);
81
+ controller.abort();
82
+ setLoading(false);
83
+ };
84
+ }
85
+ send();
69
86
  return () => {
70
- clearTimeout(timeout);
71
87
  controller.abort();
72
88
  setLoading(false);
73
89
  };
74
- }
75
- send();
76
- return () => {
77
- controller.abort();
78
- setLoading(false);
79
- };
80
- }, [
81
- action,
82
- JSON.stringify(options2.params || params),
83
- reloadTimes,
84
- options2.skip
85
- ]);
90
+ },
91
+ [
92
+ action,
93
+ JSON.stringify(options2.params || params),
94
+ reloadTimes,
95
+ options2.skip
96
+ ]
97
+ );
86
98
  return {
87
99
  action,
88
100
  params,
@@ -122,14 +134,20 @@ function FaasReactClient({
122
134
  setData
123
135
  });
124
136
  const [loaded, setLoaded] = react.useState(false);
125
- react.useEffect(function() {
126
- if (!loaded && !request.loading)
127
- setLoaded(true);
128
- }, [request.loading]);
129
- react.useEffect(function() {
130
- if (onDataChange)
131
- onDataChange(request);
132
- }, [JSON.stringify(request.data)]);
137
+ react.useEffect(
138
+ function() {
139
+ if (!loaded && !request.loading)
140
+ setLoaded(true);
141
+ },
142
+ [request.loading]
143
+ );
144
+ react.useEffect(
145
+ function() {
146
+ if (onDataChange)
147
+ onDataChange(request);
148
+ },
149
+ [JSON.stringify(request.data)]
150
+ );
133
151
  if (loaded) {
134
152
  if (children)
135
153
  return react.cloneElement(children, request);
package/dist/index.mjs CHANGED
@@ -28,59 +28,71 @@ function FaasReactClient({
28
28
  const [params, setParams] = useState(defaultParams);
29
29
  const [reloadTimes, setReloadTimes] = useState(0);
30
30
  const [fails, setFails] = useState(0);
31
- useEffect(function() {
32
- if (JSON.stringify(defaultParams) !== JSON.stringify(params)) {
33
- setParams(defaultParams);
34
- }
35
- }, [JSON.stringify(defaultParams)]);
36
- useEffect(function() {
37
- if (!action || (options2 == null ? void 0 : options2.skip)) {
38
- setLoading(false);
39
- return;
40
- }
41
- setLoading(true);
42
- const controller = new AbortController();
43
- function send() {
44
- const request = client.action(action, options2.params || params, { signal: controller.signal });
45
- setPromise(request);
46
- request.then((r) => (options2 == null ? void 0 : options2.setData) ? options2.setData(r.data) : setData(r.data)).catch(async (e) => {
47
- if ((e == null ? void 0 : e.message) === "The user aborted a request." || (e == null ? void 0 : e.message) === "Aborted")
48
- return;
49
- if (!fails && typeof (e == null ? void 0 : e.message) === "string" && e.message.indexOf("Failed to fetch") >= 0) {
50
- console.warn(`FaasReactClient: ${e.message} retry...`);
51
- setFails(1);
52
- return send();
53
- }
54
- if (onError)
55
- try {
56
- await onError(action, params)(e);
57
- } catch (error2) {
58
- setError(error2);
31
+ useEffect(
32
+ function() {
33
+ if (JSON.stringify(defaultParams) !== JSON.stringify(params)) {
34
+ setParams(defaultParams);
35
+ }
36
+ },
37
+ [JSON.stringify(defaultParams)]
38
+ );
39
+ useEffect(
40
+ function() {
41
+ if (!action || (options2 == null ? void 0 : options2.skip)) {
42
+ setLoading(false);
43
+ return;
44
+ }
45
+ setLoading(true);
46
+ const controller = new AbortController();
47
+ function send() {
48
+ const request = client.action(
49
+ action,
50
+ options2.params || params,
51
+ { signal: controller.signal }
52
+ );
53
+ setPromise(request);
54
+ request.then(
55
+ (r) => (options2 == null ? void 0 : options2.setData) ? options2.setData(r.data) : setData(r.data)
56
+ ).catch(async (e) => {
57
+ if ((e == null ? void 0 : e.message) === "The user aborted a request." || (e == null ? void 0 : e.message) === "Aborted")
58
+ return;
59
+ if (!fails && typeof (e == null ? void 0 : e.message) === "string" && e.message.indexOf("Failed to fetch") >= 0) {
60
+ console.warn(`FaasReactClient: ${e.message} retry...`);
61
+ setFails(1);
62
+ return send();
59
63
  }
60
- else
61
- setError(e);
62
- return Promise.reject(e);
63
- }).finally(() => setLoading(false));
64
- }
65
- if (options2 == null ? void 0 : options2.debounce) {
66
- const timeout = setTimeout(send, options2.debounce);
64
+ if (onError)
65
+ try {
66
+ await onError(action, params)(e);
67
+ } catch (error2) {
68
+ setError(error2);
69
+ }
70
+ else
71
+ setError(e);
72
+ return Promise.reject(e);
73
+ }).finally(() => setLoading(false));
74
+ }
75
+ if (options2 == null ? void 0 : options2.debounce) {
76
+ const timeout = setTimeout(send, options2.debounce);
77
+ return () => {
78
+ clearTimeout(timeout);
79
+ controller.abort();
80
+ setLoading(false);
81
+ };
82
+ }
83
+ send();
67
84
  return () => {
68
- clearTimeout(timeout);
69
85
  controller.abort();
70
86
  setLoading(false);
71
87
  };
72
- }
73
- send();
74
- return () => {
75
- controller.abort();
76
- setLoading(false);
77
- };
78
- }, [
79
- action,
80
- JSON.stringify(options2.params || params),
81
- reloadTimes,
82
- options2.skip
83
- ]);
88
+ },
89
+ [
90
+ action,
91
+ JSON.stringify(options2.params || params),
92
+ reloadTimes,
93
+ options2.skip
94
+ ]
95
+ );
84
96
  return {
85
97
  action,
86
98
  params,
@@ -120,14 +132,20 @@ function FaasReactClient({
120
132
  setData
121
133
  });
122
134
  const [loaded, setLoaded] = useState(false);
123
- useEffect(function() {
124
- if (!loaded && !request.loading)
125
- setLoaded(true);
126
- }, [request.loading]);
127
- useEffect(function() {
128
- if (onDataChange)
129
- onDataChange(request);
130
- }, [JSON.stringify(request.data)]);
135
+ useEffect(
136
+ function() {
137
+ if (!loaded && !request.loading)
138
+ setLoaded(true);
139
+ },
140
+ [request.loading]
141
+ );
142
+ useEffect(
143
+ function() {
144
+ if (onDataChange)
145
+ onDataChange(request);
146
+ },
147
+ [JSON.stringify(request.data)]
148
+ );
131
149
  if (loaded) {
132
150
  if (children)
133
151
  return cloneElement(children, request);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@faasjs/react",
3
- "version": "0.0.3-beta.107",
3
+ "version": "0.0.3-beta.108",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -22,7 +22,7 @@
22
22
  "dist"
23
23
  ],
24
24
  "dependencies": {
25
- "@faasjs/browser": "0.0.3-beta.107"
25
+ "@faasjs/browser": "0.0.3-beta.108"
26
26
  },
27
27
  "peerDependencies": {
28
28
  "react": "*"