removed http client for a simple fetch wrapper with logging in utils
This commit is contained in:
parent
89cbfb7848
commit
a07a71d92a
36 changed files with 100 additions and 1465 deletions
27
libs/utils/src/fetch.ts
Normal file
27
libs/utils/src/fetch.ts
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
/**
|
||||
* Minimal fetch wrapper with automatic debug logging
|
||||
* Drop-in replacement for native fetch with logging support
|
||||
*/
|
||||
|
||||
export function fetch(
|
||||
input: RequestInfo | URL,
|
||||
init?: RequestInit & { logger?: any }
|
||||
): Promise<Response> {
|
||||
const logger = init?.logger || console;
|
||||
const url = typeof input === 'string' ? input : input instanceof URL ? input.href : input.url;
|
||||
const method = init?.method || 'GET';
|
||||
|
||||
logger.debug('HTTP request', { method, url });
|
||||
|
||||
return globalThis.fetch(input, init).then(response => {
|
||||
logger.debug('HTTP response', {
|
||||
url,
|
||||
status: response.status,
|
||||
ok: response.ok
|
||||
});
|
||||
return response;
|
||||
}).catch(error => {
|
||||
logger.debug('HTTP error', { url, error: error.message });
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
|
|
@ -2,3 +2,5 @@ export * from './calculations/index';
|
|||
export * from './common';
|
||||
export * from './dateUtils';
|
||||
export * from './generic-functions';
|
||||
export * from './fetch';
|
||||
export * from './user-agent';
|
||||
|
|
|
|||
30
libs/utils/src/user-agent.ts
Normal file
30
libs/utils/src/user-agent.ts
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
/**
|
||||
* User Agent utility for generating random user agents
|
||||
*/
|
||||
|
||||
// Simple list of common user agents to avoid external dependency
|
||||
const USER_AGENTS = [
|
||||
// Chrome on Windows
|
||||
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
|
||||
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36',
|
||||
// Chrome on Mac
|
||||
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
|
||||
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36',
|
||||
// Firefox on Windows
|
||||
'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:120.0) Gecko/20100101 Firefox/120.0',
|
||||
'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:119.0) Gecko/20100101 Firefox/119.0',
|
||||
// Firefox on Mac
|
||||
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15) Gecko/20100101 Firefox/120.0',
|
||||
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15) Gecko/20100101 Firefox/119.0',
|
||||
// Safari on Mac
|
||||
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.1 Safari/605.1.15',
|
||||
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Safari/605.1.15',
|
||||
// Edge on Windows
|
||||
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0',
|
||||
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0',
|
||||
];
|
||||
|
||||
export function getRandomUserAgent(): string {
|
||||
const index = Math.floor(Math.random() * USER_AGENTS.length);
|
||||
return USER_AGENTS[index]!;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue