burger-api 0.9.7 → 1.0.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +84 -241
- package/dist/src/adapter/bun/index.d.ts +14 -0
- package/dist/src/adapter/bun/index.js +73 -0
- package/dist/src/adapter/bun/types.d.ts +15 -0
- package/dist/src/adapter/bun/types.js +1 -0
- package/dist/src/adapter/index.d.ts +1 -0
- package/dist/src/adapter/index.js +1 -0
- package/dist/src/adapter/types.d.ts +49 -0
- package/dist/src/adapter/types.js +1 -0
- package/dist/src/adapter/web-standard/fetch-handler.d.ts +39 -0
- package/dist/src/adapter/web-standard/fetch-handler.js +54 -0
- package/dist/src/adapter/web-standard/index.d.ts +8 -0
- package/dist/src/adapter/web-standard/index.js +7 -0
- package/dist/src/analysis/route-access-analyzer.d.ts +10 -0
- package/dist/src/analysis/route-access-analyzer.js +287 -0
- package/dist/src/chain/chain.d.ts +16 -0
- package/dist/src/chain/chain.js +17 -0
- package/dist/src/chain/flattener.d.ts +3 -0
- package/dist/src/chain/flattener.js +78 -0
- package/dist/src/chain/node.d.ts +25 -0
- package/dist/src/chain/node.js +1 -0
- package/dist/src/compiler/conventions.d.ts +67 -0
- package/dist/src/compiler/conventions.js +129 -0
- package/dist/src/compiler/index.d.ts +13 -0
- package/dist/src/compiler/index.js +11 -0
- package/dist/src/compiler/module-loader.d.ts +75 -0
- package/dist/src/compiler/module-loader.js +276 -0
- package/dist/src/compiler/route-module.d.ts +97 -0
- package/dist/src/compiler/route-module.js +1 -0
- package/dist/src/compiler/route-tree.d.ts +19 -0
- package/dist/src/compiler/route-tree.js +28 -0
- package/dist/src/compiler/scanner.d.ts +42 -0
- package/dist/src/compiler/scanner.js +240 -0
- package/dist/src/context/context-set.d.ts +26 -0
- package/dist/src/context/context-set.js +45 -0
- package/dist/src/context/context.d.ts +357 -0
- package/dist/src/context/context.js +401 -0
- package/dist/src/context/query-parser.d.ts +23 -0
- package/dist/src/context/query-parser.js +132 -0
- package/dist/src/context/route-access.d.ts +7 -0
- package/dist/src/context/route-access.js +18 -0
- package/dist/src/context/types.d.ts +62 -0
- package/dist/src/context/types.js +5 -0
- package/dist/src/core/asset-mime.d.ts +9 -0
- package/dist/src/core/asset-mime.js +35 -0
- package/dist/src/core/assets.d.ts +30 -0
- package/dist/src/core/assets.js +67 -0
- package/dist/src/core/docs-providers.d.ts +13 -0
- package/dist/src/core/docs-providers.js +89 -0
- package/dist/src/core/embedded-assets.d.ts +18 -0
- package/dist/src/core/embedded-assets.js +13 -0
- package/dist/src/core/openapi.d.ts +9 -10
- package/dist/src/core/openapi.js +333 -127
- package/dist/src/core/page-router.d.ts +13 -17
- package/dist/src/core/page-router.js +86 -55
- package/dist/src/core/server.d.ts +20 -11
- package/dist/src/core/server.js +41 -45
- package/dist/src/errors/forbidden.d.ts +14 -0
- package/dist/src/errors/forbidden.js +16 -0
- package/dist/src/errors/http-error.d.ts +49 -0
- package/dist/src/errors/http-error.js +155 -0
- package/dist/src/errors/method-not-allowed.d.ts +13 -0
- package/dist/src/errors/method-not-allowed.js +15 -0
- package/dist/src/errors/not-found.d.ts +14 -0
- package/dist/src/errors/not-found.js +16 -0
- package/dist/src/errors/unauthorized.d.ts +14 -0
- package/dist/src/errors/unauthorized.js +16 -0
- package/dist/src/index.d.ts +275 -50
- package/dist/src/index.js +974 -333
- package/dist/src/lifecycle/executor.d.ts +64 -0
- package/dist/src/lifecycle/executor.js +201 -0
- package/dist/src/lifecycle/hook-runner.d.ts +11 -0
- package/dist/src/lifecycle/hook-runner.js +156 -0
- package/dist/src/lifecycle/jit.d.ts +50 -0
- package/dist/src/lifecycle/jit.js +258 -0
- package/dist/src/lifecycle/transform.d.ts +26 -0
- package/dist/src/lifecycle/transform.js +99 -0
- package/dist/src/lifecycle/types.d.ts +135 -0
- package/dist/src/lifecycle/types.js +1 -0
- package/dist/src/plugin/composer.d.ts +3 -0
- package/dist/src/plugin/composer.js +28 -0
- package/dist/src/plugin/registry.d.ts +25 -0
- package/dist/src/plugin/registry.js +107 -0
- package/dist/src/plugin/types.d.ts +20 -0
- package/dist/src/plugin/types.js +1 -0
- package/dist/src/router/allow-cache.d.ts +20 -0
- package/dist/src/router/allow-cache.js +26 -0
- package/dist/src/router/compiler.d.ts +39 -0
- package/dist/src/router/compiler.js +867 -0
- package/dist/src/router/define.d.ts +67 -0
- package/dist/src/router/define.js +34 -0
- package/dist/src/router/index.d.ts +6 -0
- package/dist/src/router/index.js +5 -0
- package/dist/src/router/param-extract.d.ts +33 -0
- package/dist/src/router/param-extract.js +87 -0
- package/dist/src/router/regex-matcher.d.ts +42 -0
- package/dist/src/router/regex-matcher.js +216 -0
- package/dist/src/router/router.d.ts +214 -0
- package/dist/src/router/router.js +670 -0
- package/dist/src/router/static-map.d.ts +39 -0
- package/dist/src/router/static-map.js +48 -0
- package/dist/src/router/trie.d.ts +69 -0
- package/dist/src/router/trie.js +572 -0
- package/dist/src/router/types.d.ts +104 -0
- package/dist/src/router/types.js +1 -0
- package/dist/src/runtime/capabilities.d.ts +29 -0
- package/dist/src/runtime/capabilities.js +48 -0
- package/dist/src/types/index.d.ts +260 -109
- package/dist/src/types/index.js +1 -1
- package/dist/src/types/inference.d.ts +78 -0
- package/dist/src/types/inference.js +20 -0
- package/dist/src/types/openapi-config.d.ts +164 -0
- package/dist/src/types/openapi-config.js +8 -0
- package/dist/src/utils/env.d.ts +13 -0
- package/dist/src/utils/env.js +24 -0
- package/dist/src/utils/fs.d.ts +29 -0
- package/dist/src/utils/fs.js +75 -0
- package/dist/src/utils/index.d.ts +1 -1
- package/dist/src/utils/index.js +2 -2
- package/dist/src/utils/pathConversion.js +13 -7
- package/dist/src/utils/response.d.ts +42 -6
- package/dist/src/utils/response.js +189 -7
- package/dist/src/utils/routing.d.ts +32 -13
- package/dist/src/utils/routing.js +41 -18
- package/dist/src/utils/thenable.d.ts +8 -0
- package/dist/src/utils/thenable.js +12 -0
- package/dist/src/utils/timing-safe.d.ts +9 -0
- package/dist/src/utils/timing-safe.js +20 -0
- package/dist/src/utils/wildcard.d.ts +6 -7
- package/dist/src/utils/wildcard.js +38 -55
- package/dist/src/validation/adapter.d.ts +54 -0
- package/dist/src/validation/adapter.js +75 -0
- package/dist/src/validation/adapters/standard.d.ts +11 -0
- package/dist/src/validation/adapters/standard.js +110 -0
- package/dist/src/validation/adapters/zod.d.ts +9 -0
- package/dist/src/validation/adapters/zod.js +128 -0
- package/dist/src/validation/cache.d.ts +26 -0
- package/dist/src/validation/cache.js +35 -0
- package/dist/src/validation/coerce.d.ts +28 -0
- package/dist/src/validation/coerce.js +176 -0
- package/dist/src/validation/compiler.d.ts +27 -0
- package/dist/src/validation/compiler.js +191 -0
- package/dist/src/validation/error.d.ts +69 -0
- package/dist/src/validation/error.js +137 -0
- package/dist/src/validation/index.d.ts +16 -0
- package/dist/src/validation/index.js +12 -0
- package/dist/src/validation/response.d.ts +25 -0
- package/dist/src/validation/response.js +85 -0
- package/dist/src/validation/types.d.ts +148 -0
- package/dist/src/validation/types.js +6 -0
- package/dist/src/validation/validator.d.ts +39 -0
- package/dist/src/validation/validator.js +441 -0
- package/dist/src/ws/adapter.d.ts +230 -0
- package/dist/src/ws/adapter.js +660 -0
- package/dist/src/ws/compiler.d.ts +37 -0
- package/dist/src/ws/compiler.js +178 -0
- package/dist/src/ws/index.d.ts +11 -0
- package/dist/src/ws/index.js +8 -0
- package/dist/src/ws/platform.d.ts +82 -0
- package/dist/src/ws/platform.js +122 -0
- package/dist/src/ws/router.d.ts +47 -0
- package/dist/src/ws/router.js +183 -0
- package/dist/src/ws/scanner.d.ts +97 -0
- package/dist/src/ws/scanner.js +253 -0
- package/dist/src/ws/types.d.ts +381 -0
- package/dist/src/ws/types.js +213 -0
- package/package.json +72 -60
- package/dist/src/core/api-router.d.ts +0 -65
- package/dist/src/core/api-router.js +0 -264
- package/dist/src/core/swagger-ui.d.ts +0 -1
- package/dist/src/core/swagger-ui.js +0 -33
- package/dist/src/middleware/validator.d.ts +0 -14
- package/dist/src/middleware/validator.js +0 -133
- package/dist/src/utils/error.d.ts +0 -17
- package/dist/src/utils/error.js +0 -63
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Runtime-capability model: what each `burger-api build --target` platform
|
|
3
|
+
* supports. Pure data, zero platform imports.
|
|
4
|
+
*
|
|
5
|
+
* Capability is a build-time property, not reliably detectable at request
|
|
6
|
+
* time (Vercel and plain Node look identical via `globalThis`), so a build
|
|
7
|
+
* declares its target up front (`ServerOptions.runtimeTarget`).
|
|
8
|
+
*
|
|
9
|
+
* Single source of truth for the CLI's build-time validation (e.g. rejecting
|
|
10
|
+
* a WebSocket route on `--target=vercel`) and the docs compatibility page.
|
|
11
|
+
*/
|
|
12
|
+
export type RuntimeTarget = 'bun' | 'node' | 'cloudflare' | 'deno' | 'vercel';
|
|
13
|
+
export interface RuntimeCapability {
|
|
14
|
+
/** Web-standard HTTP request/response handling. Every target speaks `fetch`. */
|
|
15
|
+
http: true;
|
|
16
|
+
/** Persistent WebSocket upgrades. */
|
|
17
|
+
websocket: boolean;
|
|
18
|
+
/**
|
|
19
|
+
* This target's static-asset story. `'disk'` targets can also serve
|
|
20
|
+
* `burger-api build`'s embedded assets (no fs needed); `'platform-native'`
|
|
21
|
+
* targets should prefer the platform's own static hosting.
|
|
22
|
+
*/
|
|
23
|
+
staticFiles: 'disk' | 'platform-native';
|
|
24
|
+
/** Direct filesystem access at request time. */
|
|
25
|
+
filesystem: boolean;
|
|
26
|
+
/** A long-running process rather than a per-invocation cold start. */
|
|
27
|
+
persistentProcess: boolean;
|
|
28
|
+
}
|
|
29
|
+
export declare const RUNTIME_CAPABILITIES: Record<RuntimeTarget, RuntimeCapability>;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Runtime-capability model: what each `burger-api build --target` platform
|
|
3
|
+
* supports. Pure data, zero platform imports.
|
|
4
|
+
*
|
|
5
|
+
* Capability is a build-time property, not reliably detectable at request
|
|
6
|
+
* time (Vercel and plain Node look identical via `globalThis`), so a build
|
|
7
|
+
* declares its target up front (`ServerOptions.runtimeTarget`).
|
|
8
|
+
*
|
|
9
|
+
* Single source of truth for the CLI's build-time validation (e.g. rejecting
|
|
10
|
+
* a WebSocket route on `--target=vercel`) and the docs compatibility page.
|
|
11
|
+
*/
|
|
12
|
+
export const RUNTIME_CAPABILITIES = {
|
|
13
|
+
bun: {
|
|
14
|
+
http: true,
|
|
15
|
+
websocket: true,
|
|
16
|
+
staticFiles: 'disk',
|
|
17
|
+
filesystem: true,
|
|
18
|
+
persistentProcess: true,
|
|
19
|
+
},
|
|
20
|
+
node: {
|
|
21
|
+
http: true,
|
|
22
|
+
websocket: true,
|
|
23
|
+
staticFiles: 'disk',
|
|
24
|
+
filesystem: true,
|
|
25
|
+
persistentProcess: true,
|
|
26
|
+
},
|
|
27
|
+
deno: {
|
|
28
|
+
http: true,
|
|
29
|
+
websocket: true,
|
|
30
|
+
staticFiles: 'disk',
|
|
31
|
+
filesystem: true,
|
|
32
|
+
persistentProcess: true,
|
|
33
|
+
},
|
|
34
|
+
cloudflare: {
|
|
35
|
+
http: true,
|
|
36
|
+
websocket: true,
|
|
37
|
+
staticFiles: 'platform-native',
|
|
38
|
+
filesystem: false,
|
|
39
|
+
persistentProcess: false,
|
|
40
|
+
},
|
|
41
|
+
vercel: {
|
|
42
|
+
http: true,
|
|
43
|
+
websocket: false,
|
|
44
|
+
staticFiles: 'platform-native',
|
|
45
|
+
filesystem: false,
|
|
46
|
+
persistentProcess: false,
|
|
47
|
+
},
|
|
48
|
+
};
|
|
@@ -1,9 +1,26 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type {
|
|
3
|
-
|
|
4
|
-
type
|
|
5
|
-
import {
|
|
6
|
-
|
|
1
|
+
import type { BurgerContext, BurgerEnv, BurgerExecutionContext } from '../context/context.js';
|
|
2
|
+
import type { SchemaInput, ValidatorConfig } from '../validation/types.js';
|
|
3
|
+
import type { RouteHooks } from '../lifecycle/types.js';
|
|
4
|
+
export type { RouteHooks, GlobalHooks, TransformMap } from '../lifecycle/types.js';
|
|
5
|
+
import type { OpenAPIConfig } from './openapi-config.js';
|
|
6
|
+
import type { RuntimeAdapter } from '../adapter/types.js';
|
|
7
|
+
import type { WebSocketRouteDefinition } from '../ws/types.js';
|
|
8
|
+
import type { HTTPMethod, LowercaseHTTPMethod } from '../utils/routing.js';
|
|
9
|
+
import type { RuntimeTarget } from '../runtime/capabilities.js';
|
|
10
|
+
/**
|
|
11
|
+
* Minimal structural view of the running server exposed to `fetch` handlers.
|
|
12
|
+
* The web-standard surface: upgrade a WebSocket upgrade request (Bun) or stop
|
|
13
|
+
* the server. Never references Bun types.
|
|
14
|
+
*/
|
|
15
|
+
export interface ServerInfo {
|
|
16
|
+
upgrade(request: Request, options?: Record<string, unknown>): boolean;
|
|
17
|
+
stop(): void;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Framework-level server options. Deliberately Web-Standard: no Bun types.
|
|
21
|
+
* Bun-specific tuning lives on the adapter (`BunAdapterStartOptions`).
|
|
22
|
+
*/
|
|
23
|
+
export interface ServerOptions {
|
|
7
24
|
/**
|
|
8
25
|
* The title of the API. This is an optional property that can be used
|
|
9
26
|
* to specify a custom title for the API documentation.
|
|
@@ -16,7 +33,7 @@ export interface ServerOptions extends Omit<BunServerOptions, 'fetch' | 'port' |
|
|
|
16
33
|
description?: string;
|
|
17
34
|
/**
|
|
18
35
|
* The directory path to load API routes from.
|
|
19
|
-
* If not specified,
|
|
36
|
+
* If not specified, `src/api` is auto-resolved when it exists.
|
|
20
37
|
*/
|
|
21
38
|
apiDir?: string;
|
|
22
39
|
/**
|
|
@@ -25,20 +42,20 @@ export interface ServerOptions extends Omit<BunServerOptions, 'fetch' | 'port' |
|
|
|
25
42
|
*/
|
|
26
43
|
apiPrefix?: string;
|
|
27
44
|
/**
|
|
28
|
-
* The directory path to load
|
|
29
|
-
* If not specified,
|
|
30
|
-
* Page routes are not yet supported, but will be supported in the future.
|
|
45
|
+
* The directory path to load page routes from.
|
|
46
|
+
* If not specified, `src/pages` is auto-resolved when it exists.
|
|
31
47
|
*/
|
|
32
48
|
pageDir?: string;
|
|
33
49
|
/**
|
|
34
|
-
* The prefix for the
|
|
35
|
-
* If not specified, the
|
|
50
|
+
* The prefix for the page routes.
|
|
51
|
+
* If not specified, pages mount at the root (no prefix).
|
|
36
52
|
*/
|
|
37
53
|
pagePrefix?: string;
|
|
38
54
|
/**
|
|
39
|
-
*
|
|
55
|
+
* The directory path to load WebSocket routes from.
|
|
56
|
+
* If not specified, no WebSocket routes are loaded.
|
|
40
57
|
*/
|
|
41
|
-
|
|
58
|
+
wsDir?: string;
|
|
42
59
|
/**
|
|
43
60
|
* The version of the API. This is an optional property that can be used
|
|
44
61
|
* to specify the version of the API.
|
|
@@ -60,78 +77,112 @@ export interface ServerOptions extends Omit<BunServerOptions, 'fetch' | 'port' |
|
|
|
60
77
|
* and no runtime filesystem scanning is performed. Used for bundled/executable builds.
|
|
61
78
|
*/
|
|
62
79
|
pageRoutes?: PageDefinition[];
|
|
63
|
-
}
|
|
64
|
-
type DefaultRequestProperties = {
|
|
65
|
-
params?: Record<string, unknown>;
|
|
66
|
-
query?: Record<string, unknown>;
|
|
67
|
-
body?: Record<string, unknown>;
|
|
68
|
-
};
|
|
69
|
-
export interface BurgerRequest<RequestValidatedProperties extends DefaultRequestProperties = DefaultRequestProperties> extends Omit<BunRequest<string>, 'params'> {
|
|
70
80
|
/**
|
|
71
|
-
*
|
|
72
|
-
*
|
|
73
|
-
*
|
|
74
|
-
*
|
|
75
|
-
* For example, if the route is `/users/:id`, and the request path is
|
|
76
|
-
* `/users/123`, then the `params` property will be `{ id: '123' }`.
|
|
81
|
+
* Pre-built static asset routes from the CLI build (files under
|
|
82
|
+
* `<pageDir>/assets/` embedded as base64). When present, assets are
|
|
83
|
+
* served from the embedded table; otherwise dev reads them from disk
|
|
84
|
+
* under `pageDir`.
|
|
77
85
|
*/
|
|
78
|
-
|
|
86
|
+
assetRoutes?: import('../core/assets.js').EmbeddedAsset[];
|
|
79
87
|
/**
|
|
80
|
-
*
|
|
81
|
-
*
|
|
82
|
-
*
|
|
83
|
-
*
|
|
84
|
-
*
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
*
|
|
88
|
+
* Pre-built WebSocket routes (e.g. from CLI build). When provided, wsDir is
|
|
89
|
+
* ignored and no runtime filesystem scanning is performed. Wired through
|
|
90
|
+
* both `serve()` (Bun) and `fetchHandler()`/`toFetchHandler()` (the
|
|
91
|
+
* WinterCG entry) — see `ws/platform.ts` for per-runtime upgrade support.
|
|
92
|
+
* Used for bundled/executable builds.
|
|
93
|
+
*/
|
|
94
|
+
wsRoutes?: WebSocketRouteDefinition[];
|
|
95
|
+
/**
|
|
96
|
+
* Validation configuration: coercion, response-validation mode, and error
|
|
97
|
+
* rendering.
|
|
98
|
+
*/
|
|
99
|
+
validation?: ValidatorConfig;
|
|
100
|
+
/**
|
|
101
|
+
* JIT-compile each route's HookPlan into a single async function.
|
|
102
|
+
* ON by default; capability-probed at startup, so runtimes that forbid
|
|
103
|
+
* dynamic code generation (Cloudflare Workers) silently keep the
|
|
104
|
+
* interpreter. Set `false` to opt out.
|
|
105
|
+
*/
|
|
106
|
+
jit?: boolean;
|
|
107
|
+
/**
|
|
108
|
+
* Dynamic-route dispatch engine for the fetch-fallback path.
|
|
109
|
+
* `'auto'` (default) and `'trie'` use the radix trie; `'regex'` opts into
|
|
110
|
+
* a compiled regex alternation matcher. Static dispatch is unaffected.
|
|
111
|
+
*/
|
|
112
|
+
engine?: 'auto' | 'regex' | 'trie';
|
|
113
|
+
/**
|
|
114
|
+
* OpenAPI configuration for production builds. When using pre-built
|
|
115
|
+
* `apiRoutes`, the convention file cannot be discovered from the filesystem,
|
|
116
|
+
* so the config must be passed here. In dev mode, `openapi.config.ts` is
|
|
117
|
+
* auto-discovered and this field is ignored.
|
|
118
|
+
*/
|
|
119
|
+
openapi?: OpenAPIConfig;
|
|
120
|
+
/**
|
|
121
|
+
* Pre-resolved global hooks module (e.g. from `src/hooks.ts`).
|
|
122
|
+
* Only used in production builds (when `apiRoutes` is provided).
|
|
123
|
+
* In dev mode, `src/hooks.ts` is auto-discovered and this field is ignored.
|
|
89
124
|
*/
|
|
90
|
-
|
|
125
|
+
globalHooks?: Record<string, unknown>;
|
|
91
126
|
/**
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
*
|
|
95
|
-
* For example, if the route is `/users/[...]`, and the request path is
|
|
96
|
-
* `/users/123/456`, then the `wildcardParams` property will be `['123', '456']`.
|
|
127
|
+
* Pre-resolved plugins module (e.g. from `src/plugins.ts`).
|
|
128
|
+
* Only used in production builds (when `apiRoutes` is provided).
|
|
129
|
+
* In dev mode, `src/plugins.ts` is auto-discovered and this field is ignored.
|
|
97
130
|
*/
|
|
98
|
-
|
|
131
|
+
pluginsModule?: Record<string, unknown>;
|
|
132
|
+
/**
|
|
133
|
+
* Pre-resolved providers module (e.g. from `src/providers.ts`).
|
|
134
|
+
* Only used in production builds (when `apiRoutes` is provided).
|
|
135
|
+
* In dev mode, `src/providers.ts` is auto-discovered and this field is ignored.
|
|
136
|
+
*/
|
|
137
|
+
providersModule?: Record<string, unknown>;
|
|
138
|
+
/**
|
|
139
|
+
* Optional hostname to bind for `serve()`. Web-standard; forwarded to the
|
|
140
|
+
* adapter.
|
|
141
|
+
*/
|
|
142
|
+
hostname?: string;
|
|
143
|
+
/**
|
|
144
|
+
* Maximum request body size in bytes for `serve()` (Bun's default is
|
|
145
|
+
* 128 MB). Larger bodies are rejected by the runtime with 413.
|
|
146
|
+
*/
|
|
147
|
+
maxRequestBodySize?: number;
|
|
148
|
+
/**
|
|
149
|
+
* Optional runtime adapter override (test/embed seam). Defaults to the Bun
|
|
150
|
+
* adapter, loaded lazily on first `serve()` so non-Bun bundles never
|
|
151
|
+
* import it.
|
|
152
|
+
*/
|
|
153
|
+
adapter?: RuntimeAdapter;
|
|
154
|
+
/**
|
|
155
|
+
* The deployment target this build was produced for — set automatically
|
|
156
|
+
* by `burger-api build --target=<platform>`. When present, WebSocket
|
|
157
|
+
* upgrade handling trusts this over live runtime detection: some targets
|
|
158
|
+
* (Node, Vercel) are structurally indistinguishable via `globalThis`
|
|
159
|
+
* alone, so a declared target resolves the ambiguity that live
|
|
160
|
+
* detection cannot. See `runtime/capabilities.ts`.
|
|
161
|
+
*/
|
|
162
|
+
runtimeTarget?: RuntimeTarget;
|
|
99
163
|
}
|
|
100
|
-
/**
|
|
101
|
-
* Represents what a middleware can return to control the request flow:
|
|
102
|
-
* - Response: Stop here, send this response back to the client
|
|
103
|
-
* - Function(Response): Continue processing, but transform the final response after handler runs
|
|
104
|
-
* - undefined: I'm done, continue to the next middleware or handler
|
|
105
|
-
*/
|
|
106
|
-
export type BurgerNext = Response | ((response: Response) => Promise<Response>) | undefined;
|
|
107
|
-
/**
|
|
108
|
-
* A middleware function that processes HTTP requests.
|
|
109
|
-
*
|
|
110
|
-
* What middleware can do:
|
|
111
|
-
* - Check if the request is valid (auth, validation, etc.)
|
|
112
|
-
* - Stop the request by returning a Response
|
|
113
|
-
* - Let the request continue by returning undefined
|
|
114
|
-
* - Transform the final response by returning a function
|
|
115
|
-
*
|
|
116
|
-
* @param request - The HTTP request with Burger framework enhancements
|
|
117
|
-
* @returns One of three things:
|
|
118
|
-
* - Response: Stop here, send this response back
|
|
119
|
-
* - Function: Transform the final response after handler runs
|
|
120
|
-
* - undefined: Continue to the next middleware or handler
|
|
121
|
-
*/
|
|
122
|
-
export type Middleware = ((request: BurgerRequest) => Promise<BurgerNext>) | ((request: BurgerRequest) => BurgerNext);
|
|
123
164
|
/**
|
|
124
165
|
* A request handler function that processes incoming HTTP requests.
|
|
125
|
-
* @param
|
|
166
|
+
* @param ctx - The BurgerContext object containing request data and services.
|
|
126
167
|
* @returns A Response object or a Promise that resolves to a Response object.
|
|
127
168
|
*/
|
|
128
|
-
export type RequestHandler = (
|
|
169
|
+
export type RequestHandler = (ctx: BurgerContext) => Promise<Response> | Response;
|
|
129
170
|
/**
|
|
130
171
|
* A fetch handler function that can be used to handle a request.
|
|
131
172
|
* This can be a function that returns a Promise of a Response,
|
|
132
173
|
* or a function that returns a Response.
|
|
174
|
+
*
|
|
175
|
+
* `server` is an optional structural view of the running server (upgrade/stop)
|
|
176
|
+
* — Web-Standard, never Bun-specific.
|
|
177
|
+
*/
|
|
178
|
+
export type FetchHandler = (request: Request, server?: ServerInfo, env?: BurgerEnv, executionCtx?: BurgerExecutionContext) => Promise<Response> | Response;
|
|
179
|
+
/**
|
|
180
|
+
* The handler returned by `burger.fetchHandler()` and wrapped by
|
|
181
|
+
* `toFetchHandler(burger)`: a Web-Standard dispatch entry that accepts the
|
|
182
|
+
* platform bindings (`env`, `executionCtx`) supplied by WinterCG runtimes
|
|
183
|
+
* (Cloudflare Workers, Vercel, Deno, Node 24+). On Bun both are `undefined`.
|
|
133
184
|
*/
|
|
134
|
-
export type
|
|
185
|
+
export type EnvFetchHandler = (request: Request, env?: BurgerEnv, executionCtx?: BurgerExecutionContext) => Promise<Response>;
|
|
135
186
|
export interface RouteDefinition {
|
|
136
187
|
/**
|
|
137
188
|
* The path of the route.
|
|
@@ -139,17 +190,21 @@ export interface RouteDefinition {
|
|
|
139
190
|
path: string;
|
|
140
191
|
/**
|
|
141
192
|
* An object containing the request handlers for each HTTP method.
|
|
142
|
-
* The keys are the HTTP method names (e.g. "GET", "POST", etc.)
|
|
193
|
+
* The keys are the HTTP method names (e.g. "GET", "POST", etc.);
|
|
194
|
+
* only the methods in the {@link HTTPMethod} union are accepted —
|
|
195
|
+
* anything else fails at compile time and would 405 at runtime.
|
|
143
196
|
* The values are the request handlers for that method.
|
|
144
197
|
*/
|
|
145
|
-
handlers:
|
|
146
|
-
[method: string]: RequestHandler;
|
|
147
|
-
};
|
|
198
|
+
handlers: Partial<Record<HTTPMethod, RequestHandler>>;
|
|
148
199
|
/**
|
|
149
|
-
*
|
|
150
|
-
*
|
|
200
|
+
* Lifecycle hooks declared in `hooks.ts`. Carried raw from the
|
|
201
|
+
* compiler and compiled into a frozen `HookPlan` by RouterCompiler. Mapped
|
|
202
|
+
* onto the single pipeline: `beforeRoute` (global → route) runs before
|
|
203
|
+
* the handler; `afterRoute` / `mapResponse` are response hooks.
|
|
204
|
+
* `route.ts` contains handlers only — there is no
|
|
205
|
+
* per-route `hooks` export.
|
|
151
206
|
*/
|
|
152
|
-
|
|
207
|
+
hooks?: RouteHooks;
|
|
153
208
|
/**
|
|
154
209
|
* An optional route schema to validate the request data against.
|
|
155
210
|
* This property is set by the user when defining a route.
|
|
@@ -171,50 +226,145 @@ export interface RouteDefinition {
|
|
|
171
226
|
* This property is used internally to identify wildcard routes.
|
|
172
227
|
*/
|
|
173
228
|
isWildcard?: boolean;
|
|
229
|
+
/**
|
|
230
|
+
* Route-specific configuration from `config.ts`. Available as `ctx.config`
|
|
231
|
+
* at runtime. Core reads only `responseValidation` ('off' | 'dev' |
|
|
232
|
+
* 'enforce' — overrides `ServerOptions.validation.responseValidation`
|
|
233
|
+
* for this route). Every other key (auth, cache, timeout, …) is plain
|
|
234
|
+
* data that core never acts on — hooks and plugins (e.g. an auth plugin
|
|
235
|
+
* reading `ctx.config.auth`) give it meaning.
|
|
236
|
+
*
|
|
237
|
+
* Uppercase method keys (`{ POST: { auth: { required: true } } }`) are
|
|
238
|
+
* per-method overrides, shallow-merged over the route-wide options for
|
|
239
|
+
* that method only.
|
|
240
|
+
*/
|
|
241
|
+
config?: Record<string, unknown>;
|
|
174
242
|
}
|
|
175
243
|
/**
|
|
176
|
-
*
|
|
177
|
-
*
|
|
244
|
+
* The per-method body of a route schema. For each HTTP method you can
|
|
245
|
+
* optionally define:
|
|
178
246
|
* - params: for URL parameters,
|
|
179
247
|
* - query: for query string parameters,
|
|
180
|
-
* - body: for the request
|
|
248
|
+
* - headers / cookies / body: for the corresponding request data,
|
|
249
|
+
* - response: per-status response schemas validated after the handler.
|
|
181
250
|
*/
|
|
182
|
-
export
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
251
|
+
export interface MethodSchema {
|
|
252
|
+
params?: SchemaInput;
|
|
253
|
+
query?: SchemaInput;
|
|
254
|
+
headers?: SchemaInput;
|
|
255
|
+
cookies?: SchemaInput;
|
|
256
|
+
body?: SchemaInput;
|
|
257
|
+
/** Per-route opt-in override for coercion. */
|
|
258
|
+
coerce?: boolean;
|
|
259
|
+
/** Per-status-code response schemas, validated after the handler. */
|
|
260
|
+
response?: Record<string, SchemaInput>;
|
|
261
|
+
}
|
|
189
262
|
/**
|
|
190
|
-
*
|
|
191
|
-
* Each key is an HTTP method name (in lowercase) and the value is an object
|
|
192
|
-
* containing the OpenAPI metadata for that method.
|
|
193
|
-
*
|
|
194
|
-
* If the `openapi` property is not defined, the route will not be included in
|
|
195
|
-
* the generated OpenAPI documentation.
|
|
263
|
+
* Define a type for the route schema.
|
|
196
264
|
*
|
|
197
|
-
*
|
|
198
|
-
*
|
|
265
|
+
* Keys are HTTP method names; both lowercase (canonical, matching the
|
|
266
|
+
* compiled form) and uppercase (accepted by the compiler for programmatic
|
|
267
|
+
* routes) are legal at runtime, so both are accepted here. Anything else
|
|
268
|
+
* fails at compile time.
|
|
199
269
|
*/
|
|
200
|
-
export type
|
|
201
|
-
|
|
202
|
-
|
|
270
|
+
export type RouteSchema = Partial<Record<LowercaseHTTPMethod | HTTPMethod, MethodSchema>> & {
|
|
271
|
+
/**
|
|
272
|
+
* `export const coerce = true` in `schema.ts`: default coercion for
|
|
273
|
+
* every method in the file. A per-method `coerce` overrides nothing —
|
|
274
|
+
* either flag enables coercion for that method.
|
|
275
|
+
*/
|
|
276
|
+
coerce?: boolean;
|
|
277
|
+
};
|
|
278
|
+
/**
|
|
279
|
+
* Per-method OpenAPI metadata. Each key is an HTTP method name in lowercase
|
|
280
|
+
* (the compiled form the OpenAPI generator reads); uppercase keys are a
|
|
281
|
+
* silent no-op at runtime, so they are rejected at compile time.
|
|
282
|
+
*/
|
|
283
|
+
export interface OpenAPIMeta {
|
|
284
|
+
summary?: string;
|
|
285
|
+
description?: string;
|
|
286
|
+
tags?: string[];
|
|
287
|
+
operationId?: string;
|
|
288
|
+
deprecated?: boolean;
|
|
289
|
+
responses?: Record<string, Record<string, unknown>>;
|
|
290
|
+
externalDocs?: {
|
|
203
291
|
description?: string;
|
|
204
|
-
|
|
205
|
-
operationId?: string;
|
|
206
|
-
deprecated?: boolean;
|
|
207
|
-
responses?: Record<string, any>;
|
|
208
|
-
externalDocs?: {
|
|
209
|
-
description?: string;
|
|
210
|
-
url?: string;
|
|
211
|
-
};
|
|
292
|
+
url?: string;
|
|
212
293
|
};
|
|
213
|
-
}
|
|
294
|
+
}
|
|
295
|
+
/**
|
|
296
|
+
* Optional OpenAPI metadata to enrich the route's documentation.
|
|
297
|
+
* Keyed by lowercase HTTP method; see {@link OpenAPIMeta}.
|
|
298
|
+
*
|
|
299
|
+
* Routes without `openapi` metadata ARE included in the generated document:
|
|
300
|
+
* the operation is derived from the route path, handlers and schema.
|
|
301
|
+
*/
|
|
302
|
+
export type openapi = Partial<Record<LowercaseHTTPMethod, OpenAPIMeta>>;
|
|
303
|
+
/**
|
|
304
|
+
* The shape of a `burger.build.ts` export — build-time settings read by the
|
|
305
|
+
* CLI (dirs, prefixes, debug). The dir/prefix fields are always present in
|
|
306
|
+
* a scaffolded file (convention defaults apply otherwise). Build-time only;
|
|
307
|
+
* runtime options belong in `new Burger({...})` (`ServerOptions`).
|
|
308
|
+
*/
|
|
309
|
+
export interface BuildConfig {
|
|
310
|
+
/** Directory with API route files (e.g. `./src/api`). */
|
|
311
|
+
apiDir: string;
|
|
312
|
+
/** Directory with HTML page files (e.g. `./src/pages`). */
|
|
313
|
+
pageDir: string;
|
|
314
|
+
/** URL prefix for API routes (default `/api`). */
|
|
315
|
+
apiPrefix: string;
|
|
316
|
+
/** URL prefix for page routes (default `/`). */
|
|
317
|
+
pagePrefix: string;
|
|
318
|
+
/** Directory with WebSocket route files (e.g. `./src/websocket`). */
|
|
319
|
+
wsDir?: string;
|
|
320
|
+
/** Extra logging when true. */
|
|
321
|
+
debug?: boolean;
|
|
322
|
+
/**
|
|
323
|
+
* Deployment target for `burger-api build --target=<platform>`.
|
|
324
|
+
* Defaults to `'bun'`. See `RUNTIME_CAPABILITIES` for what each target
|
|
325
|
+
* supports.
|
|
326
|
+
*/
|
|
327
|
+
target?: RuntimeTarget;
|
|
328
|
+
}
|
|
329
|
+
/**
|
|
330
|
+
* Empty interface for module augmentation. Extend this to type the route's
|
|
331
|
+
* `config.ts` options, so `ctx.config` is typed for hooks and plugins:
|
|
332
|
+
*
|
|
333
|
+
* ```ts
|
|
334
|
+
* declare module "burger-api" {
|
|
335
|
+
* interface RouteConfig {
|
|
336
|
+
* auth: boolean;
|
|
337
|
+
* timeout: number;
|
|
338
|
+
* }
|
|
339
|
+
* }
|
|
340
|
+
* ```
|
|
341
|
+
*
|
|
342
|
+
* Without augmentation, unknown keys read as `unknown`; augment to type
|
|
343
|
+
* them.
|
|
344
|
+
*
|
|
345
|
+
* Core itself only reads `responseValidation`; keys such as `auth`,
|
|
346
|
+
* `cache` or `timeout` do nothing unless a plugin or hook reads them.
|
|
347
|
+
*/
|
|
348
|
+
export interface RouteConfig {
|
|
349
|
+
/**
|
|
350
|
+
* Response-validation mode for this route, overriding
|
|
351
|
+
* `ServerOptions.validation.responseValidation`:
|
|
352
|
+
* `'off'` (skip), `'dev'` (warn only), `'enforce'` (replace with an
|
|
353
|
+
* error response).
|
|
354
|
+
*/
|
|
355
|
+
responseValidation?: 'off' | 'dev' | 'enforce';
|
|
356
|
+
/**
|
|
357
|
+
* Any other option (`auth`, `cache`, `timeout`, …) is plain data core
|
|
358
|
+
* never acts on. The index signature keeps `satisfies RouteConfig`
|
|
359
|
+
* compiling for them; augment `RouteConfig` to type them.
|
|
360
|
+
*/
|
|
361
|
+
[key: string]: unknown;
|
|
362
|
+
}
|
|
214
363
|
export interface PageDefinition {
|
|
215
364
|
path: string;
|
|
216
365
|
handler: RequestHandler;
|
|
217
|
-
|
|
366
|
+
/** Origin of the page (file path) — used in collision errors. */
|
|
367
|
+
source?: string;
|
|
218
368
|
}
|
|
219
369
|
export interface TrieNode {
|
|
220
370
|
children: Map<string, TrieNode>;
|
|
@@ -224,4 +374,5 @@ export interface TrieNode {
|
|
|
224
374
|
isWildcard?: boolean;
|
|
225
375
|
route?: RouteDefinition;
|
|
226
376
|
}
|
|
227
|
-
export {};
|
|
377
|
+
export type { ContextField, ContextInit, ContextSet, RouteAccessInfo, RouteMeta, } from '../context/types.js';
|
|
378
|
+
export type { OpenAPIConfig, DocsProvider, DocsProviderOptions, OpenAPIObject, JsonSchemaConverter, DocsAuth, OpenAPIServer, OpenAPIContact, OpenAPILicense, OpenAPIExternalDocs, } from './openapi-config.js';
|
package/dist/src/types/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
export {};
|
|
@@ -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 {};
|