fetchclient 0.42.1-alpha.0.4
Install from the command line:
Learn more about npm packages
$ npm install @exceptionless/fetchclient@0.42.1-alpha.0.4
Install via package.json:
"@exceptionless/fetchclient": "0.42.1-alpha.0.4"
About this version
FetchClient is a library that makes it easier to use the fetch API for JSON APIs. It provides the following features:
- Makes fetch easier to use for JSON APIs
- Automatic model validation
- Caching
- Middleware
- Rate limiting
- Problem Details support
- Option to parse dates in responses
npm install --save @exceptionless/fetchclient
import { FetchClient } from '@exceptionless/fetchclient';
type Products = {
products: Array<{ id: number; name: string }>;
};
const client = new FetchClient();
const response = await client.getJSON<Products>(
`https://dummyjson.com/products/search?q=iphone&limit=10`,
);
const products = response.data;
import { getJSON } from '@exceptionless/fetchclient';
type Products = {
products: Array<{ id: number; name: string }>;
};
const response = await getJSON<Products>(
`https://dummyjson.com/products/search?q=iphone&limit=10`,
);
const products = response.data;
import { FetchClient, setModelValidator } from '@exceptionless/fetchclient';
setModelValidator(async (data: object | null) => {
// use zod or any other validator
const problem = new ProblemDetails();
const d = data as { password: string };
if (d?.password?.length < 6) {
problem.errors.password = [
"Password must be longer than or equal to 6 characters.",
];
}
return problem;
});
const client = new FetchClient();
const data = {
email: "test@test",
password: "test",
};
const response = await client.postJSON(
"https://jsonplaceholder.typicode.com/todos/1",
data,
);
if (!response.ok) {
// check response problem
console.log(response.problem.detail);
}
import { FetchClient } from '@exceptionless/fetchclient';
type Todo = { userId: number; id: number; title: string; completed: boolean };
const client = new FetchClient();
const response = await client.getJSON<Todo>(
`https://jsonplaceholder.typicode.com/todos/1`,
{
cacheKey: ["todos", "1"],
cacheDuration: 1000 * 60, // expires in 1 minute
}
);
// invalidate programmatically
client.cache.delete(["todos", "1"]);
import { FetchClient, useMiddleware } from '@exceptionless/fetchclient';
type Products = {
products: Array<{ id: number; name: string }>;
};
useMiddleware(async (ctx, next) => {
console.log('starting request')
await next();
console.log('completed request')
});
const client = new FetchClient();
const response = await client.getJSON<Products>(
`https://dummyjson.com/products/search?q=iphone&limit=10`,
);
import { FetchClientProvider } from '@exceptionless/fetchclient';
const provider = new FetchClientProvider();
// Enable rate limiting: max 100 requests per hour
provider.enableRateLimit({
maxRequests: 100,
windowMs: 60 * 60 * 1000, // 1 hour
});
const client = provider.getFetchClient();
// Requests exceeding the limit will receive HTTP 429 responses
const response = await client.getJSON('https://api.example.com/data', {
expectedStatusCodes: [200, 429] // Handle 429 without throwing
});
if (response.status === 429) {
const retryAfter = response.headers.get('Retry-After');
console.log(`Rate limited. Retry after ${retryAfter} seconds`);
}
Also, take a look at the tests:
Run tests:
deno run test
Lint code:
deno lint
Format code:
deno fmt
Type check code:
deno run check
MIT © Exceptionless
Details
- fetchclient
-
exceptionless
- about 2 months ago
- Apache-2.0
- 4 dependencies
Assets
- fetchclient-0.42.1-alpha.0.4.tgz
Download activity
- Total downloads 0
- Last 30 days 0
- Last week 0
- Today 0