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

    Function fetchDecorator

    • A decorator function that wraps the native fetch API with additional features:

      • Automatic response parsing based on content type
      • Request hash generation for tracking
      • Error handling
      • Response cloning to prevent body stream consumption

      Parameters

      • input: URL | RequestInfo

        The request URL or Request object

      • Optionalinit: RequestInit

        Optional request configuration

      Returns Promise<FetchDecoratorResponse>

      A promise that resolves to a FetchDecoratorResponse

      Error if the response is not ok (status not in 200-299 range)

      // Basic GET request
      const response = await fetchDecorator("https://api.example.com/data");
      console.log(response.data); // Parsed response data
      console.log(response.requestHash); // Unique request hash

      // POST request with JSON body
      const response = await fetchDecorator("https://api.example.com/data", {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({ name: "test" })
      });

      // Using Request object
      const request = new Request("https://api.example.com/data", {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({ name: "test" })
      });
      const response = await fetchDecorator(request);

      // Handling different response types
      const response = await fetchDecorator("https://api.example.com/data");
      if (response.headers.get("content-type")?.includes("application/json")) {
      // Data is already parsed as JSON
      console.log(response.data);
      } else if (response.headers.get("content-type")?.includes("text/")) {
      // Data is already parsed as text
      console.log(response.data);
      } else {
      // Data is a Blob
      const blob = response.data as Blob;
      // Handle blob data
      }