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:
		
							
								
								
									
										60
									
								
								test/test.all.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										60
									
								
								test/test.all.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,60 @@ | ||||
| import { expect, tap } from '@git.zone/tstest/tapbundle'; | ||||
| import { webrequest } from '../ts/index.js'; | ||||
|  | ||||
| // Simple smoke tests for v4 API | ||||
|  | ||||
| // Test 1: Basic fetch-compatible API | ||||
| tap.test('should work as fetch replacement', async () => { | ||||
|   const response = await webrequest('https://httpbin.org/get', { | ||||
|     method: 'GET', | ||||
|   }); | ||||
|   expect(response).toBeInstanceOf(Response); | ||||
|   expect(response.ok).toEqual(true); | ||||
|   console.log('API response status:', response.status); | ||||
| }); | ||||
|  | ||||
| // Test 2: JSON convenience method | ||||
| tap.test('should support getJson convenience method', async () => { | ||||
|   interface HttpBinResponse { | ||||
|     url: string; | ||||
|     headers: Record<string, string>; | ||||
|   } | ||||
|  | ||||
|   const data = await webrequest.getJson<HttpBinResponse>('https://httpbin.org/get'); | ||||
|   console.log('getJson url:', data.url); | ||||
|   expect(data).toHaveProperty('url'); | ||||
|   expect(data).toHaveProperty('headers'); | ||||
| }); | ||||
|  | ||||
| // Test 3: POST with JSON | ||||
| tap.test('should support postJson', async () => { | ||||
|   interface PostResponse { | ||||
|     json: any; | ||||
|     url: string; | ||||
|   } | ||||
|  | ||||
|   const data = await webrequest.postJson<PostResponse>( | ||||
|     'https://httpbin.org/post', | ||||
|     { test: 'data' } | ||||
|   ); | ||||
|   expect(data).toHaveProperty('url'); | ||||
|   expect(data).toHaveProperty('json'); | ||||
|   console.log('postJson works'); | ||||
| }); | ||||
|  | ||||
| // Test 4: Caching | ||||
| tap.test('should support caching', async () => { | ||||
|   const data1 = await webrequest.getJson('https://httpbin.org/get', { | ||||
|     cacheStrategy: 'cache-first' | ||||
|   }); | ||||
|  | ||||
|   const data2 = await webrequest.getJson('https://httpbin.org/get', { | ||||
|     cacheStrategy: 'cache-first' | ||||
|   }); | ||||
|  | ||||
|   expect(data1).toHaveProperty('url'); | ||||
|   expect(data2).toHaveProperty('url'); | ||||
|   console.log('Caching works'); | ||||
| }); | ||||
|  | ||||
| export default tap.start(); | ||||
		Reference in New Issue
	
	Block a user