stigmergy 1.0.95 → 1.0.97
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 +20 -0
- package/bin/stigmergy +11 -0
- package/docs/http-request-handler.md +289 -0
- package/docs/json-parser.md +102 -0
- package/docs/rest_client.md +144 -0
- package/examples/encryption-example.js +67 -0
- package/examples/json-parser-example.js +120 -0
- package/examples/rest_client_example.js +54 -0
- package/package.json +6 -5
- package/src/auth.js +28 -26
- package/src/auth_command.js +51 -38
- package/src/calculator.js +102 -9
- package/src/core/cli_help_analyzer.js +621 -573
- package/src/core/cli_parameter_handler.js +110 -104
- package/src/core/cli_tools.js +76 -76
- package/src/core/error_handler.js +154 -55
- package/src/core/memory_manager.js +70 -63
- package/src/core/rest_client.js +160 -0
- package/src/core/smart_router.js +130 -117
- package/src/data_encryption.js +143 -0
- package/src/data_structures.js +440 -0
- package/src/deploy.js +38 -32
- package/src/index.js +9 -9
- package/src/main.js +889 -752
- package/src/main_english.js +1292 -887
- package/src/main_fixed.js +1039 -902
- package/src/utils.js +462 -75
- package/src/weatherProcessor.js +78 -55
- package/test/encryption-simple-test.js +110 -0
- package/test/encryption.test.js +129 -0
- package/test/hash_table_test.js +114 -0
- package/test/json-parser-test.js +161 -0
- package/test/rest_client.test.js +85 -0
package/README.md
CHANGED
|
@@ -95,6 +95,26 @@ stigmergy call qwen "translate to English"
|
|
|
95
95
|
stigmergy call copilot "suggest improvements"
|
|
96
96
|
```
|
|
97
97
|
|
|
98
|
+
### User Authentication
|
|
99
|
+
|
|
100
|
+
Stigmergy CLI now supports user authentication to protect your AI tools and configurations:
|
|
101
|
+
|
|
102
|
+
```bash
|
|
103
|
+
# Register a new account
|
|
104
|
+
stigmergy register <username> <password>
|
|
105
|
+
|
|
106
|
+
# Log in to your account
|
|
107
|
+
stigmergy login <username> <password>
|
|
108
|
+
|
|
109
|
+
# Check authentication status
|
|
110
|
+
stigmergy auth-status
|
|
111
|
+
|
|
112
|
+
# Log out of your account
|
|
113
|
+
stigmergy logout
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
Protected commands (like `call`, `setup`, `deploy`, `install`) require authentication before use.
|
|
117
|
+
|
|
98
118
|
### Installation Workflow
|
|
99
119
|
|
|
100
120
|
1. **Scan**: `stigmergy scan` - Detects installed AI CLI tools
|
package/bin/stigmergy
CHANGED
|
@@ -6,6 +6,17 @@
|
|
|
6
6
|
const path = require('path');
|
|
7
7
|
const { spawn } = require('child_process');
|
|
8
8
|
|
|
9
|
+
// Set up global error handlers for the launcher
|
|
10
|
+
process.on('unhandledRejection', (reason, promise) => {
|
|
11
|
+
console.error('[FATAL] Launcher Unhandled Rejection at:', promise, 'reason:', reason);
|
|
12
|
+
process.exit(1);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
process.on('uncaughtException', (error) => {
|
|
16
|
+
console.error('[FATAL] Launcher Uncaught Exception:', error);
|
|
17
|
+
process.exit(1);
|
|
18
|
+
});
|
|
19
|
+
|
|
9
20
|
// Get the path to the main script
|
|
10
21
|
const mainScript = path.join(__dirname, '..', 'src', 'main_english.js');
|
|
11
22
|
|
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
# HTTP Request Handler Documentation
|
|
2
|
+
|
|
3
|
+
This document provides comprehensive documentation for the HTTP request handling functionality implemented in the Stigmergy CLI system.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
The HTTP request handler is implemented as a `RestClient` class that provides a simple interface for making HTTP requests to REST APIs. It supports common HTTP methods (GET, POST, PUT, PATCH, DELETE) and includes features for handling headers, query parameters, and request bodies.
|
|
8
|
+
|
|
9
|
+
## Class: RestClient
|
|
10
|
+
|
|
11
|
+
### Constructor
|
|
12
|
+
|
|
13
|
+
```javascript
|
|
14
|
+
new RestClient(baseURL, defaultHeaders)
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Creates a new REST client instance.
|
|
18
|
+
|
|
19
|
+
**Parameters:**
|
|
20
|
+
- `baseURL` (string): The base URL for all API requests. Defaults to an empty string.
|
|
21
|
+
- `defaultHeaders` (Object): Default headers to include in all requests. Defaults to `{ 'Content-Type': 'application/json' }`.
|
|
22
|
+
|
|
23
|
+
**Example:**
|
|
24
|
+
```javascript
|
|
25
|
+
const client = new RestClient('https://api.example.com');
|
|
26
|
+
const clientWithHeaders = new RestClient('https://api.example.com', {
|
|
27
|
+
'Authorization': 'Bearer token',
|
|
28
|
+
'User-Agent': 'MyApp/1.0'
|
|
29
|
+
});
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
### Methods
|
|
33
|
+
|
|
34
|
+
#### request()
|
|
35
|
+
|
|
36
|
+
```javascript
|
|
37
|
+
async request(method, url, options)
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Makes an HTTP request with the specified method and options.
|
|
41
|
+
|
|
42
|
+
**Parameters:**
|
|
43
|
+
- `method` (string): HTTP method (GET, POST, PUT, PATCH, DELETE, etc.)
|
|
44
|
+
- `url` (string): Request URL (will be appended to baseURL)
|
|
45
|
+
- `options` (Object): Request options
|
|
46
|
+
- `headers` (Object): Additional headers for this request
|
|
47
|
+
- `body` (Object|string): Request body data
|
|
48
|
+
- `params` (Object): Query parameters to append to the URL
|
|
49
|
+
- `timeout` (number): Request timeout in milliseconds (default: 10000)
|
|
50
|
+
|
|
51
|
+
**Returns:**
|
|
52
|
+
Promise that resolves to an object with the following properties:
|
|
53
|
+
- `status` (number): HTTP status code
|
|
54
|
+
- `statusText` (string): HTTP status text
|
|
55
|
+
- `headers` (Object): Response headers
|
|
56
|
+
- `data` (Object|string): Response data (parsed as JSON if possible, otherwise as text)
|
|
57
|
+
|
|
58
|
+
**Throws:**
|
|
59
|
+
Error if the request fails or receives a non-2xx response.
|
|
60
|
+
|
|
61
|
+
**Example:**
|
|
62
|
+
```javascript
|
|
63
|
+
const response = await client.request('GET', '/users', {
|
|
64
|
+
params: { page: 1, limit: 10 },
|
|
65
|
+
headers: { 'X-Custom-Header': 'value' }
|
|
66
|
+
});
|
|
67
|
+
console.log(response.data);
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
#### get()
|
|
71
|
+
|
|
72
|
+
```javascript
|
|
73
|
+
async get(url, options)
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
Makes a GET request.
|
|
77
|
+
|
|
78
|
+
**Parameters:**
|
|
79
|
+
- `url` (string): Request URL
|
|
80
|
+
- `options` (Object): Request options (same as in `request()`)
|
|
81
|
+
|
|
82
|
+
**Returns:**
|
|
83
|
+
Promise that resolves to the response object.
|
|
84
|
+
|
|
85
|
+
**Example:**
|
|
86
|
+
```javascript
|
|
87
|
+
const users = await client.get('/users', { params: { active: true } });
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
#### post()
|
|
91
|
+
|
|
92
|
+
```javascript
|
|
93
|
+
async post(url, data, options)
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
Makes a POST request.
|
|
97
|
+
|
|
98
|
+
**Parameters:**
|
|
99
|
+
- `url` (string): Request URL
|
|
100
|
+
- `data` (Object): Data to send in request body
|
|
101
|
+
- `options` (Object): Request options (same as in `request()`)
|
|
102
|
+
|
|
103
|
+
**Returns:**
|
|
104
|
+
Promise that resolves to the response object.
|
|
105
|
+
|
|
106
|
+
**Example:**
|
|
107
|
+
```javascript
|
|
108
|
+
const newUser = await client.post('/users', {
|
|
109
|
+
name: 'John Doe',
|
|
110
|
+
email: 'john@example.com'
|
|
111
|
+
});
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
#### put()
|
|
115
|
+
|
|
116
|
+
```javascript
|
|
117
|
+
async put(url, data, options)
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
Makes a PUT request.
|
|
121
|
+
|
|
122
|
+
**Parameters:**
|
|
123
|
+
- `url` (string): Request URL
|
|
124
|
+
- `data` (Object): Data to send in request body
|
|
125
|
+
- `options` (Object): Request options (same as in `request()`)
|
|
126
|
+
|
|
127
|
+
**Returns:**
|
|
128
|
+
Promise that resolves to the response object.
|
|
129
|
+
|
|
130
|
+
**Example:**
|
|
131
|
+
```javascript
|
|
132
|
+
const updatedUser = await client.put('/users/123', {
|
|
133
|
+
name: 'Jane Doe',
|
|
134
|
+
email: 'jane@example.com'
|
|
135
|
+
});
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
#### patch()
|
|
139
|
+
|
|
140
|
+
```javascript
|
|
141
|
+
async patch(url, data, options)
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
Makes a PATCH request.
|
|
145
|
+
|
|
146
|
+
**Parameters:**
|
|
147
|
+
- `url` (string): Request URL
|
|
148
|
+
- `data` (Object): Data to send in request body
|
|
149
|
+
- `options` (Object): Request options (same as in `request()`)
|
|
150
|
+
|
|
151
|
+
**Returns:**
|
|
152
|
+
Promise that resolves to the response object.
|
|
153
|
+
|
|
154
|
+
**Example:**
|
|
155
|
+
```javascript
|
|
156
|
+
const partialUpdate = await client.patch('/users/123', {
|
|
157
|
+
lastLogin: new Date().toISOString()
|
|
158
|
+
});
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
#### delete()
|
|
162
|
+
|
|
163
|
+
```javascript
|
|
164
|
+
async delete(url, options)
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
Makes a DELETE request.
|
|
168
|
+
|
|
169
|
+
**Parameters:**
|
|
170
|
+
- `url` (string): Request URL
|
|
171
|
+
- `options` (Object): Request options (same as in `request()`)
|
|
172
|
+
|
|
173
|
+
**Returns:**
|
|
174
|
+
Promise that resolves to the response object.
|
|
175
|
+
|
|
176
|
+
**Example:**
|
|
177
|
+
```javascript
|
|
178
|
+
await client.delete('/users/123');
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
#### setDefaultHeaders()
|
|
182
|
+
|
|
183
|
+
```javascript
|
|
184
|
+
setDefaultHeaders(headers)
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
Sets default headers to be included in all requests.
|
|
188
|
+
|
|
189
|
+
**Parameters:**
|
|
190
|
+
- `headers` (Object): Headers to set as default
|
|
191
|
+
|
|
192
|
+
**Example:**
|
|
193
|
+
```javascript
|
|
194
|
+
client.setDefaultHeaders({
|
|
195
|
+
'Authorization': 'Bearer token',
|
|
196
|
+
'X-API-Key': 'secret-key'
|
|
197
|
+
});
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
#### setAuthorization()
|
|
201
|
+
|
|
202
|
+
```javascript
|
|
203
|
+
setAuthorization(token, type)
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
Sets the Authorization header for all requests.
|
|
207
|
+
|
|
208
|
+
**Parameters:**
|
|
209
|
+
- `token` (string): Authorization token
|
|
210
|
+
- `type` (string): Authorization type (default: 'Bearer')
|
|
211
|
+
|
|
212
|
+
**Example:**
|
|
213
|
+
```javascript
|
|
214
|
+
client.setAuthorization('my-secret-token');
|
|
215
|
+
client.setAuthorization('username:password', 'Basic');
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
## Usage Examples
|
|
219
|
+
|
|
220
|
+
### Basic GET Request
|
|
221
|
+
|
|
222
|
+
```javascript
|
|
223
|
+
const RestClient = require('./src/core/rest_client');
|
|
224
|
+
|
|
225
|
+
const client = new RestClient('https://jsonplaceholder.typicode.com');
|
|
226
|
+
|
|
227
|
+
// Get a list of posts
|
|
228
|
+
const posts = await client.get('/posts', { params: { _limit: 5 } });
|
|
229
|
+
console.log(posts.data);
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
### Creating a Resource
|
|
233
|
+
|
|
234
|
+
```javascript
|
|
235
|
+
const newPost = {
|
|
236
|
+
title: 'My New Post',
|
|
237
|
+
body: 'This is the content',
|
|
238
|
+
userId: 1
|
|
239
|
+
};
|
|
240
|
+
|
|
241
|
+
const createdPost = await client.post('/posts', newPost);
|
|
242
|
+
console.log('Created post with ID:', createdPost.data.id);
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
### Using Authentication
|
|
246
|
+
|
|
247
|
+
```javascript
|
|
248
|
+
// Set authorization header
|
|
249
|
+
client.setAuthorization('your-api-token-here');
|
|
250
|
+
|
|
251
|
+
// All subsequent requests will include the authorization header
|
|
252
|
+
const protectedData = await client.get('/protected-resource');
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
### Custom Headers
|
|
256
|
+
|
|
257
|
+
```javascript
|
|
258
|
+
const client = new RestClient('https://api.example.com', {
|
|
259
|
+
'X-Custom-Header': 'custom-value',
|
|
260
|
+
'User-Agent': 'MyApp/1.0'
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
const response = await client.get('/data');
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
## Error Handling
|
|
267
|
+
|
|
268
|
+
All methods will throw an Error if:
|
|
269
|
+
1. The HTTP request fails (network error, timeout, etc.)
|
|
270
|
+
2. The server responds with a non-2xx status code
|
|
271
|
+
|
|
272
|
+
Errors can be caught using try/catch blocks:
|
|
273
|
+
|
|
274
|
+
```javascript
|
|
275
|
+
try {
|
|
276
|
+
const response = await client.get('/users');
|
|
277
|
+
console.log(response.data);
|
|
278
|
+
} catch (error) {
|
|
279
|
+
console.error('Request failed:', error.message);
|
|
280
|
+
// Handle error appropriately
|
|
281
|
+
}
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
## Notes
|
|
285
|
+
|
|
286
|
+
1. The client automatically serializes JavaScript objects to JSON for request bodies when using POST, PUT, PATCH, or DELETE methods.
|
|
287
|
+
2. Response data is automatically parsed as JSON if the response has a `Content-Type` of `application/json`; otherwise, it's returned as text.
|
|
288
|
+
3. Query parameters are properly encoded using `URLSearchParams`.
|
|
289
|
+
4. Default timeout is 10 seconds but can be customized per request.
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
# JSON Parser and Validator
|
|
2
|
+
|
|
3
|
+
This module provides a robust JSON parsing and validation utility function that can validate JSON data against a schema.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- Parses JSON strings with detailed error reporting
|
|
8
|
+
- Validates JSON data against a schema with comprehensive validation rules
|
|
9
|
+
- Supports nested objects and arrays
|
|
10
|
+
- Provides detailed error messages for validation failures
|
|
11
|
+
|
|
12
|
+
## Installation
|
|
13
|
+
|
|
14
|
+
The function is part of the utils module in this project:
|
|
15
|
+
|
|
16
|
+
```javascript
|
|
17
|
+
const { parseAndValidateJSON } = require('./src/utils');
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Usage
|
|
21
|
+
|
|
22
|
+
### Basic Parsing
|
|
23
|
+
|
|
24
|
+
```javascript
|
|
25
|
+
const { parseAndValidateJSON } = require('./src/utils');
|
|
26
|
+
|
|
27
|
+
// Parse JSON without validation
|
|
28
|
+
const jsonString = '{"name": "John", "age": 30}';
|
|
29
|
+
const data = parseAndValidateJSON(jsonString);
|
|
30
|
+
console.log(data); // { name: 'John', age: 30 }
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
### Parsing with Schema Validation
|
|
34
|
+
|
|
35
|
+
```javascript
|
|
36
|
+
const jsonString = '{"username": "john_doe", "email": "john@example.com", "age": 25}';
|
|
37
|
+
|
|
38
|
+
const schema = {
|
|
39
|
+
required: ["username", "email"],
|
|
40
|
+
properties: {
|
|
41
|
+
username: {
|
|
42
|
+
type: "string",
|
|
43
|
+
minLength: 3,
|
|
44
|
+
maxLength: 20
|
|
45
|
+
},
|
|
46
|
+
email: {
|
|
47
|
+
type: "string"
|
|
48
|
+
},
|
|
49
|
+
age: {
|
|
50
|
+
type: "number",
|
|
51
|
+
minimum: 0,
|
|
52
|
+
maximum: 120
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
try {
|
|
58
|
+
const data = parseAndValidateJSON(jsonString, schema);
|
|
59
|
+
console.log('Valid data:', data);
|
|
60
|
+
} catch (error) {
|
|
61
|
+
console.error('Validation failed:', error.message);
|
|
62
|
+
}
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## Schema Validation Rules
|
|
66
|
+
|
|
67
|
+
The schema validation supports the following rules:
|
|
68
|
+
|
|
69
|
+
- `required`: Array of required field names
|
|
70
|
+
- `properties`: Object defining property validation rules
|
|
71
|
+
- `type`: Data type (string, number, boolean, object, array)
|
|
72
|
+
- `enum`: Array of allowed values
|
|
73
|
+
- `minimum`/`maximum`: Numeric range limits
|
|
74
|
+
- `minLength`/`maxLength`: String length limits
|
|
75
|
+
- `minItems`/`maxItems`: Array size limits
|
|
76
|
+
- `nullable`: Allow null values when set to true
|
|
77
|
+
- `items`: Schema for array items
|
|
78
|
+
- Nested object validation through recursive schema definitions
|
|
79
|
+
|
|
80
|
+
## Examples
|
|
81
|
+
|
|
82
|
+
See `examples/json-parser-example.js` for comprehensive usage examples.
|
|
83
|
+
|
|
84
|
+
## Testing
|
|
85
|
+
|
|
86
|
+
Run the tests with:
|
|
87
|
+
|
|
88
|
+
```bash
|
|
89
|
+
node test/json-parser-test.js
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
## Error Handling
|
|
93
|
+
|
|
94
|
+
The function throws descriptive errors for:
|
|
95
|
+
|
|
96
|
+
- Invalid JSON syntax
|
|
97
|
+
- Missing required fields
|
|
98
|
+
- Type mismatches
|
|
99
|
+
- Value out of range
|
|
100
|
+
- String length violations
|
|
101
|
+
- Array size violations
|
|
102
|
+
- Enum value violations
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
# REST Client Documentation
|
|
2
|
+
|
|
3
|
+
The REST Client is a simple, promise-based HTTP client for making requests to REST APIs. It provides methods for all common HTTP verbs and handles JSON serialization/deserialization automatically.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
The REST Client is part of the Stigmergy CLI project and is available at `src/core/rest_client.js`.
|
|
8
|
+
|
|
9
|
+
## Usage
|
|
10
|
+
|
|
11
|
+
### Importing the Client
|
|
12
|
+
|
|
13
|
+
```javascript
|
|
14
|
+
const RestClient = require('./src/core/rest_client');
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
### Creating an Instance
|
|
18
|
+
|
|
19
|
+
```javascript
|
|
20
|
+
// Create a client with a base URL
|
|
21
|
+
const client = new RestClient('https://api.example.com');
|
|
22
|
+
|
|
23
|
+
// Create a client with default headers
|
|
24
|
+
const client = new RestClient('https://api.example.com', {
|
|
25
|
+
'Authorization': 'Bearer your-token',
|
|
26
|
+
'Content-Type': 'application/json'
|
|
27
|
+
});
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
### Making Requests
|
|
31
|
+
|
|
32
|
+
#### GET Requests
|
|
33
|
+
|
|
34
|
+
```javascript
|
|
35
|
+
// Simple GET request
|
|
36
|
+
const response = await client.get('/users');
|
|
37
|
+
|
|
38
|
+
// GET request with query parameters
|
|
39
|
+
const response = await client.get('/users', {
|
|
40
|
+
params: {
|
|
41
|
+
page: 1,
|
|
42
|
+
limit: 10
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
// GET request with custom headers
|
|
47
|
+
const response = await client.get('/users', {
|
|
48
|
+
headers: {
|
|
49
|
+
'X-API-Key': 'your-key'
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
#### POST Requests
|
|
55
|
+
|
|
56
|
+
```javascript
|
|
57
|
+
// POST request with JSON data
|
|
58
|
+
const userData = {
|
|
59
|
+
name: 'John Doe',
|
|
60
|
+
email: 'john@example.com'
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
const response = await client.post('/users', userData);
|
|
64
|
+
|
|
65
|
+
// POST request with custom headers
|
|
66
|
+
const response = await client.post('/users', userData, {
|
|
67
|
+
headers: {
|
|
68
|
+
'X-API-Key': 'your-key'
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
#### PUT and PATCH Requests
|
|
74
|
+
|
|
75
|
+
```javascript
|
|
76
|
+
// PUT request
|
|
77
|
+
const response = await client.put('/users/123', updatedUserData);
|
|
78
|
+
|
|
79
|
+
// PATCH request
|
|
80
|
+
const response = await client.patch('/users/123', partialUpdateData);
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
#### DELETE Requests
|
|
84
|
+
|
|
85
|
+
```javascript
|
|
86
|
+
// DELETE request
|
|
87
|
+
const response = await client.delete('/users/123');
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### Setting Default Headers
|
|
91
|
+
|
|
92
|
+
```javascript
|
|
93
|
+
// Set default headers for all requests
|
|
94
|
+
client.setDefaultHeaders({
|
|
95
|
+
'Authorization': 'Bearer your-token',
|
|
96
|
+
'X-API-Key': 'your-key'
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
// Set authorization header specifically
|
|
100
|
+
client.setAuthorization('your-token'); // Defaults to Bearer
|
|
101
|
+
client.setAuthorization('your-token', 'Basic'); // Specify token type
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### Response Format
|
|
105
|
+
|
|
106
|
+
All methods return a Promise that resolves to a response object with the following structure:
|
|
107
|
+
|
|
108
|
+
```javascript
|
|
109
|
+
{
|
|
110
|
+
status: 200, // HTTP status code
|
|
111
|
+
statusText: 'OK', // HTTP status text
|
|
112
|
+
headers: {}, // Response headers
|
|
113
|
+
data: {} // Response data (parsed JSON or text)
|
|
114
|
+
}
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
### Error Handling
|
|
118
|
+
|
|
119
|
+
Errors are thrown as JavaScript Error objects with descriptive messages:
|
|
120
|
+
|
|
121
|
+
```javascript
|
|
122
|
+
try {
|
|
123
|
+
const response = await client.get('/users');
|
|
124
|
+
console.log(response.data);
|
|
125
|
+
} catch (error) {
|
|
126
|
+
console.error('Request failed:', error.message);
|
|
127
|
+
}
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
## Examples
|
|
131
|
+
|
|
132
|
+
See `examples/rest_client_example.js` for complete usage examples.
|
|
133
|
+
|
|
134
|
+
## Running Tests
|
|
135
|
+
|
|
136
|
+
```bash
|
|
137
|
+
npm run test:rest-client
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
## Running Examples
|
|
141
|
+
|
|
142
|
+
```bash
|
|
143
|
+
npm run example:rest-client
|
|
144
|
+
```
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Example usage of the encryption functions
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
const { encryptData, decryptData, generateKey } = require('../src/utils');
|
|
6
|
+
|
|
7
|
+
// Generate a secure key (in production, use a proper key management system)
|
|
8
|
+
const secretKey = generateKey();
|
|
9
|
+
|
|
10
|
+
console.log('Data Encryption Example');
|
|
11
|
+
console.log('=====================');
|
|
12
|
+
|
|
13
|
+
// Data to encrypt
|
|
14
|
+
const plaintext = "This is confidential information that needs to be protected.";
|
|
15
|
+
|
|
16
|
+
console.log('Original data:', plaintext);
|
|
17
|
+
|
|
18
|
+
try {
|
|
19
|
+
// Encrypt the data
|
|
20
|
+
const encryptedObj = encryptData(plaintext, secretKey);
|
|
21
|
+
console.log('\nEncrypted data:');
|
|
22
|
+
console.log('Encrypted:', encryptedObj.encryptedData);
|
|
23
|
+
console.log('IV:', encryptedObj.iv);
|
|
24
|
+
console.log('Auth Tag:', encryptedObj.authTag);
|
|
25
|
+
|
|
26
|
+
// Decrypt the data
|
|
27
|
+
const decrypted = decryptData(encryptedObj, secretKey);
|
|
28
|
+
console.log('\nDecrypted data:', decrypted);
|
|
29
|
+
|
|
30
|
+
// Verify data integrity
|
|
31
|
+
console.log('\nData integrity check:', plaintext === decrypted ? 'PASS' : 'FAIL');
|
|
32
|
+
|
|
33
|
+
} catch (error) {
|
|
34
|
+
console.error('Encryption/decryption failed:', error.message);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// Example with different data types
|
|
38
|
+
console.log('\n\nExample with different data types:');
|
|
39
|
+
console.log('---------------------------------');
|
|
40
|
+
|
|
41
|
+
// Working with buffers
|
|
42
|
+
const bufferData = Buffer.from('Binary data to encrypt', 'utf8');
|
|
43
|
+
console.log('Buffer data to encrypt:', bufferData.toString('utf8'));
|
|
44
|
+
|
|
45
|
+
try {
|
|
46
|
+
const encryptedBuffer = encryptData(bufferData, secretKey);
|
|
47
|
+
const decryptedBuffer = decryptData(encryptedBuffer, secretKey);
|
|
48
|
+
console.log('Decrypted buffer data:', decryptedBuffer);
|
|
49
|
+
} catch (error) {
|
|
50
|
+
console.error('Buffer encryption failed:', error.message);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Working with Unicode characters
|
|
54
|
+
console.log('\nUnicode example:');
|
|
55
|
+
const unicodeData = "Hello, 世界! 🌍 Welcome to 数据加密!";
|
|
56
|
+
console.log('Unicode data to encrypt:', unicodeData);
|
|
57
|
+
|
|
58
|
+
try {
|
|
59
|
+
const encryptedUnicode = encryptData(unicodeData, secretKey);
|
|
60
|
+
const decryptedUnicode = decryptData(encryptedUnicode, secretKey);
|
|
61
|
+
console.log('Decrypted unicode data:', decryptedUnicode);
|
|
62
|
+
console.log('Unicode integrity check:', unicodeData === decryptedUnicode ? 'PASS' : 'FAIL');
|
|
63
|
+
} catch (error) {
|
|
64
|
+
console.error('Unicode encryption failed:', error.message);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
console.log('\nExample completed.');
|