{ "$schema": "https://ui.shadcn.com/schema/registry-item.json", "name": "middleware-express", "title": "Express Middleware", "description": "Express middleware that attaches request-scoped amplio context.", "type": "registry:lib", "docs": "Wire the middleware: `app.use(amplioMiddleware())` before your routes. `npx @useamplio/cli@alpha doctor` verifies it is imported.", "dependencies": [ "@useamplio/amplio@^0.1.0-alpha.15" ], "files": [ { "path": "registry/middleware/express.ts", "target": "~/telemetry/middleware/express.ts", "type": "registry:lib", "content": "import type { NextFunction, Request, Response } from \"express\";\nimport { createRequestLogger, getLogger, runWithLogger, type Logger } from \"@useamplio/amplio\";\n\ndeclare global {\n namespace Express {\n interface Request {\n amplio?: Logger;\n }\n }\n}\n\nexport function amplioMiddleware() {\n return (req: Request, res: Response, next: NextFunction) => {\n const requestLogger = createRequestLogger({\n method: req.method,\n path: req.path,\n }).set({\n http: {\n route: req.route?.path,\n ip: req.ip,\n user_agent: req.get(\"user-agent\") ?? undefined,\n },\n });\n\n req.amplio = requestLogger;\n\n runWithLogger(requestLogger, () => {\n res.on(\"finish\", () => {\n if (requestLogger.sealed) {\n return;\n }\n requestLogger.set({\n http: { status: res.statusCode },\n status: res.statusCode,\n });\n requestLogger.emit();\n });\n\n next();\n });\n };\n}\n\n// Accessor for the request logger. Named get*, not use*: this is not a React hook.\nexport function getRequestLogger(req: Request): Logger {\n return req.amplio ?? getLogger();\n}\n\n" } ] }