te.js 2.1.0 → 2.1.2

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 (70) hide show
  1. package/README.md +197 -196
  2. package/auto-docs/analysis/handler-analyzer.js +58 -58
  3. package/auto-docs/analysis/source-resolver.js +101 -101
  4. package/auto-docs/constants.js +37 -37
  5. package/auto-docs/docs-llm/index.js +7 -7
  6. package/auto-docs/docs-llm/prompts.js +222 -222
  7. package/auto-docs/docs-llm/provider.js +132 -132
  8. package/auto-docs/index.js +146 -146
  9. package/auto-docs/openapi/endpoint-processor.js +277 -277
  10. package/auto-docs/openapi/generator.js +107 -107
  11. package/auto-docs/openapi/level3.js +131 -131
  12. package/auto-docs/openapi/spec-builders.js +244 -244
  13. package/auto-docs/ui/docs-ui.js +186 -186
  14. package/auto-docs/utils/logger.js +17 -17
  15. package/auto-docs/utils/strip-usage.js +10 -10
  16. package/cli/docs-command.js +315 -315
  17. package/cli/fly-command.js +71 -71
  18. package/cli/index.js +56 -56
  19. package/cors/index.js +71 -0
  20. package/database/index.js +165 -165
  21. package/database/mongodb.js +146 -146
  22. package/database/redis.js +201 -201
  23. package/docs/README.md +36 -36
  24. package/docs/ammo.md +362 -362
  25. package/docs/api-reference.md +490 -490
  26. package/docs/auto-docs.md +216 -216
  27. package/docs/cli.md +152 -152
  28. package/docs/configuration.md +275 -275
  29. package/docs/database.md +390 -390
  30. package/docs/error-handling.md +438 -438
  31. package/docs/file-uploads.md +333 -333
  32. package/docs/getting-started.md +214 -214
  33. package/docs/middleware.md +355 -355
  34. package/docs/rate-limiting.md +393 -393
  35. package/docs/routing.md +302 -302
  36. package/lib/llm/client.js +73 -0
  37. package/lib/llm/index.js +7 -0
  38. package/lib/llm/parse.js +89 -0
  39. package/package.json +64 -62
  40. package/rate-limit/algorithms/fixed-window.js +141 -141
  41. package/rate-limit/algorithms/sliding-window.js +147 -147
  42. package/rate-limit/algorithms/token-bucket.js +115 -115
  43. package/rate-limit/base.js +165 -165
  44. package/rate-limit/index.js +147 -147
  45. package/rate-limit/storage/base.js +104 -104
  46. package/rate-limit/storage/memory.js +101 -101
  47. package/rate-limit/storage/redis.js +88 -88
  48. package/server/ammo/body-parser.js +220 -220
  49. package/server/ammo/dispatch-helper.js +103 -103
  50. package/server/ammo/enhancer.js +57 -57
  51. package/server/ammo.js +454 -415
  52. package/server/endpoint.js +97 -74
  53. package/server/error.js +9 -9
  54. package/server/errors/code-context.js +125 -125
  55. package/server/errors/llm-error-service.js +140 -140
  56. package/server/files/helper.js +33 -33
  57. package/server/files/uploader.js +143 -143
  58. package/server/handler.js +158 -119
  59. package/server/target.js +185 -175
  60. package/server/targets/middleware-validator.js +22 -22
  61. package/server/targets/path-validator.js +21 -21
  62. package/server/targets/registry.js +160 -160
  63. package/server/targets/shoot-validator.js +21 -21
  64. package/te.js +428 -402
  65. package/utils/auto-register.js +17 -17
  66. package/utils/configuration.js +64 -64
  67. package/utils/errors-llm-config.js +84 -84
  68. package/utils/request-logger.js +43 -43
  69. package/utils/status-codes.js +82 -82
  70. package/utils/tejas-entrypoint-html.js +18 -18
