You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is a script to ease the use of making fetch requests.
3
+
This is a script to ease the use of making fetch requests.
4
4
It features the ability to stop consequetive requests, delay a request and parse querystrings from JSON.
5
5
6
6
## Installation
7
+
7
8
```
8
9
npm install @wezz/fetchmanager
9
10
```
10
11
11
12
## Usage
13
+
12
14
### Initialize Fetch Manager
15
+
13
16
```
14
17
import { FetchManager } from '@wezz/fetchmanager';
15
18
// On document ready
@@ -20,7 +23,9 @@ const data = response.json();
20
23
```
21
24
22
25
## Options
26
+
23
27
When doing a fetch request you should define a options object that looks like this:
28
+
24
29
```
25
30
{
26
31
"key": "unique request key",
@@ -32,52 +37,61 @@ When doing a fetch request you should define a options object that looks like th
32
37
"json": true
33
38
}
34
39
```
35
-
*Note that the option fetchoptions is passed down directly to fetch.*<br/>
40
+
41
+
_Note that the option fetchoptions is passed down directly to fetch._<br/>
36
42
[Click here for the fetch options documentation](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#supplying_request_options)
37
43
38
44
### Key
45
+
39
46
If a key is set then the "signal" will be stored and if another request is being sent with an identical key, the current request will be aborted.
40
47
So if you're doing requests related to a users input (typing in an input field) and sending a request per input, this would stop all the old requests and only deliver the last request made as to stop race conditions.
41
48
42
49
### Url
50
+
43
51
This is the URL you wish to fetch, without parameters
44
52
45
53
### Querystring
54
+
46
55
This can be empty, an object that will be parsed to a querystring, or just a string.
47
56
48
57
### Request delay
49
-
This delays a request with the specified amount of miliseconds.
58
+
59
+
This delays a request with the specified amount of miliseconds.
50
60
51
61
### Cache
62
+
52
63
If this is enabled then any request will be cached in either local or session storage. Session storage is default.
53
-
You can either send in a boolean,
64
+
You can either send in a boolean,
54
65
or you can send in an object like this
66
+
55
67
```
56
68
{ permanent: true, cachekey: 'myCacheKey' }
57
69
```
58
70
59
71
If permanent is set to true it will be stored in local storage.
60
72
61
73
### JSON
74
+
62
75
The JSON options will add the header "Content-Type": application/json and it is enabled by default.
63
-
If another content type header has been specified it will not override it.
76
+
If another content type header has been specified it will not override it.
64
77
65
78
Set this to false if you need to fetch any mimetype that is not json.
66
79
67
80
### Fetch options
81
+
68
82
Fetch options is standard fetch opions.
69
83
[Read more about fetch options on MDN](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#supplying_request_options).
70
84
71
-
72
85
## Development & Demo
86
+
73
87
Clone this repo
74
88
Run
75
-
``` npm install ```
76
-
77
-
To run the interactive demo, run
78
-
``` npm run demo ```
89
+
`npm install`
79
90
91
+
To run the interactive demo, run
92
+
`npm run demo`
80
93
81
94
## Breaking changes
82
-
In the Version 1 release the response is no longer automatically parsed to JSON.
95
+
96
+
In the Version 1 release the response is no longer automatically parsed to JSON.
83
97
Consumers of the package will get the regular response object back just as if they had used the native fetch function.
0 commit comments