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

    Function generateRequestHash

    • Generates a unique hash for a request based on its URL, method, headers, and body. This hash can be used to identify and track unique requests.

      Parameters

      • input: URL | RequestInfo

        The request URL or Request object

      • Optionaloptions: RequestInit

        Optional request configuration

      Returns Promise<string>

      A promise that resolves to a unique hash string

      // With URL string
      const hash1 = await generateRequestHash("https://api.example.com/data", {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({ name: "test" })
      });

      // With Request object
      const request = new Request("https://api.example.com/data", {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({ name: "test" })
      });
      const hash2 = await generateRequestHash(request);
      export async function generateRequestHash(
      input: RequestInfo | URL,
      options?: RequestInit,
      ): Promise<string> {
      const url = input instanceof Request ? input.url : String(input);
      const method = input instanceof Request ? input.method : options?.method || 'GET';
      const headers = input instanceof Request ? input.headers : options?.headers || {};
      const body = input instanceof Request ? input.body : options?.body || '';
      const contentType =
      input instanceof Request
      ? input.headers.get('content-type') || ''
      : ((headers instanceof Headers
      ? headers.get('content-type')
      : Reflect.get(headers, 'content-type')) ?? '');

      const data = {
      url,
      method,
      headers,
      body,
      contentType,
      };

      const dataString = JSON.stringify(data);
      return generateSimpleHash(dataString);
      }