async-fetch 0.1.7 → 0.1.8

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
@@ -10,7 +10,7 @@ $ npm i async-fetch
10
10
 
11
11
  ## Usage
12
12
 
13
- Provide your config and handle the response.
13
+ Provide your request config and handle the response.
14
14
 
15
15
  ```javascript
16
16
  import React from "react";
@@ -43,7 +43,7 @@ return pending ? (
43
43
  The minimum requirement for the hook is either a url string or an object with a url property.
44
44
 
45
45
  | Key | Type | Definition | Default |
46
- |----------------|----------|----------------------------------------------------------------------------------------------------------------|---------|
46
+ | -------------- | -------- | -------------------------------------------------------------------------------------------------------------- | ------- |
47
47
  | url | String | URL to send request to. | |
48
48
  | query | Object | Query parameters to include in the request (alt key name: "params"). | |
49
49
  | data | Object | Data object to include in the request body. | |
@@ -51,7 +51,7 @@ The minimum requirement for the hook is either a url string or an object with a
51
51
  | onStart | Function | Callback function to run before the request is sent. | |
52
52
  | onSuccess | Function | Callback function to run after the response has been parsed. The parsed response is available in the callback. | |
53
53
  | onFail | Function | Callback function to run when the request responds with an error. The error is available in the callback. | |
54
- | onFinish | Function | Callback function to run when after the request has completed, regardless of success or failure. | |
54
+ | onFinish | Function | Callback function to run after the request has completed, regardless of success or failure. | |
55
55
  | useEffect | Array | Dependency array for the useEffect. | [] |
56
56
  | ignoreEffect | Boolean | Condition where if true the request won't send unless called using the sendRequest OUT property. | |
57
57
  | poll | Number | Time interval (in milliseconds) for polling. | |
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "async-fetch",
3
- "version": "0.1.7",
3
+ "version": "0.1.8",
4
4
  "description": "Asynchronously use fetch for requests within React components.",
5
5
  "main": "useAsyncFetch.js",
6
6
  "scripts": {
package/useAsyncFetch.js CHANGED
@@ -15,22 +15,24 @@ function useAsyncFetch(props, fetchProps = {}) {
15
15
  onFail,
16
16
  onFinish,
17
17
  ignoreEffect,
18
- useEffect: useEffectDependency = [],
18
+ useEffect: useEffectDependencies = [],
19
19
  poll,
20
20
  ...fetchProps2
21
- } = props && props.constructor === Object ? props : {};
21
+ } = props instanceof Object ? props : {};
22
22
 
23
- if (props && props.constructor === String) url = props;
23
+ if (typeof props === "string") url = props;
24
24
 
25
25
  const [pending1, setPending1] = useState();
26
+
26
27
  const [pending2, setPending2] = useState();
28
+
27
29
  const [error, setError] = useState();
30
+
28
31
  const [data, setData] = useState();
32
+
29
33
  const [unmounted, setUnmounted] = useState();
30
34
 
31
- function cancelActiveRequest() {
32
- if (controller && controller.abort) controller.abort();
33
- }
35
+ const cancelActiveRequest = () => controller?.abort?.();
34
36
 
35
37
  async function sendRequest() {
36
38
  try {
@@ -38,17 +40,6 @@ function useAsyncFetch(props, fetchProps = {}) {
38
40
 
39
41
  controller = new AbortController();
40
42
 
41
- const options = {
42
- signal: controller && controller.signal,
43
- ...fetchProps,
44
- ...fetchProps2,
45
- };
46
-
47
- if (query || params)
48
- url += "?" + new URLSearchParams(query || params).toString();
49
-
50
- if (requestData) options.body = JSON.stringify(requestData);
51
-
52
43
  if (!unmounted) {
53
44
  if (pending1) {
54
45
  setPending2(true);
@@ -57,7 +48,15 @@ function useAsyncFetch(props, fetchProps = {}) {
57
48
  if (onStart) onStart();
58
49
  }
59
50
 
60
- const response = await fetch(url, options);
51
+ if (query || params)
52
+ url += "?" + new URLSearchParams(query || params).toString();
53
+
54
+ const response = await fetch(url, {
55
+ signal: controller?.signal,
56
+ body: requestData && JSON.stringify(requestData),
57
+ ...fetchProps,
58
+ ...fetchProps2,
59
+ });
61
60
 
62
61
  if (!response.ok)
63
62
  throw new Error(response.statusText || response.status.toString());
@@ -82,10 +81,9 @@ function useAsyncFetch(props, fetchProps = {}) {
82
81
  }
83
82
  }
84
83
  }
85
-
86
84
  useEffect(() => {
87
85
  if (ignoreEffect !== true) sendRequest();
88
- }, useEffectDependency); // eslint-disable-line
86
+ }, useEffectDependencies); // eslint-disable-line
89
87
 
90
88
  useInterval(() => {
91
89
  sendRequest();
package/useInterval.js CHANGED
@@ -1,9 +1,7 @@
1
1
  import { useEffect, useRef } from "react";
2
2
 
3
- const noop = () => {};
4
-
5
3
  function useInterval(callback, poll) {
6
- const savedCallback = useRef(noop);
4
+ const savedCallback = useRef(() => {});
7
5
 
8
6
  useEffect(() => {
9
7
  savedCallback.current = callback;
@@ -13,7 +11,9 @@ function useInterval(callback, poll) {
13
11
  if (!poll) return;
14
12
  const tick = () => savedCallback.current();
15
13
  const id = setInterval(tick, poll);
16
- return () => clearInterval(id);
14
+ return () => {
15
+ clearInterval(id);
16
+ };
17
17
  }, [poll]);
18
18
  }
19
19