2
2
3
3
` fetch ` but with super-powers
4
4
5
- - 🖇 Interceptors
5
+ - 🔗 Interceptors
6
6
- 🔐 Strongly typed errors
7
+ - 🔌 Platform adapters
7
8
8
9
## Install
9
10
@@ -19,16 +20,22 @@ npm install fetch-prime fp-ts
19
20
20
21
``` ts
21
22
import * as E from " fp-ts/Either" ;
22
- import { pipe } from " fp-ts/function" ;
23
+ import { fetch } from " fetch-prime/Fetch" ;
24
+ import adapter from " fetch-prime/Adapters/Platform" ;
25
+
26
+ const response = await fetch (" /users" )(adapter );
27
+
28
+ if (E .isRight (response ) && response .right .ok ) {
29
+ const users = await response .json ();
30
+ }
23
31
24
- import { fetch , map } from " fetch-prime/Fetch " ;
32
+ // or
25
33
import { chain } from " fetch-prime/Function" ;
26
- import * as Result from " fetch-prime/Response" ;
27
- import adapter from " fetch-prime/Adapters/Platform" ;
34
+ import * as Response from " fetch-prime/Response" ;
28
35
29
36
const result = await fetch (" /users" )(adapter );
30
- const res = pipe ( result , E .chainW (Result .filterStatusOk ));
31
- const users = await chain (res , (res ) => res .json ());
37
+ const ok = E .chainW (Response .filterStatusOk )( result );
38
+ const users = await chain (ok , (res ) => res .json ());
32
39
```
33
40
34
41
### With interceptor
@@ -41,47 +48,51 @@ const baseURL = "https://reqres.in/api";
41
48
42
49
// our list of interceptors
43
50
const interceptors = Interceptor .of (BaseURL (baseURL ));
51
+ // or
52
+ const interceptors = Interceptor .add (Interceptor .empty (), BaseURL (baseURL ));
44
53
45
54
// make function that executes our interceptors
46
55
const interceptor = Interceptor .make (interceptors );
47
56
48
- // we finally make the HTTP adapter using the native Fetch API
49
- const adapter = interceptor (adapter );
57
+ // we finally make the HTTP adapter
58
+ const intercept_adapter = interceptor (adapter );
50
59
51
- const result = await program ( adapter );
60
+ const response = await fetch ( " /users " )( intercept_adapter );
52
61
```
53
62
54
- ## POST Request
63
+ ## Adapters
64
+
65
+ ` fetch-prime ` provides a default adapter that uses the platform fetch.
55
66
56
67
``` ts
57
- const request = fetch (" /users" , { method: " POST" });
58
- // ...
68
+ import FetchAdapter from " fetch-prime/Adapters/Platform" ;
59
69
```
60
70
71
+ > You can write your own adapter i.e using XMLHTTPRequest
72
+
61
73
## Interceptors
62
74
63
- ` fetch-prime ` ships with default interceptors
75
+ ` fetch-prime ` ships with the following interceptors
64
76
65
77
- Base URL
66
78
- Timeout
67
79
- Logger
68
80
- Status Filter
69
81
- Bearer and Basic authentication
70
82
71
- ### Status Filter
83
+ ### Example
72
84
73
- To avoid manually forking the response into the error and success paths
85
+ Instead of checking if the response is ok i.e 200
74
86
75
87
``` ts
76
- const result = await fetch (" /users" )(adapter );
88
+ const response = await fetch (" /users" )(adapter );
77
89
78
- // equivalent to response.ok ? response.json() : // handle not ok status
79
- const res = pipe (result , E .chainW (Result .filterStatusOk ));
80
-
81
- const users = await chain (res , (res ) => res .json ());
90
+ if (E .isRight (response ) && response .right .ok ) {
91
+ const users = await response .json ();
92
+ }
82
93
```
83
94
84
- We can delegate that to an interceptor. So we can decode the response body without worrying about the OK status
95
+ We can delegate that to a response interceptor that performs that check.
85
96
86
97
``` ts
87
98
const interceptors = Interceptor .of (StatusOK );
@@ -92,10 +103,7 @@ const adapter = interceptor(adapter);
92
103
93
104
const request = await fetch (" /users" )(adapter );
94
105
const users = await chain (request , (res ) => res .json ());
95
-
96
- if (E .isLeft (users ) && users .left instanceof StatusError ) {
97
- // do something with error status response
98
- }
106
+ // ...
99
107
```
100
108
101
109
### Writing your own interceptor
0 commit comments