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,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
|
package/dist/src/utils/index.js
CHANGED
|
@@ -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
|
-
|
|
12
|
-
|
|
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.
|
|
63
|
-
|
|
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
|
-
*
|
|
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
|
|
7
|
+
export declare const notFound: () => Response;
|
|
5
8
|
/**
|
|
6
|
-
* The
|
|
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
|
|
12
|
+
export declare const openApiError: () => Response;
|
|
9
13
|
/**
|
|
10
|
-
*
|
|
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
|
|
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;
|
|
@@ -1,17 +1,199 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* The
|
|
2
|
+
* The 404 body is a constant (RFC 9457 Problem Details), so it is serialized
|
|
3
|
+
* once; every call builds a fresh `Response` over it.
|
|
3
4
|
*/
|
|
4
|
-
|
|
5
|
-
|
|
5
|
+
const NOT_FOUND_BODY = JSON.stringify({
|
|
6
|
+
type: 'about:blank',
|
|
7
|
+
title: 'Not Found',
|
|
8
|
+
status: 404,
|
|
9
|
+
detail: 'Not Found',
|
|
6
10
|
});
|
|
7
11
|
/**
|
|
8
|
-
*
|
|
12
|
+
* 404 init with a prebuilt `Headers`: the constructor copies it, which is
|
|
13
|
+
* much cheaper than parsing a plain headers object per call. Built on first
|
|
14
|
+
* use, never at module scope (Workers restrict work during module load).
|
|
9
15
|
*/
|
|
10
|
-
|
|
16
|
+
let notFoundInit;
|
|
11
17
|
/**
|
|
12
|
-
*
|
|
18
|
+
* Builds the framework's 404 response (RFC 9457 Problem Details). A fresh
|
|
19
|
+
* `Response` per call: a cached template cannot be cloned reliably — runtimes
|
|
20
|
+
* (workerd) lock a shared template's body stream once it has been served, so
|
|
21
|
+
* the next `clone()` throws and the request 500s.
|
|
13
22
|
*/
|
|
14
|
-
export const
|
|
23
|
+
export const notFound = () => new Response(NOT_FOUND_BODY, (notFoundInit ??= {
|
|
24
|
+
status: 404,
|
|
25
|
+
headers: new Headers({
|
|
26
|
+
'Content-Type': 'application/problem+json',
|
|
27
|
+
}),
|
|
28
|
+
}));
|
|
29
|
+
/**
|
|
30
|
+
* The OpenAPI error response. A factory — see {@link notFound}: never share a
|
|
31
|
+
* `Response` instance with a body across requests.
|
|
32
|
+
*/
|
|
33
|
+
export const openApiError = () => Response.json({
|
|
15
34
|
error: 'API Router not configured',
|
|
16
35
|
message: 'Please provide an apiDir option when initializing the Burger instance to enable OpenAPI documentation.',
|
|
17
36
|
});
|
|
37
|
+
/**
|
|
38
|
+
* Per-`Allow` 405 templates, built once and reused. Bodies are single-use, so
|
|
39
|
+
* each request gets a fresh `Response` over the same immutable init; the map
|
|
40
|
+
* is bounded by the route table's `Allow` values.
|
|
41
|
+
*/
|
|
42
|
+
const methodNotAllowedTemplates = new Map();
|
|
43
|
+
/**
|
|
44
|
+
* Builds a 405 (RFC 9457) with the `Allow` header listing the methods the
|
|
45
|
+
* matched route supports.
|
|
46
|
+
* @param allow - comma-separated allowed methods, e.g. "GET, POST"
|
|
47
|
+
*/
|
|
48
|
+
export function methodNotAllowed(allow) {
|
|
49
|
+
let template = methodNotAllowedTemplates.get(allow);
|
|
50
|
+
if (template === undefined) {
|
|
51
|
+
template = {
|
|
52
|
+
body: JSON.stringify({
|
|
53
|
+
type: 'about:blank',
|
|
54
|
+
title: 'Method Not Allowed',
|
|
55
|
+
status: 405,
|
|
56
|
+
detail: `Supported methods: ${allow}`,
|
|
57
|
+
}),
|
|
58
|
+
headers: new Headers({
|
|
59
|
+
Allow: allow,
|
|
60
|
+
'Content-Type': 'application/problem+json',
|
|
61
|
+
}),
|
|
62
|
+
};
|
|
63
|
+
methodNotAllowedTemplates.set(allow, template);
|
|
64
|
+
}
|
|
65
|
+
return new Response(template.body, {
|
|
66
|
+
status: 405,
|
|
67
|
+
headers: template.headers,
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
export const createAutoOptionsHandler = (allowMethods) => {
|
|
71
|
+
const allowHeader = allowMethods.join(', ');
|
|
72
|
+
const handler = (() => new Response(null, {
|
|
73
|
+
status: 204,
|
|
74
|
+
headers: { Allow: allowHeader },
|
|
75
|
+
}));
|
|
76
|
+
handler.isAutoOptions = true;
|
|
77
|
+
handler.allowHeader = allowHeader;
|
|
78
|
+
return handler;
|
|
79
|
+
};
|
|
80
|
+
import { SET_HEADERS, TrackedContextSet, } from '../context/context-set.js';
|
|
81
|
+
/**
|
|
82
|
+
* Whether a `ContextSet` carries any response mutation. For the framework's
|
|
83
|
+
* `TrackedContextSet` it is one flag read; plain objects fall back to a scan.
|
|
84
|
+
* `applySet` uses this to skip rebuilding the `Response`.
|
|
85
|
+
*/
|
|
86
|
+
export function hasSetMutations(set) {
|
|
87
|
+
if (!set)
|
|
88
|
+
return false;
|
|
89
|
+
if (set instanceof TrackedContextSet)
|
|
90
|
+
return set.flags !== 0;
|
|
91
|
+
if (set.status !== undefined)
|
|
92
|
+
return true;
|
|
93
|
+
const headers = set.headers;
|
|
94
|
+
if (headers) {
|
|
95
|
+
if (headers instanceof Headers) {
|
|
96
|
+
// Bun's `Headers.size` typing is unreliable; iterate to detect content.
|
|
97
|
+
for (const _ of headers) {
|
|
98
|
+
return true;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
else if (Object.keys(headers).length > 0) {
|
|
102
|
+
return true;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Null-body statuses (1xx/204/205/304) reject a body on Node and would leak
|
|
109
|
+
* an unused stream elsewhere. Cancel it before dropping it.
|
|
110
|
+
*/
|
|
111
|
+
function bodyForStatus(response, status) {
|
|
112
|
+
const nullBody = status === 204 ||
|
|
113
|
+
status === 205 ||
|
|
114
|
+
status === 304 ||
|
|
115
|
+
(status >= 100 && status < 200);
|
|
116
|
+
if (!nullBody)
|
|
117
|
+
return response.body;
|
|
118
|
+
if (response.body !== null) {
|
|
119
|
+
void response.body.cancel().catch(() => { });
|
|
120
|
+
}
|
|
121
|
+
return null;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Merges a `ContextSet` (`ctx.set`) into the outgoing `Response`:
|
|
125
|
+
* - `set.headers` is merged over the response's headers; explicitly set values
|
|
126
|
+
* win and other handler headers are kept.
|
|
127
|
+
* - `set.status` overrides the response status only when defined, and NEVER
|
|
128
|
+
* overrides an error status (>= 400) — errors stay errors.
|
|
129
|
+
* - Runs once at the pipeline exit, for every response path; returns the
|
|
130
|
+
* original `Response` unchanged when there is nothing to apply.
|
|
131
|
+
*/
|
|
132
|
+
export function applySet(response, set) {
|
|
133
|
+
if (!set)
|
|
134
|
+
return response;
|
|
135
|
+
// Errors are authoritative: `ctx.set.status` may only restyle successes.
|
|
136
|
+
const setStatus = response.status >= 400 ? undefined : set.status;
|
|
137
|
+
const finalStatus = setStatus ?? response.status;
|
|
138
|
+
if (set instanceof TrackedContextSet) {
|
|
139
|
+
const flags = set.flags;
|
|
140
|
+
if (flags === 0)
|
|
141
|
+
return response;
|
|
142
|
+
if ((flags & SET_HEADERS) === 0) {
|
|
143
|
+
// Status-only mutation: the `Response` constructor copies the
|
|
144
|
+
// header list itself, so no explicit `new Headers(...)` copy is
|
|
145
|
+
// needed.
|
|
146
|
+
return new Response(bodyForStatus(response, finalStatus), {
|
|
147
|
+
status: finalStatus,
|
|
148
|
+
statusText: response.statusText,
|
|
149
|
+
headers: response.headers,
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
else if (!hasSetMutations(set)) {
|
|
154
|
+
return response;
|
|
155
|
+
}
|
|
156
|
+
const headers = new Headers(response.headers);
|
|
157
|
+
const setHeaders = set.headers;
|
|
158
|
+
if (setHeaders) {
|
|
159
|
+
if (setHeaders instanceof Headers) {
|
|
160
|
+
// Preserve every Set-Cookie value (iteration may combine them).
|
|
161
|
+
const getSetCookie = setHeaders.getSetCookie;
|
|
162
|
+
if (typeof getSetCookie === 'function') {
|
|
163
|
+
for (const cookie of getSetCookie.call(setHeaders)) {
|
|
164
|
+
headers.append('set-cookie', cookie);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
for (const entry of setHeaders) {
|
|
168
|
+
if (entry[0].toLowerCase() === 'set-cookie')
|
|
169
|
+
continue;
|
|
170
|
+
headers.set(entry[0], entry[1]);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
else {
|
|
174
|
+
for (const key in setHeaders) {
|
|
175
|
+
const value = setHeaders[key];
|
|
176
|
+
if (value === undefined)
|
|
177
|
+
continue;
|
|
178
|
+
// Arrays append (multiple cookies); a scalar Set-Cookie is
|
|
179
|
+
// appended too, so a handler-set cookie survives. Other
|
|
180
|
+
// headers keep "ctx.set wins".
|
|
181
|
+
if (Array.isArray(value)) {
|
|
182
|
+
for (const item of value)
|
|
183
|
+
headers.append(key, item);
|
|
184
|
+
}
|
|
185
|
+
else if (key.toLowerCase() === 'set-cookie') {
|
|
186
|
+
headers.append(key, value);
|
|
187
|
+
}
|
|
188
|
+
else {
|
|
189
|
+
headers.set(key, value);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
return new Response(bodyForStatus(response, finalStatus), {
|
|
195
|
+
status: finalStatus,
|
|
196
|
+
statusText: response.statusText,
|
|
197
|
+
headers,
|
|
198
|
+
});
|
|
199
|
+
}
|