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/docs/cli.md ADDED
@@ -0,0 +1,152 @@
1
+ # CLI Reference
2
+
3
+ Tejas includes a command-line interface for starting your server and generating API documentation.
4
+
5
+ ## Installation
6
+
7
+ The CLI is included when you install te.js. It is available as the `tejas` command:
8
+
9
+ ```bash
10
+ npm install te.js
11
+ ```
12
+
13
+ If installed locally, use `npx tejas` to run commands.
14
+
15
+ ## Commands
16
+
17
+ ### tejas fly
18
+
19
+ Start the Tejas server by running your application's entry point.
20
+
21
+ ```bash
22
+ tejas fly [file]
23
+ ```
24
+
25
+ **Entry point resolution** (first match wins):
26
+
27
+ 1. CLI argument — `tejas fly app.js`
28
+ 2. `tejas.config.json` — `"entry": "src/index.js"`
29
+ 3. `package.json` — `"main": "index.js"`
30
+ 4. Convention files — `index.js`, `app.js`, or `server.js` in the current directory
31
+
32
+ **Examples:**
33
+
34
+ ```bash
35
+ # Use automatic entry point resolution
36
+ npx tejas fly
37
+
38
+ # Specify an entry file explicitly
39
+ npx tejas fly src/server.js
40
+ ```
41
+
42
+ The server process inherits your environment and current working directory. Press `Ctrl+C` to stop.
43
+
44
+ ---
45
+
46
+ ### tejas generate:docs
47
+
48
+ Generate an OpenAPI 3.0 specification from your registered targets using LLM-powered analysis.
49
+
50
+ ```bash
51
+ tejas generate:docs [--ci]
52
+ ```
53
+
54
+ #### Interactive Mode (default)
55
+
56
+ Walks you through configuration step by step:
57
+
58
+ ```bash
59
+ npx tejas generate:docs
60
+ ```
61
+
62
+ You will be prompted for:
63
+
64
+ | Prompt | Default | Description |
65
+ |--------|---------|-------------|
66
+ | Targets directory | `targets` | Directory containing `.target.js` files |
67
+ | Output file | `./openapi.json` | Where to write the OpenAPI spec |
68
+ | API title | `API` | Title in the spec's `info` block |
69
+ | API version | `1.0.0` | Version in the spec's `info` block |
70
+ | API description | *(empty)* | Optional description |
71
+ | LLM provider base URL | `https://api.openai.com/v1` | Any OpenAI-compatible endpoint |
72
+ | API key | *(from env)* | LLM provider API key |
73
+ | Model | `gpt-4o-mini` | LLM model name |
74
+ | Token usage level | `1` | Enhancement depth (1–3) |
75
+ | Preview docs? | No | Serve a live Scalar UI preview |
76
+
77
+ After generation, if you chose to preview, a local server starts on port 3333 (configurable via the `DOCS_PORT` env var).
78
+
79
+ #### CI Mode
80
+
81
+ Non-interactive mode for automated pipelines. Uses configuration from `tejas.config.json` and environment variables:
82
+
83
+ ```bash
84
+ npx tejas generate:docs --ci
85
+ ```
86
+
87
+ **Required environment:**
88
+
89
+ | Variable | Fallback | Description |
90
+ |----------|----------|-------------|
91
+ | `LLM_API_KEY` | `OPENAI_API_KEY` | LLM provider API key |
92
+
93
+ **Optional environment:**
94
+
95
+ | Variable | Default | Description |
96
+ |----------|---------|-------------|
97
+ | `LLM_BASE_URL` | `https://api.openai.com/v1` | LLM provider base URL |
98
+ | `LLM_MODEL` | `gpt-4o-mini` | LLM model name |
99
+
100
+ All other options are read from the `docs` section of `tejas.config.json`. See [Auto-Documentation](./auto-docs.md) for the full config reference.
101
+
102
+ ---
103
+
104
+ ### tejas docs:on-push
105
+
106
+ Generate documentation automatically when pushing to your production branch. Designed for use in a git `pre-push` hook.
107
+
108
+ ```bash
109
+ tejas docs:on-push
110
+ ```
111
+
112
+ This command reads git's pre-push stdin to determine which branch is being pushed. If it matches the configured production branch, it runs `generate:docs` in CI mode.
113
+
114
+ **Setup with Husky:**
115
+
116
+ ```json
117
+ // package.json
118
+ {
119
+ "husky": {
120
+ "hooks": {
121
+ "pre-push": "tejas docs:on-push"
122
+ }
123
+ }
124
+ }
125
+ ```
126
+
127
+ **Configuration:**
128
+
129
+ | Source | Key | Default |
130
+ |--------|-----|---------|
131
+ | `tejas.config.json` | `docs.productionBranch` | `"main"` |
132
+ | Environment variable | `DOCS_PRODUCTION_BRANCH` | `"main"` |
133
+
134
+ If the push is not targeting the production branch, the command exits silently with no action.
135
+
136
+ ---
137
+
138
+ ## Environment Variables Summary
139
+
140
+ | Variable | Used By | Description |
141
+ |----------|---------|-------------|
142
+ | `LLM_API_KEY` | `generate:docs`, `docs:on-push` | LLM provider API key |
143
+ | `OPENAI_API_KEY` | `generate:docs`, `docs:on-push` | Fallback API key |
144
+ | `LLM_BASE_URL` | `generate:docs`, `docs:on-push` | LLM provider endpoint |
145
+ | `LLM_MODEL` | `generate:docs`, `docs:on-push` | LLM model name |
146
+ | `DOCS_PORT` | `generate:docs` | Preview server port (default `3333`) |
147
+ | `DOCS_PRODUCTION_BRANCH` | `docs:on-push` | Branch name to trigger generation |
148
+
149
+ ## Next Steps
150
+
151
+ - [Auto-Documentation](./auto-docs.md) — Enhancement levels, endpoint metadata, and Scalar UI
152
+ - [Configuration](./configuration.md) — Full `tejas.config.json` reference
@@ -0,0 +1,233 @@
1
+ # Configuration
2
+
3
+ Tejas provides a flexible configuration system that merges settings from multiple sources.
4
+
5
+ ## Configuration Sources
6
+
7
+ Settings are loaded from three sources. Later sources override earlier ones:
8
+
9
+ 1. **`tejas.config.json`** — file in your project root (lowest priority)
10
+ 2. **Environment variables** — from `.env` or system environment
11
+ 3. **Constructor options** — passed to `new Tejas({...})` (highest priority)
12
+
13
+ All configuration keys are normalized: nested objects are flattened with underscores and uppercased. For example, `log.http_requests` becomes the environment variable `LOG_HTTP_REQUESTS`.
14
+
15
+ ## Quick Start
16
+
17
+ The simplest way to configure Tejas is with a `tejas.config.json` file:
18
+
19
+ ```json
20
+ {
21
+ "port": 3000,
22
+ "dir": {
23
+ "targets": "targets"
24
+ },
25
+ "log": {
26
+ "http_requests": true,
27
+ "exceptions": true
28
+ }
29
+ }
30
+ ```
31
+
32
+ Or pass options directly to the constructor:
33
+
34
+ ```javascript
35
+ import Tejas from 'te.js';
36
+
37
+ const app = new Tejas({
38
+ port: 3000,
39
+ log: { http_requests: true }
40
+ });
41
+
42
+ app.takeoff();
43
+ ```
44
+
45
+ ## Complete Configuration Reference
46
+
47
+ ### Core
48
+
49
+ | Config Key | Env Variable | Type | Default | Description |
50
+ |------------|-------------|------|---------|-------------|
51
+ | `entry` | `ENTRY` | string | *(auto-resolved)* | Entry file for `tejas fly`. Falls back to `package.json` `main`, then `index.js` / `app.js` / `server.js` |
52
+ | `port` | `PORT` | number | `1403` | Server port |
53
+ | `dir.targets` | `DIR_TARGETS` | string | `"targets"` | Directory containing `.target.js` files for auto-discovery |
54
+
55
+ ### Logging
56
+
57
+ | Config Key | Env Variable | Type | Default | Description |
58
+ |------------|-------------|------|---------|-------------|
59
+ | `log.http_requests` | `LOG_HTTP_REQUESTS` | boolean | `false` | Log incoming HTTP requests (method, path, status, time) |
60
+ | `log.exceptions` | `LOG_EXCEPTIONS` | boolean | `false` | Log unhandled exceptions and errors |
61
+
62
+ ### Request Body
63
+
64
+ | Config Key | Env Variable | Type | Default | Description |
65
+ |------------|-------------|------|---------|-------------|
66
+ | `body.max_size` | `BODY_MAX_SIZE` | number | `10485760` (10 MB) | Maximum request body size in bytes. Requests exceeding this receive a 413 error |
67
+ | `body.timeout` | `BODY_TIMEOUT` | number | `30000` (30 s) | Body parsing timeout in milliseconds. Requests exceeding this receive a 408 error |
68
+
69
+ ### Auto-Documentation
70
+
71
+ These options configure the `tejas generate:docs` CLI command and the auto-documentation system. See [Auto-Documentation](./auto-docs.md) for full details.
72
+
73
+ | Config Key | Env Variable | Type | Default | Description |
74
+ |------------|-------------|------|---------|-------------|
75
+ | `docs.dirTargets` | `DOCS_DIR_TARGETS` | string | `"targets"` | Target directory for doc generation (can differ from `dir.targets`) |
76
+ | `docs.output` | — | string | `"./openapi.json"` | Output path for the generated OpenAPI spec |
77
+ | `docs.title` | — | string | `"API"` | API title in the OpenAPI `info` block |
78
+ | `docs.version` | — | string | `"1.0.0"` | API version in the OpenAPI `info` block |
79
+ | `docs.description` | — | string | `""` | API description |
80
+ | `docs.level` | — | number | `1` | LLM enhancement level (1–3). Higher = better docs, more tokens |
81
+ | `docs.llm.baseURL` | `LLM_BASE_URL` | string | `"https://api.openai.com/v1"` | LLM provider endpoint |
82
+ | `docs.llm.apiKey` | `LLM_API_KEY` | string | — | LLM provider API key |
83
+ | `docs.llm.model` | `LLM_MODEL` | string | `"gpt-4o-mini"` | LLM model name |
84
+ | `docs.overviewPath` | — | string | `"./API_OVERVIEW.md"` | Path for the generated overview page (level 3 only) |
85
+ | `docs.productionBranch` | `DOCS_PRODUCTION_BRANCH` | string | `"main"` | Git branch that triggers `docs:on-push` |
86
+
87
+ ## Configuration File
88
+
89
+ Create a `tejas.config.json` in your project root:
90
+
91
+ ```json
92
+ {
93
+ "entry": "app.js",
94
+ "port": 3000,
95
+ "dir": {
96
+ "targets": "targets"
97
+ },
98
+ "log": {
99
+ "http_requests": true,
100
+ "exceptions": true
101
+ },
102
+ "body": {
103
+ "max_size": 5242880,
104
+ "timeout": 15000
105
+ },
106
+ "docs": {
107
+ "output": "./openapi.json",
108
+ "title": "My API",
109
+ "version": "1.0.0",
110
+ "level": 2,
111
+ "productionBranch": "main"
112
+ }
113
+ }
114
+ ```
115
+
116
+ ## Environment Variables
117
+
118
+ Create a `.env` file in your project root. Tejas uses [tej-env](https://www.npmjs.com/package/tej-env) to load it automatically:
119
+
120
+ ```bash
121
+ # Server
122
+ PORT=3000
123
+
124
+ # Logging
125
+ LOG_HTTP_REQUESTS=true
126
+ LOG_EXCEPTIONS=true
127
+
128
+ # Body limits
129
+ BODY_MAX_SIZE=5242880
130
+ BODY_TIMEOUT=15000
131
+
132
+ # Target directory
133
+ DIR_TARGETS=targets
134
+
135
+ # LLM (for tejas generate:docs)
136
+ LLM_BASE_URL=https://api.openai.com/v1
137
+ LLM_API_KEY=sk-...
138
+ LLM_MODEL=gpt-4o-mini
139
+ ```
140
+
141
+ ## Constructor Options
142
+
143
+ Pass an object to `new Tejas()` using the same nested structure as `tejas.config.json`:
144
+
145
+ ```javascript
146
+ import Tejas from 'te.js';
147
+
148
+ const app = new Tejas({
149
+ port: 3000,
150
+ log: {
151
+ http_requests: true,
152
+ exceptions: true
153
+ },
154
+ body: {
155
+ max_size: 10 * 1024 * 1024,
156
+ timeout: 30000
157
+ }
158
+ });
159
+ ```
160
+
161
+ Constructor options have the highest priority and override both the config file and environment variables.
162
+
163
+ ## How Merging Works
164
+
165
+ All configuration is flattened into a single-level object with uppercased keys:
166
+
167
+ ```javascript
168
+ // tejas.config.json
169
+ { "log": { "http_requests": true } }
170
+
171
+ // Becomes accessible as:
172
+ env('LOG_HTTP_REQUESTS') // true
173
+ ```
174
+
175
+ The merge order is: config file values, then env vars override those, then constructor options override everything.
176
+
177
+ ## Accessing Configuration at Runtime
178
+
179
+ Use the `env()` function from `tej-env` to read any configuration value:
180
+
181
+ ```javascript
182
+ import { env } from 'tej-env';
183
+
184
+ target.register('/info', (ammo) => {
185
+ ammo.fire({
186
+ port: env('PORT'),
187
+ maxBodySize: env('BODY_MAX_SIZE')
188
+ });
189
+ });
190
+ ```
191
+
192
+ ## Auto-Registration
193
+
194
+ Tejas automatically discovers and imports all files ending in `.target.js` from the configured `dir.targets` directory (recursively, including subdirectories):
195
+
196
+ ```
197
+ targets/
198
+ ├── user.target.js --> Auto-loaded
199
+ ├── auth.target.js --> Auto-loaded
200
+ ├── utils.js --> Ignored (wrong suffix)
201
+ └── api/
202
+ └── v1.target.js --> Auto-loaded
203
+ ```
204
+
205
+ To change the targets directory:
206
+
207
+ ```json
208
+ {
209
+ "dir": {
210
+ "targets": "src/routes"
211
+ }
212
+ }
213
+ ```
214
+
215
+ ## Database Configuration
216
+
217
+ Database connections are configured via `takeoff()` options, not through the config file:
218
+
219
+ ```javascript
220
+ app.takeoff({
221
+ withRedis: { url: 'redis://localhost:6379' },
222
+ withMongo: { uri: 'mongodb://localhost:27017/myapp' }
223
+ });
224
+ ```
225
+
226
+ See [Database Integration](./database.md) for details.
227
+
228
+ ## Next Steps
229
+
230
+ - [Getting Started](./getting-started.md) — Build your first Tejas application
231
+ - [Routing](./routing.md) — Define routes and endpoints
232
+ - [Auto-Documentation](./auto-docs.md) — Generate OpenAPI docs from your code
233
+ - [CLI Reference](./cli.md) — `tejas fly` and doc generation commands