@sigitex/route 1.0.2 → 1.0.3

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 (2) hide show
  1. package/README.md +41 -0
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -377,3 +377,44 @@ Error classes that can be thrown from handlers:
377
377
  | `MethodNotAllowed` | 405 | `"Method not allowed."` |
378
378
  | `InvalidRequest` | 400 | `"Invalid request."` |
379
379
  | `ServerError` | -- | `"Internal error."` |
380
+
381
+ ## Internals
382
+
383
+ ### Request Lifecycle
384
+
385
+ When `route()` is called it creates a `Router` and returns its `route` method. On each request:
386
+
387
+ 1. A `RequestContext` is created with `request`, `env`, `url`, `bind`, and `dispatch`.
388
+ 2. If a `Container` was provided, it is cloned and the context is bound into it.
389
+ 3. Each handler is dispatched in order. The first to return a non-`undefined` value wins.
390
+ 4. If no handler matches, a JSON 404 is returned. Uncaught errors produce a JSON 500.
391
+
392
+ ### `bind(bindings)`
393
+
394
+ Merges key-value pairs into the current request's context. With a container, values are registered in the IoC container; without one, they're `Object.assign`ed directly onto the context object. This is how middleware like `cookies()` and `requestId()` expose their state to downstream handlers.
395
+
396
+ ### `dispatch(handler, middlewares)`
397
+
398
+ Runs a single handler through a middleware chain:
399
+
400
+ 1. Each middleware's `before` hook runs in order. If any returns a value, it short-circuits as the response.
401
+ 2. The handler is invoked. If it returns `undefined`, dispatch returns `undefined` (no match).
402
+ 3. The response is bound to context via `bind({ response })`.
403
+ 4. Each middleware's `after` hook runs in order. If any returns a value, it replaces the response.
404
+ 5. The final response is returned.
405
+
406
+ Both `bind` and `dispatch` are exposed on the context, so handlers like `prefix` and `pattern` can create nested dispatch chains with their own middleware stacks.
407
+
408
+ ### Container Integration
409
+
410
+ When `RouterOptions.container` is provided (a `@sigitex/bind` `Container`), the router uses dependency injection instead of plain context objects:
411
+
412
+ - The container is **cloned per-request** so concurrent requests don't share state.
413
+ - `bind()` registers values in the container rather than mutating the context.
414
+ - `invoke()` calls `container.call(handler)` instead of `handler(context)`, letting the container resolve handler parameters by type.
415
+
416
+ Without a container, handlers receive the context object directly as their first argument.
417
+
418
+ ### Response Coercion
419
+
420
+ Return values from handlers are coerced: `Response` instances pass through as-is; anything else is `JSON.stringify`ed into a `Response`. Returning `undefined` signals "no match" and the next handler is tried.
package/package.json CHANGED
@@ -43,5 +43,5 @@
43
43
  "files": [
44
44
  "src"
45
45
  ],
46
- "version": "1.0.2"
46
+ "version": "1.0.3"
47
47
  }