A light-weight module that brings Fetch API to Node.js
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Utility functions for HTTP status code validation and common operations with node-fetch.
Utility function to check if an HTTP status code represents a redirect response.
/**
* Check if HTTP status code is a redirect
* @param code - HTTP status code to check
* @returns True if code is 301, 302, 303, 307, or 308
*/
function isRedirect(code: number): boolean;Usage Examples:
import { isRedirect } from 'node-fetch';
// Check redirect status codes
console.log(isRedirect(301)); // true - Moved Permanently
console.log(isRedirect(302)); // true - Found
console.log(isRedirect(303)); // true - See Other
console.log(isRedirect(307)); // true - Temporary Redirect
console.log(isRedirect(308)); // true - Permanent Redirect
// Non-redirect status codes
console.log(isRedirect(200)); // false - OK
console.log(isRedirect(404)); // false - Not Found
console.log(isRedirect(500)); // false - Internal Server Error
// Use in response handling
const response = await fetch('https://example.com/some-endpoint');
if (isRedirect(response.status)) {
console.log('Response is a redirect');
const location = response.headers.get('location');
console.log('Redirect location:', location);
} else {
console.log('Response is not a redirect');
}
// Custom redirect handling logic
async function fetchWithRedirectInfo(url) {
const response = await fetch(url, { redirect: 'manual' });
return {
response,
isRedirect: isRedirect(response.status),
location: response.headers.get('location')
};
}
const result = await fetchWithRedirectInfo('https://github.com/nodejs/node');
if (result.isRedirect) {
console.log(`Redirect detected: ${response.url} -> ${result.location}`);
}The isRedirect function checks for the following HTTP status codes:
type RedirectStatusCode = 301 | 302 | 303 | 307 | 308;Integration with fetch redirect options:
// Manual redirect handling with isRedirect
const response = await fetch('https://example.com', { redirect: 'manual' });
if (isRedirect(response.status)) {
const location = response.headers.get('location');
// Custom logic for different redirect types
switch (response.status) {
case 301:
case 308:
console.log('Permanent redirect to:', location);
break;
case 302:
case 307:
console.log('Temporary redirect to:', location);
break;
case 303:
console.log('See other at:', location);
break;
}
}