@fgrzl/fetch 1.0.0-ci.11 โ 1.1.0-alpha.10
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 +63 -39
- package/dist/cjs/index.js +1123 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/index.min.js +2 -0
- package/dist/cjs/index.min.js.map +1 -0
- package/dist/index.d.ts +1435 -2
- package/dist/index.js +1076 -16
- package/dist/index.js.map +1 -1
- package/dist/index.min.js +2 -0
- package/dist/index.min.js.map +1 -0
- package/package.json +57 -13
- package/dist/client.d.ts +0 -18
- package/dist/client.js +0 -53
- package/dist/client.js.map +0 -1
- package/dist/csrf.d.ts +0 -7
- package/dist/csrf.js +0 -26
- package/dist/csrf.js.map +0 -1
- package/dist/index.test.d.ts +0 -1
- package/dist/index.test.js +0 -51
- package/dist/index.test.js.map +0 -1
- package/dist/unauthorized.d.ts +0 -5
- package/dist/unauthorized.js +0 -16
- package/dist/unauthorized.js.map +0 -1
package/README.md
CHANGED
|
@@ -3,77 +3,101 @@
|
|
|
3
3
|
|
|
4
4
|
# @fgrzl/fetch
|
|
5
5
|
|
|
6
|
-
A
|
|
6
|
+
A production-ready HTTP client for TypeScript that **just works** out of the box.
|
|
7
7
|
|
|
8
8
|
## โจ Features
|
|
9
9
|
|
|
10
|
+
- **Pit of Success Design**: Simple defaults that just work, customizable when needed
|
|
10
11
|
- Simple API: `api.get('/api/user')`
|
|
11
|
-
- Built-in CSRF token support
|
|
12
|
-
-
|
|
12
|
+
- Built-in CSRF token support (XSRF-TOKEN standard)
|
|
13
|
+
- Smart 401 redirect handling with return URL preservation
|
|
14
|
+
- Retry middleware with configurable strategies
|
|
13
15
|
- Custom middleware support (request/response)
|
|
14
16
|
- TypeScript-first, small and dependency-free
|
|
15
17
|
|
|
16
18
|
## ๐ฆ Installation
|
|
17
19
|
|
|
20
|
+
**Install**
|
|
21
|
+
|
|
18
22
|
```bash
|
|
19
23
|
npm install @fgrzl/fetch
|
|
20
24
|
```
|
|
21
25
|
|
|
22
|
-
|
|
26
|
+
**Use immediately** - no configuration required:
|
|
23
27
|
|
|
24
28
|
```ts
|
|
25
29
|
import api from "@fgrzl/fetch";
|
|
26
30
|
|
|
27
|
-
|
|
31
|
+
// It just works! ๐
|
|
32
|
+
const users = await api.get("/api/users");
|
|
33
|
+
const newUser = await api.post("/api/users", { name: "John" });
|
|
34
|
+
|
|
35
|
+
// Built-in error handling
|
|
36
|
+
if (users.ok) {
|
|
37
|
+
console.log("Users:", users.data);
|
|
38
|
+
} else {
|
|
39
|
+
console.error("Error:", users.error?.message);
|
|
40
|
+
}
|
|
28
41
|
```
|
|
29
42
|
|
|
30
|
-
|
|
43
|
+
**Need custom config?** Add it when you need it:
|
|
31
44
|
|
|
32
45
|
```ts
|
|
33
|
-
import { FetchClient } from "
|
|
34
|
-
import { useCSRF } from "./csrf";
|
|
35
|
-
import { useUnauthorized } from "./unauthorized";
|
|
36
|
-
|
|
37
|
-
const api = new FetchClient({
|
|
38
|
-
credentials: "same-origin",
|
|
39
|
-
});
|
|
46
|
+
import { FetchClient, useAuthentication } from "@fgrzl/fetch";
|
|
40
47
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
headerName: "X-CSRF-Token",
|
|
48
|
+
const authClient = useAuthentication(new FetchClient(), {
|
|
49
|
+
tokenProvider: () => localStorage.getItem("token") || "",
|
|
44
50
|
});
|
|
45
51
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
52
|
+
// Smart defaults - just works
|
|
53
|
+
useCSRF(client);
|
|
54
|
+
useAuthorization(client); // Redirects to /login with return URL
|
|
55
|
+
useRetry(client);
|
|
56
|
+
|
|
57
|
+
// All requests now return FetchResponse<T>
|
|
58
|
+
interface User {
|
|
59
|
+
id: number;
|
|
60
|
+
name: string;
|
|
61
|
+
}
|
|
62
|
+
const userResponse = await client.get<User>("/api/user");
|
|
63
|
+
if (userResponse.ok) {
|
|
64
|
+
console.log(userResponse.data.name); // Typed access to data
|
|
65
|
+
} else {
|
|
66
|
+
console.error(`Failed with status ${userResponse.status}`);
|
|
67
|
+
}
|
|
49
68
|
```
|
|
50
69
|
|
|
51
|
-
##
|
|
70
|
+
## โจ What You Get Out of the Box
|
|
52
71
|
|
|
53
|
-
|
|
72
|
+
- **Zero Configuration** - Works immediately with smart defaults
|
|
73
|
+
- **CSRF Protection** - Automatic XSRF-TOKEN handling
|
|
74
|
+
- **Retry Logic** - Exponential backoff for failed requests
|
|
75
|
+
- **Request Logging** - Built-in observability
|
|
76
|
+
- **TypeScript First** - Full type safety and IntelliSense
|
|
77
|
+
- **Middleware System** - Composable and extensible
|
|
54
78
|
|
|
55
|
-
|
|
56
|
-
client.useRequestMiddleware(async (req, url) => {
|
|
57
|
-
return [{ ...req, headers: { ...req.headers, "X-Debug": "true" } }, url];
|
|
58
|
-
});
|
|
59
|
-
```
|
|
79
|
+
## ๐ Documentation
|
|
60
80
|
|
|
61
|
-
|
|
81
|
+
Ready to go deeper? Check out our comprehensive guides:
|
|
62
82
|
|
|
63
|
-
|
|
83
|
+
- **[Getting Started](docs/getting-started.md)** - Installation and basic usage
|
|
84
|
+
- **[Configuration](docs/configuration.md)** - Advanced client setup
|
|
85
|
+
- **[Middleware](docs/middleware.md)** - Authentication, caching, retries
|
|
86
|
+
- **[Error Handling](docs/error-handling.md)** - Robust error management
|
|
87
|
+
- **[TypeScript Guide](docs/typescript.md)** - Type-safe API calls
|
|
88
|
+
- **[Troubleshooting](docs/troubleshooting.md)** - Common issues and solutions
|
|
64
89
|
|
|
65
|
-
|
|
66
|
-
- CSRF token from `XSRF-TOKEN` cookie
|
|
67
|
-
- 401 redirect to `/login?returnTo=...`
|
|
90
|
+
## ๐๏ธ Architecture
|
|
68
91
|
|
|
69
|
-
|
|
92
|
+
Built on a **"pit of success"** philosophy where:
|
|
70
93
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
94
|
+
- Simple things are simple (`api.get("/path")`)
|
|
95
|
+
- Complex things are possible (custom middleware stacks)
|
|
96
|
+
- TypeScript guides you to correct usage
|
|
97
|
+
- Smart defaults handle 80% of use cases
|
|
74
98
|
|
|
75
|
-
|
|
99
|
+
[Learn more about our architecture โ](docs/architecture.md)
|
|
76
100
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
101
|
+
## License
|
|
102
|
+
|
|
103
|
+
MIT
|