ChemPare Documentation - v0.0.0
    Preparing search index...

    Function getRequestHash

    • Generates a unique hash for a given Request object based on its method, URL path, search parameters, and body. This hash can be used to identify and cache requests. This is used to store the mocked response body at a location with a unique filename (based on the hash). This is necessary because the only other way to differentiate between request really is to store the file in a location that contains the hostname, request method, request path and then some way to differentiate between the request body and parameters. But even trying that causes issues. Trying to dynamically import the file ./responses/${reqUrl.hostname}/${reqUrl.path}/${searchQuery}.json throws the exception:

      Error: Unknown variable dynamic import: ./responses/www.biofuranchem.com/_api/wix-ecommerce-storefront-web/api/acid.json.
      Note that variables only represent file names one level deep.

      So instead, we just use the hash to store the file in a location that is guaranteed to be unique. This is the same way that the python request_cache library works, and that worked pretty well.

      Parameters

      • request: Request

        The Request object to generate a hash for

      Returns RequestHashObject

      A RequestHashObject containing: - hash: The MD5 hash of the request - file: The suggested file location for caching the request - url: The parsed URL object from the request

      const request = new Request('https://api.example.com/data?q=test', {
      method: 'GET'
      });
      const hashObj = getRequestHash(request);
      // Returns: {
      // hash: '01b5190db0c0f8c232d2ad4d8957d5f4',
      // file: 'api.example.com/01b5190db0c0f8c232d2ad4d8957d5f4.json',
      // url: {
      // href: "https://api.example.com/data?q=test",
      // host: "api.example.com",
      // hostname:"api.example.com",
      // pathname: "/data",
      // search: "?q=test"
      // searchParams: URLSearchParams,
      // origin: "https://api.example.com",
      // protocol: "https:",
      // ...other URL properties
      // }
      // }