reqforge 0.5.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,41 @@ npm install reqforge
11
11
  ## Quick Start
12
12
 
13
13
  ```javascript
14
- const { config, utils, request, Logger } = 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
- // Make a GET request
16
+ // Make a request with error handling
21
17
  request.get('/users')
22
18
  .then(res => res.json())
23
- .then(data => console.log(data));
24
-
25
- // Use custom logger
26
- const logger = new Logger('[MyApp]');
27
- logger.info('Application started');
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
+
39
+ ## Authentication
40
+
41
+ The `auth` module provides:
42
+
43
+ - `login(username, password)` - Login and store token
44
+ - `logout()` - Logout and clear token
45
+ - `getToken()` - Get current auth token
46
+ - `setToken(token)` - Set auth token manually
47
+ - `isAuthenticated()` - Check if authenticated
48
+
30
49
  ## Request Methods
31
50
 
32
51
  The `request` module provides:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "reqforge",
3
- "version": "0.5.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/auth.js ADDED
@@ -0,0 +1,66 @@
1
+ /**
2
+ * Authentication module for ReqForge
3
+ * @module auth
4
+ */
5
+
6
+ const { sendRequest } = require('./request');
7
+
8
+ let authToken = null;
9
+
10
+ /**
11
+ * Login with username and password
12
+ * @param {string} username - Username
13
+ * @param {string} password - Password
14
+ * @returns {Promise<Object>} Login response with token
15
+ */
16
+ function login(username, password) {
17
+ return sendRequest('/login', { username, password }, { method: 'POST' })
18
+ .then(response => response.json())
19
+ .then(data => {
20
+ authToken = data.token;
21
+ return data;
22
+ });
23
+ }
24
+
25
+ /**
26
+ * Logout and clear auth token
27
+ * @returns {Promise<void>}
28
+ */
29
+ function logout() {
30
+ return sendRequest('/logout', {}, { method: 'POST' })
31
+ .then(() => {
32
+ authToken = null;
33
+ });
34
+ }
35
+
36
+ /**
37
+ * Get current auth token
38
+ * @returns {string|null} Current auth token
39
+ */
40
+ function getToken() {
41
+ return authToken;
42
+ }
43
+
44
+ /**
45
+ * Set auth token
46
+ * @param {string} token - Auth token to set
47
+ */
48
+ function setToken(token) {
49
+ authToken = token;
50
+ }
51
+
52
+ /**
53
+ * Check if user is authenticated
54
+ * @returns {boolean} True if authenticated
55
+ */
56
+ function isAuthenticated() {
57
+ return authToken !== null;
58
+ }
59
+
60
+ module.exports = {
61
+ login,
62
+ logout,
63
+ getToken,
64
+ setToken,
65
+ isAuthenticated
66
+ };
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
@@ -7,10 +7,14 @@ const config = require('./config');
7
7
  const utils = require('./utils');
8
8
  const request = require('./request');
9
9
  const Logger = require('./logger');
10
+ const auth = require('./auth');
11
+ const errors = require('./errors');
10
12
 
11
13
  module.exports = {
12
14
  config,
13
15
  utils,
14
16
  request,
15
- Logger
17
+ Logger,
18
+ auth,
19
+ errors
16
20
  };