Skip to content

Commit a35f6dc

Browse files
committed
Update readme and docs
1 parent 2dca2db commit a35f6dc

File tree

5 files changed

+205
-349
lines changed

5 files changed

+205
-349
lines changed

docs/.vitepress/config.mts

Lines changed: 0 additions & 64 deletions
This file was deleted.

docs/.vitepress/config.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@ export default defineConfig({
77
base: "/",
88
ignoreDeadLinks: true,
99
head: [
10-
["link", {
11-
rel: "icon",
12-
href:
13-
"https://raw.githubusercontent.com/FoundatioFx/Foundatio/main/media/foundatio-icon.png",
14-
type: "image/png",
15-
}],
10+
[
11+
"link",
12+
{
13+
rel: "icon",
14+
href:
15+
"https://raw.githubusercontent.com/FoundatioFx/Foundatio/main/media/foundatio-icon.png",
16+
type: "image/png",
17+
},
18+
],
1619
["meta", { name: "theme-color", content: "#3c8772" }],
1720
],
1821
themeConfig: {
@@ -25,6 +28,7 @@ export default defineConfig({
2528
siteTitle: "FetchClient",
2629
nav: [
2730
{ text: "Guide", link: "/guide/what-is-fetchclient" },
31+
{ text: "API", link: "https://jsr.io/@foundatiofx/fetchclient/doc" },
2832
{ text: "GitHub", link: "https://github.com/FoundatioFx/FetchClient" },
2933
],
3034
sidebar: {
@@ -38,6 +42,8 @@ export default defineConfig({
3842
},
3943
{ text: "Getting Started", link: "/guide/getting-started" },
4044
{ text: "Features", link: "/guide/features" },
45+
{ text: "Usage Examples", link: "/guide/usage-examples" },
46+
{ text: "Contributing", link: "/guide/contributing" },
4147
],
4248
},
4349
],

docs/guide/contributing.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Contributing
2+
3+
## Development
4+
5+
- Run tests: `deno test --allow-net`
6+
- Lint: `deno lint`
7+
- Format: `deno fmt`
8+
- Type check: `deno check scripts/*.ts *.ts src/*.ts`
9+
- Build npm package: `deno run -A scripts/build.ts`
10+
11+
## Docs
12+
13+
See `docs/`:
14+
15+
- Dev: `npm run dev`
16+
- Build: `npm run build`
17+
- Preview: `npm run preview`
18+
19+
## Links
20+
21+
- API Reference: <https://jsr.io/@foundatiofx/fetchclient/doc>
22+
- Tests: `src/FetchClient.test.ts`
23+
24+
## License
25+
26+
MIT © Foundatio

docs/guide/usage-examples.md

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
# Usage Examples
2+
3+
This page consolidates common examples from the README.
4+
5+
## Model Validator
6+
7+
```ts
8+
import {
9+
FetchClient,
10+
ProblemDetails,
11+
setModelValidator,
12+
} from "@foundatiofx/fetchclient";
13+
14+
setModelValidator(async (data: object | null) => {
15+
// use zod or any other validator
16+
const problem = new ProblemDetails();
17+
const d = data as { password: string };
18+
if (d?.password?.length < 6) {
19+
problem.errors.password = [
20+
"Password must be longer than or equal to 6 characters.",
21+
];
22+
}
23+
return problem;
24+
});
25+
26+
const client = new FetchClient();
27+
const data = { email: "test@test", password: "test" };
28+
29+
const response = await client.postJSON(
30+
"https://jsonplaceholder.typicode.com/todos/1",
31+
data,
32+
);
33+
34+
if (!response.ok) {
35+
console.log(response.problem.detail);
36+
}
37+
```
38+
39+
## Caching
40+
41+
```ts
42+
import { FetchClient } from "@foundatiofx/fetchclient";
43+
44+
type Todo = { userId: number; id: number; title: string; completed: boolean };
45+
46+
const client = new FetchClient();
47+
const response = await client.getJSON<Todo>(
48+
`https://jsonplaceholder.typicode.com/todos/1`,
49+
{
50+
cacheKey: ["todos", "1"],
51+
cacheDuration: 1000 * 60, // 1 minute
52+
},
53+
);
54+
55+
// Invalidate programmatically
56+
client.cache.delete(["todos", "1"]);
57+
```
58+
59+
## Rate Limiting
60+
61+
```ts
62+
import { FetchClient, useRateLimit } from "@foundatiofx/fetchclient";
63+
64+
useRateLimit({ maxRequests: 100, windowSeconds: 60 });
65+
66+
const client = new FetchClient();
67+
await client.getJSON(`https://api.example.com/data`);
68+
```
69+
70+
## Request Timeout & Cancellation
71+
72+
```ts
73+
import { FetchClient } from "@foundatiofx/fetchclient";
74+
75+
const client = new FetchClient();
76+
77+
// Timeout per request
78+
await client.getJSON(`https://api.example.com/data`, { timeout: 5000 });
79+
80+
// AbortSignal
81+
const controller = new AbortController();
82+
setTimeout(() => controller.abort(), 1000);
83+
84+
await client.getJSON(`https://api.example.com/data`, {
85+
signal: controller.signal,
86+
});
87+
```
88+
89+
## Error Handling
90+
91+
```ts
92+
import { FetchClient } from "@foundatiofx/fetchclient";
93+
94+
const client = new FetchClient();
95+
96+
try {
97+
await client.getJSON(`https://api.example.com/data`);
98+
} catch (error) {
99+
if ((error as any).problem) {
100+
console.log((error as any).problem.title);
101+
console.log((error as any).problem.detail);
102+
}
103+
}
104+
105+
// Or handle specific status codes
106+
await client.getJSON(`https://api.example.com/data`, {
107+
expectedStatusCodes: [404, 500],
108+
errorCallback: (response) => {
109+
if (response.status === 404) {
110+
console.log("Resource not found");
111+
return true; // Don't throw
112+
}
113+
},
114+
});
115+
```
116+
117+
## Authentication
118+
119+
```ts
120+
import { FetchClient, setAccessTokenFunc } from "@foundatiofx/fetchclient";
121+
122+
setAccessTokenFunc(() => localStorage.getItem("token"));
123+
124+
const client = new FetchClient();
125+
await client.getJSON(`https://api.example.com/data`);
126+
// Authorization: Bearer <token>
127+
```
128+
129+
## Base URL
130+
131+
```ts
132+
import { FetchClient, setBaseUrl } from "@foundatiofx/fetchclient";
133+
134+
setBaseUrl("https://api.example.com");
135+
136+
const client = new FetchClient();
137+
await client.getJSON(`/users/123`);
138+
// Requests to https://api.example.com/users/123
139+
```
140+
141+
## Loading State
142+
143+
```ts
144+
import { FetchClient } from "@foundatiofx/fetchclient";
145+
146+
const client = new FetchClient();
147+
148+
client.loading.on((isLoading) => {
149+
console.log(`Loading: ${isLoading}`);
150+
});
151+
152+
console.log(client.isLoading);
153+
console.log(client.requestCount);
154+
```

0 commit comments

Comments
 (0)