te.js 2.0.3 → 2.1.1

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 (68) hide show
  1. package/README.md +197 -187
  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 -0
  6. package/auto-docs/{llm → docs-llm}/prompts.js +222 -222
  7. package/auto-docs/{llm → docs-llm}/provider.js +132 -187
  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/database/index.js +165 -165
  20. package/database/mongodb.js +146 -146
  21. package/database/redis.js +201 -201
  22. package/docs/README.md +36 -36
  23. package/docs/ammo.md +362 -362
  24. package/docs/api-reference.md +490 -489
  25. package/docs/auto-docs.md +216 -215
  26. package/docs/cli.md +152 -152
  27. package/docs/configuration.md +275 -233
  28. package/docs/database.md +390 -391
  29. package/docs/error-handling.md +438 -417
  30. package/docs/file-uploads.md +333 -334
  31. package/docs/getting-started.md +214 -215
  32. package/docs/middleware.md +355 -356
  33. package/docs/rate-limiting.md +393 -394
  34. package/docs/routing.md +302 -302
  35. package/package.json +62 -62
  36. package/rate-limit/algorithms/fixed-window.js +141 -141
  37. package/rate-limit/algorithms/sliding-window.js +147 -147
  38. package/rate-limit/algorithms/token-bucket.js +115 -115
  39. package/rate-limit/base.js +165 -165
  40. package/rate-limit/index.js +147 -147
  41. package/rate-limit/storage/base.js +104 -104
  42. package/rate-limit/storage/memory.js +101 -101
  43. package/rate-limit/storage/redis.js +88 -88
  44. package/server/ammo/body-parser.js +220 -220
  45. package/server/ammo/dispatch-helper.js +103 -103
  46. package/server/ammo/enhancer.js +57 -57
  47. package/server/ammo.js +454 -356
  48. package/server/endpoint.js +97 -74
  49. package/server/error.js +9 -9
  50. package/server/errors/code-context.js +125 -0
  51. package/server/errors/llm-error-service.js +140 -0
  52. package/server/files/helper.js +33 -33
  53. package/server/files/uploader.js +143 -143
  54. package/server/handler.js +158 -113
  55. package/server/target.js +185 -175
  56. package/server/targets/middleware-validator.js +22 -22
  57. package/server/targets/path-validator.js +21 -21
  58. package/server/targets/registry.js +160 -160
  59. package/server/targets/shoot-validator.js +21 -21
  60. package/te.js +428 -363
  61. package/utils/auto-register.js +17 -17
  62. package/utils/configuration.js +64 -64
  63. package/utils/errors-llm-config.js +84 -0
  64. package/utils/request-logger.js +43 -43
  65. package/utils/status-codes.js +82 -82
  66. package/utils/tejas-entrypoint-html.js +18 -18
  67. package/auto-docs/llm/index.js +0 -6
  68. package/auto-docs/llm/parse.js +0 -88
