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.
Files changed (175) hide show
  1. package/README.md +84 -241
  2. package/dist/src/adapter/bun/index.d.ts +14 -0
  3. package/dist/src/adapter/bun/index.js +73 -0
  4. package/dist/src/adapter/bun/types.d.ts +15 -0
  5. package/dist/src/adapter/bun/types.js +1 -0
  6. package/dist/src/adapter/index.d.ts +1 -0
  7. package/dist/src/adapter/index.js +1 -0
  8. package/dist/src/adapter/types.d.ts +49 -0
  9. package/dist/src/adapter/types.js +1 -0
  10. package/dist/src/adapter/web-standard/fetch-handler.d.ts +39 -0
  11. package/dist/src/adapter/web-standard/fetch-handler.js +54 -0
  12. package/dist/src/adapter/web-standard/index.d.ts +8 -0
  13. package/dist/src/adapter/web-standard/index.js +7 -0
  14. package/dist/src/analysis/route-access-analyzer.d.ts +10 -0
  15. package/dist/src/analysis/route-access-analyzer.js +287 -0
  16. package/dist/src/chain/chain.d.ts +16 -0
  17. package/dist/src/chain/chain.js +17 -0
  18. package/dist/src/chain/flattener.d.ts +3 -0
  19. package/dist/src/chain/flattener.js +78 -0
  20. package/dist/src/chain/node.d.ts +25 -0
  21. package/dist/src/chain/node.js +1 -0
  22. package/dist/src/compiler/conventions.d.ts +67 -0
  23. package/dist/src/compiler/conventions.js +129 -0
  24. package/dist/src/compiler/index.d.ts +13 -0
  25. package/dist/src/compiler/index.js +11 -0
  26. package/dist/src/compiler/module-loader.d.ts +75 -0
  27. package/dist/src/compiler/module-loader.js +276 -0
  28. package/dist/src/compiler/route-module.d.ts +97 -0
  29. package/dist/src/compiler/route-module.js +1 -0
  30. package/dist/src/compiler/route-tree.d.ts +19 -0
  31. package/dist/src/compiler/route-tree.js +28 -0
  32. package/dist/src/compiler/scanner.d.ts +42 -0
  33. package/dist/src/compiler/scanner.js +240 -0
  34. package/dist/src/context/context-set.d.ts +26 -0
  35. package/dist/src/context/context-set.js +45 -0
  36. package/dist/src/context/context.d.ts +357 -0
  37. package/dist/src/context/context.js +401 -0
  38. package/dist/src/context/query-parser.d.ts +23 -0
  39. package/dist/src/context/query-parser.js +132 -0
  40. package/dist/src/context/route-access.d.ts +7 -0
  41. package/dist/src/context/route-access.js +18 -0
  42. package/dist/src/context/types.d.ts +62 -0
  43. package/dist/src/context/types.js +5 -0
  44. package/dist/src/core/asset-mime.d.ts +9 -0
  45. package/dist/src/core/asset-mime.js +35 -0
  46. package/dist/src/core/assets.d.ts +30 -0
  47. package/dist/src/core/assets.js +67 -0
  48. package/dist/src/core/docs-providers.d.ts +13 -0
  49. package/dist/src/core/docs-providers.js +89 -0
  50. package/dist/src/core/embedded-assets.d.ts +18 -0
  51. package/dist/src/core/embedded-assets.js +13 -0
  52. package/dist/src/core/openapi.d.ts +9 -10
  53. package/dist/src/core/openapi.js +333 -127
  54. package/dist/src/core/page-router.d.ts +13 -17
  55. package/dist/src/core/page-router.js +86 -55
  56. package/dist/src/core/server.d.ts +20 -11
  57. package/dist/src/core/server.js +41 -45
  58. package/dist/src/errors/forbidden.d.ts +14 -0
  59. package/dist/src/errors/forbidden.js +16 -0
  60. package/dist/src/errors/http-error.d.ts +49 -0
  61. package/dist/src/errors/http-error.js +155 -0
  62. package/dist/src/errors/method-not-allowed.d.ts +13 -0
  63. package/dist/src/errors/method-not-allowed.js +15 -0
  64. package/dist/src/errors/not-found.d.ts +14 -0
  65. package/dist/src/errors/not-found.js +16 -0
  66. package/dist/src/errors/unauthorized.d.ts +14 -0
  67. package/dist/src/errors/unauthorized.js +16 -0
  68. package/dist/src/index.d.ts +275 -50
  69. package/dist/src/index.js +974 -333
  70. package/dist/src/lifecycle/executor.d.ts +64 -0
  71. package/dist/src/lifecycle/executor.js +201 -0
  72. package/dist/src/lifecycle/hook-runner.d.ts +11 -0
  73. package/dist/src/lifecycle/hook-runner.js +156 -0
  74. package/dist/src/lifecycle/jit.d.ts +50 -0
  75. package/dist/src/lifecycle/jit.js +258 -0
  76. package/dist/src/lifecycle/transform.d.ts +26 -0
  77. package/dist/src/lifecycle/transform.js +99 -0
  78. package/dist/src/lifecycle/types.d.ts +135 -0
  79. package/dist/src/lifecycle/types.js +1 -0
  80. package/dist/src/plugin/composer.d.ts +3 -0
  81. package/dist/src/plugin/composer.js +28 -0
  82. package/dist/src/plugin/registry.d.ts +25 -0
  83. package/dist/src/plugin/registry.js +107 -0
  84. package/dist/src/plugin/types.d.ts +20 -0
  85. package/dist/src/plugin/types.js +1 -0
  86. package/dist/src/router/allow-cache.d.ts +20 -0
  87. package/dist/src/router/allow-cache.js +26 -0
  88. package/dist/src/router/compiler.d.ts +39 -0
  89. package/dist/src/router/compiler.js +867 -0
  90. package/dist/src/router/define.d.ts +67 -0
  91. package/dist/src/router/define.js +34 -0
  92. package/dist/src/router/index.d.ts +6 -0
  93. package/dist/src/router/index.js +5 -0
  94. package/dist/src/router/param-extract.d.ts +33 -0
  95. package/dist/src/router/param-extract.js +87 -0
  96. package/dist/src/router/regex-matcher.d.ts +42 -0
  97. package/dist/src/router/regex-matcher.js +216 -0
  98. package/dist/src/router/router.d.ts +214 -0
  99. package/dist/src/router/router.js +670 -0
  100. package/dist/src/router/static-map.d.ts +39 -0
  101. package/dist/src/router/static-map.js +48 -0
  102. package/dist/src/router/trie.d.ts +69 -0
  103. package/dist/src/router/trie.js +572 -0
  104. package/dist/src/router/types.d.ts +104 -0
  105. package/dist/src/router/types.js +1 -0
  106. package/dist/src/runtime/capabilities.d.ts +29 -0
  107. package/dist/src/runtime/capabilities.js +48 -0
  108. package/dist/src/types/index.d.ts +260 -109
  109. package/dist/src/types/index.js +1 -1
  110. package/dist/src/types/inference.d.ts +78 -0
  111. package/dist/src/types/inference.js +20 -0
  112. package/dist/src/types/openapi-config.d.ts +164 -0
  113. package/dist/src/types/openapi-config.js +8 -0
  114. package/dist/src/utils/env.d.ts +13 -0
  115. package/dist/src/utils/env.js +24 -0
  116. package/dist/src/utils/fs.d.ts +29 -0
  117. package/dist/src/utils/fs.js +75 -0
  118. package/dist/src/utils/index.d.ts +1 -1
  119. package/dist/src/utils/index.js +2 -2
  120. package/dist/src/utils/pathConversion.js +13 -7
  121. package/dist/src/utils/response.d.ts +42 -6
  122. package/dist/src/utils/response.js +189 -7
  123. package/dist/src/utils/routing.d.ts +32 -13
  124. package/dist/src/utils/routing.js +41 -18
  125. package/dist/src/utils/thenable.d.ts +8 -0
  126. package/dist/src/utils/thenable.js +12 -0
  127. package/dist/src/utils/timing-safe.d.ts +9 -0
  128. package/dist/src/utils/timing-safe.js +20 -0
  129. package/dist/src/utils/wildcard.d.ts +6 -7
  130. package/dist/src/utils/wildcard.js +38 -55
  131. package/dist/src/validation/adapter.d.ts +54 -0
  132. package/dist/src/validation/adapter.js +75 -0
  133. package/dist/src/validation/adapters/standard.d.ts +11 -0
  134. package/dist/src/validation/adapters/standard.js +110 -0
  135. package/dist/src/validation/adapters/zod.d.ts +9 -0
  136. package/dist/src/validation/adapters/zod.js +128 -0
  137. package/dist/src/validation/cache.d.ts +26 -0
  138. package/dist/src/validation/cache.js +35 -0
  139. package/dist/src/validation/coerce.d.ts +28 -0
  140. package/dist/src/validation/coerce.js +176 -0
  141. package/dist/src/validation/compiler.d.ts +27 -0
  142. package/dist/src/validation/compiler.js +191 -0
  143. package/dist/src/validation/error.d.ts +69 -0
  144. package/dist/src/validation/error.js +137 -0
  145. package/dist/src/validation/index.d.ts +16 -0
  146. package/dist/src/validation/index.js +12 -0
  147. package/dist/src/validation/response.d.ts +25 -0
  148. package/dist/src/validation/response.js +85 -0
  149. package/dist/src/validation/types.d.ts +148 -0
  150. package/dist/src/validation/types.js +6 -0
  151. package/dist/src/validation/validator.d.ts +39 -0
  152. package/dist/src/validation/validator.js +441 -0
  153. package/dist/src/ws/adapter.d.ts +230 -0
  154. package/dist/src/ws/adapter.js +660 -0
  155. package/dist/src/ws/compiler.d.ts +37 -0
  156. package/dist/src/ws/compiler.js +178 -0
  157. package/dist/src/ws/index.d.ts +11 -0
  158. package/dist/src/ws/index.js +8 -0
  159. package/dist/src/ws/platform.d.ts +82 -0
  160. package/dist/src/ws/platform.js +122 -0
  161. package/dist/src/ws/router.d.ts +47 -0
  162. package/dist/src/ws/router.js +183 -0
  163. package/dist/src/ws/scanner.d.ts +97 -0
  164. package/dist/src/ws/scanner.js +253 -0
  165. package/dist/src/ws/types.d.ts +381 -0
  166. package/dist/src/ws/types.js +213 -0
  167. package/package.json +72 -60
  168. package/dist/src/core/api-router.d.ts +0 -65
  169. package/dist/src/core/api-router.js +0 -264
  170. package/dist/src/core/swagger-ui.d.ts +0 -1
  171. package/dist/src/core/swagger-ui.js +0 -33
  172. package/dist/src/middleware/validator.d.ts +0 -14
  173. package/dist/src/middleware/validator.js +0 -133
  174. package/dist/src/utils/error.d.ts +0 -17
  175. package/dist/src/utils/error.js +0 -63
@@ -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;
@@ -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
- }