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
|
@@ -1,133 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* createValidationMiddleware:
|
|
3
|
-
* - Precompute a smaller runtime descriptor per method to avoid
|
|
4
|
-
* repeatedly touching the full schema object on every request.
|
|
5
|
-
* - Uses safeParse (sync) for Zod schemas (no try/catch around safeParse).
|
|
6
|
-
* - Parses querystring manually (no URL constructor, no throw).
|
|
7
|
-
* - Lazily allocates the errors container only when needed.
|
|
8
|
-
* - Avoids unnecessary object allocations for successful paths.
|
|
9
|
-
*
|
|
10
|
-
* @param schema - The route schema to validate the request against.
|
|
11
|
-
* @returns The middleware function that validates the request.
|
|
12
|
-
*/
|
|
13
|
-
export function createValidationMiddleware(schema) {
|
|
14
|
-
// Precompute a minimal descriptor per HTTP method (lowercased)
|
|
15
|
-
// This work runs once at middleware creation time (fast startup cost, zero cost on each request for those computations).
|
|
16
|
-
const methodMap = {};
|
|
17
|
-
// Loop through the schema and create a method map
|
|
18
|
-
for (const rawMethod of Object.keys(schema)) {
|
|
19
|
-
// Convert the method to lowercase
|
|
20
|
-
const key = rawMethod.toLowerCase();
|
|
21
|
-
// Get the method schema
|
|
22
|
-
const m = schema[rawMethod] || {};
|
|
23
|
-
// Create a method map
|
|
24
|
-
methodMap[key] = {
|
|
25
|
-
paramsSchema: m.params, // URL parameters schema
|
|
26
|
-
querySchema: m.query, // Query parameters schema
|
|
27
|
-
bodySchema: m.body, // Request body schema
|
|
28
|
-
hasParams: !!m.params, // Whether the method has URL parameters
|
|
29
|
-
hasQuery: !!m.query, // Whether the method has query parameters
|
|
30
|
-
hasBody: !!m.body, // Whether the method has a request body
|
|
31
|
-
};
|
|
32
|
-
}
|
|
33
|
-
/**
|
|
34
|
-
* The middleware function that validates the request.
|
|
35
|
-
*
|
|
36
|
-
* @param req - The request object.
|
|
37
|
-
* @returns The next middleware or handler.
|
|
38
|
-
*/
|
|
39
|
-
return async (req) => {
|
|
40
|
-
// If the request has been validated, continue.
|
|
41
|
-
if (req.validated) {
|
|
42
|
-
return undefined; // Proceed to the next middleware or handler
|
|
43
|
-
}
|
|
44
|
-
// Determine the HTTP method (in lowercase) to match the schema.
|
|
45
|
-
const method = (req.method || 'get').toLowerCase();
|
|
46
|
-
// Get the schema for the current method.
|
|
47
|
-
const methodSchema = methodMap[method];
|
|
48
|
-
// If there's no schema for this method, continue.
|
|
49
|
-
if (!methodSchema) {
|
|
50
|
-
return undefined; // Proceed to the next middleware or handler
|
|
51
|
-
}
|
|
52
|
-
// Get the schemas for the current method.
|
|
53
|
-
const { paramsSchema, querySchema, bodySchema, hasParams, hasQuery, hasBody, } = methodSchema;
|
|
54
|
-
/**
|
|
55
|
-
* Object to store validated request data. This will be attached to the
|
|
56
|
-
* request object if validation is successful. The object will contain
|
|
57
|
-
* validated data for the following fields:
|
|
58
|
-
*
|
|
59
|
-
* - `params`: Validated URL parameters (if available and schema provided).
|
|
60
|
-
* - `query`: Validated query parameters (if available and schema provided).
|
|
61
|
-
* - `body`: Validated request body (if JSON and schema provided).
|
|
62
|
-
*/
|
|
63
|
-
const validated = {};
|
|
64
|
-
// Lazy errors — create only if/when an error occurs
|
|
65
|
-
let errors = null;
|
|
66
|
-
// Validate URL parameters (if available and schema provided).
|
|
67
|
-
if (hasParams && req.params && paramsSchema) {
|
|
68
|
-
const result = paramsSchema.safeParse(req.params);
|
|
69
|
-
if (result.success) {
|
|
70
|
-
validated.params = result.data;
|
|
71
|
-
}
|
|
72
|
-
else {
|
|
73
|
-
if (!errors)
|
|
74
|
-
errors = {};
|
|
75
|
-
errors.params = result.error.issues;
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
// Validate query parameters if available and schema provided.
|
|
79
|
-
if (hasQuery && querySchema) {
|
|
80
|
-
const url = new URL(req.url);
|
|
81
|
-
const queryParams = Object.fromEntries(url.searchParams.entries());
|
|
82
|
-
const result = querySchema.safeParse(queryParams);
|
|
83
|
-
if (result.success) {
|
|
84
|
-
validated.query = result.data;
|
|
85
|
-
}
|
|
86
|
-
else {
|
|
87
|
-
if (!errors)
|
|
88
|
-
errors = {};
|
|
89
|
-
errors.query = result.error.issues;
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
// Get the content type header
|
|
93
|
-
const contentType = req.headers.get('content-type') ??
|
|
94
|
-
req.headers.get('Content-Type') ??
|
|
95
|
-
'';
|
|
96
|
-
// Validate request body if it's JSON
|
|
97
|
-
if (hasBody &&
|
|
98
|
-
contentType?.includes('application/json') &&
|
|
99
|
-
bodySchema) {
|
|
100
|
-
try {
|
|
101
|
-
// Attempt to parse the JSON body.
|
|
102
|
-
const bodyData = await req.json();
|
|
103
|
-
const result = bodySchema.safeParse(bodyData);
|
|
104
|
-
if (result.success) {
|
|
105
|
-
// Set the validated body.
|
|
106
|
-
validated.body = result.data;
|
|
107
|
-
// Set the json method to return the validated body.
|
|
108
|
-
req.json = async () => result.data;
|
|
109
|
-
}
|
|
110
|
-
else {
|
|
111
|
-
if (!errors)
|
|
112
|
-
errors = {};
|
|
113
|
-
errors.body = result.error.issues;
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
catch (error) {
|
|
117
|
-
// Create a message from the error.
|
|
118
|
-
const msg = error instanceof Error ? error.message : String(error);
|
|
119
|
-
if (!errors)
|
|
120
|
-
errors = {};
|
|
121
|
-
errors.body = [{ message: msg }];
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
if (errors) {
|
|
125
|
-
// If validation fails, return a 400 response with error details.
|
|
126
|
-
return Response.json({ errors }, { status: 400 });
|
|
127
|
-
}
|
|
128
|
-
// Attach validated data to the request.
|
|
129
|
-
req.validated = validated;
|
|
130
|
-
// Continue to the next middleware or handler.
|
|
131
|
-
return undefined; // Proceed
|
|
132
|
-
};
|
|
133
|
-
}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Generates an HTTP error response based on the request's "Accept" header.
|
|
3
|
-
*
|
|
4
|
-
* If the "Accept" header includes "text/html" and debugging is enabled, it returns
|
|
5
|
-
* an HTML response with the error message and stack trace.
|
|
6
|
-
*
|
|
7
|
-
* If the "Accept" header includes "application/json" or debugging is enabled, it
|
|
8
|
-
* returns a JSON response with the error message and optionally the stack trace.
|
|
9
|
-
*
|
|
10
|
-
* Otherwise, it returns a plain text response indicating an internal server error.
|
|
11
|
-
*
|
|
12
|
-
* @param error - The error object or message to include in the response.
|
|
13
|
-
* @param req - The incoming HTTP request object.
|
|
14
|
-
* @param debug - A boolean indicating whether debugging information should be included.
|
|
15
|
-
* @returns A Response object with the appropriate error details and status code.
|
|
16
|
-
*/
|
|
17
|
-
export declare function errorResponse(error: unknown, req: Request, debug: boolean): Response;
|
package/dist/src/utils/error.js
DELETED
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
import { normalizePath } from './index';
|
|
2
|
-
/**
|
|
3
|
-
* Generates an HTTP error response based on the request's "Accept" header.
|
|
4
|
-
*
|
|
5
|
-
* If the "Accept" header includes "text/html" and debugging is enabled, it returns
|
|
6
|
-
* an HTML response with the error message and stack trace.
|
|
7
|
-
*
|
|
8
|
-
* If the "Accept" header includes "application/json" or debugging is enabled, it
|
|
9
|
-
* returns a JSON response with the error message and optionally the stack trace.
|
|
10
|
-
*
|
|
11
|
-
* Otherwise, it returns a plain text response indicating an internal server error.
|
|
12
|
-
*
|
|
13
|
-
* @param error - The error object or message to include in the response.
|
|
14
|
-
* @param req - The incoming HTTP request object.
|
|
15
|
-
* @param debug - A boolean indicating whether debugging information should be included.
|
|
16
|
-
* @returns A Response object with the appropriate error details and status code.
|
|
17
|
-
*/
|
|
18
|
-
export function errorResponse(error, req, debug) {
|
|
19
|
-
// Extract request details
|
|
20
|
-
const method = req.method;
|
|
21
|
-
const url = req.url;
|
|
22
|
-
const logPrefix = `[${method} ${url}]`;
|
|
23
|
-
// Log detailed error context
|
|
24
|
-
console.error(`${logPrefix} Error:`, error);
|
|
25
|
-
const message = error instanceof Error
|
|
26
|
-
? error.message
|
|
27
|
-
: String(error ?? 'Unknown error');
|
|
28
|
-
const stack = error instanceof Error
|
|
29
|
-
? (error.stack ?? 'No stack trace available')
|
|
30
|
-
: '';
|
|
31
|
-
const tagLabel = stack ? stack.split('\n')[0].split(':')[0] : 'Error';
|
|
32
|
-
const acceptHeader = req.headers.get('accept') || '';
|
|
33
|
-
if (acceptHeader.includes('text/html') && debug) {
|
|
34
|
-
const body = `<!doctype html><html lang="en"><meta charset="UTF-8"><title>Error Occurred - BurgerAPI</title><style>body{margin:0;padding:0;background-color:#f4f4f4;font-family:Nunito,sans-serif;color:#333}.whoops-container{width:80%;margin:5% auto;background:#fff;border-radius:8px;box-shadow:0 4px 12px rgba(0,0,0,.15);overflow:hidden}.whoops-header{background-color:#e74c3c;color:#fff;padding:20px;display:flex;align-items:center;justify-content:space-between}.whoops-header .title-container{display:flex;align-items:center}.whoops-header .title{font-size:1.5em;margin-left:10px}.whoops-header .debug-mode{font-weight:600;display:flex;align-items:center;padding:10px;background-color:#fff;color:#e74c3c;box-shadow:0 4px 12px rgba(0,0,0,.15);border-radius:8px}.whoops-header .debug-mode .fa-check-circle{color:#28a745;margin-right:5px;font-size:1.2em}.whoops-content{padding:2em}.tag{font-weight:600;color:#4a5568;background-color:#edf2f7;padding:8px 15px;border-radius:6px;display:inline-block;font-size:14px}.error-message{font-size:1.25em;line-height:1.6;margin-bottom:1.5em;color:#2d3748}h2{color:#e3342f;font-size:1.5em;margin-bottom:.8em;border-bottom:1px solid #edf2f7;padding-bottom:.5em}pre{background:#f9fafc;border-left:4px solid #e3342f;padding:1.5em;border-radius:4px;font-family:"Source Code Pro",Menlo,Monaco,Consolas,"Courier New",monospace;font-size:1em;line-height:1.6;overflow-x:auto;color:#4a5568}.footer{text-align:center;padding-top:25px;font-size:.9em;color:#718096;border-top:1px solid #edf2f7}.error-icon{font-size:2em}.request-info{display:flex;justify-content:space-between;align-items:center}</style><link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css"rel="stylesheet"><div class="whoops-container"><div class="whoops-header"><div class="title-container"><i class="fas error-icon fa-exclamation-triangle"></i><div class="title">Whoops! Something went wrong</div></div><div class="debug-mode"><i class="fas fa-check-circle"></i> Debug Mode Enabled</div></div><div class="whoops-content"><div class="request-info"><div><strong>Request:</strong> ${method} ${url}</div><div class="tag">${tagLabel}</div></div><p class="error-message">${message}</p><h2>Stack Trace</h2><pre>${stack}</pre><div class="footer">“It’s not a bug, it’s an undocumented feature.” — Anonymous</div></div></div>
|
|
35
|
-
`;
|
|
36
|
-
// Return the html response
|
|
37
|
-
return new Response(body, {
|
|
38
|
-
status: 500,
|
|
39
|
-
headers: { 'Content-Type': 'text/html' },
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
else if (debug) {
|
|
43
|
-
// Log detailed error context
|
|
44
|
-
console.error(`${logPrefix} Error:`, error);
|
|
45
|
-
// Create a JSON response
|
|
46
|
-
const body = JSON.stringify({
|
|
47
|
-
request: `${method} ${url}`,
|
|
48
|
-
error: error instanceof Error ? error.message : 'Unknown error',
|
|
49
|
-
stack: error instanceof Error ? normalizePath(error.stack || '') : '',
|
|
50
|
-
});
|
|
51
|
-
// Return the JSON response
|
|
52
|
-
return new Response(body, {
|
|
53
|
-
status: 500,
|
|
54
|
-
headers: { 'Content-Type': 'application/json' },
|
|
55
|
-
});
|
|
56
|
-
}
|
|
57
|
-
else {
|
|
58
|
-
// Log detailed error context
|
|
59
|
-
console.error(`${logPrefix} Error:`, error);
|
|
60
|
-
// Return a plain text response
|
|
61
|
-
return new Response('Internal Server Error', { status: 500 });
|
|
62
|
-
}
|
|
63
|
-
}
|