feat: Implement comprehensive web request handling with caching, retry, and interceptors

- Added cache strategies: NetworkFirst, CacheFirst, StaleWhileRevalidate, NetworkOnly, and CacheOnly.
- Introduced InterceptorManager for managing request, response, and error interceptors.
- Developed RetryManager for handling request retries with customizable backoff strategies.
- Implemented RequestDeduplicator to prevent simultaneous identical requests.
- Created timeout utilities for handling request timeouts.
- Enhanced WebrequestClient to support global interceptors, caching, and retry logic.
- Added convenience methods for common HTTP methods (GET, POST, PUT, DELETE) with JSON handling.
- Established a fetch-compatible webrequest function for seamless integration.
- Defined core type structures for caching, retry options, interceptors, and web request configurations.
This commit is contained in:
2025-10-20 09:59:24 +00:00
parent e228ed4ba0
commit 54afcc46e2
30 changed files with 18693 additions and 4031 deletions

View File

@@ -1,4 +1,5 @@
# @push.rocks/webrequest
securely request from browsers
## Install
@@ -29,7 +30,7 @@ Create an instance of `WebRequest`. You can optionally pass configuration option
```typescript
const webRequest = new WebRequest({
logging: true // Optional: enables logging, defaults to true
logging: true, // Optional: enables logging, defaults to true
});
```
@@ -104,11 +105,11 @@ fetchDataWithCache();
async function requestFromMultipleEndpoints() {
const endpoints = [
'https://api.primary-example.com/data',
'https://api.backup-example.com/data'
'https://api.backup-example.com/data',
];
try {
const response = await webRequest.requestMultiEndpoint(endpoints, {
method: 'GET'
method: 'GET',
});
const data = await response.json();
console.log(data);
@@ -133,7 +134,7 @@ async function customRequest() {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ custom: 'data' }),
timeoutMs: 10000 // Timeout in milliseconds
timeoutMs: 10000, // Timeout in milliseconds
});
if (response.ok) {
const result = await response.json();
@@ -153,14 +154,9 @@ customRequest();
`@push.rocks/webrequest` offers a streamlined, secure way to handle web requests from browsers, supporting various HTTP methods, response caching, and requests to multiple endpoints with fault tolerance. Its TypeScript integration ensures type safety and enhances developer productivity by enabling IntelliSense in supported IDEs.
## License and Legal Information
This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository.
This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository.
**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.