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