@morojs/moro 1.8.10 → 1.8.12

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/dist/moro.d.ts CHANGED
@@ -3,7 +3,7 @@ import { HttpRequest, HttpResponse } from './core/http/index.js';
3
3
  import { ModuleConfig } from './types/module.js';
4
4
  import { MoroOptions } from './types/core.js';
5
5
  import { MoroEventBus } from './core/events/index.js';
6
- import { RouteSchema } from './core/routing/index.js';
6
+ import { RouteSchema, StaticBody } from './core/routing/index.js';
7
7
  import { RouteBuilder as UnifiedRouteBuilder } from './core/routing/unified-router.js';
8
8
  import { DocsConfig } from './core/docs/index.js';
9
9
  import { EventEmitter } from 'events';
@@ -30,21 +30,21 @@ export declare class RouteGroup {
30
30
  constructor(prefix: string, app: Moro);
31
31
  private prefixPath;
32
32
  get(path: string): UnifiedRouteBuilder;
33
- get(path: string, handler: (req: HttpRequest, res: HttpResponse) => any, options?: any): this;
33
+ get(path: string, handler: ((req: HttpRequest, res: HttpResponse) => any) | StaticBody, options?: any): this;
34
34
  post(path: string): UnifiedRouteBuilder;
35
- post(path: string, handler: (req: HttpRequest, res: HttpResponse) => any, options?: any): this;
35
+ post(path: string, handler: ((req: HttpRequest, res: HttpResponse) => any) | StaticBody, options?: any): this;
36
36
  put(path: string): UnifiedRouteBuilder;
37
- put(path: string, handler: (req: HttpRequest, res: HttpResponse) => any, options?: any): this;
37
+ put(path: string, handler: ((req: HttpRequest, res: HttpResponse) => any) | StaticBody, options?: any): this;
38
38
  delete(path: string): UnifiedRouteBuilder;
39
- delete(path: string, handler: (req: HttpRequest, res: HttpResponse) => any, options?: any): this;
39
+ delete(path: string, handler: ((req: HttpRequest, res: HttpResponse) => any) | StaticBody, options?: any): this;
40
40
  patch(path: string): UnifiedRouteBuilder;
41
- patch(path: string, handler: (req: HttpRequest, res: HttpResponse) => any, options?: any): this;
41
+ patch(path: string, handler: ((req: HttpRequest, res: HttpResponse) => any) | StaticBody, options?: any): this;
42
42
  head(path: string): UnifiedRouteBuilder;
43
- head(path: string, handler: (req: HttpRequest, res: HttpResponse) => any, options?: any): this;
43
+ head(path: string, handler: ((req: HttpRequest, res: HttpResponse) => any) | StaticBody, options?: any): this;
44
44
  options(path: string): UnifiedRouteBuilder;
45
- options(path: string, handler: (req: HttpRequest, res: HttpResponse) => any, options?: any): this;
45
+ options(path: string, handler: ((req: HttpRequest, res: HttpResponse) => any) | StaticBody, options?: any): this;
46
46
  /** Register a handler for all HTTP methods on the given path (Express-compatible). */
47
- all(path: string, handler: (req: HttpRequest, res: HttpResponse) => any, options?: any): this;
47
+ all(path: string, handler: ((req: HttpRequest, res: HttpResponse) => any) | StaticBody, options?: any): this;
48
48
  group(prefix: string, callback: (group: RouteGroup) => void): this;
49
49
  }
