wooks 0.7.28 → 0.7.30

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/index.cjs CHANGED
@@ -46,37 +46,51 @@ var Wooks = class {
46
46
  return {};
47
47
  }
48
48
  /**
49
- * Looks up a route by method and path, setting route params in the current event context.
49
+ * Matches a route by method and path without touching any event context:
50
+ * no route params are seeded and no `ContextInjector` hooks fire.
51
+ *
52
+ * Use it to decide whether a request is handled here before creating an event
53
+ * context, then call `applyRoute()` inside the context with the returned match.
50
54
  * @param method - HTTP method (e.g., "GET", "POST").
51
55
  * @param path - URL path to match against registered routes.
56
+ * @returns The route match, or `null` when no route with handlers matches.
52
57
  */
53
- lookup(method, path, ctx = (0, _wooksjs_event_core.current)()) {
58
+ matchRoute(method, path) {
54
59
  const found = this.getRouter().lookup(method, path || "");
55
- ctx.set(_wooksjs_event_core.routeParamsKey, found?.ctx?.params || {});
60
+ return found && found.route.handlers.length ? found : null;
61
+ }
62
+ applyRoute(method, match, ctx = (0, _wooksjs_event_core.current)()) {
56
63
  const ci = (0, _wooksjs_event_core.getContextInjector)();
57
- if (found?.route?.handlers.length) ci?.hook(method, "Handler:routed", found.route.path);
58
- else ci?.hook(method, "Handler:not_found");
64
+ if (match) {
65
+ ctx.set(_wooksjs_event_core.routeParamsKey, match.ctx.params || {});
66
+ ci?.hook(method, "Handler:routed", match.route.path);
67
+ return match.route.handlers;
68
+ }
69
+ ctx.set(_wooksjs_event_core.routeParamsKey, {});
70
+ ci?.hook(method, "Handler:not_found");
71
+ return null;
72
+ }
73
+ /**
74
+ * Looks up a route by method and path, setting route params in the current event context.
75
+ * @param method - HTTP method (e.g., "GET", "POST").
76
+ * @param path - URL path to match against registered routes.
77
+ */
78
+ lookup(method, path, ctx = (0, _wooksjs_event_core.current)()) {
79
+ const match = this.matchRoute(method, path);
59
80
  return {
60
- handlers: found?.route?.handlers || null,
61
- segments: found?.route?.segments || null,
62
- firstStatic: found?.route?.firstStatic || null,
63
- path: found?.route?.path || null
81
+ handlers: this.applyRoute(method, match, ctx),
82
+ segments: match?.route.segments || null,
83
+ firstStatic: match?.route.firstStatic || null,
84
+ path: match?.route.path || null
64
85
  };
65
86
  }
66
87
  /**
67
88
  * Fast lookup that returns only the handlers array (or null).
68
89
  * Avoids allocating a result object on each request.
90
+ * Equivalent to `applyRoute(method, matchRoute(method, path), ctx)`.
69
91
  */
70
92
  lookupHandlers(method, path, ctx = (0, _wooksjs_event_core.current)()) {
71
- const found = this.getRouter().lookup(method, path || "");
72
- ctx.set(_wooksjs_event_core.routeParamsKey, found?.ctx?.params || {});
73
- const ci = (0, _wooksjs_event_core.getContextInjector)();
74
- if (found?.route?.handlers.length) {
75
- ci?.hook(method, "Handler:routed", found.route.path);
76
- return found.route.handlers;
77
- }
78
- ci?.hook(method, "Handler:not_found");
79
- return null;
93
+ return this.applyRoute(method, this.matchRoute(method, path), ctx);
80
94
  }
81
95
  /**
82
96
  * Registers a route handler for the given method and path.
package/dist/index.d.ts CHANGED
@@ -3,7 +3,7 @@ export { EventContextOptions, Logger, useEventId, useLogger, useRouteParams } fr
3
3
  import { IncomingMessage } from 'http';
4
4
  import { Duplex } from 'stream';
5
5
  import { TConsoleBase, TProstoLoggerOptions } from '@prostojs/logger';
6
- import { ProstoRouter, TParsedSegment, TProstoRouterPathHandle } from '@prostojs/router';
6
+ import { TProstoLookupResult, ProstoRouter, TParsedSegment, TProstoRouterPathHandle } from '@prostojs/router';
7
7
  export { TProstoRouterPathHandle } from '@prostojs/router';
8
8
 
9
9
  /** A route handler function that returns a response synchronously or asynchronously. */
@@ -40,6 +40,8 @@ interface TWooksOptions {
40
40
  cacheLimit?: number;
41
41
  };
42
42
  }
