or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

body-processing.mderror-handling.mdfile-blob.mdheaders.mdhttp-client.mdindex.mdrequest-response.mdutilities.md

utilities.mddocs/

0

# Utility Functions

1

2

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

3

4

## Capabilities

5

6

### isRedirect Function

7

8

Utility function to check if an HTTP status code represents a redirect response.

9

10

```javascript { .api }

11

/**

12

* Check if HTTP status code is a redirect

13

* @param code - HTTP status code to check

14

* @returns True if code is 301, 302, 303, 307, or 308

15

*/

16

function isRedirect(code: number): boolean;

17

```

18

19

**Usage Examples:**

20

21

```javascript

22

import { isRedirect } from 'node-fetch';

23

24

// Check redirect status codes

25

console.log(isRedirect(301)); // true - Moved Permanently

26

console.log(isRedirect(302)); // true - Found

27

console.log(isRedirect(303)); // true - See Other

28

console.log(isRedirect(307)); // true - Temporary Redirect

29

console.log(isRedirect(308)); // true - Permanent Redirect

30

31

// Non-redirect status codes

32

console.log(isRedirect(200)); // false - OK

33

console.log(isRedirect(404)); // false - Not Found

34

console.log(isRedirect(500)); // false - Internal Server Error

35

36

// Use in response handling

37

const response = await fetch('https://example.com/some-endpoint');

38

39

if (isRedirect(response.status)) {

40

console.log('Response is a redirect');

41

const location = response.headers.get('location');

42

console.log('Redirect location:', location);

43

} else {

44

console.log('Response is not a redirect');

45

}

46

47

// Custom redirect handling logic

48

async function fetchWithRedirectInfo(url) {

49

const response = await fetch(url, { redirect: 'manual' });

50

51

return {

52

response,

53

isRedirect: isRedirect(response.status),

54

location: response.headers.get('location')

55

};

56

}

57

58

const result = await fetchWithRedirectInfo('https://github.com/nodejs/node');

59

if (result.isRedirect) {

60

console.log(`Redirect detected: ${response.url} -> ${result.location}`);

61

}

62

```

63

64

### Supported Redirect Codes

65

66

The `isRedirect` function checks for the following HTTP status codes:

67

68

```javascript { .api }

69

type RedirectStatusCode = 301 | 302 | 303 | 307 | 308;

70

```

71

72

- **301 Moved Permanently**: The resource has been moved permanently to a new location

73

- **302 Found**: The resource temporarily resides at a different location

74

- **303 See Other**: The response can be found at a different location using GET

75

- **307 Temporary Redirect**: The resource temporarily resides at a different location, method must not change

76

- **308 Permanent Redirect**: The resource has been moved permanently, method must not change

77

78

**Integration with fetch redirect options:**

79

80

```javascript

81

// Manual redirect handling with isRedirect

82

const response = await fetch('https://example.com', { redirect: 'manual' });

83

84

if (isRedirect(response.status)) {

85

const location = response.headers.get('location');

86

87

// Custom logic for different redirect types

88

switch (response.status) {

89

case 301:

90

case 308:

91

console.log('Permanent redirect to:', location);

92

break;

93

case 302:

94

case 307:

95

console.log('Temporary redirect to:', location);

96

break;

97

case 303:

98

console.log('See other at:', location);

99

break;

100

}

101

}

102

```