The request URL
The request options (method, headers, body, etc.)
A hexadecimal hash string uniquely identifying the request
const hash = await generateRequestHash('https://api.example.com/data', {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({ query: 'borohydride' }),
});
export async function generateRequestHash(url: string, options: RequestInit): Promise<string> {
const data = {
url,
method: options.method || 'GET',
headers: options.headers || {},
body: options.body || '',
// headers may be a Headers instance or string[][]; only plain-object headers
// expose content-type by index, matching the previous behavior.
contentType: (options.headers as Record<string, string> | undefined)?.['content-type'] || '',
};
const dataString = JSON.stringify(data);
return generateSimpleHash(dataString);
}
Generates a deterministic hash for an HTTP request based on its URL, method, headers, body, and content type. Two requests with identical parameters will always produce the same hash, making this suitable for cache keying and request deduplication.