@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 CHANGED
@@ -3,77 +3,101 @@
3
3
 
4
4
  # @fgrzl/fetch
5
5
 
6
- A lightweight, middleware-friendly fetch client for TypeScript projects.
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
- - Automatic 401 redirect handling
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
- ## ๐Ÿš€ Quick Start
26
+ **Use immediately** - no configuration required:
23
27
 
24
28
  ```ts
25
29
  import api from "@fgrzl/fetch";
26
30
 
27
- const data = await api.get("/api/user");
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
- Or create a custom instance:
43
+ **Need custom config?** Add it when you need it:
31
44
 
32
45
  ```ts
33
- import { FetchClient } from "./client";
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
- useCSRF(api, {
42
- cookieName: "csrf_token",
43
- headerName: "X-CSRF-Token",
48
+ const authClient = useAuthentication(new FetchClient(), {
49
+ tokenProvider: () => localStorage.getItem("token") || "",
44
50
  });
45
51
 
46
- useUnauthorized(api, {
47
- loginPath: "/login",
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
- ## ๐Ÿงฉ Middleware
70
+ ## โœจ What You Get Out of the Box
52
71
 
53
- Add your own middleware:
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
- ```ts
56
- client.useRequestMiddleware(async (req, url) => {
57
- return [{ ...req, headers: { ...req.headers, "X-Debug": "true" } }, url];
58
- });
59
- ```
79
+ ## ๐Ÿ“š Documentation
60
80
 
61
- ## ๐Ÿ” CSRF + 401 Handling
81
+ Ready to go deeper? Check out our comprehensive guides:
62
82
 
63
- The default export is pre-configured with:
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
- - `credentials: 'same-origin'`
66
- - CSRF token from `XSRF-TOKEN` cookie
67
- - 401 redirect to `/login?returnTo=...`
90
+ ## ๐Ÿ—๏ธ Architecture
68
91
 
69
- ## ๐Ÿงช Testing
92
+ Built on a **"pit of success"** philosophy where:
70
93
 
71
- ```bash
72
- npm run test
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
- ## ๐Ÿ›  Build
99
+ [Learn more about our architecture โ†’](docs/architecture.md)
76
100
 
77
- ```bash
78
- npm run build
79
- ```
101
+ ## License
102
+
103
+ MIT