ChemPal Documentation - v1.6.0
    Preparing search index...

    Function generateRequestHash

    • 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.

      Parameters

      • url: string

        The request URL

      • options: RequestInit

        The request options (method, headers, body, etc.)

      Returns Promise<string>

      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);
      }