CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-node-fetch

A light-weight module that brings Fetch API to Node.js

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

utilities.mddocs/

Utility Functions

Utility functions for HTTP status code validation and common operations with node-fetch.

Capabilities

isRedirect Function

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}`);
}

Supported Redirect Codes

The isRedirect function checks for the following HTTP status codes:

type RedirectStatusCode = 301 | 302 | 303 | 307 | 308;
  • 301 Moved Permanently: The resource has been moved permanently to a new location
  • 302 Found: The resource temporarily resides at a different location
  • 303 See Other: The response can be found at a different location using GET
  • 307 Temporary Redirect: The resource temporarily resides at a different location, method must not change
  • 308 Permanent Redirect: The resource has been moved permanently, method must not change

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;
  }
}

docs

body-processing.md

error-handling.md

file-blob.md

headers.md

http-client.md

index.md

request-response.md

utilities.md

tile.json