package/README.md CHANGED
@@ -1,187 +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
- ## Features
45
-
46
- - **AI-Native (MCP)** — Ship with an MCP server so AI assistants can scaffold projects, generate routes, and write correct code with full framework knowledge
47
- - **Simple Routing** — Clean, method-agnostic URL structures with parameterized routes
48
- - **Express Compatible** — Use existing Express middleware alongside Tejas middleware
49
- - **Zero-Config Error Handling** — No try-catch needed! Tejas catches all errors automatically
50
- - **Built-in Rate Limiting** — Three algorithms (Token Bucket, Sliding Window, Fixed Window) with memory or Redis storage
51
- - **Database Ready** — First-class Redis and MongoDB support with auto-install of drivers
52
- - **File Uploads** — Easy file handling with size limits and type validation
53
- - **Auto-Documentation** — Generate OpenAPI specs from your code with LLM-powered analysis (`tejas generate:docs`)
54
- - **Interactive API Docs** — Serve a Scalar API reference UI with `app.serveDocs()`
55
- - **Auto-Discovery** — Automatic route registration from `.target.js` files
56
- - **Request Logging** — Built-in HTTP request and exception logging
57
-
58
- ## AI-Assisted Setup (MCP)
59
-
60
- > **Recommended** — The best way to get started with Tejas in the age of AI.
61
-
62
- 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.
63
-
64
- **Cursor** — add this to `.cursor/mcp.json`:
65
-
66
- ```json
67
- {
68
- "mcpServers": {
69
- "tejas": {
70
- "command": "npx",
71
- "args": ["-y", "tejas-mcp"]
72
- }
73
- }
74
- }
75
- ```
76
-
77
- **Other MCP-compatible IDEs** — run `npx tejas-mcp` as the server command (stdio transport, no config needed).
78
-
79
- 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.
80
-
81
- ## Quick Start
82
-
83
- ### Install
84
-
85
- ```bash
86
- npm install te.js
87
- ```
88
-
89
- ### Create Your App
90
-
91
- ```javascript
92
- // index.js
93
- import Tejas from 'te.js';
94
-
95
- const app = new Tejas({ port: 3000 });
96
- app.takeoff();
97
- ```
98
-
99
- ### Define Routes
100
-
101
- ```javascript
102
- // targets/user.target.js
103
- import { Target } from 'te.js';
104
-
105
- const users = new Target('/users');
106
-
107
- users.register('/', (ammo) => {
108
- if (ammo.GET) {
109
- ammo.fire([{ id: 1, name: 'John' }]);
110
- } else if (ammo.POST) {
111
- const { name, email } = ammo.payload;
112
- ammo.fire(201, { id: 2, name, email });
113
- } else {
114
- ammo.notAllowed();
115
- }
116
- });
117
-
118
- users.register('/:id', (ammo) => {
119
- const { id } = ammo.payload;
120
- ammo.fire({ id, name: 'John Doe' });
121
- });
122
- ```
123
-
124
- ### Run
125
-
126
- ```bash
127
- node index.js
128
- # Server running at http://localhost:3000
129
- ```
130
-
131
- ## Core Concepts
132
-
133
- | Tejas Term | Purpose | Express Equivalent |
134
- | ----------- | ------------------------ | ------------------ |
135
- | `Tejas` | Application instance | `express()` |
136
- | `Target` | Route group/router | `Router()` |
137
- | `Ammo` | Request/response context | `req` + `res` |
138
- | `fire()` | Send response | `res.send()` |
139
- | `midair()` | Register middleware | `use()` |
140
- | `takeoff()` | Start server | `listen()` |
141
-
142
- ## CLI
143
-
144
- ```bash
145
- tejas fly [file] # Start the server
146
- tejas generate:docs [--ci] # Generate OpenAPI docs (interactive or CI mode)
147
- tejas docs:on-push # Auto-generate docs when pushing to production branch
148
- ```
149
-
150
- ## API Documentation
151
-
152
- Generate and serve interactive API docs:
153
-
154
- ```bash
155
- npx tejas generate:docs
156
- ```
157
-
158
- ```javascript
159
- app.serveDocs({ specPath: './openapi.json' });
160
- app.takeoff();
161
- // Visit http://localhost:1403/docs
162
- ```
163
-
164
- ## Documentation
165
-
166
- For comprehensive documentation, see the [docs folder](./docs) or visit [tejas-documentation.vercel.app](https://tejas-documentation.vercel.app).
167
-
168
- - [Getting Started](./docs/getting-started.md) — Installation and quick start
169
- - [Configuration](./docs/configuration.md) — All configuration options
170
- - [Routing](./docs/routing.md) — Target-based routing system
171
- - [Ammo](./docs/ammo.md) — Request/response handling
172
- - [Middleware](./docs/middleware.md) — Global, target, and route middleware
173
- - [Error Handling](./docs/error-handling.md) — Zero-config error handling
174
- - [Database](./docs/database.md) Redis and MongoDB integration
175
- - [Rate Limiting](./docs/rate-limiting.md) — API protection
176
- - [File Uploads](./docs/file-uploads.md) — File handling
177
- - [CLI Reference](./docs/cli.md) — Command-line interface
178
- - [Auto-Documentation](./docs/auto-docs.md) — OpenAPI generation
179
- - [API Reference](./docs/api-reference.md) — Complete API docs
180
-
181
- ## Contributing
182
-
183
- Contributions are welcome! Please feel free to submit a Pull Request.
184
-
185
- ## License
186
-
187
- 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;