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
@@ -1,275 +1,275 @@
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
- ### LLM configuration (feature as parent, LLM inside each feature)
70
-
71
- Tejas uses a **feature-as-parent** pattern: each feature that needs an LLM has its own `*.llm` block (`docs.llm` for auto-documentation, `errors.llm` for LLM-inferred errors). **Inheritance from `LLM_*`:** unset feature-specific values fall back to `LLM_BASE_URL`, `LLM_API_KEY`, and `LLM_MODEL`. One set of `LLM_*` env vars can serve both features when you don't override with `DOCS_LLM_*` or `ERRORS_LLM_*`. You can also use different LLMs per feature (e.g. a lighter model for errors, a stronger one for docs).
72
-
73
- ### Auto-Documentation
74
-
75
- These options configure the `tejas generate:docs` CLI command and the auto-documentation system. The **`docs.llm`** block is the LLM configuration for this feature. See [Auto-Documentation](./auto-docs.md) for full details.
76
-
77
- | Config Key | Env Variable | Type | Default | Description |
78
- |------------|-------------|------|---------|-------------|
79
- | `docs.dirTargets` | `DOCS_DIR_TARGETS` | string | `"targets"` | Target directory for doc generation (can differ from `dir.targets`) |
80
- | `docs.output` | — | string | `"./openapi.json"` | Output path for the generated OpenAPI spec |
81
- | `docs.title` | — | string | `"API"` | API title in the OpenAPI `info` block |
82
- | `docs.version` | — | string | `"1.0.0"` | API version in the OpenAPI `info` block |
83
- | `docs.description` | — | string | `""` | API description |
84
- | `docs.level` | — | number | `1` | LLM enhancement level (1–3). Higher = better docs, more tokens |
85
- | `docs.llm.baseURL` | `DOCS_LLM_BASE_URL` or `LLM_BASE_URL` | string | `"https://api.openai.com/v1"` | LLM provider endpoint for auto-docs |
86
- | `docs.llm.apiKey` | `DOCS_LLM_API_KEY` or `LLM_API_KEY` | string | — | LLM provider API key for auto-docs |
87
- | `docs.llm.model` | `DOCS_LLM_MODEL` or `LLM_MODEL` | string | `"gpt-4o-mini"` | LLM model for auto-docs |
88
- | `docs.overviewPath` | — | string | `"./API_OVERVIEW.md"` | Path for the generated overview page (level 3 only) |
89
- | `docs.productionBranch` | `DOCS_PRODUCTION_BRANCH` | string | `"main"` | Git branch that triggers `docs:on-push` |
90
-
91
- ### Error handling (LLM-inferred errors)
92
-
93
- When [LLM-inferred error codes and messages](./error-handling.md#llm-inferred-errors) are enabled, the **`errors.llm`** block configures the LLM used for inferring status code and message when you call `ammo.throw()` without explicit code or message. Unset values fall back to `LLM_BASE_URL`, `LLM_API_KEY`, `LLM_MODEL`. You can also enable (and optionally set connection options) by calling **`app.withLLMErrors(config?)`** before `takeoff()` — e.g. `app.withLLMErrors()` to use env/config for baseURL, apiKey, model, or `app.withLLMErrors({ baseURL, apiKey, model, messageType })` to override in code.
94
-
95
- | Config Key | Env Variable | Type | Default | Description |
96
- |------------|-------------|------|---------|-------------|
97
- | `errors.llm.enabled` | `ERRORS_LLM_ENABLED` or `LLM_*` (for connection) | boolean | `false` | Enable LLM-inferred error code and message for `ammo.throw()` |
98
- | `errors.llm.baseURL` | `ERRORS_LLM_BASE_URL` or `LLM_BASE_URL` | string | `"https://api.openai.com/v1"` | LLM provider endpoint for error inference |
99
- | `errors.llm.apiKey` | `ERRORS_LLM_API_KEY` or `LLM_API_KEY` | string | — | LLM provider API key for error inference |
100
- | `errors.llm.model` | `ERRORS_LLM_MODEL` or `LLM_MODEL` | string | `"gpt-4o-mini"` | LLM model for error inference |
101
- | `errors.llm.messageType` | `ERRORS_LLM_MESSAGE_TYPE` or `LLM_MESSAGE_TYPE` | `"endUser"` \| `"developer"` | `"endUser"` | Default tone for LLM-generated message: `endUser` (safe for clients) or `developer` (technical detail). Overridable per `ammo.throw()` call. |
102
-
103
- When enabled, the same behaviour applies whether you call `ammo.throw()` or the framework calls it when it catches an error — one mechanism, no separate config.
104
-
105
- ## Configuration File
106
-
107
- Create a `tejas.config.json` in your project root:
108
-
109
- ```json
110
- {
111
- "entry": "app.js",
112
- "port": 3000,
113
- "dir": {
114
- "targets": "targets"
115
- },
116
- "log": {
117
- "http_requests": true,
118
- "exceptions": true
119
- },
120
- "body": {
121
- "max_size": 5242880,
122
- "timeout": 15000
123
- },
124
- "docs": {
125
- "output": "./openapi.json",
126
- "title": "My API",
127
- "version": "1.0.0",
128
- "level": 2,
129
- "productionBranch": "main",
130
- "llm": {
131
- "baseURL": "https://api.openai.com/v1",
132
- "model": "gpt-4o-mini"
133
- }
134
- },
135
- "errors": {
136
- "llm": {
137
- "enabled": true,
138
- "baseURL": "https://api.openai.com/v1",
139
- "model": "gpt-4o-mini",
140
- "messageType": "endUser"
141
- }
142
- }
143
- }
144
- ```
145
-
146
- ## Environment Variables
147
-
148
- Create a `.env` file in your project root. Tejas uses [tej-env](https://www.npmjs.com/package/tej-env) to load it automatically:
149
-
150
- ```bash
151
- # Server
152
- PORT=3000
153
-
154
- # Logging
155
- LOG_HTTP_REQUESTS=true
156
- LOG_EXCEPTIONS=true
157
-
158
- # Body limits
159
- BODY_MAX_SIZE=5242880
160
- BODY_TIMEOUT=15000
161
-
162
- # Target directory
163
- DIR_TARGETS=targets
164
-
165
- # LLM — shared fallback for docs.llm and errors.llm when feature-specific vars are unset
166
- LLM_BASE_URL=https://api.openai.com/v1
167
- LLM_API_KEY=sk-...
168
- LLM_MODEL=gpt-4o-mini
169
-
170
- # Optional: override per feature (docs.llm)
171
- # DOCS_LLM_BASE_URL=...
172
- # DOCS_LLM_API_KEY=...
173
- # DOCS_LLM_MODEL=...
174
-
175
- # Optional: override for error-inference (errors.llm)
176
- # ERRORS_LLM_ENABLED=true
177
- # ERRORS_LLM_BASE_URL=...
178
- # ERRORS_LLM_API_KEY=...
179
- # ERRORS_LLM_MODEL=...
180
- # ERRORS_LLM_MESSAGE_TYPE=endUser # or "developer" for technical messages
181
- ```
182
-
183
- ## Constructor Options
184
-
185
- Pass an object to `new Tejas()` using the same nested structure as `tejas.config.json`:
186
-
187
- ```javascript
188
- import Tejas from 'te.js';
189
-
190
- const app = new Tejas({
191
- port: 3000,
192
- log: {
193
- http_requests: true,
194
- exceptions: true
195
- },
196
- body: {
197
- max_size: 10 * 1024 * 1024,
198
- timeout: 30000
199
- }
200
- });
201
- ```
202
-
203
- Constructor options have the highest priority and override both the config file and environment variables.
204
-
205
- ## How Merging Works
206
-
207
- All configuration is flattened into a single-level object with uppercased keys:
208
-
209
- ```javascript
210
- // tejas.config.json
211
- { "log": { "http_requests": true } }
212
-
213
- // Becomes accessible as:
214
- env('LOG_HTTP_REQUESTS') // true
215
- ```
216
-
217
- The merge order is: config file values, then env vars override those, then constructor options override everything.
218
-
219
- ## Accessing Configuration at Runtime
220
-
221
- Use the `env()` function from `tej-env` to read any configuration value:
222
-
223
- ```javascript
224
- import { env } from 'tej-env';
225
-
226
- target.register('/info', (ammo) => {
227
- ammo.fire({
228
- port: env('PORT'),
229
- maxBodySize: env('BODY_MAX_SIZE')
230
- });
231
- });
232
- ```
233
-
234
- ## Auto-Registration
235
-
236
- Tejas automatically discovers and imports all files ending in `.target.js` from the configured `dir.targets` directory (recursively, including subdirectories):
237
-
238
- ```
239
- targets/
240
- ├── user.target.js --> Auto-loaded
241
- ├── auth.target.js --> Auto-loaded
242
- ├── utils.js --> Ignored (wrong suffix)
243
- └── api/
244
- └── v1.target.js --> Auto-loaded
245
- ```
246
-
247
- To change the targets directory:
248
-
249
- ```json
250
- {
251
- "dir": {
252
- "targets": "src/routes"
253
- }
254
- }
255
- ```
256
-
257
- ## Database Configuration
258
-
259
- Database connections are configured via `takeoff()` options, not through the config file:
260
-
261
- ```javascript
262
- app.takeoff({
263
- withRedis: { url: 'redis://localhost:6379' },
264
- withMongo: { uri: 'mongodb://localhost:27017/myapp' }
265
- });
266
- ```
267
-
268
- See [Database Integration](./database.md) for details.
269
-
270
- ## Next Steps
271
-
272
- - [Getting Started](./getting-started.md) — Build your first Tejas application
273
- - [Routing](./routing.md) — Define routes and endpoints
274
- - [Auto-Documentation](./auto-docs.md) — Generate OpenAPI docs from your code
275
- - [CLI Reference](./cli.md) — `tejas fly` and doc generation commands
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
+ ### LLM configuration (feature as parent, LLM inside each feature)
70
+
71
+ Tejas uses a **feature-as-parent** pattern: each feature that needs an LLM has its own `*.llm` block (`docs.llm` for auto-documentation, `errors.llm` for LLM-inferred errors). **Inheritance from `LLM_*`:** unset feature-specific values fall back to `LLM_BASE_URL`, `LLM_API_KEY`, and `LLM_MODEL`. One set of `LLM_*` env vars can serve both features when you don't override with `DOCS_LLM_*` or `ERRORS_LLM_*`. You can also use different LLMs per feature (e.g. a lighter model for errors, a stronger one for docs).
72
+
73
+ ### Auto-Documentation
74
+
75
+ These options configure the `tejas generate:docs` CLI command and the auto-documentation system. The **`docs.llm`** block is the LLM configuration for this feature. See [Auto-Documentation](./auto-docs.md) for full details.
76
+
77
+ | Config Key | Env Variable | Type | Default | Description |
78
+ |------------|-------------|------|---------|-------------|
79
+ | `docs.dirTargets` | `DOCS_DIR_TARGETS` | string | `"targets"` | Target directory for doc generation (can differ from `dir.targets`) |
80
+ | `docs.output` | — | string | `"./openapi.json"` | Output path for the generated OpenAPI spec |
81
+ | `docs.title` | — | string | `"API"` | API title in the OpenAPI `info` block |
82
+ | `docs.version` | — | string | `"1.0.0"` | API version in the OpenAPI `info` block |
83
+ | `docs.description` | — | string | `""` | API description |
84
+ | `docs.level` | — | number | `1` | LLM enhancement level (1–3). Higher = better docs, more tokens |
85
+ | `docs.llm.baseURL` | `DOCS_LLM_BASE_URL` or `LLM_BASE_URL` | string | `"https://api.openai.com/v1"` | LLM provider endpoint for auto-docs |
86
+ | `docs.llm.apiKey` | `DOCS_LLM_API_KEY` or `LLM_API_KEY` | string | — | LLM provider API key for auto-docs |
87
+ | `docs.llm.model` | `DOCS_LLM_MODEL` or `LLM_MODEL` | string | `"gpt-4o-mini"` | LLM model for auto-docs |
88
+ | `docs.overviewPath` | — | string | `"./API_OVERVIEW.md"` | Path for the generated overview page (level 3 only) |
89
+ | `docs.productionBranch` | `DOCS_PRODUCTION_BRANCH` | string | `"main"` | Git branch that triggers `docs:on-push` |
90
+
91
+ ### Error handling (LLM-inferred errors)
92
+
93
+ When [LLM-inferred error codes and messages](./error-handling.md#llm-inferred-errors) are enabled, the **`errors.llm`** block configures the LLM used for inferring status code and message when you call `ammo.throw()` without explicit code or message. Unset values fall back to `LLM_BASE_URL`, `LLM_API_KEY`, `LLM_MODEL`. You can also enable (and optionally set connection options) by calling **`app.withLLMErrors(config?)`** before `takeoff()` — e.g. `app.withLLMErrors()` to use env/config for baseURL, apiKey, model, or `app.withLLMErrors({ baseURL, apiKey, model, messageType })` to override in code.
94
+
95
+ | Config Key | Env Variable | Type | Default | Description |
96
+ |------------|-------------|------|---------|-------------|
97
+ | `errors.llm.enabled` | `ERRORS_LLM_ENABLED` or `LLM_*` (for connection) | boolean | `false` | Enable LLM-inferred error code and message for `ammo.throw()` |
98
+ | `errors.llm.baseURL` | `ERRORS_LLM_BASE_URL` or `LLM_BASE_URL` | string | `"https://api.openai.com/v1"` | LLM provider endpoint for error inference |
99
+ | `errors.llm.apiKey` | `ERRORS_LLM_API_KEY` or `LLM_API_KEY` | string | — | LLM provider API key for error inference |
100
+ | `errors.llm.model` | `ERRORS_LLM_MODEL` or `LLM_MODEL` | string | `"gpt-4o-mini"` | LLM model for error inference |
101
+ | `errors.llm.messageType` | `ERRORS_LLM_MESSAGE_TYPE` or `LLM_MESSAGE_TYPE` | `"endUser"` \| `"developer"` | `"endUser"` | Default tone for LLM-generated message: `endUser` (safe for clients) or `developer` (technical detail). Overridable per `ammo.throw()` call. |
102
+
103
+ When enabled, the same behaviour applies whether you call `ammo.throw()` or the framework calls it when it catches an error — one mechanism, no separate config.
104
+
105
+ ## Configuration File
106
+
107
+ Create a `tejas.config.json` in your project root:
108
+
109
+ ```json
110
+ {
111
+ "entry": "app.js",
112
+ "port": 3000,
113
+ "dir": {
114
+ "targets": "targets"
115
+ },
116
+ "log": {
117
+ "http_requests": true,
118
+ "exceptions": true
119
+ },
120
+ "body": {
121
+ "max_size": 5242880,
122
+ "timeout": 15000
123
+ },
124
+ "docs": {
125
+ "output": "./openapi.json",
126
+ "title": "My API",
127
+ "version": "1.0.0",
128
+ "level": 2,
129
+ "productionBranch": "main",
130
+ "llm": {
131
+ "baseURL": "https://api.openai.com/v1",
132
+ "model": "gpt-4o-mini"
133
+ }
134
+ },
135
+ "errors": {
136
+ "llm": {
137
+ "enabled": true,
138
+ "baseURL": "https://api.openai.com/v1",
139
+ "model": "gpt-4o-mini",
140
+ "messageType": "endUser"
141
+ }
142
+ }
143
+ }
144
+ ```
145
+
146
+ ## Environment Variables
147
+
148
+ Create a `.env` file in your project root. Tejas uses [tej-env](https://www.npmjs.com/package/tej-env) to load it automatically:
149
+
150
+ ```bash
151
+ # Server
152
+ PORT=3000
153
+
154
+ # Logging
155
+ LOG_HTTP_REQUESTS=true
156
+ LOG_EXCEPTIONS=true
157
+
158
+ # Body limits
159
+ BODY_MAX_SIZE=5242880
160
+ BODY_TIMEOUT=15000
161
+
162
+ # Target directory
163
+ DIR_TARGETS=targets
164
+
165
+ # LLM — shared fallback for docs.llm and errors.llm when feature-specific vars are unset
166
+ LLM_BASE_URL=https://api.openai.com/v1
167
+ LLM_API_KEY=sk-...
168
+ LLM_MODEL=gpt-4o-mini
169
+
170
+ # Optional: override per feature (docs.llm)
171
+ # DOCS_LLM_BASE_URL=...
172
+ # DOCS_LLM_API_KEY=...
173
+ # DOCS_LLM_MODEL=...
174
+
175
+ # Optional: override for error-inference (errors.llm)
176
+ # ERRORS_LLM_ENABLED=true
177
+ # ERRORS_LLM_BASE_URL=...
178
+ # ERRORS_LLM_API_KEY=...
179
+ # ERRORS_LLM_MODEL=...
180
+ # ERRORS_LLM_MESSAGE_TYPE=endUser # or "developer" for technical messages
181
+ ```
182
+
183
+ ## Constructor Options
184
+
185
+ Pass an object to `new Tejas()` using the same nested structure as `tejas.config.json`:
186
+
187
+ ```javascript
188
+ import Tejas from 'te.js';
189
+
190
+ const app = new Tejas({
191
+ port: 3000,
192
+ log: {
193
+ http_requests: true,
194
+ exceptions: true
195
+ },
196
+ body: {
197
+ max_size: 10 * 1024 * 1024,
198
+ timeout: 30000
199
+ }
200
+ });
201
+ ```
202
+
203
+ Constructor options have the highest priority and override both the config file and environment variables.
204
+
205
+ ## How Merging Works
206
+
207
+ All configuration is flattened into a single-level object with uppercased keys:
208
+
209
+ ```javascript
210
+ // tejas.config.json
211
+ { "log": { "http_requests": true } }
212
+
213
+ // Becomes accessible as:
214
+ env('LOG_HTTP_REQUESTS') // true
215
+ ```
216
+
217
+ The merge order is: config file values, then env vars override those, then constructor options override everything.
218
+
219
+ ## Accessing Configuration at Runtime
220
+
221
+ Use the `env()` function from `tej-env` to read any configuration value:
222
+
223
+ ```javascript
224
+ import { env } from 'tej-env';
225
+
226
+ target.register('/info', (ammo) => {
227
+ ammo.fire({
228
+ port: env('PORT'),
229
+ maxBodySize: env('BODY_MAX_SIZE')
230
+ });
231
+ });
232
+ ```
233
+
234
+ ## Auto-Registration
235
+
236
+ Tejas automatically discovers and imports all files ending in `.target.js` from the configured `dir.targets` directory (recursively, including subdirectories):
237
+
238
+ ```
239
+ targets/
240
+ ├── user.target.js --> Auto-loaded
241
+ ├── auth.target.js --> Auto-loaded
242
+ ├── utils.js --> Ignored (wrong suffix)
243
+ └── api/
244
+ └── v1.target.js --> Auto-loaded
245
+ ```
246
+
247
+ To change the targets directory:
248
+
249
+ ```json
250
+ {
251
+ "dir": {
252
+ "targets": "src/routes"
253
+ }
254
+ }
255
+ ```
256
+
257
+ ## Database Configuration
258
+
259
+ Database connections are configured via `takeoff()` options, not through the config file:
260
+
261
+ ```javascript
262
+ app.takeoff({
263
+ withRedis: { url: 'redis://localhost:6379' },
264
+ withMongo: { uri: 'mongodb://localhost:27017/myapp' }
265
+ });
266
+ ```
267
+
268
+ See [Database Integration](./database.md) for details.
269
+
270
+ ## Next Steps
271
+
272
+ - [Getting Started](./getting-started.md) — Build your first Tejas application
273
+ - [Routing](./routing.md) — Define routes and endpoints
274
+ - [Auto-Documentation](./auto-docs.md) — Generate OpenAPI docs from your code
275
+ - [CLI Reference](./cli.md) — `tejas fly` and doc generation commands