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.
Files changed (80) hide show
  1. package/.cursor/plans/ai_native_framework_features_5bb1a20a.plan.md +234 -0
  2. package/.cursor/plans/auto_error_fix_agent_e68979c5.plan.md +356 -0
  3. package/.cursor/plans/tejas_framework_test_suite_5e3c6fad.plan.md +168 -0
  4. package/.prettierignore +31 -0
  5. package/README.md +156 -14
  6. package/auto-docs/analysis/handler-analyzer.js +58 -0
  7. package/auto-docs/analysis/source-resolver.js +101 -0
  8. package/auto-docs/constants.js +37 -0
  9. package/auto-docs/index.js +146 -0
  10. package/auto-docs/llm/index.js +6 -0
  11. package/auto-docs/llm/parse.js +88 -0
  12. package/auto-docs/llm/prompts.js +222 -0
  13. package/auto-docs/llm/provider.js +187 -0
  14. package/auto-docs/openapi/endpoint-processor.js +277 -0
  15. package/auto-docs/openapi/generator.js +107 -0
  16. package/auto-docs/openapi/level3.js +131 -0
  17. package/auto-docs/openapi/spec-builders.js +244 -0
  18. package/auto-docs/ui/docs-ui.js +186 -0
  19. package/auto-docs/utils/logger.js +17 -0
  20. package/auto-docs/utils/strip-usage.js +10 -0
  21. package/cli/docs-command.js +315 -0
  22. package/cli/fly-command.js +71 -0
  23. package/cli/index.js +57 -0
  24. package/database/index.js +163 -5
  25. package/database/mongodb.js +146 -0
  26. package/database/redis.js +201 -0
  27. package/docs/README.md +36 -0
  28. package/docs/ammo.md +362 -0
  29. package/docs/api-reference.md +489 -0
  30. package/docs/auto-docs.md +215 -0
  31. package/docs/cli.md +152 -0
  32. package/docs/configuration.md +233 -0
  33. package/docs/database.md +391 -0
  34. package/docs/error-handling.md +417 -0
  35. package/docs/file-uploads.md +334 -0
  36. package/docs/getting-started.md +181 -0
  37. package/docs/middleware.md +356 -0
  38. package/docs/rate-limiting.md +394 -0
  39. package/docs/routing.md +302 -0
  40. package/example/API_OVERVIEW.md +77 -0
  41. package/example/README.md +155 -0
  42. package/example/index.js +27 -2
  43. package/example/openapi.json +390 -0
  44. package/example/package.json +5 -2
  45. package/example/services/cache.service.js +25 -0
  46. package/example/services/user.service.js +42 -0
  47. package/example/start-redis.js +2 -0
  48. package/example/targets/cache.target.js +35 -0
  49. package/example/targets/index.target.js +11 -2
  50. package/example/targets/users.target.js +60 -0
  51. package/example/tejas.config.json +13 -1
  52. package/package.json +20 -5
  53. package/rate-limit/algorithms/fixed-window.js +141 -0
  54. package/rate-limit/algorithms/sliding-window.js +147 -0
  55. package/rate-limit/algorithms/token-bucket.js +115 -0
  56. package/rate-limit/base.js +165 -0
  57. package/rate-limit/index.js +147 -0
  58. package/rate-limit/storage/base.js +104 -0
  59. package/rate-limit/storage/memory.js +102 -0
  60. package/rate-limit/storage/redis.js +88 -0
  61. package/server/ammo/body-parser.js +152 -25
  62. package/server/ammo/enhancer.js +6 -2
  63. package/server/ammo.js +356 -327
  64. package/server/endpoint.js +21 -0
  65. package/server/handler.js +113 -87
  66. package/server/target.js +50 -9
  67. package/server/targets/registry.js +111 -6
  68. package/te.js +363 -137
  69. package/tests/auto-docs/handler-analyzer.test.js +44 -0
  70. package/tests/auto-docs/openapi-generator.test.js +103 -0
  71. package/tests/auto-docs/parse.test.js +63 -0
  72. package/tests/auto-docs/source-resolver.test.js +58 -0
  73. package/tests/helpers/index.js +37 -0
  74. package/tests/helpers/mock-http.js +342 -0
  75. package/tests/helpers/test-utils.js +446 -0
  76. package/tests/setup.test.js +148 -0
  77. package/utils/configuration.js +13 -10
  78. package/vitest.config.js +54 -0
  79. package/database/mongo.js +0 -67
  80. 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
