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,78 @@
1
+ /**
2
+ * Type-level inference from `schema.ts` method exports to `ctx.validated`.
3
+ *
4
+ * Pattern: in `route.ts`, type the handler parameter with the route's schema
5
+ * export and `ctx.validated` becomes fully inferred:
6
+ *
7
+ * ```ts
8
+ * // schema.ts
9
+ * export const GET = { query: z.object({ q: z.string().optional() }) };
10
+ *
11
+ * // route.ts
12
+ * import type { GET as RouteSchema } from './schema';
13
+ * export async function GET(ctx: BurgerContext<typeof RouteSchema>) {
14
+ * ctx.validated.query; // { q?: string } | undefined
15
+ * }
16
+ * ```
17
+ *
18
+ * Types only — no runtime logic.
19
+ */
20
+ import type { z } from 'zod';
21
+ import type { SchemaInput, StandardSchemaV1 } from '../validation/types.js';
22
+ /**
23
+ * The shape of a per-method `schema.ts` export: one optional schema per
24
+ * request slot.
25
+ */
26
+ export interface RouteMethodSchema {
27
+ params?: SchemaInput;
28
+ query?: SchemaInput;
29
+ headers?: SchemaInput;
30
+ cookies?: SchemaInput;
31
+ body?: SchemaInput;
32
+ }
33
+ /**
34
+ * The validated output shape of a single schema. Zod infers the output type;
35
+ * Standard Schema v1 validators use their `~standard.types.output` when
36
+ * declared; anything else is `unknown`.
37
+ */
38
+ export type InferSchemaOutput<T> = T extends z.ZodTypeAny ? z.infer<T> : T extends StandardSchemaV1 ? NonNullable<T['~standard']['types']> extends {
39
+ output: infer O;
40
+ } ? O : unknown : unknown;
41
+ /**
42
+ * The fallback validated shape used when no inference applies. Matches the
43
+ * `BurgerValidated` augmentation slots, which remain the escape hatch for
44
+ * anything inference cannot express.
45
+ */
46
+ export interface DefaultValidated {
47
+ params?: unknown;
48
+ query?: unknown;
49
+ headers?: unknown;
50
+ cookies?: unknown;
51
+ body?: unknown;
52
+ }
53
+ /**
54
+ * The validated output of one schema slot value: an inline schema is
55
+ * inferred; anything else is `unknown`.
56
+ */
57
+ type SlotOutput<T> = T extends SchemaInput ? InferSchemaOutput<T> : unknown;
58
+ /**
59
+ * A slot that is ALWAYS populated after validation when the route declares
60
+ * it: `query`/`headers`/`cookies` validate on every request, `params`
61
+ * whenever the route has `[param]` segments, and `body` whenever the handler
62
+ * runs (a non-JSON or missing body is rejected with 415/422 first). Declared
63
+ * slots are therefore non-optional; undeclared slots stay optional `unknown`
64
+ * and are never set at runtime.
65
+ */
66
+ type AlwaysSlot<TRoute, K extends keyof RouteMethodSchema> = K extends keyof TRoute ? {
67
+ [P in K]: SlotOutput<TRoute[K]>;
68
+ } : {
69
+ [P in K]?: unknown;
70
+ };
71
+ /**
72
+ * Map a `RouteMethodSchema` to its inferred `ctx.validated` shape.
73
+ *
74
+ * Declared slots are non-optional (always populated after validation);
75
+ * undeclared slots are optional `unknown`.
76
+ */
77
+ export type InferValidated<TRoute> = TRoute extends object ? TRoute extends RouteMethodSchema ? AlwaysSlot<TRoute, 'params'> & AlwaysSlot<TRoute, 'query'> & AlwaysSlot<TRoute, 'headers'> & AlwaysSlot<TRoute, 'cookies'> & AlwaysSlot<TRoute, 'body'> : DefaultValidated : DefaultValidated;
78
+ export {};
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Type-level inference from `schema.ts` method exports to `ctx.validated`.
3
+ *
4
+ * Pattern: in `route.ts`, type the handler parameter with the route's schema
5
+ * export and `ctx.validated` becomes fully inferred:
6
+ *
7
+ * ```ts
8
+ * // schema.ts
9
+ * export const GET = { query: z.object({ q: z.string().optional() }) };
10
+ *
11
+ * // route.ts
12
+ * import type { GET as RouteSchema } from './schema';
13
+ * export async function GET(ctx: BurgerContext<typeof RouteSchema>) {
14
+ * ctx.validated.query; // { q?: string } | undefined
15
+ * }
16
+ * ```
17
+ *
18
+ * Types only — no runtime logic.
19
+ */
20
+ export {};
@@ -0,0 +1,164 @@
1
+ /**
2
+ * Configuration for OpenAPI document generation and docs UI.
3
+ *
4
+ * Defined in the `openapi.config.ts` convention file, auto-discovered next to
5
+ * the entry point: `src/openapi.config.ts` when `src/` exists, or root
6
+ * `openapi.config.ts` for flat project structures.
7
+ */
8
+ /**
9
+ * A function that takes an OpenAPI document and returns HTML or a Response
10
+ * for the documentation UI.
11
+ *
12
+ * Built-in providers: `scalarDocs()`, `swaggerDocs()`, `redocDocs()`.
13
+ * Users can pass any custom function. `options.specUrl` is the path the
14
+ * spec is served at (`OpenAPIConfig.path`, default `/openapi.json`) — load
15
+ * the spec from there rather than hardcoding it.
16
+ */
17
+ export type DocsProvider = (spec: OpenAPIObject, options: DocsProviderOptions) => string | Response;
18
+ /** Runtime details passed to a {@link DocsProvider}. */
19
+ export interface DocsProviderOptions {
20
+ /** URL path of the OpenAPI JSON spec, e.g. `/openapi.json`. */
21
+ specUrl: string;
22
+ }
23
+ /**
24
+ * Minimal OpenAPI 3.1 document shape used by the docs provider.
25
+ * Full type not imported to avoid pulling in external dependencies.
26
+ */
27
+ export interface OpenAPIObject {
28
+ openapi: string;
29
+ info: Record<string, unknown>;
30
+ servers?: Array<{
31
+ url: string;
32
+ description?: string;
33
+ }>;
34
+ paths: Record<string, unknown>;
35
+ components?: Record<string, unknown>;
36
+ security?: Array<Record<string, string[]>>;
37
+ tags?: Array<{
38
+ name: string;
39
+ description?: string;
40
+ }>;
41
+ externalDocs?: {
42
+ url: string;
43
+ description?: string;
44
+ };
45
+ [key: string]: unknown;
46
+ }
47
+ /**
48
+ * Converter function that takes a Standard Schema object and returns
49
+ * a JSON Schema compatible with OpenAPI 3.1.
50
+ *
51
+ * Each validation library provides its own converter:
52
+ * - Zod 4: `z.toJSONSchema`
53
+ * - Valibot: custom converter
54
+ * - ArkType: custom converter
55
+ */
56
+ export type JsonSchemaConverter = (schema: unknown) => Record<string, unknown>;
57
+ /**
58
+ * Basic auth credentials for protecting the `/docs` endpoint.
59
+ */
60
+ export interface DocsAuth {
61
+ username: string;
62
+ password: string;
63
+ }
64
+ /**
65
+ * OpenAPI server object (per OpenAPI 3.1 spec).
66
+ */
67
+ export interface OpenAPIServer {
68
+ url: string;
69
+ description?: string;
70
+ }
71
+ /**
72
+ * OpenAPI contact object (per OpenAPI 3.1 spec).
73
+ */
74
+ export interface OpenAPIContact {
75
+ name?: string;
76
+ url?: string;
77
+ email?: string;
78
+ }
79
+ /**
80
+ * OpenAPI license object (per OpenAPI 3.1 spec).
81
+ */
82
+ export interface OpenAPILicense {
83
+ name: string;
84
+ url?: string;
85
+ }
86
+ /**
87
+ * OpenAPI external docs object (per OpenAPI 3.1 spec).
88
+ */
89
+ export interface OpenAPIExternalDocs {
90
+ url: string;
91
+ description?: string;
92
+ }
93
+ /**
94
+ * Configuration for the `openapi.config.ts` convention file.
95
+ *
96
+ * @example
97
+ * ```ts
98
+ * export default {
99
+ * title: "My API",
100
+ * description: "User management API",
101
+ * version: "2.0.0",
102
+ * servers: [{ url: "https://api.example.com" }],
103
+ * contact: { name: "Team", email: "api@example.com" },
104
+ * license: { name: "MIT" },
105
+ * docsAuth: { username: "admin", password: process.env.DOCS_PASSWORD },
106
+ * mapJsonSchema: { zod: z.toJSONSchema },
107
+ * } satisfies OpenAPIConfig;
108
+ * ```
109
+ */
110
+ export interface OpenAPIConfig {
111
+ /** API title. Defaults to "Burger API". */
112
+ title?: string;
113
+ /** API description. Defaults to "Burger API documentation". */
114
+ description?: string;
115
+ /** API version. Defaults to "1.0.0". */
116
+ version?: string;
117
+ /** Server URLs for the API. */
118
+ servers?: OpenAPIServer[];
119
+ /** API contact information. */
120
+ contact?: OpenAPIContact;
121
+ /** API license information. */
122
+ license?: OpenAPILicense;
123
+ /** URL to the API terms of service. */
124
+ termsOfService?: string;
125
+ /** Link to external documentation. */
126
+ externalDocs?: OpenAPIExternalDocs;
127
+ /**
128
+ * Root security requirements. Defaults to `[]` ("no auth required") —
129
+ * pass your schemes when the API uses authentication.
130
+ */
131
+ security?: Array<Record<string, string[]>>;
132
+ /** Path for the OpenAPI JSON spec endpoint. Defaults to "/openapi.json". */
133
+ path?: string;
134
+ /** Path for the documentation UI endpoint. Defaults to "/docs". */
135
+ docsPath?: string;
136
+ /** Whether OpenAPI endpoints are enabled. Defaults to true. */
137
+ enabled?: boolean;
138
+ /** Basic auth credentials for protecting the /docs endpoint. Omit for no protection. */
139
+ docsAuth?: DocsAuth;
140
+ /**
141
+ * Documentation UI provider function.
142
+ * Defaults to Swagger UI via CDN.
143
+ * Built-in alternatives: `scalarDocs()`, `redocDocs()`.
144
+ */
145
+ provider?: DocsProvider;
146
+ /**
147
+ * Map of validator library names to their JSON Schema converter functions.
148
+ * The framework looks up the converter by the schema's library identifier
149
+ * and calls it to produce OpenAPI-compatible JSON Schema.
150
+ *
151
+ * Zod 4 schemas with native `toJSONSchema()` are handled automatically.
152
+ * This option is for other libraries (Valibot, ArkType, Effect Schema,
153
+ * etc.).
154
+ *
155
+ * @example
156
+ * ```ts
157
+ * mapJsonSchema: {
158
+ * zod: z.toJSONSchema,
159
+ * valibot: toJsonSchema,
160
+ * }
161
+ * ```
162
+ */
163
+ mapJsonSchema?: Record<string, JsonSchemaConverter>;
164
+ }
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Configuration for OpenAPI document generation and docs UI.
3
+ *
4
+ * Defined in the `openapi.config.ts` convention file, auto-discovered next to
5
+ * the entry point: `src/openapi.config.ts` when `src/` exists, or root
6
+ * `openapi.config.ts` for flat project structures.
7
+ */
8
+ export {};
@@ -0,0 +1,13 @@
1
+ /**
2
+ * `true` only when `NODE_ENV === 'development'` — debug output (stack, cause,
3
+ * response-validation detail) is opt-in. Reading `process.env` throws on Deno
4
+ * without `--allow-env`, and `process` may not exist on some runtimes (e.g.
5
+ * Workers without `nodejs_compat`); both fall back to `false`.
6
+ */
7
+ export declare function isDevelopmentEnv(): boolean;
8
+ /**
9
+ * Resolves the effective debug flag: an explicit option wins, otherwise
10
+ * `NODE_ENV === 'development'`. Every debug consumer (router errors, hook
11
+ * plans, the Bun adapter) must go through this so all error paths agree.
12
+ */
13
+ export declare function resolveDebug(explicit?: boolean): boolean;
@@ -0,0 +1,24 @@
1
+ /**
2
+ * `true` only when `NODE_ENV === 'development'` — debug output (stack, cause,
3
+ * response-validation detail) is opt-in. Reading `process.env` throws on Deno
4
+ * without `--allow-env`, and `process` may not exist on some runtimes (e.g.
5
+ * Workers without `nodejs_compat`); both fall back to `false`.
6
+ */
7
+ export function isDevelopmentEnv() {
8
+ if (typeof process === 'undefined')
9
+ return false;
10
+ try {
11
+ return process.env.NODE_ENV === 'development';
12
+ }
13
+ catch {
14
+ return false;
15
+ }
16
+ }
17
+ /**
18
+ * Resolves the effective debug flag: an explicit option wins, otherwise
19
+ * `NODE_ENV === 'development'`. Every debug consumer (router errors, hook
20
+ * plans, the Bun adapter) must go through this so all error paths agree.
21
+ */
22
+ export function resolveDebug(explicit) {
23
+ return explicit ?? isDevelopmentEnv();
24
+ }
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Resolves a scan root (apiDir / pageDir / wsDir) for the dev pipeline.
3
+ *
4
+ * Absolute paths are used as-is; existing paths relative to the project root
5
+ * win; otherwise `BURGER_API_APP_DIR` (set by `burger-api dev` to the entry
6
+ * file's directory, e.g. `<root>/src`) is tried, so `apiDir: 'api'` resolves
7
+ * to `src/api`. Throws with the candidate paths when none exist.
8
+ *
9
+ * @param dir - The configured directory path
10
+ * @param label - Kind of directory, e.g. 'Routes', 'Pages', 'WebSocket'
11
+ * @param option - The option name, e.g. 'apiDir'
12
+ * @returns The resolved directory path
13
+ * @throws Error listing the tried candidates when the directory is not found
14
+ */
15
+ export declare function resolveScanDir(dir: string, label: string, option: string): string;
16
+ /**
17
+ * Resolves the app root (where `hooks.ts` / `plugins.ts` / `providers.ts` /
18
+ * `openapi.config.ts` live) when no API directory points at it. Uses
19
+ * `BURGER_API_APP_DIR` (set by `burger-api dev` to the entry file's
20
+ * directory), then `./src`.
21
+ */
22
+ export declare function resolveAppRootDir(): string | undefined;
23
+ /**
24
+ * Convention default for a scan root the app did not configure
25
+ * (`src/api`, `src/pages`, `src/websocket`) — matches the CLI build's
26
+ * defaults so dev and production mount the same directories. Returns the
27
+ * path only when it exists: `./src/<name>`, else `<BURGER_API_APP_DIR>/<name>`.
28
+ */
29
+ export declare function resolveConventionDir(name: 'api' | 'pages' | 'websocket'): string | undefined;
@@ -0,0 +1,75 @@
1
+ import { existsSync } from 'node:fs';
2
+ import { isAbsolute, join, relative } from 'node:path';
3
+ /**
4
+ * Resolves a scan root (apiDir / pageDir / wsDir) for the dev pipeline.
5
+ *
6
+ * Absolute paths are used as-is; existing paths relative to the project root
7
+ * win; otherwise `BURGER_API_APP_DIR` (set by `burger-api dev` to the entry
8
+ * file's directory, e.g. `<root>/src`) is tried, so `apiDir: 'api'` resolves
9
+ * to `src/api`. Throws with the candidate paths when none exist.
10
+ *
11
+ * @param dir - The configured directory path
12
+ * @param label - Kind of directory, e.g. 'Routes', 'Pages', 'WebSocket'
13
+ * @param option - The option name, e.g. 'apiDir'
14
+ * @returns The resolved directory path
15
+ * @throws Error listing the tried candidates when the directory is not found
16
+ */
17
+ export function resolveScanDir(dir, label, option) {
18
+ if (isAbsolute(dir)) {
19
+ if (existsSync(dir))
20
+ return dir;
21
+ throw new Error(`${label} directory "${dir}" does not exist. ` +
22
+ `Check the ${option} option in src/index.ts.`);
23
+ }
24
+ if (existsSync(dir))
25
+ return dir;
26
+ const appDir = process.env.BURGER_API_APP_DIR;
27
+ const srcCandidate = appDir ? join(appDir, dir) : undefined;
28
+ if (srcCandidate && existsSync(srcCandidate))
29
+ return srcCandidate;
30
+ const tried = [`"./${dir.replace(/^\.\//, '')}" (project root)`];
31
+ if (srcCandidate) {
32
+ const shown = relative(process.cwd(), srcCandidate).replaceAll('\\', '/');
33
+ tried.push(`"./${shown}" (src/)`);
34
+ }
35
+ else {
36
+ tried.push(`"./src/${dir.replace(/^\.\//, '')}" (src/)`);
37
+ }
38
+ const message = `${label} directory "${dir}" does not exist. Tried ${tried.join(' and ')}. ` +
39
+ `Check the ${option} option in src/index.ts.`;
40
+ throw new Error(appDir
41
+ ? message
42
+ : message + ` Run via "burger-api dev" to enable the src/ fallback.`);
43
+ }
44
+ /**
45
+ * Resolves the app root (where `hooks.ts` / `plugins.ts` / `providers.ts` /
46
+ * `openapi.config.ts` live) when no API directory points at it. Uses
47
+ * `BURGER_API_APP_DIR` (set by `burger-api dev` to the entry file's
48
+ * directory), then `./src`.
49
+ */
50
+ export function resolveAppRootDir() {
51
+ const appDir = process.env.BURGER_API_APP_DIR;
52
+ if (appDir && existsSync(appDir))
53
+ return appDir;
54
+ if (existsSync('./src'))
55
+ return './src';
56
+ return undefined;
57
+ }
58
+ /**
59
+ * Convention default for a scan root the app did not configure
60
+ * (`src/api`, `src/pages`, `src/websocket`) — matches the CLI build's
61
+ * defaults so dev and production mount the same directories. Returns the
62
+ * path only when it exists: `./src/<name>`, else `<BURGER_API_APP_DIR>/<name>`.
63
+ */
64
+ export function resolveConventionDir(name) {
65
+ const rel = `./src/${name}`;
66
+ if (existsSync(rel))
67
+ return rel;
68
+ const appDir = process.env.BURGER_API_APP_DIR;
69
+ if (appDir) {
70
+ const candidate = join(appDir, name);
71
+ if (existsSync(candidate))
72
+ return candidate;
73
+ }
74
+ return undefined;
75
+ }
@@ -1,4 +1,4 @@
1
- export { ROUTE_CONSTANTS, HTTP_METHODS, compareRoutes, getRouteSpecificity, collectRoutes, } from './routing';
1
+ export { ROUTE_CONSTANTS, HTTP_METHODS, compareRoutes, getRouteSpecificity, collectRoutes, } from './routing.js';
2
2
  /**
3
3
  * Resolves the given path to the specified directory.
4
4
  * @param directory the directory to resolve to
@@ -1,6 +1,6 @@
1
- import { resolve } from 'path';
1
+ import { resolve } from 'node:path';
2
2
  // Export constants
3
- export { ROUTE_CONSTANTS, HTTP_METHODS, compareRoutes, getRouteSpecificity, collectRoutes, } from './routing';
3
+ export { ROUTE_CONSTANTS, HTTP_METHODS, compareRoutes, getRouteSpecificity, collectRoutes, } from './routing.js';
4
4
  /**
5
5
  * Resolves the given path to the specified directory.
6
6
  * @param directory the directory to resolve to
@@ -1,6 +1,6 @@
1
- import * as path from 'path';
2
- import { cleanPrefix } from './index';
3
- import { ROUTE_CONSTANTS } from './routing';
1
+ import * as path from 'node:path';
2
+ import { cleanPrefix } from './index.js';
3
+ import { ROUTE_CONSTANTS } from './routing.js';
4
4
  /**
5
5
  * Converts a file path to an API route path.
6
6
  * @param filePath The file path to convert.
@@ -8,8 +8,11 @@ import { ROUTE_CONSTANTS } from './routing';
8
8
  * @returns The API route path.
9
9
  */
10
10
  export function filePathToApiRoutePath(filePath, prefix) {
11
- if (filePath.endsWith('route.ts')) {
12
- filePath = filePath.slice(0, -'route.ts'.length);
11
+ for (const ext of ['.ts', '.js', '.mjs']) {
12
+ if (filePath.endsWith(`route${ext}`)) {
13
+ filePath = filePath.slice(0, -`route${ext}`.length);
14
+ break;
15
+ }
13
16
  }
14
17
  const segments = filePath.split(path.sep);
15
18
  const resultSegments = [];
@@ -59,8 +62,11 @@ export function filePathToPageRoutePath(filePath, prefix) {
59
62
  continue;
60
63
  }
61
64
  if (segment.startsWith(ROUTE_CONSTANTS.DYNAMIC_FOLDER_START) &&
62
- segment.endsWith(ROUTE_CONSTANTS.DYNAMIC_FOLDER_END)) {
63
- resultSegments.push(ROUTE_CONSTANTS.DYNAMIC_SEGMENT_PREFIX + segment.slice(1, -1));
65
+ segment.includes(ROUTE_CONSTANTS.DYNAMIC_FOLDER_END)) {
66
+ // Page dynamic segments are FILENAMES (`[name].tsx`) — the
67
+ // extension trails the closing bracket, so match on `includes`.
68
+ const end = segment.indexOf(ROUTE_CONSTANTS.DYNAMIC_FOLDER_END);
69
+ resultSegments.push(ROUTE_CONSTANTS.DYNAMIC_SEGMENT_PREFIX + segment.slice(1, end));
64
70
  }
65
71
  else {
66
72
  resultSegments.push(segment);
@@ -1,12 +1,48 @@
1
1
  /**
2
- * The method not allowed response
2
+ * Builds the framework's 404 response (RFC 9457 Problem Details). A fresh
3
+ * `Response` per call: a cached template cannot be cloned reliably — runtimes
4
+ * (workerd) lock a shared template's body stream once it has been served, so
5
+ * the next `clone()` throws and the request 500s.
3
6
  */
4
- export declare const METHOD_NOT_ALLOWED: Response;
7
+ export declare const notFound: () => Response;
5
8
  /**
6
- * The not found response
9
+ * The OpenAPI error response. A factory — see {@link notFound}: never share a
10
+ * `Response` instance with a body across requests.
7
11
  */
8
- export declare const NOT_FOUND: Response;
12
+ export declare const openApiError: () => Response;
9
13
  /**
10
- * The OpenAPI error response
14
+ * Builds a 405 (RFC 9457) with the `Allow` header listing the methods the
15
+ * matched route supports.
16
+ * @param allow - comma-separated allowed methods, e.g. "GET, POST"
11
17
  */
12
- export declare const OPENAPI_ERROR: Response;
18
+ export declare function methodNotAllowed(allow: string): Response;
19
+ /**
20
+ * The auto-generated OPTIONS handler (CORS preflight, 204), built per route so
21
+ * it can advertise the route's methods via `Allow` (RFC 9110).
22
+ *
23
+ * Tagged with `isAutoOptions` so the router compiler can recognize it (e.g.
24
+ * for Bun native static responses) without relying on function identity.
25
+ */
26
+ export interface AutoOptionsHandler {
27
+ (): Response;
28
+ isAutoOptions: true;
29
+ allowHeader: string;
30
+ }
31
+ export declare const createAutoOptionsHandler: (allowMethods: string[]) => AutoOptionsHandler;
32
+ import type { ContextSet } from '../context/types.js';
33
+ /**
34
+ * Whether a `ContextSet` carries any response mutation. For the framework's
35
+ * `TrackedContextSet` it is one flag read; plain objects fall back to a scan.
36
+ * `applySet` uses this to skip rebuilding the `Response`.
37
+ */
38
+ export declare function hasSetMutations(set?: ContextSet): boolean;
39
+ /**
40
+ * Merges a `ContextSet` (`ctx.set`) into the outgoing `Response`:
41
+ * - `set.headers` is merged over the response's headers; explicitly set values
42
+ * win and other handler headers are kept.
43
+ * - `set.status` overrides the response status only when defined, and NEVER
44
+ * overrides an error status (>= 400) — errors stay errors.
45
+ * - Runs once at the pipeline exit, for every response path; returns the
46
+ * original `Response` unchanged when there is nothing to apply.
47
+ */
48
+ export declare function applySet(response: Response, set?: ContextSet): Response;