Express utilities and decorators for building web applications with Inversify dependency injection container
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Core server setup and configuration functionality for integrating Inversify container with Express application. The InversifyExpressServer class handles route registration, middleware resolution, and HTTP context management.
Main server wrapper class that integrates Inversify dependency injection container with Express.js application.
/**
* Wrapper for the express server with Inversify container integration
*/
class InversifyExpressServer {
/**
* Creates a new InversifyExpressServer instance
* @param container - Container loaded with all controllers and their dependencies
* @param customRouter - Optional express.Router custom router
* @param routingConfig - Optional routing configuration
* @param customApp - Optional express.Application custom app
* @param authProvider - Optional authentication provider constructor
* @param forceControllers - Optional boolean setting to force controllers (defaults to true)
*/
constructor(
container: interfaces.Container,
customRouter?: Router | null,
routingConfig?: RoutingConfig | null,
customApp?: Application | null,
authProvider?: (new () => AuthProvider) | null,
forceControllers?: boolean
);
/**
* Sets the configuration function to be applied to the application.
* Note that the config function is not actually executed until a call to build().
* This method is chainable.
* @param fn - Function in which app-level middleware can be registered
*/
setConfig(fn: ConfigFunction): this;
/**
* Sets the error handler configuration function to be applied to the application.
* Note that the error config function is not actually executed until a call to build().
* This method is chainable.
* @param fn - Function in which app-level error handlers can be registered
*/
setErrorConfig(fn: ConfigFunction): this;
/**
* Applies all routes and configuration to the server, returning the express application.
*/
build(): Application;
}/**
* Configuration function type for app-level setup
*/
type ConfigFunction = (app: Application) => void;
/**
* Routing configuration interface
*/
interface RoutingConfig {
/** Root path for all routes (default: '/') */
rootPath: string;
}Usage Examples:
import { Container } from "inversify";
import { InversifyExpressServer } from "inversify-express-utils";
import express from "express";
const container = new Container();
// Basic server setup
const server = new InversifyExpressServer(container);
const app = server.build();
// Server with configuration
const server2 = new InversifyExpressServer(container);
server2.setConfig((app) => {
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use("/api/docs", express.static("docs"));
});
server2.setErrorConfig((app) => {
app.use((error, req, res, next) => {
console.error(error);
res.status(500).json({ message: "Internal server error" });
});
});
const app2 = server2.build();
// Server with custom router and routing config
const customRouter = express.Router();
const server3 = new InversifyExpressServer(
container,
customRouter,
{ rootPath: "/api/v1" }
);
// Server with authentication provider
class CustomAuthProvider implements AuthProvider {
async getUser(req, res, next) {
// Custom authentication logic
return {
details: { id: 1, name: "User" },
isAuthenticated: async () => true,
isInRole: async (role) => role === "user",
isResourceOwner: async (resourceId) => true
};
}
}
const server4 = new InversifyExpressServer(
container,
null,
null,
null,
CustomAuthProvider
);The server automatically creates and manages HTTP context for each request, providing access to the container, request/response objects, and user information.
/**
* HTTP context interface providing request-scoped access to container and user info
*/
interface HttpContext<T = unknown> {
/** Child container created for this specific request */
container: interfaces.Container;
/** Express request object */
request: Request;
/** Express response object */
response: Response;
/** Authenticated user principal */
user: Principal<T>;
}/** Default root path for routing */
const DEFAULT_ROUTING_ROOT_PATH: string = '/';
/** Error message for duplicate controller names */
const DUPLICATED_CONTROLLER_NAME: (name: string) => string;
/** Error message when no controllers are found */
const NO_CONTROLLERS_FOUND: string = 'No controllers have been found! Please ensure that you have register at least one Controller.';