reqforge 0.5.0 → 0.6.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,32 @@ npm install reqforge
11
11
  ## Quick Start
12
12
 
13
13
  ```javascript
14
- const { config, utils, request, Logger } = require('reqforge');
14
+ const { config, utils, request, Logger, auth } = require('reqforge');
15
15
 
16
16
  // Get the base URL for API requests
17
17
  const baseURL = config.getBaseURL();
18
18
  console.log(baseURL); // https://api.example.com/v1
19
19
 
20
+ // Login
21
+ auth.login('username', 'password')
22
+ .then(data => console.log('Logged in:', data.token));
23
+
20
24
  // Make a GET request
21
25
  request.get('/users')
22
26
  .then(res => res.json())
23
27
  .then(data => console.log(data));
24
-
25
- // Use custom logger
26
- const logger = new Logger('[MyApp]');
27
- logger.info('Application started');
28
28
  ```
29
29
 
30
+ ## Authentication
31
+
32
+ The `auth` module provides:
33
+
34
+ - `login(username, password)` - Login and store token
35
+ - `logout()` - Logout and clear token
36
+ - `getToken()` - Get current auth token
37
+ - `setToken(token)` - Set auth token manually
38
+ - `isAuthenticated()` - Check if authenticated
39
+
30
40
  ## Request Methods
31
41
 
32
42
  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.6.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/index.js CHANGED
@@ -7,10 +7,12 @@ 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');
10
11
 
11
12
  module.exports = {
12
13
  config,
13
14
  utils,
14
15
  request,
15
- Logger
16
+ Logger,
17
+ auth
16
18
  };