Skip to content

Commit 523beb2

Browse files
committed
documentation
1 parent 8feec7d commit 523beb2

File tree

2 files changed

+34
-26
lines changed

2 files changed

+34
-26
lines changed

.DS_Store

6 KB
Binary file not shown.

README.md

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
`fetch` but with super-powers
44

5-
- 🖇 Interceptors
5+
- 🔗 Interceptors
66
- 🔐 Strongly typed errors
7+
- 🔌 Platform adapters
78

89
## Install
910

@@ -19,16 +20,22 @@ npm install fetch-prime fp-ts
1920

2021
```ts
2122
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+
}
2331

24-
import { fetch, map } from "fetch-prime/Fetch";
32+
// or
2533
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";
2835

2936
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());
3239
```
3340

3441
### With interceptor
@@ -41,47 +48,51 @@ const baseURL = "https://reqres.in/api";
4148

4249
// our list of interceptors
4350
const interceptors = Interceptor.of(BaseURL(baseURL));
51+
// or
52+
const interceptors = Interceptor.add(Interceptor.empty(), BaseURL(baseURL));
4453

4554
// make function that executes our interceptors
4655
const interceptor = Interceptor.make(interceptors);
4756

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);
5059

51-
const result = await program(adapter);
60+
const response = await fetch("/users")(intercept_adapter);
5261
```
5362

54-
## POST Request
63+
## Adapters
64+
65+
`fetch-prime` provides a default adapter that uses the platform fetch.
5566

5667
```ts
57-
const request = fetch("/users", { method: "POST" });
58-
// ...
68+
import FetchAdapter from "fetch-prime/Adapters/Platform";
5969
```
6070

71+
> You can write your own adapter i.e using XMLHTTPRequest
72+
6173
## Interceptors
6274

63-
`fetch-prime` ships with default interceptors
75+
`fetch-prime` ships with the following interceptors
6476

6577
- Base URL
6678
- Timeout
6779
- Logger
6880
- Status Filter
6981
- Bearer and Basic authentication
7082

71-
### Status Filter
83+
### Example
7284

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
7486

7587
```ts
76-
const result = await fetch("/users")(adapter);
88+
const response = await fetch("/users")(adapter);
7789

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+
}
8293
```
8394

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.
8596

8697
```ts
8798
const interceptors = Interceptor.of(StatusOK);
@@ -92,10 +103,7 @@ const adapter = interceptor(adapter);
92103

93104
const request = await fetch("/users")(adapter);
94105
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+
// ...
99107
```
100108

101109
### Writing your own interceptor

0 commit comments

Comments
 (0)