- * Throws a 404 Not Found error.
156
- *
157
- * @description
158
- * This is a convenience method that throws a 404 Not Found error.
159
- * It's equivalent to calling `throw(404) ` or `fire(404)`.
160
- *
161
- * @throws {TejError} Always throws a TejError with status code 404
162
- *
163
- * @example
164
- * // If resource not found
165
- * if (!resource) {
166
- * ammo.notFound();
167
- * }
168
- */
169
- notFound() {
170
- throw new TejError(404, 'Not Found');
171
- }
172
-
173
- /**
174
- * Throws a 405 Method Not Allowed error.
175
- *
176
- * @description
177
- * This is a convenience method that throws a 405 Method Not Allowed error.
178
- * It's equivalent to calling `throw(405)` or `fire(405)`.
179
- *
180
- * @throws {TejError} Always throws a TejError with status code 405
181
- *
182
- * @example
183
- * // If method not allowed
184
- * if (!allowedMethods.includes(ammo.method)) {
185
- * ammo.notAllowed();
186
- * }
187
- */
188
- notAllowed() {
189
- throw new TejError(405, 'Method Not Allowed');
190
- }
191
-
192
- /**
193
- * Throws a 401 Unauthorized error.
194
- *
195
- * @description
196
- * This is a convenience method that throws a 401 Unauthorized error.
197
- * It's equivalent to calling `throw(401) ` or `fire(401)`.
198
- *
199
- * @throws {TejError} Always throws a TejError with status code 401
200
- *
201
- * @example
202
- * // If user is not authenticated
203
- * if (!user) {
204
- * ammo.unauthorized();
205
- * }
206
- */
207
- unauthorized() {
208
- throw new TejError(401, 'Unauthorized');
209
- }
210
-
211
- /**
212
- * Sends the default entry point HTML.
213
- *
214
- * @description
215
- * This method sends the default HTML entry point for the application.
216
- * It's typically used as a fallback when no specific route is matched.
217
- *
218
- * @example
219
- * // In a catch-all route
220
- * ammo.defaultEntry();
221
- */
222
- defaultEntry() {
223
- this.fire(html);
224
- }
225
-
226
- /**
227
- * Throws an error response with appropriate status code and message.
228
- *
229
- * @param {number|Error|string} [arg1] - Status code, Error object, or error message
230
- * @param {string} [arg2] - Error message (only used when arg1 is a status code)
231
- *
232
- * @description
233
- * The throw method is flexible and can handle different argument patterns:
234
- *
235
- * 1. No arguments: Sends a 500 Internal Server Error response
236
- * 2. Status code: Sends a response with the given status code and default message
237
- * 3. Status code and message: Sends a response with the given status code and message
238
- * 4. Error object: Extracts status code and message from the error
239
- * 5. String: Treats as error message with 500 status code
240
- *
241
- * The key difference between throw() and fire() is that throw() can accept an Error instance
242
- * and has special handling for it. Internally, throw() still calls fire() to send the response.
243
- *
244
- * @example
245
- * // Throw a 404 Not Found error
246
- * ammo.throw(404);
247
- *
248
- * @example
249
- * // Throw a 404 Not Found error with custom message
250
- * ammo.throw(404, 'Resource not found');
251
- *
252
- * @example
253
- * // Throw an error from an Error object
254
- * ammo.throw(new Error('Something went wrong'));
255
- *
256
- * @example
257
- * // Throw an error with a custom message
258
- * ammo.throw('Something went wrong');
259
- */
260
- throw() {
261
- // Handle different argument patterns
262
- const args = Array.from(arguments);
263
-
264
- // Case 1: No arguments provided
265
- if (args.length === 0) {
266
- this.fire(500, 'Internal Server Error');
267
- return;
268
- }
269
-
270
- // Case 2: First argument is a status code
271
- if (isStatusCode(args[0])) {
272
- const statusCode = args[0];
273
- const message = args[1] || toStatusMessage(statusCode);
274
- this.fire(statusCode, message);
275
- return;
276
- }
277
-
278
- // Case 3.1: First argument is an instance of TejError
279
- if (args[0] instanceof TejError) {
280
- const error = args[0];
281
- const statusCode = error.code;
282
- const message = error.message;
283
-
284
- this.fire(statusCode, message);
285
- return;
286
- }
287
-
288
- // Case 3.2: First argument is an Error object
289
- if (args[0] instanceof Error) {
290
- const error = args[0];
291
-
292
- // Check if error message is a numeric status code
293
- if (!isNaN(parseInt(error.message))) {
294
- const statusCode = parseInt(error.message);
295
- const message = toStatusMessage(statusCode) || toStatusMessage(500);
296
- this.fire(statusCode, message);
297
- return;
298
- }
299
-
300
- // Use error message as status code if it's a valid status code string
301
- const statusCode = toStatusCode(error.message);
302
- if (statusCode) {
303
- this.fire(statusCode, error.message);
304
- return;
305
- }
306
-
307
- // Default error handling
308
- this.fire(500, error.message);
309
- return;
310
- }
311
-
312
- // Case 4: First argument is a string or other value
313
- const errorValue = args[0];
314
-
315
- // Check if the string represents a status code
316
- const statusCode = toStatusCode(errorValue);
317
- if (statusCode) {
318
- this.fire(statusCode, toStatusMessage(statusCode));
319
- return;
320
- }
321
-
322
- // Default case: treat as error message
323
- this.fire(500, errorValue.toString());
324
- }
325
- }
326
-
327
- export default Ammo;
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;