@basaltkit/express 1.4.1 → 1.5.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/dist/index.d.ts CHANGED
@@ -1,9 +1,9 @@
1
1
  import { Container } from '@basaltkit/core';
2
- import { type BasaltRoute, type RequestEnricher, type RouteGuard } from '@basaltkit/http';
2
+ import { type HttpErrorReporter, type BasaltRoute, type RequestEnricher, type RouteGuard } from '@basaltkit/http';
3
3
  import { type Express } from 'express';
4
4
  export declare const EXPRESS: import("@basaltkit/core").Token<Express>;
5
5
  /** Mounts Basalt routes on an Express app (usable without the plugin). */
6
- export declare function registerRoutes(app: Express, routes: BasaltRoute[], container?: Container, enrichers?: RequestEnricher[], guards?: RouteGuard[]): void;
6
+ export declare function registerRoutes(app: Express, routes: BasaltRoute[], container?: Container, enrichers?: RequestEnricher[], guards?: RouteGuard[], onError?: HttpErrorReporter): void;
7
7
  export interface ExpressPluginOptions {
8
8
  routes?: BasaltRoute[];
9
9
  /**
@@ -21,6 +21,12 @@ export interface ExpressPluginOptions {
21
21
  * HTML default. Default: true. Pass false to keep Express's own handling
22
22
  * (e.g. when the app mounts its own catch-all after boot).
23
23
  */
24
+ /**
25
+ * Where failed requests are reported. Default: 5xx via `console.error` (with
26
+ * the stack) and 4xx via `console.warn`, prefixed `[basalt:http]`. Pass your
27
+ * own to route them into a real logger, or `() => {}` to silence them.
28
+ */
29
+ onError?: HttpErrorReporter;
24
30
  notFound?: boolean;
25
31
  }
26
32
  /**
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { Container, createToken, definePlugin, ensureMetadata } from '@basaltkit/core';
2
- import { NOT_FOUND_RESPONSE, HttpServerCollector, HTTP_SERVER, runRoute, toErrorResponse, isSseResponse, sseProducerOf, driveSse, SSE_HEADERS, GUARDED_META_BUCKET, assertRoutesGuarded, } from '@basaltkit/http';
2
+ import { NOT_FOUND_RESPONSE, HttpServerCollector, HTTP_SERVER, runRoute, toErrorResponse, reportHttpError, isSseResponse, sseProducerOf, driveSse, SSE_HEADERS, GUARDED_META_BUCKET, assertRoutesGuarded, } from '@basaltkit/http';
3
3
  import express, {} from 'express';
4
4
  export const EXPRESS = createToken('express');
5
5
  function toNeutralRequest(req) {
@@ -52,7 +52,7 @@ class ExpressReply {
52
52
  return this;
53
53
  }
54
54
  }
55
- function basaltHandler(definition, container, enrichers, guards) {
55
+ function basaltHandler(definition, container, enrichers, guards, onError) {
56
56
  return async (req, res) => {
57
57
  const reply = new ExpressReply(res);
58
58
  try {
@@ -75,16 +75,23 @@ function basaltHandler(definition, container, enrichers, guards) {
75
75
  }
76
76
  catch (error) {
77
77
  const { status, body } = toErrorResponse(error);
78
+ // This adapter previously reported nothing at all — a 500 reached the
79
+ // client and left no trace whatsoever on the server.
80
+ const entry = { error, status, code: body.error.code, method: req.method, url: req.originalUrl };
81
+ if (onError)
82
+ onError(entry);
83
+ else
84
+ reportHttpError(entry);
78
85
  if (!res.headersSent)
79
86
  res.status(status).json(body);
80
87
  }
81
88
  };
82
89
  }
83
90
  /** Mounts Basalt routes on an Express app (usable without the plugin). */
84
- export function registerRoutes(app, routes, container, enrichers = [], guards = []) {
91
+ export function registerRoutes(app, routes, container, enrichers = [], guards = [], onError) {
85
92
  const router = app;
86
93
  for (const definition of routes) {
87
- router[definition.method.toLowerCase()](definition.url, basaltHandler(definition, container, enrichers, guards));
94
+ router[definition.method.toLowerCase()](definition.url, basaltHandler(definition, container, enrichers, guards, onError));
88
95
  }
89
96
  }
90
97
  /**
@@ -134,7 +141,7 @@ export function expressPlugin(options = {}) {
134
141
  return;
135
142
  next();
136
143
  });
137
- registerRoutes(app, routes, container, enrichers, guards);
144
+ registerRoutes(app, routes, container, enrichers, guards, options.onError);
138
145
  for (const { method, url, handler } of collector.extraRoutes) {
139
146
  router[method.toLowerCase()](url, async (req, res) => {
140
147
  const reply = new ExpressReply(res);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@basaltkit/express",
3
- "version": "1.4.1",
3
+ "version": "1.5.0",
4
4
  "engines": {
5
5
  "node": ">=22.5.0"
6
6
  },
@@ -19,7 +19,7 @@
19
19
  ],
20
20
  "dependencies": {
21
21
  "@basaltkit/core": "^1.3.1",
22
- "@basaltkit/http": "^1.14.0"
22
+ "@basaltkit/http": "^1.15.0"
23
23
  },
24
24
  "peerDependencies": {
25
25
  "express": "^4.19.0 || ^5.0.0"