package/README.md CHANGED
@@ -1,196 +1,197 @@
1
- <p align="center">
2
- <img src="https://tejas-documentation.vercel.app/tejas-logo.svg" alt="Tejas Logo" width="200">
3
- </p>
4
-
5
- <h1 align="center">Tejas</h1>
6
-
7
- <p align="center">
8
- <strong>A Node.js Framework for Powerful Backend Services</strong>
9
- </p>
10
-
11
- <p align="center">
12
- <a href="https://www.npmjs.com/package/te.js"><img src="https://img.shields.io/npm/v/te.js.svg" alt="npm version"></a>
13
- <a href="https://www.npmjs.com/package/te.js"><img src="https://img.shields.io/npm/dm/te.js.svg" alt="npm downloads"></a>
14
- <a href="https://github.com/hirakchhatbar/te.js/blob/main/LICENSE"><img src="https://img.shields.io/npm/l/te.js.svg" alt="license"></a>
15
- </p>
16
-
17
- <p align="center">
18
- <a href="https://tejas-documentation.vercel.app">Documentation</a> •
19
- <a href="#ai-assisted-setup-mcp">AI Setup (MCP)</a> •
20
- <a href="#quick-start">Quick Start</a> •
21
- <a href="#features">Features</a> •
22
- <a href="./docs">Full Docs</a>
23
- </p>
24
-
25
- ---
26
-
27
- ## What is Tejas?
28
-
29
- Tejas (meaning "radiance" in Hindi) is a modern, lightweight Node.js framework designed for building robust backend services. It offers an intuitive API with aviation-inspired naming conventions, making your code both expressive and enjoyable to write.
30
-
31
- ```javascript
32
- import Tejas, { Target } from 'te.js';
33
-
34
- const app = new Tejas();
35
- const api = new Target('/api');
36
-
37
- api.register('/hello/:name', (ammo) => {
38
- ammo.fire({ message: `Hello, ${ammo.payload.name}!` });
39
- });
40
-
41
- app.takeoff();
42
- ```
43
-
44
-
45
- ## Features
46
-
47
- - **AI-Native (MCP)** — Ship with an MCP server so AI assistants can scaffold projects, generate routes, and write correct code with full framework knowledge
48
- - **Simple Routing** — Clean, method-agnostic URL structures with parameterized routes
49
- - **Express Compatible** — Use existing Express middleware alongside Tejas middleware
50
- - **Zero-Config Error Handling** — No try-catch needed! Tejas catches all errors automatically. Opt in to have an LLM determine error code and message when you don't specify them (see [Error Handling](./docs/error-handling.md))
51
- - **Built-in Rate Limiting** — Three algorithms (Token Bucket, Sliding Window, Fixed Window) with memory or Redis storage
52
- - **Database Ready** — First-class Redis and MongoDB support with auto-install of drivers
53
- - **File Uploads** — Easy file handling with size limits and type validation
54
- - **Auto-Documentation** — Generate OpenAPI specs from your code with LLM-powered analysis (`tejas generate:docs`)
55
- - **Interactive API Docs** — Serve a Scalar API reference UI with `app.serveDocs()`
56
- - **Auto-Discovery** — Automatic route registration from `.target.js` files
57
- - **Request Logging** — Built-in HTTP request and exception logging
58
-
59
-
60
- ## AI-Assisted Setup (MCP)
61
-
62
- > **Recommended** — The best way to get started with Tejas in the age of AI.
63
-
64
- The [Tejas MCP server](https://www.npmjs.com/package/tejas-mcp) gives your IDE's AI assistant full knowledge of the framework — documentation, code examples, and purpose-built tools to scaffold projects and generate correct code. No more hallucinated APIs.
65
-
66
- **Cursor** — add this to `.cursor/mcp.json`:
67
-
68
- ```json
69
- {
70
- "mcpServers": {
71
- "tejas": {
72
- "command": "npx",
73
- "args": ["-y", "tejas-mcp"]
74
- }
75
- }
76
- }
77
- ```
78
-
79
- **Other MCP-compatible IDEs** — run `npx tejas-mcp` as the server command (stdio transport, no config needed).
80
-
81
- Once connected, prompt your AI with things like *"Scaffold a new te.js project called my-api"* or *"Create a REST API with user CRUD routes"* — the assistant will generate framework-correct code using real te.js patterns.
82
-
83
-
84
- ## Quick Start
85
-
86
- ### Install
87
-
88
- ```bash
89
- npm install te.js
90
- ```
91
-
92
- ### Create Your App
93
-
94
- ```javascript
95
- // index.js
96
- import Tejas from 'te.js';
97
-
98
- const app = new Tejas({ port: 3000 });
99
- app.takeoff();
100
- ```
101
-
102
- ### Define Routes
103
-
104
- ```javascript
105
- // targets/user.target.js
106
- import { Target } from 'te.js';
107
-
108
- const users = new Target('/users');
109
-
110
- users.register('/', (ammo) => {
111
- if (ammo.GET) {
112
- ammo.fire([{ id: 1, name: 'John' }]);
113
- } else if (ammo.POST) {
114
- const { name, email } = ammo.payload;
115
- ammo.fire(201, { id: 2, name, email });
116
- } else {
117
- ammo.notAllowed();
118
- }
119
- });
120
-
121
- users.register('/:id', (ammo) => {
122
- const { id } = ammo.payload;
123
- ammo.fire({ id, name: 'John Doe' });
124
- });
125
- ```
126
-
127
- ### Run
128
-
129
- ```bash
130
- node index.js
131
- # Server running at http://localhost:3000
132
- ```
133
-
134
-
135
- ## Core Concepts
136
-
137
- | Tejas Term | Purpose | Express Equivalent |
138
- | ----------- | ------------------------ | ------------------ |
139
- | `Tejas` | Application instance | `express()` |
140
- | `Target` | Route group/router | `Router()` |
141
- | `Ammo` | Request/response context | `req` + `res` |
142
- | `fire()` | Send response | `res.send()` |
143
- | `midair()` | Register middleware | `use()` |
144
- | `takeoff()` | Start server | `listen()` |
145
-
146
-
147
- ## CLI
148
-
149
- ```bash
150
- tejas fly [file] # Start the server
151
- tejas generate:docs [--ci] # Generate OpenAPI docs (interactive or CI mode)
152
- tejas docs:on-push # Auto-generate docs when pushing to production branch
153
- ```
154
-
155
-
156
- ## API Documentation
157
-
158
- Generate and serve interactive API docs:
159
-
160
- ```bash
161
- npx tejas generate:docs
162
- ```
163
-
164
- ```javascript
165
- app.serveDocs({ specPath: './openapi.json' });
166
- app.takeoff();
167
- // Visit http://localhost:1403/docs
168
- ```
169
-
170
-
171
- ## Documentation
172
-
173
- For comprehensive documentation, see the [docs folder](./docs) or visit [tejas-documentation.vercel.app](https://tejas-documentation.vercel.app).
174
-
175
- - [Getting Started](./docs/getting-started.md) — Installation and quick start
176
- - [Configuration](./docs/configuration.md) — All configuration options
177
- - [Routing](./docs/routing.md) — Target-based routing system
178
- - [Ammo](./docs/ammo.md) — Request/response handling
179
- - [Middleware](./docs/middleware.md) — Global, target, and route middleware
180
- - [Error Handling](./docs/error-handling.md) — Zero-config error handling
181
- - [Database](./docs/database.md) — Redis and MongoDB integration
182
- - [Rate Limiting](./docs/rate-limiting.md) — API protection
183
- - [File Uploads](./docs/file-uploads.md) — File handling
184
- - [CLI Reference](./docs/cli.md) — Command-line interface
185
- - [Auto-Documentation](./docs/auto-docs.md) — OpenAPI generation
186
- - [API Reference](./docs/api-reference.md) — Complete API docs
187
-
188
-
189
- ## Contributing
190
-
191
- Contributions are welcome! Please feel free to submit a Pull Request.
192
-
193
-
194
- ## License
195
-
196
- ISC © [Hirak Chhatbar](https://github.com/hirakchhatbar)
1
+ <p align="center">
2
+ <img src="https://tejas-documentation.vercel.app/tejas-logo.svg" alt="Tejas Logo" width="200">
3
+ </p>
4
+
5
+ <h1 align="center">Tejas</h1>
6
+
7
+ <p align="center">
8
+ <strong>A Node.js Framework for Powerful Backend Services</strong>
9
+ </p>
10
+
11
+ <p align="center">
12
+ <a href="https://www.npmjs.com/package/te.js"><img src="https://img.shields.io/npm/v/te.js.svg" alt="npm version"></a>
13
+ <a href="https://www.npmjs.com/package/te.js"><img src="https://img.shields.io/npm/dm/te.js.svg" alt="npm downloads"></a>
14
+ <a href="https://github.com/hirakchhatbar/te.js/blob/main/LICENSE"><img src="https://img.shields.io/npm/l/te.js.svg" alt="license"></a>
15
+ </p>
16
+
17
+ <p align="center">
18
+ <a href="https://tejas-documentation.vercel.app">Documentation</a> •
19
+ <a href="#ai-assisted-setup-mcp">AI Setup (MCP)</a> •
20
+ <a href="#quick-start">Quick Start</a> •
21
+ <a href="#features">Features</a> •
22
+ <a href="./docs">Full Docs</a>
23
+ </p>
24
+
25
+ ---
26
+
27
+ ## What is Tejas?
28
+
29
+ Tejas (meaning "radiance" in Hindi) is a modern, lightweight Node.js framework designed for building robust backend services. It offers an intuitive API with aviation-inspired naming conventions, making your code both expressive and enjoyable to write.
30
+
31
+ ```javascript
32
+ import Tejas, { Target } from 'te.js';
33
+
34
+ const app = new Tejas();
35
+ const api = new Target('/api');
36
+
37
+ api.register('/hello/:name', (ammo) => {
38
+ ammo.fire({ message: `Hello, ${ammo.payload.name}!` });
39
+ });
40
+
41
+ app.takeoff();
42
+ ```
43
+
44
+
45
+ ## Features
46
+
47
+ - **AI-Native (MCP)** — Ship with an MCP server so AI assistants can scaffold projects, generate routes, and write correct code with full framework knowledge
48
+ - **Simple Routing** — Clean, method-agnostic URL structures with parameterized routes
49
+ - **Express Compatible** — Use existing Express middleware alongside Tejas middleware
50
+ - **Zero-Config Error Handling** — No try-catch needed! Tejas catches all errors automatically. Opt in to have an LLM determine error code and message when you don't specify them (see [Error Handling](./docs/error-handling.md))
51
+ - **Built-in Rate Limiting** — Three algorithms (Token Bucket, Sliding Window, Fixed Window) with memory or Redis storage
52
+ - **Method Safety & CORS** — Opt-in method restriction per route (`register(path, { methods }, handler)` or `ammo.only('GET')`), global allowed-methods filter, and `app.withCORS()` for cross-origin requests
53
+ - **Database Ready** — First-class Redis and MongoDB support with auto-install of drivers
54
+ - **File Uploads** — Easy file handling with size limits and type validation
55
+ - **Auto-Documentation** — Generate OpenAPI specs from your code with LLM-powered analysis (`tejas generate:docs`)
56
+ - **Interactive API Docs** — Serve a Scalar API reference UI with `app.serveDocs()`
57
+ - **Auto-Discovery** — Automatic route registration from `.target.js` files
58
+ - **Request Logging** — Built-in HTTP request and exception logging
59
+
60
+
61
+ ## AI-Assisted Setup (MCP)
62
+
63
+ > **Recommended** — The best way to get started with Tejas in the age of AI.
64
+
65
+ The [Tejas MCP server](https://www.npmjs.com/package/tejas-mcp) gives your IDE's AI assistant full knowledge of the framework — documentation, code examples, and purpose-built tools to scaffold projects and generate correct code. No more hallucinated APIs.
66
+
67
+ **Cursor** — add this to `.cursor/mcp.json`:
68
+
69
+ ```json
70
+ {
71
+ "mcpServers": {
72
+ "tejas": {
73
+ "command": "npx",
74
+ "args": ["-y", "tejas-mcp"]
75
+ }
76
+ }
77
+ }
78
+ ```
79
+
80
+ **Other MCP-compatible IDEs** — run `npx tejas-mcp` as the server command (stdio transport, no config needed).
81
+
82
+ Once connected, prompt your AI with things like *"Scaffold a new te.js project called my-api"* or *"Create a REST API with user CRUD routes"* — the assistant will generate framework-correct code using real te.js patterns.
83
+
84
+
85
+ ## Quick Start
86
+
87
+ ### Install
88
+
89
+ ```bash
90
+ npm install te.js
91
+ ```
92
+
93
+ ### Create Your App
94
+
95
+ ```javascript
96
+ // index.js
97
+ import Tejas from 'te.js';
98
+
99
+ const app = new Tejas({ port: 3000 });
100
+ app.takeoff();
101
+ ```
102
+
103
+ ### Define Routes
104
+
105
+ ```javascript
106
+ // targets/user.target.js
107
+ import { Target } from 'te.js';
108
+
109
+ const users = new Target('/users');
110
+
111
+ users.register('/', (ammo) => {
112
+ if (ammo.GET) {
113
+ ammo.fire([{ id: 1, name: 'John' }]);
114
+ } else if (ammo.POST) {
115
+ const { name, email } = ammo.payload;
116
+ ammo.fire(201, { id: 2, name, email });
117
+ } else {
118
+ ammo.notAllowed();
119
+ }
120
+ });
121
+
122
+ users.register('/:id', (ammo) => {
123
+ const { id } = ammo.payload;
124
+ ammo.fire({ id, name: 'John Doe' });
125
+ });
126
+ ```
127
+
128
+ ### Run
129
+
130
+ ```bash
131
+ node index.js
132
+ # Server running at http://localhost:3000
133
+ ```
134
+
135
+
136
+ ## Core Concepts
137
+
138
+ | Tejas Term | Purpose | Express Equivalent |
139
+ | ----------- | ------------------------ | ------------------ |
140
+ | `Tejas` | Application instance | `express()` |
141
+ | `Target` | Route group/router | `Router()` |
142
+ | `Ammo` | Request/response context | `req` + `res` |
143
+ | `fire()` | Send response | `res.send()` |
144
+ | `midair()` | Register middleware | `use()` |
145
+ | `takeoff()` | Start server | `listen()` |
146
+
147
+
148
+ ## CLI
149
+
150
+ ```bash
151
+ tejas fly [file] # Start the server
152
+ tejas generate:docs [--ci] # Generate OpenAPI docs (interactive or CI mode)
153
+ tejas docs:on-push # Auto-generate docs when pushing to production branch
154
+ ```
155
+
156
+
157
+ ## API Documentation
158
+
159
+ Generate and serve interactive API docs:
160
+
161
+ ```bash
162
+ npx tejas generate:docs
163
+ ```
164
+
165
+ ```javascript
166
+ app.serveDocs({ specPath: './openapi.json' });
167
+ app.takeoff();
168
+ // Visit http://localhost:1403/docs
169
+ ```
170
+
171
+
172
+ ## Documentation
173
+
174
+ For comprehensive documentation, see the [docs folder](./docs) or visit [tejas-documentation.vercel.app](https://tejas-documentation.vercel.app).
175
+
176
+ - [Getting Started](./docs/getting-started.md) — Installation and quick start
177
+ - [Configuration](./docs/configuration.md) — All configuration options
178
+ - [Routing](./docs/routing.md) — Target-based routing system
179
+ - [Ammo](./docs/ammo.md) — Request/response handling
180
+ - [Middleware](./docs/middleware.md) — Global, target, and route middleware
181
+ - [Error Handling](./docs/error-handling.md) — Zero-config error handling
182
+ - [Database](./docs/database.md) — Redis and MongoDB integration
183
+ - [Rate Limiting](./docs/rate-limiting.md) — API protection
184
+ - [File Uploads](./docs/file-uploads.md) — File handling
185
+ - [CLI Reference](./docs/cli.md) — Command-line interface
186
+ - [Auto-Documentation](./docs/auto-docs.md) — OpenAPI generation
187
+ - [API Reference](./docs/api-reference.md) — Complete API docs
188
+
189
+
190
+ ## Contributing
191
+
192
+ Contributions are welcome! Please feel free to submit a Pull Request.
193
+
194
+
195
+ ## License
196
+
197
+ ISC © [Hirak Chhatbar](https://github.com/hirakchhatbar)
@@ -1,58 +1,58 @@
1
- /**
2
- * Handler analyzer for auto-documentation.
3
- * Detects HTTP methods and extracts basic info from handler source via handler.toString().
4
- * Used when endpoint metadata does not declare `methods`; otherwise explicit metadata wins.
5
- */
6
-
7
- const ALL_METHODS = [
8
- 'GET',
9
- 'POST',
10
- 'PUT',
11
- 'DELETE',
12
- 'PATCH',
13
- 'HEAD',
14
- 'OPTIONS',
15
- ];
16
-
17
- /**
18
- * Detects which HTTP methods the handler checks (e.g. ammo.GET, ammo.POST).
19
- * Matches property access patterns like `.GET`, `ammo.GET`, avoiding false positives
20
- * in strings or unrelated identifiers.
21
- *
22
- * When no method checks are found, the endpoint is treated as method-agnostic
23
- * and accepts ALL methods (te.js default behavior).
24
- *
25
- * @param {Function} handler - The endpoint handler function
26
- * @returns {string[]} Detected method names, or ALL_METHODS if none detected
27
- */
28
- function detectMethods(handler) {
29
- if (typeof handler !== 'function') return [...ALL_METHODS];
30
-
31
- const src = handler.toString();
32
- const detected = [];
33
-
34
- for (const m of ALL_METHODS) {
35
- // Match property access patterns like .GET, ammo.GET, avoiding
36
- // false positives in strings or unrelated identifiers
37
- const pattern = new RegExp(`\\.${m}\\b`);
38
- if (pattern.test(src)) detected.push(m);
39
- }
40
-
41
- return detected.length > 0 ? detected : [...ALL_METHODS];
42
- }
43
-
44
- /**
45
- * Analyzes a handler and returns basic info for documentation (e.g. OpenAPI).
46
- * Explicit metadata from register() should override these results when present.
47
- *
48
- * @param {Function} handler - The endpoint handler function
49
- * @returns {{ methods: string[] }} Analyzed info (methods; more fields can be added later)
50
- */
51
- function analyzeHandler(handler) {
52
- return {
53
- methods: detectMethods(handler),
54
- };
55
- }
56
-
57
- export { ALL_METHODS, detectMethods, analyzeHandler };
58
- export default analyzeHandler;
1
+ /**
2
+ * Handler analyzer for auto-documentation.
3
+ * Detects HTTP methods and extracts basic info from handler source via handler.toString().
4
+ * Used when endpoint metadata does not declare `methods`; otherwise explicit metadata wins.
5
+ */
6
+
7
+ const ALL_METHODS = [
8
+ 'GET',
9
+ 'POST',
10
+ 'PUT',
11
+ 'DELETE',
12
+ 'PATCH',
13
+ 'HEAD',
14
+ 'OPTIONS',
15
+ ];
16
+
17
+ /**
18
+ * Detects which HTTP methods the handler checks (e.g. ammo.GET, ammo.POST).
19
+ * Matches property access patterns like `.GET`, `ammo.GET`, avoiding false positives
20
+ * in strings or unrelated identifiers.
21
+ *
22
+ * When no method checks are found, the endpoint is treated as method-agnostic
23
+ * and accepts ALL methods (te.js default behavior).
24
+ *
25
+ * @param {Function} handler - The endpoint handler function
26
+ * @returns {string[]} Detected method names, or ALL_METHODS if none detected
27
+ */
28
+ function detectMethods(handler) {
29
+ if (typeof handler !== 'function') return [...ALL_METHODS];
30
+
31
+ const src = handler.toString();
32
+ const detected = [];
33
+
34
+ for (const m of ALL_METHODS) {
35
+ // Match property access patterns like .GET, ammo.GET, avoiding
36
+ // false positives in strings or unrelated identifiers
37
+ const pattern = new RegExp(`\\.${m}\\b`);
38
+ if (pattern.test(src)) detected.push(m);
39
+ }
40
+
41
+ return detected.length > 0 ? detected : [...ALL_METHODS];
42
+ }
43
+
44
+ /**
45
+ * Analyzes a handler and returns basic info for documentation (e.g. OpenAPI).
46
+ * Explicit metadata from register() should override these results when present.
47
+ *
48
+ * @param {Function} handler - The endpoint handler function
49
+ * @returns {{ methods: string[] }} Analyzed info (methods; more fields can be added later)
50
+ */
51
+ function analyzeHandler(handler) {
52
+ return {
53
+ methods: detectMethods(handler),
54
+ };
55
+ }
56
+
57
+ export { ALL_METHODS, detectMethods, analyzeHandler };
58
+ export default analyzeHandler;