burger-api 0.9.6 → 1.0.0-beta

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 (175) hide show
  1. package/README.md +84 -241
  2. package/dist/src/adapter/bun/index.d.ts +14 -0
  3. package/dist/src/adapter/bun/index.js +73 -0
  4. package/dist/src/adapter/bun/types.d.ts +15 -0
  5. package/dist/src/adapter/bun/types.js +1 -0
  6. package/dist/src/adapter/index.d.ts +1 -0
  7. package/dist/src/adapter/index.js +1 -0
  8. package/dist/src/adapter/types.d.ts +49 -0
  9. package/dist/src/adapter/types.js +1 -0
  10. package/dist/src/adapter/web-standard/fetch-handler.d.ts +39 -0
  11. package/dist/src/adapter/web-standard/fetch-handler.js +54 -0
  12. package/dist/src/adapter/web-standard/index.d.ts +8 -0
  13. package/dist/src/adapter/web-standard/index.js +7 -0
  14. package/dist/src/analysis/route-access-analyzer.d.ts +10 -0
  15. package/dist/src/analysis/route-access-analyzer.js +287 -0
  16. package/dist/src/chain/chain.d.ts +16 -0
  17. package/dist/src/chain/chain.js +17 -0
  18. package/dist/src/chain/flattener.d.ts +3 -0
  19. package/dist/src/chain/flattener.js +78 -0
  20. package/dist/src/chain/node.d.ts +25 -0
  21. package/dist/src/chain/node.js +1 -0
  22. package/dist/src/compiler/conventions.d.ts +67 -0
  23. package/dist/src/compiler/conventions.js +129 -0
  24. package/dist/src/compiler/index.d.ts +13 -0
  25. package/dist/src/compiler/index.js +11 -0
  26. package/dist/src/compiler/module-loader.d.ts +75 -0
  27. package/dist/src/compiler/module-loader.js +276 -0
  28. package/dist/src/compiler/route-module.d.ts +97 -0
  29. package/dist/src/compiler/route-module.js +1 -0
  30. package/dist/src/compiler/route-tree.d.ts +19 -0
  31. package/dist/src/compiler/route-tree.js +28 -0
  32. package/dist/src/compiler/scanner.d.ts +42 -0
  33. package/dist/src/compiler/scanner.js +201 -0
  34. package/dist/src/context/context-set.d.ts +26 -0
  35. package/dist/src/context/context-set.js +45 -0
  36. package/dist/src/context/context.d.ts +357 -0
  37. package/dist/src/context/context.js +401 -0
  38. package/dist/src/context/query-parser.d.ts +23 -0
  39. package/dist/src/context/query-parser.js +132 -0
  40. package/dist/src/context/route-access.d.ts +7 -0
  41. package/dist/src/context/route-access.js +18 -0
  42. package/dist/src/context/types.d.ts +62 -0
  43. package/dist/src/context/types.js +5 -0
  44. package/dist/src/core/asset-mime.d.ts +9 -0
  45. package/dist/src/core/asset-mime.js +35 -0
  46. package/dist/src/core/assets.d.ts +30 -0
  47. package/dist/src/core/assets.js +67 -0
  48. package/dist/src/core/docs-providers.d.ts +13 -0
  49. package/dist/src/core/docs-providers.js +89 -0
  50. package/dist/src/core/embedded-assets.d.ts +18 -0
  51. package/dist/src/core/embedded-assets.js +13 -0
  52. package/dist/src/core/openapi.d.ts +9 -10
  53. package/dist/src/core/openapi.js +333 -127
  54. package/dist/src/core/page-router.d.ts +13 -17
  55. package/dist/src/core/page-router.js +86 -55
  56. package/dist/src/core/server.d.ts +20 -11
  57. package/dist/src/core/server.js +41 -45
  58. package/dist/src/errors/forbidden.d.ts +14 -0
  59. package/dist/src/errors/forbidden.js +16 -0
  60. package/dist/src/errors/http-error.d.ts +47 -0
  61. package/dist/src/errors/http-error.js +146 -0
  62. package/dist/src/errors/method-not-allowed.d.ts +13 -0
  63. package/dist/src/errors/method-not-allowed.js +15 -0
  64. package/dist/src/errors/not-found.d.ts +14 -0
  65. package/dist/src/errors/not-found.js +16 -0
  66. package/dist/src/errors/unauthorized.d.ts +14 -0
  67. package/dist/src/errors/unauthorized.js +16 -0
  68. package/dist/src/index.d.ts +263 -50
  69. package/dist/src/index.js +919 -332
  70. package/dist/src/lifecycle/executor.d.ts +64 -0
  71. package/dist/src/lifecycle/executor.js +201 -0
  72. package/dist/src/lifecycle/hook-runner.d.ts +11 -0
  73. package/dist/src/lifecycle/hook-runner.js +156 -0
  74. package/dist/src/lifecycle/jit.d.ts +50 -0
  75. package/dist/src/lifecycle/jit.js +258 -0
  76. package/dist/src/lifecycle/transform.d.ts +26 -0
  77. package/dist/src/lifecycle/transform.js +99 -0
  78. package/dist/src/lifecycle/types.d.ts +135 -0
  79. package/dist/src/lifecycle/types.js +1 -0
  80. package/dist/src/plugin/composer.d.ts +3 -0
  81. package/dist/src/plugin/composer.js +28 -0
  82. package/dist/src/plugin/registry.d.ts +25 -0
  83. package/dist/src/plugin/registry.js +107 -0
  84. package/dist/src/plugin/types.d.ts +20 -0
  85. package/dist/src/plugin/types.js +1 -0
  86. package/dist/src/router/allow-cache.d.ts +20 -0
  87. package/dist/src/router/allow-cache.js +26 -0
  88. package/dist/src/router/compiler.d.ts +39 -0
  89. package/dist/src/router/compiler.js +817 -0
  90. package/dist/src/router/define.d.ts +67 -0
  91. package/dist/src/router/define.js +34 -0
  92. package/dist/src/router/index.d.ts +6 -0
  93. package/dist/src/router/index.js +5 -0
  94. package/dist/src/router/param-extract.d.ts +33 -0
  95. package/dist/src/router/param-extract.js +87 -0
  96. package/dist/src/router/regex-matcher.d.ts +42 -0
  97. package/dist/src/router/regex-matcher.js +216 -0
  98. package/dist/src/router/router.d.ts +201 -0
  99. package/dist/src/router/router.js +600 -0
  100. package/dist/src/router/static-map.d.ts +39 -0
  101. package/dist/src/router/static-map.js +48 -0
  102. package/dist/src/router/trie.d.ts +69 -0
  103. package/dist/src/router/trie.js +572 -0
  104. package/dist/src/router/types.d.ts +104 -0
  105. package/dist/src/router/types.js +1 -0
  106. package/dist/src/runtime/capabilities.d.ts +29 -0
  107. package/dist/src/runtime/capabilities.js +48 -0
  108. package/dist/src/types/index.d.ts +260 -109
  109. package/dist/src/types/index.js +1 -1
  110. package/dist/src/types/inference.d.ts +78 -0
  111. package/dist/src/types/inference.js +20 -0
  112. package/dist/src/types/openapi-config.d.ts +164 -0
  113. package/dist/src/types/openapi-config.js +8 -0
  114. package/dist/src/utils/env.d.ts +13 -0
  115. package/dist/src/utils/env.js +24 -0
  116. package/dist/src/utils/fs.d.ts +29 -0
  117. package/dist/src/utils/fs.js +75 -0
  118. package/dist/src/utils/index.d.ts +1 -1
  119. package/dist/src/utils/index.js +2 -2
  120. package/dist/src/utils/pathConversion.js +13 -7
  121. package/dist/src/utils/response.d.ts +42 -6
  122. package/dist/src/utils/response.js +172 -7
  123. package/dist/src/utils/routing.d.ts +32 -13
  124. package/dist/src/utils/routing.js +41 -18
  125. package/dist/src/utils/thenable.d.ts +8 -0
  126. package/dist/src/utils/thenable.js +12 -0
  127. package/dist/src/utils/timing-safe.d.ts +9 -0
  128. package/dist/src/utils/timing-safe.js +20 -0
  129. package/dist/src/utils/wildcard.d.ts +2 -9
  130. package/dist/src/utils/wildcard.js +20 -58
  131. package/dist/src/validation/adapter.d.ts +54 -0
  132. package/dist/src/validation/adapter.js +75 -0
  133. package/dist/src/validation/adapters/standard.d.ts +11 -0
  134. package/dist/src/validation/adapters/standard.js +110 -0
  135. package/dist/src/validation/adapters/zod.d.ts +9 -0
  136. package/dist/src/validation/adapters/zod.js +128 -0
  137. package/dist/src/validation/cache.d.ts +26 -0
  138. package/dist/src/validation/cache.js +35 -0
  139. package/dist/src/validation/coerce.d.ts +28 -0
  140. package/dist/src/validation/coerce.js +176 -0
  141. package/dist/src/validation/compiler.d.ts +27 -0
  142. package/dist/src/validation/compiler.js +191 -0
  143. package/dist/src/validation/error.d.ts +69 -0
  144. package/dist/src/validation/error.js +137 -0
  145. package/dist/src/validation/index.d.ts +16 -0
  146. package/dist/src/validation/index.js +12 -0
  147. package/dist/src/validation/response.d.ts +25 -0
  148. package/dist/src/validation/response.js +79 -0
  149. package/dist/src/validation/types.d.ts +148 -0
  150. package/dist/src/validation/types.js +6 -0
  151. package/dist/src/validation/validator.d.ts +39 -0
  152. package/dist/src/validation/validator.js +441 -0
  153. package/dist/src/ws/adapter.d.ts +219 -0
  154. package/dist/src/ws/adapter.js +623 -0
  155. package/dist/src/ws/compiler.d.ts +37 -0
  156. package/dist/src/ws/compiler.js +156 -0
  157. package/dist/src/ws/index.d.ts +11 -0
  158. package/dist/src/ws/index.js +8 -0
  159. package/dist/src/ws/platform.d.ts +81 -0
  160. package/dist/src/ws/platform.js +113 -0
  161. package/dist/src/ws/router.d.ts +48 -0
  162. package/dist/src/ws/router.js +174 -0
  163. package/dist/src/ws/scanner.d.ts +97 -0
  164. package/dist/src/ws/scanner.js +193 -0
  165. package/dist/src/ws/types.d.ts +380 -0
  166. package/dist/src/ws/types.js +213 -0
  167. package/package.json +72 -59
  168. package/dist/src/core/api-router.d.ts +0 -65
  169. package/dist/src/core/api-router.js +0 -264
  170. package/dist/src/core/swagger-ui.d.ts +0 -1
  171. package/dist/src/core/swagger-ui.js +0 -33
  172. package/dist/src/middleware/validator.d.ts +0 -14
  173. package/dist/src/middleware/validator.js +0 -133
  174. package/dist/src/utils/error.d.ts +0 -17
  175. package/dist/src/utils/error.js +0 -63