43
+ /** A matched route (from `Wooks.matchRoute()`): the route with its handlers plus the parsed route params. */
44
+ type TWooksRouteMatch = TProstoLookupResult<TWooksHandler>;
43
45
  /**
44
46
  * Core Wooks framework class that manages routing and logging.
45
47
  *
@@ -63,6 +65,27 @@ declare class Wooks {
63
65
  getLogger(topic: string): TConsoleBase;
64
66
  /** Returns the current logger configuration options. */
65
67
  getLoggerOptions(): TProstoLoggerOptions;
68
+ /**
69
+ * Matches a route by method and path without touching any event context:
70
+ * no route params are seeded and no `ContextInjector` hooks fire.
71
+ *
72
+ * Use it to decide whether a request is handled here before creating an event
73
+ * context, then call `applyRoute()` inside the context with the returned match.
74
+ * @param method - HTTP method (e.g., "GET", "POST").
75
+ * @param path - URL path to match against registered routes.
76
+ * @returns The route match, or `null` when no route with handlers matches.
77
+ */
78
+ matchRoute(method: string, path: string): TWooksRouteMatch | null;
79
+ /**
80
+ * Applies a route match (from `matchRoute()`) to an event context: seeds route params
81
+ * (`useRouteParams()`) and fires the `Handler:routed` / `Handler:not_found` hook.
82
+ * @param method - HTTP method the match was made for.
83
+ * @param match - Result of `matchRoute()` (`null` = not found).
84
+ * @param ctx - Event context to seed (defaults to the current one).
85
+ * @returns The matched route's handlers, or `null` when `match` is `null`.
86
+ */
87
+ applyRoute(method: string, match: TWooksRouteMatch, ctx?: EventContext): TWooksHandler[];
88
+ applyRoute(method: string, match: TWooksRouteMatch | null, ctx?: EventContext): TWooksHandler[] | null;
66
89
  /**
67
90
  * Looks up a route by method and path, setting route params in the current event context.
68
91
  * @param method - HTTP method (e.g., "GET", "POST").
@@ -77,6 +100,7 @@ declare class Wooks {
77
100
  /**
78
101
  * Fast lookup that returns only the handlers array (or null).
79
102
  * Avoids allocating a result object on each request.
103
+ * Equivalent to `applyRoute(method, matchRoute(method, path), ctx)`.
80
104
  */
81
105
  lookupHandlers(method: string, path: string, ctx?: EventContext): TWooksHandler[] | null;
82
106
  /**
@@ -144,4 +168,4 @@ declare class WooksAdapterBase {
144
168
  }
145
169
 
146
170
  export { Wooks, WooksAdapterBase, clearGlobalWooks, getGlobalWooks };
147
- export type { TWooksHandler, TWooksOptions, WooksUpgradeHandler };
171
+ export type { TWooksHandler, TWooksOptions, TWooksRouteMatch, WooksUpgradeHandler };
package/dist/index.mjs CHANGED
@@ -45,37 +45,51 @@ var Wooks = class {
45
45
  return {};
46
46
  }
47
47
  /**
48
- * Looks up a route by method and path, setting route params in the current event context.
48
+ * Matches a route by method and path without touching any event context:
49
+ * no route params are seeded and no `ContextInjector` hooks fire.
50
+ *
51
+ * Use it to decide whether a request is handled here before creating an event
52
+ * context, then call `applyRoute()` inside the context with the returned match.
49
53
  * @param method - HTTP method (e.g., "GET", "POST").
50
54
  * @param path - URL path to match against registered routes.
55
+ * @returns The route match, or `null` when no route with handlers matches.
51
56
  */
