The Response object to validate
Type predicate indicating if the response contains HTML content
// Valid HTML response
const htmlResponse = new Response('<html><body>Hello</body></html>', {
headers: { 'Content-Type': 'text/html' }
});
if (isHtmlResponse(htmlResponse)) {
const text = await htmlResponse.text();
console.log('HTML content:', text);
}
// Non-HTML response
const jsonResponse = new Response('{"data": "test"}', {
headers: { 'Content-Type': 'application/json' }
});
if (!isHtmlResponse(jsonResponse)) {
console.log('Not an HTML response');
}
export function isHtmlResponse(response: unknown): response is Response {
if (!isHttpResponse(response)) return false;
const contentType = response.headers.get("Content-Type");
return (
contentType !== null &&
(contentType.includes("text/") ||
contentType.includes("application/xhtml+xml") ||
contentType.includes("json-amazonui-streaming"))
);
}
Type guard to validate if a Response object contains HTML content. Checks both the Content-Type header and ensures it's a valid Response object.