reqforge 0.6.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -11,22 +11,31 @@ npm install reqforge
11
11
  ## Quick Start
12
12
 
13
13
  ```javascript
14
- const { config, utils, request, Logger, auth } = require('reqforge');
14
+ const { config, request, auth, errors } = require('reqforge');
15
15
 
16
- // Get the base URL for API requests
17
- const baseURL = config.getBaseURL();
18
- console.log(baseURL); // https://api.example.com/v1
19
-
20
- // Login
21
- auth.login('username', 'password')
22
- .then(data => console.log('Logged in:', data.token));
23
-
24
- // Make a GET request
16
+ // Make a request with error handling
25
17
  request.get('/users')
26
18
  .then(res => res.json())
27
- .then(data => console.log(data));
19
+ .then(data => console.log(data))
20
+ .catch(err => {
21
+ if (err instanceof errors.NetworkError) {
22
+ console.error('Network issue:', err.message);
23
+ }
24
+ });
28
25
  ```
29
26
 
27
+ ## Error Handling
28
+
29
+ The `errors` module provides:
30
+
31
+ - `ReqForgeError` - Base error class
32
+ - `NetworkError` - Network request failures
33
+ - `TimeoutError` - Request timeout errors
34
+ - `AuthError` - Authentication failures
35
+ - `ValidationError` - Validation errors
36
+ - `ErrorCodes` - Error code constants
37
+ - `createErrorFromResponse(response)` - Create error from response
38
+
30
39
  ## Authentication
31
40
 
32
41
  The `auth` module provides:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "reqforge",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
4
4
  "description": "A lightweight, flexible HTTP client library with request interception, automatic retry, and logging capabilities, designed for modern web applications.",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
package/src/errors.js ADDED
@@ -0,0 +1,97 @@
1
+ /**
2
+ * Error handling module for ReqForge
3
+ * @module errors
4
+ */
5
+
6
+ /**
7
+ * Base error class for ReqForge
8
+ */
9
+ class ReqForgeError extends Error {
10
+ constructor(message, code) {
11
+ super(message);
12
+ this.name = 'ReqForgeError';
13
+ this.code = code;
14
+ }
15
+ }
16
+
17
+ /**
18
+ * Network error
19
+ */
20
+ class NetworkError extends ReqForgeError {
21
+ constructor(message = 'Network request failed') {
22
+ super(message, 'NETWORK_ERROR');
23
+ this.name = 'NetworkError';
24
+ }
25
+ }
26
+
27
+ /**
28
+ * Timeout error
29
+ */
30
+ class TimeoutError extends ReqForgeError {
31
+ constructor(message = 'Request timeout') {
32
+ super(message, 'TIMEOUT_ERROR');
33
+ this.name = 'TimeoutError';
34
+ }
35
+ }
36
+
37
+ /**
38
+ * Authentication error
39
+ */
40
+ class AuthError extends ReqForgeError {
41
+ constructor(message = 'Authentication failed') {
42
+ super(message, 'AUTH_ERROR');
43
+ this.name = 'AuthError';
44
+ }
45
+ }
46
+
47
+ /**
48
+ * Validation error
49
+ */
50
+ class ValidationError extends ReqForgeError {
51
+ constructor(message = 'Validation failed') {
52
+ super(message, 'VALIDATION_ERROR');
53
+ this.name = 'ValidationError';
54
+ }
55
+ }
56
+
57
+ /**
58
+ * Error codes
59
+ */
60
+ const ErrorCodes = {
61
+ NETWORK_ERROR: 'NETWORK_ERROR',
62
+ TIMEOUT_ERROR: 'TIMEOUT_ERROR',
63
+ AUTH_ERROR: 'AUTH_ERROR',
64
+ VALIDATION_ERROR: 'VALIDATION_ERROR',
65
+ UNKNOWN_ERROR: 'UNKNOWN_ERROR'
66
+ };
67
+
68
+ /**
69
+ * Create error from response
70
+ * @param {Response} response - Fetch response
71
+ * @returns {ReqForgeError} Appropriate error instance
72
+ */
73
+ function createErrorFromResponse(response) {
74
+ if (response.status === 401) {
75
+ return new AuthError('Unauthorized');
76
+ }
77
+ if (response.status === 403) {
78
+ return new AuthError('Forbidden');
79
+ }
80
+ if (response.status === 404) {
81
+ return new NetworkError('Resource not found');
82
+ }
83
+ if (response.status >= 500) {
84
+ return new NetworkError('Server error');
85
+ }
86
+ return new ReqForgeError('Request failed', 'REQUEST_ERROR');
87
+ }
88
+
89
+ module.exports = {
90
+ ReqForgeError,
91
+ NetworkError,
92
+ TimeoutError,
93
+ AuthError,
94
+ ValidationError,
95
+ ErrorCodes,
96
+ createErrorFromResponse
97
+ };
package/src/index.js CHANGED
@@ -8,11 +8,13 @@ const utils = require('./utils');
8
8
  const request = require('./request');
9
9
  const Logger = require('./logger');
10
10
  const auth = require('./auth');
11
+ const errors = require('./errors');
11
12
 
12
13
  module.exports = {
13
14
  config,
14
15
  utils,
15
16
  request,
16
17
  Logger,
17
- auth
18
+ auth,
19
+ errors
18
20
  };