te.js 1.3.0 → 2.0.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/.cursor/plans/ai_native_framework_features_5bb1a20a.plan.md +234 -0
- package/.cursor/plans/auto_error_fix_agent_e68979c5.plan.md +356 -0
- package/.cursor/plans/tejas_framework_test_suite_5e3c6fad.plan.md +168 -0
- package/.prettierignore +31 -0
- package/README.md +156 -14
- package/auto-docs/analysis/handler-analyzer.js +58 -0
- package/auto-docs/analysis/source-resolver.js +101 -0
- package/auto-docs/constants.js +37 -0
- package/auto-docs/index.js +146 -0
- package/auto-docs/llm/index.js +6 -0
- package/auto-docs/llm/parse.js +88 -0
- package/auto-docs/llm/prompts.js +222 -0
- package/auto-docs/llm/provider.js +187 -0
- package/auto-docs/openapi/endpoint-processor.js +277 -0
- package/auto-docs/openapi/generator.js +107 -0
- package/auto-docs/openapi/level3.js +131 -0
- package/auto-docs/openapi/spec-builders.js +244 -0
- package/auto-docs/ui/docs-ui.js +186 -0
- package/auto-docs/utils/logger.js +17 -0
- package/auto-docs/utils/strip-usage.js +10 -0
- package/cli/docs-command.js +315 -0
- package/cli/fly-command.js +71 -0
- package/cli/index.js +57 -0
- package/database/index.js +163 -5
- package/database/mongodb.js +146 -0
- package/database/redis.js +201 -0
- package/docs/README.md +36 -0
- package/docs/ammo.md +362 -0
- package/docs/api-reference.md +489 -0
- package/docs/auto-docs.md +215 -0
- package/docs/cli.md +152 -0
- package/docs/configuration.md +233 -0
- package/docs/database.md +391 -0
- package/docs/error-handling.md +417 -0
- package/docs/file-uploads.md +334 -0
- package/docs/getting-started.md +181 -0
- package/docs/middleware.md +356 -0
- package/docs/rate-limiting.md +394 -0
- package/docs/routing.md +302 -0
- package/example/API_OVERVIEW.md +77 -0
- package/example/README.md +155 -0
- package/example/index.js +27 -2
- package/example/openapi.json +390 -0
- package/example/package.json +5 -2
- package/example/services/cache.service.js +25 -0
- package/example/services/user.service.js +42 -0
- package/example/start-redis.js +2 -0
- package/example/targets/cache.target.js +35 -0
- package/example/targets/index.target.js +11 -2
- package/example/targets/users.target.js +60 -0
- package/example/tejas.config.json +13 -1
- package/package.json +20 -5
- package/rate-limit/algorithms/fixed-window.js +141 -0
- package/rate-limit/algorithms/sliding-window.js +147 -0
- package/rate-limit/algorithms/token-bucket.js +115 -0
- package/rate-limit/base.js +165 -0
- package/rate-limit/index.js +147 -0
- package/rate-limit/storage/base.js +104 -0
- package/rate-limit/storage/memory.js +102 -0
- package/rate-limit/storage/redis.js +88 -0
- package/server/ammo/body-parser.js +152 -25
- package/server/ammo/enhancer.js +6 -2
- package/server/ammo.js +356 -327
- package/server/endpoint.js +21 -0
- package/server/handler.js +113 -87
- package/server/target.js +50 -9
- package/server/targets/registry.js +111 -6
- package/te.js +363 -137
- package/tests/auto-docs/handler-analyzer.test.js +44 -0
- package/tests/auto-docs/openapi-generator.test.js +103 -0
- package/tests/auto-docs/parse.test.js +63 -0
- package/tests/auto-docs/source-resolver.test.js +58 -0
- package/tests/helpers/index.js +37 -0
- package/tests/helpers/mock-http.js +342 -0
- package/tests/helpers/test-utils.js +446 -0
- package/tests/setup.test.js +148 -0
- package/utils/configuration.js +13 -10
- package/vitest.config.js +54 -0
- package/database/mongo.js +0 -67
- package/example/targets/user/user.target.js +0 -17
package/server/ammo.js
CHANGED
|
@@ -1,327 +1,356 @@
|
|
|
1
|
-
import { statusAndData } from './ammo/dispatch-helper.js';
|
|
2
|
-
import {
|
|
3
|
-
isStatusCode,
|
|
4
|
-
toStatusCode,
|
|
5
|
-
toStatusMessage,
|
|
6
|
-
} from '../utils/status-codes.js';
|
|
7
|
-
import html from '../utils/tejas-entrypoint-html.js';
|
|
8
|
-
import ammoEnhancer from './ammo/enhancer.js';
|
|
9
|
-
import TejError from './error.js';
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Ammo class for handling HTTP requests and responses.
|
|
13
|
-
*
|
|
14
|
-
* @description
|
|
15
|
-
* Ammo is a utility class that simplifies HTTP request handling and response generation.
|
|
16
|
-
* It provides methods for processing requests, sending responses, and handling errors.
|
|
17
|
-
*
|
|
18
|
-
* @example
|
|
19
|
-
*
|
|
20
|
-
* if (ammo.GET) {
|
|
21
|
-
* ammo.fire(200, { message: 'Hello World' });
|
|
22
|
-
* } else {
|
|
23
|
-
* ammo.notAllowed();
|
|
24
|
-
* }
|
|
25
|
-
*/
|
|
26
|
-
class Ammo {
|
|
27
|
-
/**
|
|
28
|
-
* Creates a new Ammo instance.
|
|
29
|
-
*
|
|
30
|
-
* @param {http.IncomingMessage} req - The HTTP request object
|
|
31
|
-
* @param {http.ServerResponse} res - The HTTP response object
|
|
32
|
-
*
|
|
33
|
-
* @description
|
|
34
|
-
* Initializes a new Ammo instance with the provided request and response objects.
|
|
35
|
-
* Sets up default values for various properties that will be populated by the enhance method.
|
|
36
|
-
*/
|
|
37
|
-
constructor(req, res) {
|
|
38
|
-
this.req = req;
|
|
39
|
-
this.res = res;
|
|
40
|
-
|
|
41
|
-
this.GET = false;
|
|
42
|
-
this.POST = false;
|
|
43
|
-
this.PUT = false;
|
|
44
|
-
this.DELETE = false;
|
|
45
|
-
this.PATCH = false;
|
|
46
|
-
this.HEAD = false;
|
|
47
|
-
this.OPTIONS = false;
|
|
48
|
-
|
|
49
|
-
// Request related data
|
|
50
|
-
this.ip = undefined;
|
|
51
|
-
this.headers = undefined;
|
|
52
|
-
this.payload = undefined;
|
|
53
|
-
this.method = undefined;
|
|
54
|
-
|
|
55
|
-
// URL related data
|
|
56
|
-
this.protocol = undefined;
|
|
57
|
-
this.hostname = undefined;
|
|
58
|
-
this.path = undefined;
|
|
59
|
-
this.endpoint = undefined;
|
|
60
|
-
|
|
61
|
-
this.fullURL = undefined;
|
|
62
|
-
|
|
63
|
-
// Response related data
|
|
64
|
-
this.dispatchedData = undefined;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* Enhances the Ammo instance with request data and sets HTTP method flags.
|
|
69
|
-
*
|
|
70
|
-
* @description
|
|
71
|
-
* This method processes the request and sets various properties on the Ammo instance:
|
|
72
|
-
* - HTTP method flags (GET, POST, PUT, etc.)
|
|
73
|
-
* - Request data (IP, headers, payload, method)
|
|
74
|
-
* - URL data (protocol, hostname, path, endpoint, fullURL)
|
|
75
|
-
*
|
|
76
|
-
* This method should be called before using any other Ammo methods.
|
|
77
|
-
*
|
|
78
|
-
* @returns {Promise<void>} A promise that resolves when enhancement is complete
|
|
79
|
-
*
|
|
80
|
-
* @example
|
|
81
|
-
* const ammo = new Ammo(req, res);
|
|
82
|
-
* await ammo.enhance();
|
|
83
|
-
* // Now you can use ammo.GET, ammo.path, etc.
|
|
84
|
-
*/
|
|
85
|
-
async enhance() {
|
|
86
|
-
await ammoEnhancer(this);
|
|
87
|
-
|
|
88
|
-
this.GET = this.method === 'GET';
|
|
89
|
-
this.POST = this.method === 'POST';
|
|
90
|
-
this.PUT = this.method === 'PUT';
|
|
91
|
-
this.DELETE = this.method === 'DELETE';
|
|
92
|
-
this.PATCH = this.method === 'PATCH';
|
|
93
|
-
this.HEAD = this.method === 'HEAD';
|
|
94
|
-
this.OPTIONS = this.method === 'OPTIONS';
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* Sends a response to the client with the specified data and status code.
|
|
99
|
-
*
|
|
100
|
-
* @param {number|any} [arg1] - If a number, treated as status code. Otherwise treated as data to send.
|
|
101
|
-
* @param {any} [arg2] - If arg1 is a number, this is the data to send. Otherwise ignored.
|
|
102
|
-
* @param {string} [arg3] - Optional content type override.
|
|
103
|
-
*
|
|
104
|
-
* @description
|
|
105
|
-
* The fire method is flexible and can handle different argument patterns:
|
|
106
|
-
*
|
|
107
|
-
* 1. No arguments: Sends a 204 No Content response
|
|
108
|
-
* 2. Single number: Sends a response with the given status code
|
|
109
|
-
* 3. Single non-number: Sends a 200 OK response with the given data
|
|
110
|
-
* 4. Two arguments (number, data): Sends a response with the given status code and data
|
|
111
|
-
* 5. Three arguments: Sends a response with the given status code, data, and content type
|
|
112
|
-
*
|
|
113
|
-
* The fire method can be used with any HTTP status code, including error codes (4xx, 5xx).
|
|
114
|
-
* For error responses, you can use either fire() or throw(). The main difference is that
|
|
115
|
-
* throw() can accept an Error instance and has special handling for it, while fire() only
|
|
116
|
-
* accepts status codes, strings, or other data types.
|
|
117
|
-
*
|
|
118
|
-
* @example
|
|
119
|
-
* // Send a 200 OK response with JSON data
|
|
120
|
-
* ammo.fire(200, { message: 'Success' });
|
|
121
|
-
*
|
|
122
|
-
* @example
|
|
123
|
-
* // Send a 404 Not Found response with custom message
|
|
124
|
-
* ammo.fire(404, 'Resource not found');
|
|
125
|
-
*
|
|
126
|
-
* @example
|
|
127
|
-
* // Send a 500 Internal Server Error response
|
|
128
|
-
* ammo.fire(500, 'Something went wrong');
|
|
129
|
-
*
|
|
130
|
-
* @example
|
|
131
|
-
* // Send HTML content with custom content type
|
|
132
|
-
* ammo.fire(200, '<html><body>Hello</body></html>', 'text/html');
|
|
133
|
-
*
|
|
134
|
-
* @example
|
|
135
|
-
* // Send just a status code (will use default status message)
|
|
136
|
-
* ammo.fire(204);
|
|
137
|
-
*
|
|
138
|
-
* @example
|
|
139
|
-
* // Send just data (will use 200 status code)
|
|
140
|
-
* ammo.fire({ message: 'Success' });
|
|
141
|
-
* ammo.fire('Hello World');
|
|
142
|
-
*/
|
|
143
|
-
fire() {
|
|
144
|
-
const { statusCode, data, contentType } = statusAndData(arguments);
|
|
145
|
-
const contentTypeHeader = { 'Content-Type': contentType };
|
|
146
|
-
|
|
147
|
-
this.dispatchedData = data;
|
|
148
|
-
|
|
149
|
-
this.res.writeHead(statusCode, contentTypeHeader);
|
|
150
|
-
this.res.write(data ?? '');
|
|
151
|
-
this.res.end();
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
/**
|
|
155
|
-
*
|
|
156
|
-
*
|
|
157
|
-
* @
|
|
158
|
-
*
|
|
159
|
-
*
|
|
160
|
-
*
|
|
161
|
-
*
|
|
162
|
-
*
|
|
163
|
-
*
|
|
164
|
-
*
|
|
165
|
-
*
|
|
166
|
-
*
|
|
167
|
-
*
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
*
|
|
175
|
-
*
|
|
176
|
-
*
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
*
|
|
185
|
-
*
|
|
186
|
-
*
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
*
|
|
194
|
-
*
|
|
195
|
-
*
|
|
196
|
-
*
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
*
|
|
204
|
-
*
|
|
205
|
-
*
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
*
|
|
213
|
-
*
|
|
214
|
-
*
|
|
215
|
-
*
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
*
|
|
228
|
-
*
|
|
229
|
-
*
|
|
230
|
-
* @
|
|
231
|
-
*
|
|
232
|
-
*
|
|
233
|
-
*
|
|
234
|
-
*
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
*
|
|
242
|
-
*
|
|
243
|
-
*
|
|
244
|
-
*
|
|
245
|
-
*
|
|
246
|
-
*
|
|
247
|
-
*
|
|
248
|
-
*
|
|
249
|
-
*
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
*
|
|
257
|
-
*
|
|
258
|
-
*
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
const statusCode =
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
1
|
+
import { statusAndData } from './ammo/dispatch-helper.js';
|
|
2
|
+
import {
|
|
3
|
+
isStatusCode,
|
|
4
|
+
toStatusCode,
|
|
5
|
+
toStatusMessage,
|
|
6
|
+
} from '../utils/status-codes.js';
|
|
7
|
+
import html from '../utils/tejas-entrypoint-html.js';
|
|
8
|
+
import ammoEnhancer from './ammo/enhancer.js';
|
|
9
|
+
import TejError from './error.js';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Ammo class for handling HTTP requests and responses.
|
|
13
|
+
*
|
|
14
|
+
* @description
|
|
15
|
+
* Ammo is a utility class that simplifies HTTP request handling and response generation.
|
|
16
|
+
* It provides methods for processing requests, sending responses, and handling errors.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
*
|
|
20
|
+
* if (ammo.GET) {
|
|
21
|
+
* ammo.fire(200, { message: 'Hello World' });
|
|
22
|
+
* } else {
|
|
23
|
+
* ammo.notAllowed();
|
|
24
|
+
* }
|
|
25
|
+
*/
|
|
26
|
+
class Ammo {
|
|
27
|
+
/**
|
|
28
|
+
* Creates a new Ammo instance.
|
|
29
|
+
*
|
|
30
|
+
* @param {http.IncomingMessage} req - The HTTP request object
|
|
31
|
+
* @param {http.ServerResponse} res - The HTTP response object
|
|
32
|
+
*
|
|
33
|
+
* @description
|
|
34
|
+
* Initializes a new Ammo instance with the provided request and response objects.
|
|
35
|
+
* Sets up default values for various properties that will be populated by the enhance method.
|
|
36
|
+
*/
|
|
37
|
+
constructor(req, res) {
|
|
38
|
+
this.req = req;
|
|
39
|
+
this.res = res;
|
|
40
|
+
|
|
41
|
+
this.GET = false;
|
|
42
|
+
this.POST = false;
|
|
43
|
+
this.PUT = false;
|
|
44
|
+
this.DELETE = false;
|
|
45
|
+
this.PATCH = false;
|
|
46
|
+
this.HEAD = false;
|
|
47
|
+
this.OPTIONS = false;
|
|
48
|
+
|
|
49
|
+
// Request related data
|
|
50
|
+
this.ip = undefined;
|
|
51
|
+
this.headers = undefined;
|
|
52
|
+
this.payload = undefined;
|
|
53
|
+
this.method = undefined;
|
|
54
|
+
|
|
55
|
+
// URL related data
|
|
56
|
+
this.protocol = undefined;
|
|
57
|
+
this.hostname = undefined;
|
|
58
|
+
this.path = undefined;
|
|
59
|
+
this.endpoint = undefined;
|
|
60
|
+
|
|
61
|
+
this.fullURL = undefined;
|
|
62
|
+
|
|
63
|
+
// Response related data
|
|
64
|
+
this.dispatchedData = undefined;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Enhances the Ammo instance with request data and sets HTTP method flags.
|
|
69
|
+
*
|
|
70
|
+
* @description
|
|
71
|
+
* This method processes the request and sets various properties on the Ammo instance:
|
|
72
|
+
* - HTTP method flags (GET, POST, PUT, etc.)
|
|
73
|
+
* - Request data (IP, headers, payload, method)
|
|
74
|
+
* - URL data (protocol, hostname, path, endpoint, fullURL)
|
|
75
|
+
*
|
|
76
|
+
* This method should be called before using any other Ammo methods.
|
|
77
|
+
*
|
|
78
|
+
* @returns {Promise<void>} A promise that resolves when enhancement is complete
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* const ammo = new Ammo(req, res);
|
|
82
|
+
* await ammo.enhance();
|
|
83
|
+
* // Now you can use ammo.GET, ammo.path, etc.
|
|
84
|
+
*/
|
|
85
|
+
async enhance() {
|
|
86
|
+
await ammoEnhancer(this);
|
|
87
|
+
|
|
88
|
+
this.GET = this.method === 'GET';
|
|
89
|
+
this.POST = this.method === 'POST';
|
|
90
|
+
this.PUT = this.method === 'PUT';
|
|
91
|
+
this.DELETE = this.method === 'DELETE';
|
|
92
|
+
this.PATCH = this.method === 'PATCH';
|
|
93
|
+
this.HEAD = this.method === 'HEAD';
|
|
94
|
+
this.OPTIONS = this.method === 'OPTIONS';
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Sends a response to the client with the specified data and status code.
|
|
99
|
+
*
|
|
100
|
+
* @param {number|any} [arg1] - If a number, treated as status code. Otherwise treated as data to send.
|
|
101
|
+
* @param {any} [arg2] - If arg1 is a number, this is the data to send. Otherwise ignored.
|
|
102
|
+
* @param {string} [arg3] - Optional content type override.
|
|
103
|
+
*
|
|
104
|
+
* @description
|
|
105
|
+
* The fire method is flexible and can handle different argument patterns:
|
|
106
|
+
*
|
|
107
|
+
* 1. No arguments: Sends a 204 No Content response
|
|
108
|
+
* 2. Single number: Sends a response with the given status code
|
|
109
|
+
* 3. Single non-number: Sends a 200 OK response with the given data
|
|
110
|
+
* 4. Two arguments (number, data): Sends a response with the given status code and data
|
|
111
|
+
* 5. Three arguments: Sends a response with the given status code, data, and content type
|
|
112
|
+
*
|
|
113
|
+
* The fire method can be used with any HTTP status code, including error codes (4xx, 5xx).
|
|
114
|
+
* For error responses, you can use either fire() or throw(). The main difference is that
|
|
115
|
+
* throw() can accept an Error instance and has special handling for it, while fire() only
|
|
116
|
+
* accepts status codes, strings, or other data types.
|
|
117
|
+
*
|
|
118
|
+
* @example
|
|
119
|
+
* // Send a 200 OK response with JSON data
|
|
120
|
+
* ammo.fire(200, { message: 'Success' });
|
|
121
|
+
*
|
|
122
|
+
* @example
|
|
123
|
+
* // Send a 404 Not Found response with custom message
|
|
124
|
+
* ammo.fire(404, 'Resource not found');
|
|
125
|
+
*
|
|
126
|
+
* @example
|
|
127
|
+
* // Send a 500 Internal Server Error response
|
|
128
|
+
* ammo.fire(500, 'Something went wrong');
|
|
129
|
+
*
|
|
130
|
+
* @example
|
|
131
|
+
* // Send HTML content with custom content type
|
|
132
|
+
* ammo.fire(200, '<html><body>Hello</body></html>', 'text/html');
|
|
133
|
+
*
|
|
134
|
+
* @example
|
|
135
|
+
* // Send just a status code (will use default status message)
|
|
136
|
+
* ammo.fire(204);
|
|
137
|
+
*
|
|
138
|
+
* @example
|
|
139
|
+
* // Send just data (will use 200 status code)
|
|
140
|
+
* ammo.fire({ message: 'Success' });
|
|
141
|
+
* ammo.fire('Hello World');
|
|
142
|
+
*/
|
|
143
|
+
fire() {
|
|
144
|
+
const { statusCode, data, contentType } = statusAndData(arguments);
|
|
145
|
+
const contentTypeHeader = { 'Content-Type': contentType };
|
|
146
|
+
|
|
147
|
+
this.dispatchedData = data;
|
|
148
|
+
|
|
149
|
+
this.res.writeHead(statusCode, contentTypeHeader);
|
|
150
|
+
this.res.write(data ?? '');
|
|
151
|
+
this.res.end();
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Redirects to the specified URL.
|
|
156
|
+
*
|
|
157
|
+
* @param {string} url - The URL to redirect to
|
|
158
|
+
* @param {number} [statusCode=302] - HTTP status code for redirect (default: 302)
|
|
159
|
+
*
|
|
160
|
+
* @description
|
|
161
|
+
* Sends an HTTP redirect response to the specified URL.
|
|
162
|
+
* Uses 302 (Found/Temporary Redirect) by default.
|
|
163
|
+
* Common status codes:
|
|
164
|
+
* - 301: Moved Permanently
|
|
165
|
+
* - 302: Found (Temporary Redirect)
|
|
166
|
+
* - 303: See Other
|
|
167
|
+
* - 307: Temporary Redirect (maintains HTTP method)
|
|
168
|
+
* - 308: Permanent Redirect (maintains HTTP method)
|
|
169
|
+
*
|
|
170
|
+
* @example
|
|
171
|
+
* // Temporary redirect (302)
|
|
172
|
+
* ammo.redirect('/new-location');
|
|
173
|
+
*
|
|
174
|
+
* @example
|
|
175
|
+
* // Permanent redirect (301)
|
|
176
|
+
* ammo.redirect('/new-location', 301);
|
|
177
|
+
*/
|
|
178
|
+
redirect(url, statusCode = 302) {
|
|
179
|
+
this.res.writeHead(statusCode, { Location: url });
|
|
180
|
+
this.res.end();
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Throws a 404 Not Found error.
|
|
185
|
+
*
|
|
186
|
+
* @description
|
|
187
|
+
* This is a convenience method that throws a 404 Not Found error.
|
|
188
|
+
* It's equivalent to calling `throw(404) ` or `fire(404)`.
|
|
189
|
+
*
|
|
190
|
+
* @throws {TejError} Always throws a TejError with status code 404
|
|
191
|
+
*
|
|
192
|
+
* @example
|
|
193
|
+
* // If resource not found
|
|
194
|
+
* if (!resource) {
|
|
195
|
+
* ammo.notFound();
|
|
196
|
+
* }
|
|
197
|
+
*/
|
|
198
|
+
notFound() {
|
|
199
|
+
throw new TejError(404, 'Not Found');
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Throws a 405 Method Not Allowed error.
|
|
204
|
+
*
|
|
205
|
+
* @description
|
|
206
|
+
* This is a convenience method that throws a 405 Method Not Allowed error.
|
|
207
|
+
* It's equivalent to calling `throw(405)` or `fire(405)`.
|
|
208
|
+
*
|
|
209
|
+
* @throws {TejError} Always throws a TejError with status code 405
|
|
210
|
+
*
|
|
211
|
+
* @example
|
|
212
|
+
* // If method not allowed
|
|
213
|
+
* if (!allowedMethods.includes(ammo.method)) {
|
|
214
|
+
* ammo.notAllowed();
|
|
215
|
+
* }
|
|
216
|
+
*/
|
|
217
|
+
notAllowed() {
|
|
218
|
+
throw new TejError(405, 'Method Not Allowed');
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Throws a 401 Unauthorized error.
|
|
223
|
+
*
|
|
224
|
+
* @description
|
|
225
|
+
* This is a convenience method that throws a 401 Unauthorized error.
|
|
226
|
+
* It's equivalent to calling `throw(401) ` or `fire(401)`.
|
|
227
|
+
*
|
|
228
|
+
* @throws {TejError} Always throws a TejError with status code 401
|
|
229
|
+
*
|
|
230
|
+
* @example
|
|
231
|
+
* // If user is not authenticated
|
|
232
|
+
* if (!user) {
|
|
233
|
+
* ammo.unauthorized();
|
|
234
|
+
* }
|
|
235
|
+
*/
|
|
236
|
+
unauthorized() {
|
|
237
|
+
throw new TejError(401, 'Unauthorized');
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Sends the default entry point HTML.
|
|
242
|
+
*
|
|
243
|
+
* @description
|
|
244
|
+
* This method sends the default HTML entry point for the application.
|
|
245
|
+
* It's typically used as a fallback when no specific route is matched.
|
|
246
|
+
*
|
|
247
|
+
* @example
|
|
248
|
+
* // In a catch-all route
|
|
249
|
+
* ammo.defaultEntry();
|
|
250
|
+
*/
|
|
251
|
+
defaultEntry() {
|
|
252
|
+
this.fire(html);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Throws an error response with appropriate status code and message.
|
|
257
|
+
*
|
|
258
|
+
* @param {number|Error|string} [arg1] - Status code, Error object, or error message
|
|
259
|
+
* @param {string} [arg2] - Error message (only used when arg1 is a status code)
|
|
260
|
+
*
|
|
261
|
+
* @description
|
|
262
|
+
* The throw method is flexible and can handle different argument patterns:
|
|
263
|
+
*
|
|
264
|
+
* 1. No arguments: Sends a 500 Internal Server Error response
|
|
265
|
+
* 2. Status code: Sends a response with the given status code and default message
|
|
266
|
+
* 3. Status code and message: Sends a response with the given status code and message
|
|
267
|
+
* 4. Error object: Extracts status code and message from the error
|
|
268
|
+
* 5. String: Treats as error message with 500 status code
|
|
269
|
+
*
|
|
270
|
+
* The key difference between throw() and fire() is that throw() can accept an Error instance
|
|
271
|
+
* and has special handling for it. Internally, throw() still calls fire() to send the response.
|
|
272
|
+
*
|
|
273
|
+
* @example
|
|
274
|
+
* // Throw a 404 Not Found error
|
|
275
|
+
* ammo.throw(404);
|
|
276
|
+
*
|
|
277
|
+
* @example
|
|
278
|
+
* // Throw a 404 Not Found error with custom message
|
|
279
|
+
* ammo.throw(404, 'Resource not found');
|
|
280
|
+
*
|
|
281
|
+
* @example
|
|
282
|
+
* // Throw an error from an Error object
|
|
283
|
+
* ammo.throw(new Error('Something went wrong'));
|
|
284
|
+
*
|
|
285
|
+
* @example
|
|
286
|
+
* // Throw an error with a custom message
|
|
287
|
+
* ammo.throw('Something went wrong');
|
|
288
|
+
*/
|
|
289
|
+
throw() {
|
|
290
|
+
// Handle different argument patterns
|
|
291
|
+
const args = Array.from(arguments);
|
|
292
|
+
|
|
293
|
+
// Case 1: No arguments provided
|
|
294
|
+
if (args.length === 0) {
|
|
295
|
+
this.fire(500, 'Internal Server Error');
|
|
296
|
+
return;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
// Case 2: First argument is a status code
|
|
300
|
+
if (isStatusCode(args[0])) {
|
|
301
|
+
const statusCode = args[0];
|
|
302
|
+
const message = args[1] || toStatusMessage(statusCode);
|
|
303
|
+
this.fire(statusCode, message);
|
|
304
|
+
return;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
// Case 3.1: First argument is an instance of TejError
|
|
308
|
+
if (args[0] instanceof TejError) {
|
|
309
|
+
const error = args[0];
|
|
310
|
+
const statusCode = error.code;
|
|
311
|
+
const message = error.message;
|
|
312
|
+
|
|
313
|
+
this.fire(statusCode, message);
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
// Case 3.2: First argument is an Error object
|
|
318
|
+
if (args[0] instanceof Error) {
|
|
319
|
+
const error = args[0];
|
|
320
|
+
|
|
321
|
+
// Check if error message is a numeric status code
|
|
322
|
+
if (!isNaN(parseInt(error.message))) {
|
|
323
|
+
const statusCode = parseInt(error.message);
|
|
324
|
+
const message = toStatusMessage(statusCode) || toStatusMessage(500);
|
|
325
|
+
this.fire(statusCode, message);
|
|
326
|
+
return;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
// Use error message as status code if it's a valid status code string
|
|
330
|
+
const statusCode = toStatusCode(error.message);
|
|
331
|
+
if (statusCode) {
|
|
332
|
+
this.fire(statusCode, error.message);
|
|
333
|
+
return;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
// Default error handling
|
|
337
|
+
this.fire(500, error.message);
|
|
338
|
+
return;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
// Case 4: First argument is a string or other value
|
|
342
|
+
const errorValue = args[0];
|
|
343
|
+
|
|
344
|
+
// Check if the string represents a status code
|
|
345
|
+
const statusCode = toStatusCode(errorValue);
|
|
346
|
+
if (statusCode) {
|
|
347
|
+
this.fire(statusCode, toStatusMessage(statusCode));
|
|
348
|
+
return;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
// Default case: treat as error message
|
|
352
|
+
this.fire(500, errorValue.toString());
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
export default Ammo;
|