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.
- package/README.md +197 -196
- package/auto-docs/analysis/handler-analyzer.js +58 -58
- package/auto-docs/analysis/source-resolver.js +101 -101
- package/auto-docs/constants.js +37 -37
- package/auto-docs/docs-llm/index.js +7 -7
- package/auto-docs/docs-llm/prompts.js +222 -222
- package/auto-docs/docs-llm/provider.js +132 -132
- package/auto-docs/index.js +146 -146
- package/auto-docs/openapi/endpoint-processor.js +277 -277
- package/auto-docs/openapi/generator.js +107 -107
- package/auto-docs/openapi/level3.js +131 -131
- package/auto-docs/openapi/spec-builders.js +244 -244
- package/auto-docs/ui/docs-ui.js +186 -186
- package/auto-docs/utils/logger.js +17 -17
- package/auto-docs/utils/strip-usage.js +10 -10
- package/cli/docs-command.js +315 -315
- package/cli/fly-command.js +71 -71
- package/cli/index.js +56 -56
- package/cors/index.js +71 -0
- package/database/index.js +165 -165
- package/database/mongodb.js +146 -146
- package/database/redis.js +201 -201
- package/docs/README.md +36 -36
- package/docs/ammo.md +362 -362
- package/docs/api-reference.md +490 -490
- package/docs/auto-docs.md +216 -216
- package/docs/cli.md +152 -152
- package/docs/configuration.md +275 -275
- package/docs/database.md +390 -390
- package/docs/error-handling.md +438 -438
- package/docs/file-uploads.md +333 -333
- package/docs/getting-started.md +214 -214
- package/docs/middleware.md +355 -355
- package/docs/rate-limiting.md +393 -393
- package/docs/routing.md +302 -302
- package/lib/llm/client.js +73 -0
- package/lib/llm/index.js +7 -0
- package/lib/llm/parse.js +89 -0
- package/package.json +64 -62
- package/rate-limit/algorithms/fixed-window.js +141 -141
- package/rate-limit/algorithms/sliding-window.js +147 -147
- package/rate-limit/algorithms/token-bucket.js +115 -115
- package/rate-limit/base.js +165 -165
- package/rate-limit/index.js +147 -147
- package/rate-limit/storage/base.js +104 -104
- package/rate-limit/storage/memory.js +101 -101
- package/rate-limit/storage/redis.js +88 -88
- package/server/ammo/body-parser.js +220 -220
- package/server/ammo/dispatch-helper.js +103 -103
- package/server/ammo/enhancer.js +57 -57
- package/server/ammo.js +454 -415
- package/server/endpoint.js +97 -74
- package/server/error.js +9 -9
- package/server/errors/code-context.js +125 -125
- package/server/errors/llm-error-service.js +140 -140
- package/server/files/helper.js +33 -33
- package/server/files/uploader.js +143 -143
- package/server/handler.js +158 -119
- package/server/target.js +185 -175
- package/server/targets/middleware-validator.js +22 -22
- package/server/targets/path-validator.js +21 -21
- package/server/targets/registry.js +160 -160
- package/server/targets/shoot-validator.js +21 -21
- package/te.js +428 -402
- package/utils/auto-register.js +17 -17
- package/utils/configuration.js +64 -64
- package/utils/errors-llm-config.js +84 -84
- package/utils/request-logger.js +43 -43
- package/utils/status-codes.js +82 -82
- 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
|
-
- **
|
|
53
|
-
- **
|
|
54
|
-
- **
|
|
55
|
-
- **
|
|
56
|
-
- **
|
|
57
|
-
- **
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
"
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
app
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
|
139
|
-
|
|
|
140
|
-
| `
|
|
141
|
-
| `
|
|
142
|
-
| `
|
|
143
|
-
| `
|
|
144
|
-
| `
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
tejas
|
|
152
|
-
tejas docs
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
app.
|
|
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
|
-
|
|
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;
|