52
- lookup(method, path, ctx = current()) {
57
+ matchRoute(method, path) {
53
58
  const found = this.getRouter().lookup(method, path || "");
54
- ctx.set(routeParamsKey, found?.ctx?.params || {});
59
+ return found && found.route.handlers.length ? found : null;
60
+ }
61
+ applyRoute(method, match, ctx = current()) {
55
62
  const ci = getContextInjector();
56
- if (found?.route?.handlers.length) ci?.hook(method, "Handler:routed", found.route.path);
57
- else ci?.hook(method, "Handler:not_found");
63
+ if (match) {
64
+ ctx.set(routeParamsKey, match.ctx.params || {});
65
+ ci?.hook(method, "Handler:routed", match.route.path);
66
+ return match.route.handlers;
67
+ }
68
+ ctx.set(routeParamsKey, {});
69
+ ci?.hook(method, "Handler:not_found");
70
+ return null;
71
+ }
72
+ /**
73
+ * Looks up a route by method and path, setting route params in the current event context.
74
+ * @param method - HTTP method (e.g., "GET", "POST").
75
+ * @param path - URL path to match against registered routes.
76
+ */
77
+ lookup(method, path, ctx = current()) {
78
+ const match = this.matchRoute(method, path);
58
79
  return {
59
- handlers: found?.route?.handlers || null,
60
- segments: found?.route?.segments || null,
61
- firstStatic: found?.route?.firstStatic || null,
62
- path: found?.route?.path || null
80
+ handlers: this.applyRoute(method, match, ctx),
81
+ segments: match?.route.segments || null,
82
+ firstStatic: match?.route.firstStatic || null,
83
+ path: match?.route.path || null
63
84
  };
64
85
  }
65
86
  /**
66
87
  * Fast lookup that returns only the handlers array (or null).
67
88
  * Avoids allocating a result object on each request.
89
+ * Equivalent to `applyRoute(method, matchRoute(method, path), ctx)`.
68
90
  */
69
91
  lookupHandlers(method, path, ctx = current()) {
70
- const found = this.getRouter().lookup(method, path || "");
71
- ctx.set(routeParamsKey, found?.ctx?.params || {});
72
- const ci = getContextInjector();
73
- if (found?.route?.handlers.length) {
74
- ci?.hook(method, "Handler:routed", found.route.path);
75
- return found.route.handlers;
76
- }
77
- ci?.hook(method, "Handler:not_found");
78
- return null;
92
+ return this.applyRoute(method, this.matchRoute(method, path), ctx);
79
93
  }
80
94
  /**
81
95
  * Registers a route handler for the given method and path.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wooks",
3
- "version": "0.7.28",
3
+ "version": "0.7.30",
4
4
  "description": "wooks",
5
5
  "keywords": [
6
6
  "api",
@@ -41,14 +41,14 @@
41
41
  },
42
42
  "dependencies": {
43
43
  "@prostojs/logger": "^0.4.3",
44
- "@prostojs/router": "^0.3.5",
45
- "@wooksjs/event-core": "^0.7.28"
44
+ "@prostojs/router": "^0.3.6",
45
+ "@wooksjs/event-core": "^0.7.30"
46
46
  },
47
47
  "devDependencies": {
48
48
  "typescript": "^5.9.3",
49
49
  "vitest": "^3.2.7",
50
- "@wooksjs/http-body": "^0.7.28",
51
- "@wooksjs/event-http": "^0.7.28"
50
+ "@wooksjs/event-http": "^0.7.30",
51
+ "@wooksjs/http-body": "^0.7.30"
52
52
  },
53
53
  "scripts": {
54
54
  "build": "rolldown -c ../../rolldown.config.mjs"