50
50
  export declare class Moro extends EventEmitter {
@@ -112,25 +112,25 @@ export declare class Moro extends EventEmitter {
112
112
  */
113
113
  private getDefaultOptionsFromConfig;
114
114
  get(path: string): UnifiedRouteBuilder;
115
- get(path: string, handler: (req: HttpRequest, res: HttpResponse) => any, options?: any): this;
115
+ get(path: string, handler: ((req: HttpRequest, res: HttpResponse) => any) | StaticBody, options?: any): this;
116
116
  post(path: string): UnifiedRouteBuilder;
117
- post(path: string, handler: (req: HttpRequest, res: HttpResponse) => any, options?: any): this;
117
+ post(path: string, handler: ((req: HttpRequest, res: HttpResponse) => any) | StaticBody, options?: any): this;
118
118
  put(path: string): UnifiedRouteBuilder;
119
- put(path: string, handler: (req: HttpRequest, res: HttpResponse) => any, options?: any): this;
119
+ put(path: string, handler: ((req: HttpRequest, res: HttpResponse) => any) | StaticBody, options?: any): this;
120
120
  delete(path: string): UnifiedRouteBuilder;
121
- delete(path: string, handler: (req: HttpRequest, res: HttpResponse) => any, options?: any): this;
121
+ delete(path: string, handler: ((req: HttpRequest, res: HttpResponse) => any) | StaticBody, options?: any): this;
122
122
  patch(path: string): UnifiedRouteBuilder;
123
- patch(path: string, handler: (req: HttpRequest, res: HttpResponse) => any, options?: any): this;
123
+ patch(path: string, handler: ((req: HttpRequest, res: HttpResponse) => any) | StaticBody, options?: any): this;
124
124
  head(path: string): UnifiedRouteBuilder;
125
- head(path: string, handler: (req: HttpRequest, res: HttpResponse) => any, options?: any): this;
125
+ head(path: string, handler: ((req: HttpRequest, res: HttpResponse) => any) | StaticBody, options?: any): this;
126
126
  options(path: string): UnifiedRouteBuilder;
127
- options(path: string, handler: (req: HttpRequest, res: HttpResponse) => any, options?: any): this;
127
+ options(path: string, handler: ((req: HttpRequest, res: HttpResponse) => any) | StaticBody, options?: any): this;
128
128
  /**
129
129
  * Register a handler for all HTTP methods on the given path (Express-compatible).
130
130
  * Handler is required — unlike the single-method registrations, there's no builder
131
131
  * form since `all()` fans out across methods.
132
132
  */
133
- all(path: string, handler: (req: HttpRequest, res: HttpResponse) => any, options?: any): this;
133
+ all(path: string, handler: ((req: HttpRequest, res: HttpResponse) => any) | StaticBody, options?: any): this;
134
134
  route(schema: RouteSchema): void;
135
135
  group(prefix: string, callback: (group: RouteGroup) => void): this;
136
136
  /**
package/dist/moro.js CHANGED
@@ -344,7 +344,7 @@ export class Moro extends EventEmitter {
344
344
  };
345
345
  }
346
346
  get(path, handler, options) {
347
- if (handler) {
347
+ if (handler !== undefined) {
348
348
  // Direct route registration
349
349
  return this.addRoute('GET', path, handler, options);
350
350
  }
@@ -352,7 +352,7 @@ export class Moro extends EventEmitter {
352
352
  return this.unifiedRouter.get(path);
353
353
  }
354
354
  post(path, handler, options) {
355
- if (handler) {
355
+ if (handler !== undefined) {
356
356
  // Direct route registration
357
357
  return this.addRoute('POST', path, handler, options);
358
358
  }
@@ -360,7 +360,7 @@ export class Moro extends EventEmitter {
360
360
  return this.unifiedRouter.post(path);
361
361
  }
362
362
  put(path, handler, options) {
363
- if (handler) {
363
+ if (handler !== undefined) {
364
364
  // Direct route registration
365
365
  return this.addRoute('PUT', path, handler, options);
366
366
  }
@@ -368,7 +368,7 @@ export class Moro extends EventEmitter {
368
368
  return this.unifiedRouter.put(path);
369
369
  }
370
370
  delete(path, handler, options) {
371
- if (handler) {
371
+ if (handler !== undefined) {
372
372
  // Direct route registration
373
373
  return this.addRoute('DELETE', path, handler, options);
374
374
  }
@@ -376,7 +376,7 @@ export class Moro extends EventEmitter {
376
376
  return this.unifiedRouter.delete(path);
377
377
  }
378
378
  patch(path, handler, options) {
379
- if (handler) {
379
+ if (handler !== undefined) {
380
380
  // Direct route registration
381
381
  return this.addRoute('PATCH', path, handler, options);
382
382
  }
@@ -384,13 +384,13 @@ export class Moro extends EventEmitter {
384
384
  return this.unifiedRouter.patch(path);
385
385
  }
386
386
  head(path, handler, options) {
387
- if (handler) {
387
+ if (handler !== undefined) {
388
388
  return this.addRoute('HEAD', path, handler, options);
389
389
  }
390
390
  return this.unifiedRouter.head(path);
391
391
  }
392
392
  options(path, handler, options) {
393
- if (handler) {
393
+ if (handler !== undefined) {
394
394
  return this.addRoute('OPTIONS', path, handler, options);
395
395
  }
396
396
  return this.unifiedRouter.options(path);
@@ -1237,6 +1237,23 @@ export class Moro extends EventEmitter {
1237
1237
  }
1238
1238
  // Private methods
1239
1239
  addRoute(method, path, handler, options = {}) {
1240
+ // A literal body - app.get('/', '') - is the same route the builder's
1241
+ // .handler('') registers: the unified router turns it into a handler and,
1242
+ // with nothing else configured, a reply the native engine answers itself.
1243
+ // It never enters the legacy direct-route table, whose wrapper would
1244
+ // call it as a function.
1245
+ if (typeof handler !== 'function') {
1246
+ this.unifiedRouter.registerRoute({
1247
+ method: method,
1248
+ path,
1249
+ handler: handler,
1250
+ validation: options.validation,
1251
+ rateLimit: options.rateLimit,
1252
+ cache: options.cache,
1253
+ middleware: options.middleware,
1254
+ });
1255
+ return this;
1256
+ }
1240
1257
  // Register with unified router (primary routing system)
1241
1258
  this.unifiedRouter.addRoute(method, path, handler, options.middleware || []);
1242
1259
  // Also store in legacy routes array for backward compatibility
@@ -1282,6 +1299,19 @@ export class Moro extends EventEmitter {
1282
1299
  if (httpServer && typeof httpServer.setRouterHandler === 'function') {
1283
1300
  const router = this.unifiedRouter;
1284
1301
  httpServer.setRouterHandler((req, res) => router.handleRequest(req, res));
1302
+ // A server that answers fixed replies itself (MoroEngineServer on an
1303
+ // engine with capabilities.staticRoutes) gets every route whose handler
1304
+ // is a literal body, so those requests never enter JS. The route's
1305
+ // handler stays registered: it answers HEAD, and everything on servers
1306
+ // without the capability.
1307
+ if (typeof httpServer.setStaticRoute === 'function') {
1308
+ for (const schema of router.getAllRoutes()) {
1309
+ const fixed = schema.static;
1310
+ if (fixed) {
1311
+ httpServer.setStaticRoute(schema.method, schema.path, fixed.status, fixed.headers, fixed.body);
1312
+ }
1313
+ }
1314
+ }
1285
1315
  // Servers with a native router (uWS) additionally get fast-path routes
1286
1316
  // registered directly on it - evaluated at listen so late-loaded routes
1287
1317
  // are included; error handling shares the unified router's semantics