ChemPal Documentation - v0.0.13-beta.5
    Preparing search index...
    • Type guard to validate if a Response object contains JSON content. Checks both the Content-Type header and ensures it's a valid Response object.

      Parameters

      • response: unknown

        The Response object to validate

      Returns response is Response

      Type predicate indicating if the response contains JSON content

      // Valid JSON response
      const jsonResponse = new Response('{"data": "test"}', {
      headers: { 'Content-Type': 'application/json' }
      });
      if (isJsonResponse(jsonResponse)) {
      const data = await jsonResponse.json();
      console.log('JSON data:', data);
      }

      // Non-JSON response
      const htmlResponse = new Response('<html>...</html>', {
      headers: { 'Content-Type': 'text/html' }
      });
      if (!isJsonResponse(htmlResponse)) {
      console.log('Not a JSON response');
      }
      export function isJsonResponse(response: unknown): response is Response {
      if (!isHttpResponse(response)) {
      return false;
      }
      const contentType = response.headers.get("Content-Type");
      return (
      contentType !== null && (contentType.includes("/json") || contentType.includes("/javascript"))
      );
      }