burger-api 0.1.0 → 0.2.0
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 +58 -465
- package/dist/src/core/api-router.d.ts +37 -29
- package/dist/src/core/api-router.js +96 -76
- package/dist/src/core/openapi.d.ts +2 -9
- package/dist/src/core/openapi.js +21 -27
- package/dist/src/core/page-router.d.ts +1 -1
- package/dist/src/core/page-router.js +20 -19
- package/dist/src/core/response.js +6 -6
- package/dist/src/core/server.d.ts +4 -9
- package/dist/src/core/server.js +18 -20
- package/dist/src/index.d.ts +51 -6
- package/dist/src/index.js +246 -112
- package/dist/src/middleware/validator.d.ts +1 -10
- package/dist/src/middleware/validator.js +60 -40
- package/dist/src/utils/error.js +9 -9
- package/dist/src/utils/index.d.ts +1 -1
- package/dist/src/utils/index.js +6 -6
- package/dist/src/utils/response.d.ts +12 -0
- package/dist/src/utils/response.js +17 -0
- package/dist/src/utils/routing.d.ts +9 -1
- package/dist/src/utils/routing.js +41 -15
- package/package.json +7 -6
- package/dist/src/types/index.d.ts +0 -251
- package/dist/src/types/index.js +0 -1
package/dist/src/core/server.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
// Import stuff from
|
|
2
|
-
import {
|
|
3
|
-
import { HttpResponse } from "./response.js";
|
|
1
|
+
// Import stuff from Bun
|
|
2
|
+
import { serve } from 'bun';
|
|
4
3
|
// Import stuff from utils
|
|
5
|
-
import { errorResponse } from
|
|
4
|
+
import { errorResponse } from '../utils/error.js';
|
|
6
5
|
export class Server {
|
|
7
6
|
options;
|
|
8
7
|
server = null;
|
|
@@ -13,38 +12,37 @@ export class Server {
|
|
|
13
12
|
constructor(options) {
|
|
14
13
|
this.options = options;
|
|
15
14
|
}
|
|
16
|
-
/**
|
|
17
|
-
* Starts the server with the given routes, handler, port, and callback.
|
|
18
|
-
* @param routes - The routes to be used by the server.
|
|
19
|
-
* @param handler - The handler to be used by the server.
|
|
20
|
-
* @param port - The port to listen on.
|
|
21
|
-
* @param cb - An optional callback function to be called when the server starts.
|
|
22
|
-
*/
|
|
23
15
|
start(routes, handler, port, cb) {
|
|
24
16
|
// Start Bun's native server using Bun.serve
|
|
25
|
-
this.server =
|
|
17
|
+
this.server = serve({
|
|
26
18
|
routes,
|
|
19
|
+
// Bun's fetch handler
|
|
27
20
|
fetch: async (request) => {
|
|
28
21
|
try {
|
|
29
|
-
|
|
30
|
-
const burgerReq = new HttpRequest(request);
|
|
31
|
-
// Wrap the native Response with HttpResponse to get a BurgerResponse
|
|
32
|
-
const burgerRes = new HttpResponse();
|
|
33
|
-
// Invoke and return the handler with the wrapped requests and responses
|
|
34
|
-
return await handler(burgerReq, burgerRes);
|
|
22
|
+
return await handler(request);
|
|
35
23
|
}
|
|
36
24
|
catch (error) {
|
|
37
25
|
// Return a custom error response
|
|
38
26
|
return errorResponse(error, request, this.options.debug ?? false);
|
|
39
27
|
}
|
|
40
28
|
},
|
|
29
|
+
// Global error handler
|
|
30
|
+
error(error) {
|
|
31
|
+
console.error(error);
|
|
32
|
+
return new Response(`Internal Server Error: ${error.message}`, {
|
|
33
|
+
status: 500,
|
|
34
|
+
headers: {
|
|
35
|
+
'Content-Type': 'text/plain',
|
|
36
|
+
},
|
|
37
|
+
});
|
|
38
|
+
},
|
|
41
39
|
port,
|
|
42
40
|
});
|
|
43
41
|
if (cb) {
|
|
44
42
|
cb();
|
|
45
43
|
}
|
|
46
44
|
else {
|
|
47
|
-
console.log(
|
|
45
|
+
console.log(`🍔 BurgerAPI is running at: http://${this.options.hostname || 'localhost'}:${port}`);
|
|
48
46
|
}
|
|
49
47
|
}
|
|
50
48
|
/**
|
|
@@ -56,7 +54,7 @@ export class Server {
|
|
|
56
54
|
stop() {
|
|
57
55
|
if (this.server) {
|
|
58
56
|
this.server.stop();
|
|
59
|
-
console.log(
|
|
57
|
+
console.log('Server stopped.');
|
|
60
58
|
}
|
|
61
59
|
}
|
|
62
60
|
}
|
package/dist/src/index.d.ts
CHANGED
|
@@ -1,10 +1,42 @@
|
|
|
1
|
-
import type { ServerOptions
|
|
1
|
+
import type { ServerOptions } from '@burgerTypes';
|
|
2
2
|
export declare class Burger {
|
|
3
3
|
private options;
|
|
4
|
+
/**
|
|
5
|
+
* The server instance
|
|
6
|
+
*/
|
|
4
7
|
private server;
|
|
8
|
+
/**
|
|
9
|
+
* The API router instance
|
|
10
|
+
*/
|
|
5
11
|
private apiRouter?;
|
|
12
|
+
/**
|
|
13
|
+
* The page router instance
|
|
14
|
+
*/
|
|
6
15
|
private pageRouter?;
|
|
16
|
+
/**
|
|
17
|
+
* The global middleware
|
|
18
|
+
*/
|
|
7
19
|
private globalMiddleware;
|
|
20
|
+
/**
|
|
21
|
+
* The OpenAPI document
|
|
22
|
+
*/
|
|
23
|
+
private openApiDoc;
|
|
24
|
+
/**
|
|
25
|
+
* The routes object
|
|
26
|
+
*/
|
|
27
|
+
private routes;
|
|
28
|
+
/**
|
|
29
|
+
* Pre-computed responses for reuse
|
|
30
|
+
*/
|
|
31
|
+
private readonly METHOD_NOT_ALLOWED;
|
|
32
|
+
/**
|
|
33
|
+
* The not found response
|
|
34
|
+
*/
|
|
35
|
+
private readonly NOT_FOUND;
|
|
36
|
+
/**
|
|
37
|
+
* The OpenAPI error response
|
|
38
|
+
*/
|
|
39
|
+
private readonly OPENAPI_ERROR;
|
|
8
40
|
/**
|
|
9
41
|
* Constructor for the Burger class.
|
|
10
42
|
* @param options - The options for the server and router.
|
|
@@ -16,10 +48,23 @@ export declare class Burger {
|
|
|
16
48
|
*/
|
|
17
49
|
constructor(options: ServerOptions);
|
|
18
50
|
/**
|
|
19
|
-
*
|
|
20
|
-
* @
|
|
51
|
+
* Process the page routes and add them to the routes object
|
|
52
|
+
* @returns A promise that resolves to a boolean
|
|
53
|
+
*/
|
|
54
|
+
private processPageRoutes;
|
|
55
|
+
/**
|
|
56
|
+
* Process the API routes and add them to the routes object
|
|
57
|
+
* @returns A promise that resolves to a boolean
|
|
58
|
+
*/
|
|
59
|
+
private processApiRoutes;
|
|
60
|
+
/**
|
|
61
|
+
* Process the middleware and handler
|
|
62
|
+
* @param request - The request object
|
|
63
|
+
* @param middlewares - The middleware array
|
|
64
|
+
* @param handler - The handler function
|
|
65
|
+
* @returns A promise that resolves to a response
|
|
21
66
|
*/
|
|
22
|
-
|
|
67
|
+
private processMiddleware;
|
|
23
68
|
/**
|
|
24
69
|
* Starts the server and begins listening for incoming requests.
|
|
25
70
|
* @param port - The port number to listen on. Defaults to `4000`.
|
|
@@ -28,5 +73,5 @@ export declare class Burger {
|
|
|
28
73
|
*/
|
|
29
74
|
serve(port?: number, cb?: () => void): Promise<void>;
|
|
30
75
|
}
|
|
31
|
-
export { setDir } from
|
|
32
|
-
export type { ServerOptions, RequestHandler, BurgerRequest, BurgerResponse, Middleware, openapi, } from
|
|
76
|
+
export { setDir } from './utils/index.js';
|
|
77
|
+
export type { ServerOptions, RequestHandler, BurgerRequest, BurgerResponse, BurgerNext, Middleware, openapi, } from '@burgerTypes';
|
package/dist/src/index.js
CHANGED
|
@@ -1,19 +1,52 @@
|
|
|
1
1
|
// Import stuff from core
|
|
2
|
-
import { Server } from
|
|
3
|
-
import { ApiRouter } from
|
|
4
|
-
import { PageRouter } from
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
import {
|
|
2
|
+
import { Server } from './core/server.js';
|
|
3
|
+
import { ApiRouter } from './core/api-router.js';
|
|
4
|
+
import { PageRouter } from './core/page-router.js';
|
|
5
|
+
import { generateOpenAPIDocument } from './core/openapi.js';
|
|
6
|
+
import { swaggerHtml } from './core/swagger-ui.js';
|
|
7
|
+
// Import utils
|
|
8
|
+
import { collectRoutes } from './utils/index.js';
|
|
9
|
+
import { METHOD_NOT_ALLOWED, NOT_FOUND, OPENAPI_ERROR } from './utils/response';
|
|
9
10
|
// Import middleware
|
|
10
|
-
import { createValidationMiddleware } from
|
|
11
|
+
import { createValidationMiddleware } from './middleware/validator.js';
|
|
11
12
|
export class Burger {
|
|
12
13
|
options;
|
|
14
|
+
/**
|
|
15
|
+
* The server instance
|
|
16
|
+
*/
|
|
13
17
|
server;
|
|
18
|
+
/**
|
|
19
|
+
* The API router instance
|
|
20
|
+
*/
|
|
14
21
|
apiRouter;
|
|
22
|
+
/**
|
|
23
|
+
* The page router instance
|
|
24
|
+
*/
|
|
15
25
|
pageRouter;
|
|
26
|
+
/**
|
|
27
|
+
* The global middleware
|
|
28
|
+
*/
|
|
16
29
|
globalMiddleware = [];
|
|
30
|
+
/**
|
|
31
|
+
* The OpenAPI document
|
|
32
|
+
*/
|
|
33
|
+
openApiDoc = null;
|
|
34
|
+
/**
|
|
35
|
+
* The routes object
|
|
36
|
+
*/
|
|
37
|
+
routes = {};
|
|
38
|
+
/**
|
|
39
|
+
* Pre-computed responses for reuse
|
|
40
|
+
*/
|
|
41
|
+
METHOD_NOT_ALLOWED = METHOD_NOT_ALLOWED;
|
|
42
|
+
/**
|
|
43
|
+
* The not found response
|
|
44
|
+
*/
|
|
45
|
+
NOT_FOUND = NOT_FOUND;
|
|
46
|
+
/**
|
|
47
|
+
* The OpenAPI error response
|
|
48
|
+
*/
|
|
49
|
+
OPENAPI_ERROR = OPENAPI_ERROR;
|
|
17
50
|
/**
|
|
18
51
|
* Constructor for the Burger class.
|
|
19
52
|
* @param options - The options for the server and router.
|
|
@@ -25,26 +58,203 @@ export class Burger {
|
|
|
25
58
|
*/
|
|
26
59
|
constructor(options) {
|
|
27
60
|
this.options = options;
|
|
61
|
+
// Create server instance
|
|
28
62
|
this.server = new Server(options);
|
|
29
|
-
//
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
63
|
+
// Fast initialization for routers with nullish coalescing
|
|
64
|
+
const { apiDir, pageDir, apiPrefix, pagePrefix, globalMiddleware } = options;
|
|
65
|
+
// Initialize API router if apiDir is provided
|
|
66
|
+
this.apiRouter = apiDir
|
|
67
|
+
? new ApiRouter(apiDir, apiPrefix || 'api')
|
|
68
|
+
: undefined;
|
|
69
|
+
// Initialize page router if pageDir is provided
|
|
70
|
+
this.pageRouter = pageDir
|
|
71
|
+
? new PageRouter(pageDir, pagePrefix || '')
|
|
72
|
+
: undefined;
|
|
73
|
+
// Add global middleware if any
|
|
74
|
+
this.globalMiddleware = globalMiddleware?.length
|
|
75
|
+
? globalMiddleware.slice()
|
|
76
|
+
: [];
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Process the page routes and add them to the routes object
|
|
80
|
+
* @returns A promise that resolves to a boolean
|
|
81
|
+
*/
|
|
82
|
+
async processPageRoutes() {
|
|
83
|
+
// If no page router, return false
|
|
84
|
+
if (!this.pageRouter)
|
|
85
|
+
return false;
|
|
86
|
+
// Load pages routes
|
|
87
|
+
await this.pageRouter.loadPages();
|
|
88
|
+
// If there are any page routes, add them to the routes object
|
|
89
|
+
const pages = this.pageRouter.pages;
|
|
90
|
+
// Get the length of the pages routes
|
|
91
|
+
const pageCount = pages.length;
|
|
92
|
+
// If no pages, return false
|
|
93
|
+
if (pageCount === 0)
|
|
94
|
+
return false;
|
|
95
|
+
// Loop through the pages
|
|
96
|
+
for (let i = 0; i < pageCount; i++) {
|
|
97
|
+
// Get the current page
|
|
98
|
+
const page = pages[i];
|
|
99
|
+
// Add the page to the routes
|
|
100
|
+
this.routes[page.path] = page.handler;
|
|
36
101
|
}
|
|
37
|
-
//
|
|
38
|
-
|
|
39
|
-
|
|
102
|
+
// Return true if there are any pages
|
|
103
|
+
return true;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Process the API routes and add them to the routes object
|
|
107
|
+
* @returns A promise that resolves to a boolean
|
|
108
|
+
*/
|
|
109
|
+
async processApiRoutes() {
|
|
110
|
+
// If no api router, return false
|
|
111
|
+
if (!this.apiRouter)
|
|
112
|
+
return false;
|
|
113
|
+
// Load API routes
|
|
114
|
+
await this.apiRouter.loadRoutes();
|
|
115
|
+
// Collect API routes
|
|
116
|
+
const apiRoutes = collectRoutes(this.apiRouter.routes);
|
|
117
|
+
// Get the length of the API routes
|
|
118
|
+
const routeCount = apiRoutes.length;
|
|
119
|
+
// If there are no API routes, return false
|
|
120
|
+
if (routeCount === 0)
|
|
121
|
+
return false;
|
|
122
|
+
// Generate OpenAPI document and cache it
|
|
123
|
+
this.openApiDoc = generateOpenAPIDocument(apiRoutes, this.options);
|
|
124
|
+
// Cache frequently accessed properties
|
|
125
|
+
const routes = this.routes;
|
|
126
|
+
// Get the global middleware
|
|
127
|
+
const globalMiddleware = this.globalMiddleware;
|
|
128
|
+
// Get the length of the global middleware
|
|
129
|
+
const globalMiddlewareLen = globalMiddleware.length;
|
|
130
|
+
// Process each route with optimized handler creation
|
|
131
|
+
for (let i = 0; i < routeCount; i++) {
|
|
132
|
+
/**
|
|
133
|
+
* ================================================
|
|
134
|
+
* Pre-compute the required functionality start
|
|
135
|
+
* ================================================
|
|
136
|
+
*/
|
|
137
|
+
// Get the current route object
|
|
138
|
+
const route = apiRoutes[i];
|
|
139
|
+
// Destructure the route object
|
|
140
|
+
const { path, schema, middleware: routeMiddleware, handlers, } = route;
|
|
141
|
+
// Get length of route specific middleware
|
|
142
|
+
const routeMiddlewareLen = routeMiddleware?.length || 0;
|
|
143
|
+
// Check if schema exists
|
|
144
|
+
const hasSchema = !!schema;
|
|
145
|
+
// Optimize middleware array initialization by pre-allocating size
|
|
146
|
+
const totalMiddlewareCount = globalMiddlewareLen + (hasSchema ? 1 : 0) + routeMiddlewareLen;
|
|
147
|
+
// Create optimized route handler
|
|
148
|
+
if (totalMiddlewareCount === 0) {
|
|
149
|
+
// Ultra-fast path: no middleware
|
|
150
|
+
routes[path] = (request) => {
|
|
151
|
+
const handler = handlers[request.method];
|
|
152
|
+
return handler ? handler(request) : this.METHOD_NOT_ALLOWED;
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
else {
|
|
156
|
+
// Pre-compute middleware array with known size
|
|
157
|
+
const middlewares = new Array(totalMiddlewareCount);
|
|
158
|
+
// Initialize index to track the current middleware
|
|
159
|
+
let index = 0;
|
|
160
|
+
// Copy global middleware (most common case)
|
|
161
|
+
for (let j = 0; j < globalMiddlewareLen; j++) {
|
|
162
|
+
middlewares[index++] = globalMiddleware[j];
|
|
163
|
+
}
|
|
164
|
+
// Add validation middleware if needed
|
|
165
|
+
if (hasSchema) {
|
|
166
|
+
middlewares[index++] = createValidationMiddleware(schema);
|
|
167
|
+
}
|
|
168
|
+
// Add route-specific middlewares
|
|
169
|
+
if (routeMiddleware) {
|
|
170
|
+
for (let j = 0; j < routeMiddlewareLen; j++) {
|
|
171
|
+
middlewares[index++] = routeMiddleware[j];
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* ================================================
|
|
176
|
+
* Pre-compute the required functionality end
|
|
177
|
+
* ================================================
|
|
178
|
+
*/
|
|
179
|
+
// Create handler with middleware
|
|
180
|
+
routes[path] = (request) => {
|
|
181
|
+
const handler = handlers[request.method];
|
|
182
|
+
if (!handler)
|
|
183
|
+
return this.METHOD_NOT_ALLOWED;
|
|
184
|
+
return this.processMiddleware(request, middlewares, handler);
|
|
185
|
+
};
|
|
186
|
+
}
|
|
40
187
|
}
|
|
188
|
+
// Add special routes for OpenAPI
|
|
189
|
+
routes['/openapi.json'] = () => this.openApiDoc
|
|
190
|
+
? Response.json(this.openApiDoc)
|
|
191
|
+
: this.OPENAPI_ERROR;
|
|
192
|
+
// Add special route for Swagger UI
|
|
193
|
+
routes['/docs'] = () => new Response(swaggerHtml, {
|
|
194
|
+
headers: { 'Content-Type': 'text/html' },
|
|
195
|
+
});
|
|
196
|
+
return true;
|
|
41
197
|
}
|
|
42
198
|
/**
|
|
43
|
-
*
|
|
44
|
-
* @param
|
|
199
|
+
* Process the middleware and handler
|
|
200
|
+
* @param request - The request object
|
|
201
|
+
* @param middlewares - The middleware array
|
|
202
|
+
* @param handler - The handler function
|
|
203
|
+
* @returns A promise that resolves to a response
|
|
45
204
|
*/
|
|
46
|
-
|
|
47
|
-
|
|
205
|
+
async processMiddleware(request, middlewares, handler) {
|
|
206
|
+
// Get the length of the middleware array
|
|
207
|
+
const middlewareLen = middlewares.length;
|
|
208
|
+
// Fast path: single middleware with no after functions
|
|
209
|
+
if (middlewareLen === 1) {
|
|
210
|
+
// Get the first middleware
|
|
211
|
+
const result = await middlewares[0](request);
|
|
212
|
+
// If the result is a response, return it
|
|
213
|
+
if (result instanceof Response) {
|
|
214
|
+
return result;
|
|
215
|
+
}
|
|
216
|
+
// If the result is not a function, return the handler
|
|
217
|
+
if (typeof result !== 'function') {
|
|
218
|
+
return handler(request);
|
|
219
|
+
}
|
|
220
|
+
// If the result is a function, return the result of the handler
|
|
221
|
+
return result(await handler(request));
|
|
222
|
+
}
|
|
223
|
+
// Stack to store "after" functions
|
|
224
|
+
// Regular path with pre-allocated afterStack
|
|
225
|
+
const afterStack = new Array(middlewareLen);
|
|
226
|
+
// Initialize the after middleware counter
|
|
227
|
+
let afterCount = 0;
|
|
228
|
+
// Process "before" logic
|
|
229
|
+
for (let i = 0; i < middlewareLen; i++) {
|
|
230
|
+
// Get the current middleware
|
|
231
|
+
const result = await middlewares[i](request);
|
|
232
|
+
// If the result is a response, return it
|
|
233
|
+
if (result instanceof Response) {
|
|
234
|
+
return result; // Short-circuit with a response
|
|
235
|
+
}
|
|
236
|
+
// If the result is a function, save it to the afterStack
|
|
237
|
+
else if (typeof result === 'function') {
|
|
238
|
+
afterStack[afterCount++] = result;
|
|
239
|
+
}
|
|
240
|
+
// If undefined, proceed to next middleware
|
|
241
|
+
}
|
|
242
|
+
// Get response from handler
|
|
243
|
+
let response = await handler(request);
|
|
244
|
+
// Fast path: no after functions
|
|
245
|
+
if (afterCount === 0) {
|
|
246
|
+
return response;
|
|
247
|
+
}
|
|
248
|
+
// Fast path: single after function
|
|
249
|
+
if (afterCount === 1) {
|
|
250
|
+
return afterStack[0](response);
|
|
251
|
+
}
|
|
252
|
+
// Process "after" logic in reverse order
|
|
253
|
+
for (let i = afterCount - 1; i >= 0; i--) {
|
|
254
|
+
response = await afterStack[i](response);
|
|
255
|
+
}
|
|
256
|
+
// Return the responseF
|
|
257
|
+
return response;
|
|
48
258
|
}
|
|
49
259
|
/**
|
|
50
260
|
* Starts the server and begins listening for incoming requests.
|
|
@@ -53,101 +263,25 @@ export class Burger {
|
|
|
53
263
|
* @returns A Promise that resolves when the server has started listening.
|
|
54
264
|
*/
|
|
55
265
|
async serve(port = 4000, cb) {
|
|
56
|
-
//
|
|
57
|
-
const
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
if (this.apiRouter) {
|
|
66
|
-
// Load API routes
|
|
67
|
-
await this.apiRouter.loadRoutes();
|
|
266
|
+
// Process routes in parallel if possible
|
|
267
|
+
const [pagesConfigured, apiConfigured] = await Promise.all([
|
|
268
|
+
this.processPageRoutes(),
|
|
269
|
+
this.processApiRoutes(),
|
|
270
|
+
]);
|
|
271
|
+
// Flag to track if any routes were loaded
|
|
272
|
+
const routesConfigured = pagesConfigured || apiConfigured;
|
|
273
|
+
// If routes were configured, start the server
|
|
274
|
+
if (routesConfigured) {
|
|
68
275
|
// Start the server
|
|
69
|
-
this.server.start(routes, async (
|
|
70
|
-
|
|
71
|
-
const request = new HttpRequest(req);
|
|
72
|
-
// Create a new instance of BurgerResponse for the handler
|
|
73
|
-
const response = new HttpResponse();
|
|
74
|
-
// Create URL object
|
|
75
|
-
const url = new URL(request.url);
|
|
76
|
-
// Check if the request is for /openapi.json
|
|
77
|
-
if (url.pathname === "/openapi.json") {
|
|
78
|
-
if (this.apiRouter) {
|
|
79
|
-
// Generate OpenAPI document
|
|
80
|
-
const doc = generateOpenAPIDocument(this.apiRouter, this.options);
|
|
81
|
-
// Return it as JSON
|
|
82
|
-
return response.json(doc);
|
|
83
|
-
}
|
|
84
|
-
else {
|
|
85
|
-
// Return an error if router is not available
|
|
86
|
-
return response
|
|
87
|
-
.status(500)
|
|
88
|
-
.json({ error: "Router not available" });
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
// Serve the Swagger UI at /docs
|
|
92
|
-
if (url.pathname === "/docs") {
|
|
93
|
-
return response.html(swaggerHtml);
|
|
94
|
-
}
|
|
95
|
-
// Get the route and params for the current request
|
|
96
|
-
const { route, params } = this.apiRouter.resolve(req);
|
|
97
|
-
if (!route) {
|
|
98
|
-
// Return a 404 if no route is found
|
|
99
|
-
return response.status(404).json({ error: "Route not found" });
|
|
100
|
-
}
|
|
101
|
-
// Add params to the request
|
|
102
|
-
request.params = params;
|
|
103
|
-
// Get the handler for the current HTTP method
|
|
104
|
-
const method = req.method.toUpperCase();
|
|
105
|
-
const handler = route.handlers[method];
|
|
106
|
-
if (!handler) {
|
|
107
|
-
// Return a 405 if no handler is found
|
|
108
|
-
return response.status(405).json({ error: "Method Not Allowed" });
|
|
109
|
-
}
|
|
110
|
-
/**
|
|
111
|
-
* Build the middleware composition chain.
|
|
112
|
-
* 1. Compose the Route-Specific Chain
|
|
113
|
-
* 2. Insert Validation Middleware (if a schema exists)
|
|
114
|
-
* 3. Insert Global Middleware
|
|
115
|
-
* 4. Execute the handler
|
|
116
|
-
*/
|
|
117
|
-
// 1. Compose the Route-Specific Chain
|
|
118
|
-
let routeChain = async () => handler(request, response);
|
|
119
|
-
if (route.middleware && route.middleware.length > 0) {
|
|
120
|
-
// Wrap route-specific middleware (in reverse order to preserve order of execution)
|
|
121
|
-
for (const mw of route.middleware.slice().reverse()) {
|
|
122
|
-
const next = routeChain;
|
|
123
|
-
routeChain = async () => mw(request, response, next);
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
// 2. Insert Validation Middleware (if a schema exists)
|
|
127
|
-
let composedChain = routeChain;
|
|
128
|
-
if (route.schema) {
|
|
129
|
-
const validationMw = createValidationMiddleware(route.schema);
|
|
130
|
-
composedChain = async () => validationMw(request, response, routeChain);
|
|
131
|
-
}
|
|
132
|
-
// 3. Wrap Global Middleware (in reverse order so that the first-added runs first)
|
|
133
|
-
let finalHandler = composedChain;
|
|
134
|
-
if (this.globalMiddleware.length > 0) {
|
|
135
|
-
for (const mw of this.globalMiddleware.slice().reverse()) {
|
|
136
|
-
const next = finalHandler;
|
|
137
|
-
finalHandler = async () => mw(request, response, next);
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
// Execute the full chain
|
|
141
|
-
return await finalHandler();
|
|
276
|
+
this.server.start(this.routes, async () => {
|
|
277
|
+
return this.NOT_FOUND;
|
|
142
278
|
}, port, cb);
|
|
143
279
|
}
|
|
144
280
|
else {
|
|
145
|
-
//
|
|
146
|
-
|
|
147
|
-
headers: { "Content-Type": "text/plain" },
|
|
148
|
-
}), port, cb);
|
|
281
|
+
// If no routes were configured, log an error
|
|
282
|
+
console.error('Error: No routes configured! Please provide either apiDir with route.ts files or pageDir with html files when initializing the Burger Class.');
|
|
149
283
|
}
|
|
150
284
|
}
|
|
151
285
|
}
|
|
152
286
|
// Export utils
|
|
153
|
-
export { setDir } from
|
|
287
|
+
export { setDir } from './utils/index.js';
|
|
@@ -1,11 +1,2 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
/**
|
|
3
|
-
* Creates a middleware function that validates request data according to the
|
|
4
|
-
* given schema for the current HTTP method.
|
|
5
|
-
*
|
|
6
|
-
* @param schema - The route schema to validate against.
|
|
7
|
-
* @returns A middleware function that validates request data and attaches the
|
|
8
|
-
* validated data to the request if successful, or returns a 400 response with
|
|
9
|
-
* error details if validation fails.
|
|
10
|
-
*/
|
|
1
|
+
import type { RouteSchema, Middleware } from '@burgerTypes';
|
|
11
2
|
export declare function createValidationMiddleware(schema: RouteSchema): Middleware;
|