@@ -0,0 +1,75 @@
1
+ import type { OpenAPIConfig } from '../types/index.js';
2
+ import type { RouteModule, ScanResult } from './route-module.js';
3
+ /**
4
+ * The second stage of the compiler pipeline.
5
+ *
6
+ * Consumes the inventory from {@link DirectoryScanner} and, for each route
7
+ * directory, `import()`s its convention files and assembles one
8
+ * {@link RouteModule}. Each route directory is self-contained — only its own
9
+ * files are loaded. Convention data stays raw for downstream compilation;
10
+ * `config` is attached for runtime use. Fails fast on duplicate route paths.
11
+ */
12
+ export declare class ModuleLoader {
13
+ /**
14
+ * Loads and assembles every scanned route into a `RouteModule`.
15
+ * @throws on duplicate resolved route paths.
16
+ */
17
+ load(scanned: ScanResult): Promise<RouteModule[]>;
18
+ private loadOne;
19
+ /**
20
+ * Extracts HTTP method handlers from a `route.ts` module. (The router
21
+ * compiler adds the framework OPTIONS handler to every route that does
22
+ * not define one.)
23
+ */
24
+ private extractHandlers;
25
+ /**
26
+ * Extracts `onRequest` hooks from a raw hook object.
27
+ * Returns them as an array (normalizing single hook or array).
28
+ */
29
+ private extractOnRequest;
30
+ /**
31
+ * Loads a module from an optional file path. Returns undefined when
32
+ * the path is not provided.
33
+ */
34
+ private loadOptional;
35
+ /**
36
+ * Loads `config.ts`. The default export (or the module namespace when
37
+ * there is no default) carries route-wide options; uppercase method
38
+ * exports are per-method overrides, kept under their uppercase keys and
39
+ * resolved per method at route compile time.
40
+ *
41
+ * A file with only a default export returns that object unchanged, so
42
+ * `ctx.config` keeps the exact object identity it had before.
43
+ */
44
+ private loadConfig;
45
+ /**
46
+ * Loads the `openapi.config.ts` convention file from the scanned result.
47
+ * Returns the config object, or undefined if no config file was discovered.
48
+ */
49
+ loadOpenAPIConfig(scanned: ScanResult): Promise<OpenAPIConfig | undefined>;
50
+ /**
51
+ * Loads the `plugins.ts` convention file. Returns the module namespace,
52
+ * or undefined when the file was not discovered. A present file without a
53
+ * default function export throws — never a silent skip.
54
+ */
55
+ loadPlugins(scanned: ScanResult): Promise<Record<string, unknown> | undefined>;
56
+ /**
57
+ * Loads the `providers.ts` convention file. Returns the module namespace,
58
+ * or undefined when the file was not discovered. A present file without a
59
+ * default function export throws — never a silent skip.
60
+ */
61
+ loadProviders(scanned: ScanResult): Promise<Record<string, unknown> | undefined>;
62
+ /** Imports an app-level module and validates its default export shape. */
63
+ private loadAppModule;
64
+ /**
65
+ * Merges two resolved hook objects (e.g. `hooks.ts` and inline `route.ts`
66
+ * `hooks`). Array-valued keys are concatenated (base first); `transform`
67
+ * is deep-merged; other values are overridden by `override`. Returns
68
+ * undefined when both are empty.
69
+ *
70
+ * The cast here is the dynamic-module boundary: convention files import as
71
+ * `Record<string, unknown>`, and this merge narrows the shape to
72
+ * `RouteHooks`.
73
+ */
74
+ private mergeHookObjects;
75
+ }
@@ -0,0 +1,276 @@
1
+ import { HTTP_METHODS } from '../utils/routing.js';
2
+ import { requireDefaultFunctionExport, warnUnknownHookExports, warnUnknownRouteExports, } from './conventions.js';
3
+ /** Uppercase HTTP method names for schema export detection. */
4
+ const HTTP_METHOD_SET = new Set(HTTP_METHODS);
5
+ /**
6
+ * Detects uppercase method exports in a schema module and normalizes them
7
+ * into the method-keyed `RouteSchema` format. Lowercase keys pass through
8
+ * unchanged.
9
+ */
10
+ function normalizeSchema(raw) {
11
+ const keys = Object.keys(raw);
12
+ const hasUpper = keys.some((k) => HTTP_METHOD_SET.has(k));
13
+ if (!hasUpper) {
14
+ // Already in method-keyed format or no recognizable methods.
15
+ return raw;
16
+ }
17
+ const merged = {};
18
+ for (const key of keys) {
19
+ if (HTTP_METHOD_SET.has(key)) {
20
+ merged[key.toLowerCase()] = raw[key];
21
+ }
22
+ else {
23
+ // Non-method keys pass through (e.g. `coerce`).
24
+ merged[key] = raw[key];
25
+ }
26
+ }
27
+ return merged;
28
+ }
29
+ /**
30
+ * Normalizes uppercase method exports from `openapi.ts` into the lowercase
31
+ * method-keyed format expected by `generateOpenAPIDocument`.
32
+ *
33
+ * @example
34
+ * ```ts
35
+ * // openapi.ts — named exports per HTTP method
36
+ * export const GET = { summary: 'List', tags: ['posts'] };
37
+ *
38
+ * // → normalized: { get: { summary: 'List', tags: ['posts'] } }
39
+ * ```
40
+ */
41
+ function normalizeOpenapi(raw) {
42
+ const merged = {};
43
+ for (const key of Object.keys(raw)) {
44
+ if (HTTP_METHOD_SET.has(key)) {
45
+ merged[key.toLowerCase()] = raw[key];
46
+ }
47
+ else {
48
+ merged[key] = raw[key];
49
+ }
50
+ }
51
+ return merged;
52
+ }
53
+ /**
54
+ * The second stage of the compiler pipeline.
55
+ *
56
+ * Consumes the inventory from {@link DirectoryScanner} and, for each route
57
+ * directory, `import()`s its convention files and assembles one
58
+ * {@link RouteModule}. Each route directory is self-contained — only its own
59
+ * files are loaded. Convention data stays raw for downstream compilation;
60
+ * `config` is attached for runtime use. Fails fast on duplicate route paths.
61
+ */
62
+ export class ModuleLoader {
63
+ /**
64
+ * Loads and assembles every scanned route into a `RouteModule`.
65
+ * @throws on duplicate resolved route paths.
66
+ */
67
+ async load(scanned) {
68
+ const modules = [];
69
+ const seenPaths = new Set();
70
+ // Load global hooks once (shared across all routes).
71
+ let globalHooks = scanned.globalHooks
72
+ ? await this.loadOptional(scanned.globalHooks)
73
+ : undefined;
74
+ warnUnknownHookExports(globalHooks, scanned.globalHooks ?? '', 'global');
75
+ // Extract onRequest from global hooks — these run before routing
76
+ // (pre-routing, app-level) and must NOT be merged per-route.
77
+ const globalOnRequest = this.extractOnRequest(globalHooks);
78
+ if (globalOnRequest.length > 0) {
79
+ scanned.globalOnRequest = globalOnRequest;
80
+ // Strip onRequest from merged global hooks so it's not duplicated per-route.
81
+ // ESM module namespaces are frozen — clone without onRequest instead of delete.
82
+ if (globalHooks) {
83
+ const { onRequest: _, ...rest } = globalHooks;
84
+ globalHooks =
85
+ Object.keys(rest).length > 0
86
+ ? rest
87
+ : undefined;
88
+ }
89
+ }
90
+ // The remaining global hooks are compiled with scope 'global' by the
91
+ // router compiler (NOT merged into each route's hooks), so response
92
+ // and error hooks run nearest-first (route → global).
93
+ scanned.globalRouteHooks = globalHooks;
94
+ for (const route of scanned.routes) {
95
+ const mod = await this.loadOne(route);
96
+ if (seenPaths.has(mod.path)) {
97
+ throw new Error(`Duplicate route path registered: "${mod.path}". ` +
98
+ `Two route directories resolve to the same URL.`);
99
+ }
100
+ seenPaths.add(mod.path);
101
+ modules.push(mod);
102
+ }
103
+ return modules;
104
+ }
105
+ async loadOne(route) {
106
+ // 1. Import route.ts (handlers + any inline convention exports).
107
+ const routeMod = await import(route.localFiles.route);
108
+ warnUnknownRouteExports(routeMod, route.localFiles.route, HTTP_METHODS);
109
+ const handlers = this.extractHandlers(routeMod);
110
+ // 2. Load convention files from this route's own directory only.
111
+ const rawSchema = await this.loadOptional(route.localFiles.schema);
112
+ const schema = rawSchema ? normalizeSchema(rawSchema) : undefined;
113
+ const rawOpenapi = await this.loadOptional(route.localFiles.openapi);
114
+ const openapi = rawOpenapi ? normalizeOpenapi(rawOpenapi) : undefined;
115
+ const hooks = await this.loadOptional(route.localFiles.hooks);
116
+ warnUnknownHookExports(hooks, route.localFiles.hooks ?? '', 'route');
117
+ const config = await this.loadConfig(route.localFiles.config);
118
+ // 3. Overlay inline exports from route.ts. Route-local inline wins
119
+ // over separate files.
120
+ const finalSchema = routeMod.schema ?? schema;
121
+ const finalOpenapi = routeMod.openapi ?? openapi;
122
+ const finalHooks = this.mergeHookObjects(hooks, routeMod.hooks);
123
+ const sourceFiles = { ...route.localFiles };
124
+ return {
125
+ path: route.routePath,
126
+ handlers,
127
+ schema: finalSchema,
128
+ openapi: finalOpenapi,
129
+ hooks: finalHooks,
130
+ config,
131
+ sourceFiles,
132
+ isWildcard: route.isWildcard,
133
+ };
134
+ }
135
+ /**
136
+ * Extracts HTTP method handlers from a `route.ts` module. (The router
137
+ * compiler adds the framework OPTIONS handler to every route that does
138
+ * not define one.)
139
+ */
140
+ extractHandlers(mod) {
141
+ const handlers = {};
142
+ for (const method of HTTP_METHODS) {
143
+ if (typeof mod[method] === 'function') {
144
+ handlers[method] = mod[method];
145
+ }
146
+ }
147
+ return handlers;
148
+ }
149
+ /**
150
+ * Extracts `onRequest` hooks from a raw hook object.
151
+ * Returns them as an array (normalizing single hook or array).
152
+ */
153
+ extractOnRequest(hooks) {
154
+ if (!hooks?.onRequest)
155
+ return [];
156
+ const h = hooks.onRequest;
157
+ return Array.isArray(h) ? h : [h];
158
+ }
159
+ /**
160
+ * Loads a module from an optional file path. Returns undefined when
161
+ * the path is not provided.
162
+ */
163
+ async loadOptional(filePath) {
164
+ if (!filePath)
165
+ return undefined;
166
+ const mod = await import(filePath);
167
+ return (mod.default ?? mod);
168
+ }
169
+ /**
170
+ * Loads `config.ts`. The default export (or the module namespace when
171
+ * there is no default) carries route-wide options; uppercase method
172
+ * exports are per-method overrides, kept under their uppercase keys and
173
+ * resolved per method at route compile time.
174
+ *
175
+ * A file with only a default export returns that object unchanged, so
176
+ * `ctx.config` keeps the exact object identity it had before.
177
+ */
178
+ async loadConfig(filePath) {
179
+ if (!filePath)
180
+ return undefined;
181
+ const mod = (await import(filePath));
182
+ const hasMethodExport = Object.keys(mod).some((key) => HTTP_METHOD_SET.has(key));
183
+ if (!hasMethodExport) {
184
+ return (mod.default ?? mod);
185
+ }
186
+ const base = mod.default;
187
+ const merged = {};
188
+ if (base !== null && typeof base === 'object') {
189
+ Object.assign(merged, base);
190
+ }
191
+ else if (base === undefined) {
192
+ // No default: non-method named exports stay route-wide.
193
+ for (const [key, value] of Object.entries(mod)) {
194
+ if (!HTTP_METHOD_SET.has(key))
195
+ merged[key] = value;
196
+ }
197
+ }
198
+ for (const [key, value] of Object.entries(mod)) {
199
+ if (HTTP_METHOD_SET.has(key))
200
+ merged[key] = value;
201
+ }
202
+ return merged;
203
+ }
204
+ /**
205
+ * Loads the `openapi.config.ts` convention file from the scanned result.
206
+ * Returns the config object, or undefined if no config file was discovered.
207
+ */
208
+ async loadOpenAPIConfig(scanned) {
209
+ return this.loadOptional(scanned.openAPIConfigPath);
210
+ }
211
+ /**
212
+ * Loads the `plugins.ts` convention file. Returns the module namespace,
213
+ * or undefined when the file was not discovered. A present file without a
214
+ * default function export throws — never a silent skip.
215
+ */
216
+ async loadPlugins(scanned) {
217
+ return this.loadAppModule(scanned.pluginsPath, 'plugins.ts');
218
+ }
219
+ /**
220
+ * Loads the `providers.ts` convention file. Returns the module namespace,
221
+ * or undefined when the file was not discovered. A present file without a
222
+ * default function export throws — never a silent skip.
223
+ */
224
+ async loadProviders(scanned) {
225
+ return this.loadAppModule(scanned.providersPath, 'providers.ts');
226
+ }
227
+ /** Imports an app-level module and validates its default export shape. */
228
+ async loadAppModule(filePath, label) {
229
+ if (!filePath)
230
+ return undefined;
231
+ const mod = (await import(filePath));
232
+ requireDefaultFunctionExport(mod, label, filePath);
233
+ return mod;
234
+ }
235
+ /**
236
+ * Merges two resolved hook objects (e.g. `hooks.ts` and inline `route.ts`
237
+ * `hooks`). Array-valued keys are concatenated (base first); `transform`
238
+ * is deep-merged; other values are overridden by `override`. Returns
239
+ * undefined when both are empty.
240
+ *
241
+ * The cast here is the dynamic-module boundary: convention files import as
242
+ * `Record<string, unknown>`, and this merge narrows the shape to
243
+ * `RouteHooks`.
244
+ */
245
+ mergeHookObjects(base, override) {
246
+ const result = { ...(base ?? {}) };
247
+ for (const key of Object.keys(override ?? {})) {
248
+ const b = result[key];
249
+ const o = override[key];
250
+ if (key === 'transform') {
251
+ result[key] = {
252
+ ...(b ?? {}),
253
+ ...(o ?? {}),
254
+ };
255
+ }
256
+ else if (Array.isArray(b) && Array.isArray(o)) {
257
+ result[key] = [...b, ...o];
258
+ }
259
+ else if (Array.isArray(b)) {
260
+ result[key] = [...b, o];
261
+ }
262
+ else if (Array.isArray(o)) {
263
+ result[key] = b !== undefined ? [b, ...o] : o;
264
+ }
265
+ else if (b !== undefined) {
266
+ result[key] = [b, o];
267
+ }
268
+ else {
269
+ result[key] = o;
270
+ }
271
+ }
272
+ return Object.keys(result).length > 0
273
+ ? result
274
+ : undefined;
275
+ }
276
+ }
@@ -0,0 +1,97 @@
1
+ import type { RequestHandler, RouteSchema, openapi } from '../types/index.js';
2
+ import type { HTTPMethod } from '../utils/routing.js';
3
+ import type { RouteHooks } from '../lifecycle/types.js';
4
+ import type { ConventionFile } from './conventions.js';
5
+ import type { Hook } from '../lifecycle/types.js';
6
+ /**
7
+ * The compiler's internal view of ONE route directory.
8
+ *
9
+ * `RouteModule` is the canonical intermediate produced by the Module Loader
10
+ * and consumed by the Compiler; users never see it. Each route directory is
11
+ * self-contained — no parent/group inheritance; convention data comes from
12
+ * the route's own files only.
13
+ *
14
+ * Fields are carried raw and compiled downstream: `schema` (validation
15
+ * compilation), `hooks` (frozen `HookPlan`), `openapi` (OpenAPI generation),
16
+ * `config` (runtime options such as auth, cache, timeout).
17
+ */
18
+ export interface RouteModule {
19
+ /**
20
+ * The resolved API route path, e.g. `/api/users/:id`.
21
+ */
22
+ path: string;
23
+ /**
24
+ * HTTP method handlers from `route.ts` (GET/POST/PUT/DELETE/PATCH/HEAD/OPTIONS).
25
+ */
26
+ handlers: Partial<Record<HTTPMethod, RequestHandler>>;
27
+ /**
28
+ * Validation definitions from `schema.ts` (compiled at route build time).
29
+ */
30
+ schema?: RouteSchema;
31
+ /**
32
+ * Lifecycle hooks from `hooks.ts` (compiled into a frozen hook plan).
33
+ * Stored raw so the later hook compiler owns the typing.
34
+ */
35
+ hooks?: RouteHooks;
36
+ /**
37
+ * OpenAPI metadata from `openapi.ts` (merged at compile time).
38
+ */
39
+ openapi?: openapi;
40
+ /**
41
+ * Per-route configuration from `config.ts` (auth, cache, timeout, …).
42
+ */
43
+ config?: Record<string, unknown>;
44
+ /**
45
+ * Absolute paths of the convention files that were loaded for this module,
46
+ * keyed by convention file stem. Retained for introspection/error reporting.
47
+ */
48
+ sourceFiles: Partial<Record<ConventionFile, string>>;
49
+ /**
50
+ * True when the route path contains a wildcard (`*`) segment.
51
+ */
52
+ isWildcard: boolean;
53
+ }
54
+ /**
55
+ * The Directory Scanner's output for one route directory (one that contains
56
+ * a `route.ts`) and the input to the Module Loader. No module code is
57
+ * imported by the scanner.
58
+ *
59
+ * Each route directory is self-contained — no group inheritance chain.
60
+ */
61
+ export interface ScannedRoute {
62
+ /** Resolved API route path, e.g. `/api/users/:id`. */
63
+ routePath: string;
64
+ /** Absolute path of the directory containing `route.ts`. */
65
+ routeDir: string;
66
+ /** Convention files present directly in this route directory. */
67
+ localFiles: Partial<Record<ConventionFile, string>>;
68
+ /** True when the route path contains a wildcard (`*`) segment. */
69
+ isWildcard: boolean;
70
+ }
71
+ /**
72
+ * The Directory Scanner's full output — a list of routes plus the path to
73
+ * the global hooks file (if any) at the app root.
74
+ */
75
+ export interface ScanResult {
76
+ routes: ScannedRoute[];
77
+ /** Absolute path to the global `hooks.ts` file (sibling of index.ts), or undefined. */
78
+ globalHooks?: string;
79
+ /**
80
+ * `onRequest` hooks extracted from global `src/hooks.ts`. These run
81
+ * before routing (pre-routing, app-level) and must NOT be merged per-route.
82
+ * Extracted by the ModuleLoader during `load()`.
83
+ */
84
+ globalOnRequest?: Hook[];
85
+ /**
86
+ * The rest of the global `src/hooks.ts` (everything but `onRequest`).
87
+ * Compiled once with scope 'global' — never merged into route hooks.
88
+ * Set by the ModuleLoader during `load()`.
89
+ */
90
+ globalRouteHooks?: RouteHooks;
91
+ /** Absolute path to `openapi.config.ts` (sibling of entry point), or undefined. */
92
+ openAPIConfigPath?: string;
93
+ /** Absolute path to `plugins.ts` (sibling of index.ts), or undefined. */
94
+ pluginsPath?: string;
95
+ /** Absolute path to `providers.ts` (sibling of index.ts), or undefined. */
96
+ providersPath?: string;
97
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,19 @@
1
+ import type { RouteModule } from './route-module.js';
2
+ /**
3
+ * A lightweight structural view of the compiled route set, for introspection
4
+ * and deterministic ordering. It performs no dispatch logic (that belongs to
5
+ * the router's `StaticMap`/`Trie`); it only organizes `RouteModule`s by path.
6
+ *
7
+ * Built once after the Module Loader produces the `RouteModule[]`; frozen for
8
+ * the lifetime of the server (no per-request allocation).
9
+ */
10
+ export declare class RouteTree {
11
+ private byPath;
12
+ constructor(modules: RouteModule[]);
13
+ /** All route modules, sorted by path for deterministic iteration. */
14
+ list(): RouteModule[];
15
+ /** Looks up a single route module by its resolved path. */
16
+ get(path: string): RouteModule | undefined;
17
+ /** The number of routes in the tree. */
18
+ get size(): number;
19
+ }
@@ -0,0 +1,28 @@
1
+ /**
2
+ * A lightweight structural view of the compiled route set, for introspection
3
+ * and deterministic ordering. It performs no dispatch logic (that belongs to
4
+ * the router's `StaticMap`/`Trie`); it only organizes `RouteModule`s by path.
5
+ *
6
+ * Built once after the Module Loader produces the `RouteModule[]`; frozen for
7
+ * the lifetime of the server (no per-request allocation).
8
+ */
9
+ export class RouteTree {
10
+ byPath = new Map();
11
+ constructor(modules) {
12
+ for (const mod of modules) {
13
+ this.byPath.set(mod.path, mod);
14
+ }
15
+ }
16
+ /** All route modules, sorted by path for deterministic iteration. */
17
+ list() {
18
+ return [...this.byPath.values()].sort((a, b) => a.path.localeCompare(b.path));
19
+ }
20
+ /** Looks up a single route module by its resolved path. */
21
+ get(path) {
22
+ return this.byPath.get(path);
23
+ }
24
+ /** The number of routes in the tree. */
25
+ get size() {
26
+ return this.byPath.size;
27
+ }
28
+ }
@@ -0,0 +1,42 @@
1
+ import type { ScanResult } from './route-module.js';
2
+ /**
3
+ * Walks a route directory tree and produces a pure inventory of route
4
+ * directories — the first stage of the compiler pipeline.
5
+ *
6
+ * The scanner only enumerates files and computes route paths; it never calls
7
+ * `import()` (that is the Module Loader's job), keeping the walk cheap,
8
+ * deterministic, and side-effect free. Each route directory is
9
+ * self-contained — no group inheritance chain; groups only strip URL paths.
10
+ *
11
+ * Fail-fast rules:
12
+ * - Only recognized convention files are acknowledged; `middleware.ts` is forbidden.
13
+ * - At most one dynamic (`[param]`) and one wildcard (`[...]`) folder per
14
+ * level (they may coexist; priority is static > param > wildcard).
15
+ * - Named wildcard folders (`[...slug]`) are not supported yet.
16
+ */
17
+ export declare class DirectoryScanner {
18
+ private readonly routesDir;
19
+ private readonly prefix;
20
+ constructor(routesDir: string, prefix?: string);
21
+ /**
22
+ * Scans the tree and returns a {@link ScanResult} containing:
23
+ * - `routes`: one {@link ScannedRoute} per directory that contains a `route.ts`
24
+ * - `globalHooks`: path to `hooks.ts` at the root of the routes directory (if any)
25
+ * @throws on forbidden files or conflicting dynamic/wildcard folders.
26
+ */
27
+ scan(): Promise<ScanResult>;
28
+ /**
29
+ * Recursively walks `dir` and emits a `ScannedRoute` whenever a `route.ts`
30
+ * is found. Group folders `(name)` are traversed for URL stripping but
31
+ * do NOT build an inheritance chain.
32
+ */
33
+ private walk;
34
+ }
35
+ /**
36
+ * App-root convention files for apps with no API directory (pages-only).
37
+ * Returns a route-less {@link ScanResult} so the Module Loader can load
38
+ * hooks/plugins/providers exactly like it does for API apps.
39
+ */
40
+ export declare function scanAppRootConventions(appRoot: string): Promise<ScanResult>;
41
+ /** Re-exported for convenience / symmetry with the module loader. */
42
+ export { CONVENTION_FILES } from './conventions.js';