@wthek/nestjs-middleware
Version:
NestJS middleware for handling and formatting errors using http-error-kit. Automatically detects and supports both Express and Fastify-based NestJS apps to ensure structured error responses with correct HTTP status codes.
33 lines (32 loc) • 1.15 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.KitNestJsMiddleware = KitNestJsMiddleware;
var express_middleware_1 = require("@wthek/express-middleware");
var fastify_middleware_1 = require("@wthek/fastify-middleware");
/**
* KitNestJsMiddleware is a middleware for NestJS applications that provides
* integration with @wthek/express-middleware and @wthek/fastify-middleware.
*
* The middleware is automatically configured based on the NestJS HTTP adapter type.
*
* @param app - The NestJS application instance.
*/
function KitNestJsMiddleware(app) {
try {
var adapter = app.getHttpAdapter();
var type = adapter.getType();
if (type === "express") {
app.use((0, express_middleware_1.KitExpressMiddleware)());
}
else if (type === "fastify") {
var fastifyInstance = adapter.getInstance();
(0, fastify_middleware_1.KitFastifyMiddleware)(fastifyInstance);
}
else {
throw new Error("[WTHek] Unsupported HTTP adapter: ".concat(type));
}
}
catch (error) {
throw error;
}
}