Skip to content

Commit 1c6f99a

Browse files
committed
fix: use express types and add request context middleware
1 parent aec49de commit 1c6f99a

File tree

11 files changed

+217
-163
lines changed

11 files changed

+217
-163
lines changed

README.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -291,12 +291,9 @@ res.status(HttpStatusCodes.BAD_REQUEST).send("Invalid payload");
291291
- `requestId(options?): Middleware`
292292
- `responseTime(options?): Middleware`
293293
- `timeout(timeoutMs?: number): Middleware`
294+
- `setupRequestContext: void`
294295
- `errorHandler(options?): Middleware`
295-
- `cors(options?): Middleware`
296296
- `validateRequest(schema, location?): Middleware`
297-
- `rateLimit(options?): Middleware`
298-
- `securityHeaders(options?): Middleware`
299-
- `basicAuth(validator, realm?): Middleware`
300297

301298
**Example:**
302299
```ts

package-lock.json

Lines changed: 88 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222
"pretest": "npm run compile",
2323
"dryrun": "npm pack --dry-run",
2424
"test": "cross-env LOGGER_LEVEL=silent jest",
25-
"test:coverage": "cross-env LOGGER_LEVEL=silent jest --coverage",
26-
"ver": "echo $npm_package_version"
25+
"test:coverage": "cross-env LOGGER_LEVEL=silent jest --coverage"
2726
},
2827
"keywords": [
2928
"utils",
@@ -46,6 +45,7 @@
4645
"catbee"
4746
],
4847
"dependencies": {
48+
"@types/express": "^5.0.3",
4949
"abort-controller": "^3.0.0",
5050
"pino": "^9.7.0",
5151
"reflect-metadata": "^0.2.2"

src/config.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,7 @@ export const Config = {
1515
/**
1616
* Name of the logger instance (defaults to npm package name).
1717
*/
18-
name: Env.get('LOGGER_NAME', Env.get('npm_package_name', '@catbee/utils')),
19-
20-
/**
21-
* Whether to use ISO 8601 timestamps in logs.
22-
*/
23-
isoTimestamp: Env.getBoolean('LOGGER_ISO_TIMESTAMP', false)
18+
name: Env.get('LOGGER_NAME', Env.get('npm_package_name', '@catbee/utils'))
2419
},
2520

2621
Http: {

src/utils/decorators.utils.ts

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,8 @@
11
/* eslint-disable @typescript-eslint/no-unused-vars */
22

3+
import type { RequestHandler, Router } from 'express';
34
import 'reflect-metadata';
45

5-
// --- Sample Express interfaces for type safety ---
6-
interface Request {
7-
query: any;
8-
params: any;
9-
body?: any;
10-
[key: string]: any;
11-
}
12-
interface Response {
13-
json: (body: any) => void;
14-
headersSent: boolean;
15-
[key: string]: any;
16-
}
17-
type NextFunction = (err?: any) => void;
18-
type RequestHandler = (req: Request, res: Response, next: NextFunction) => any;
19-
interface Router {
20-
[method: string]: (path: string, ...handlers: RequestHandler[]) => void;
21-
}
226
// --- End sample Express interfaces ---
237

248
const ROUTES_KEY = Symbol('routes');

src/utils/exception.utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ export function withErrorHandling<T extends (...args: any[]) => Promise<any>>(
378378
}
379379

380380
// Log the original error (consider using a proper logger)
381-
getLogger().error('Caught error in handler:', error);
381+
getLogger().error({ error }, 'Caught error in handler');
382382

383383
// Extract status code if it exists, otherwise use 500
384384
let status = HttpStatusCodes.INTERNAL_SERVER_ERROR;

src/utils/logger.utils.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,10 @@ function setupLogger(): void {
4444
}
4545
return '***';
4646
}
47-
}
47+
},
48+
timestamp: stdTimeFunctions.isoTime
4849
};
4950

50-
if (Config.Logger.isoTimestamp) {
51-
logParams.timestamp = stdTimeFunctions.isoTime;
52-
}
53-
5451
_global[GLOBAL_LOGGER_KEY] = pino(logParams);
5552
_global[GLOBAL_LOGGER_KEY].debug('Logger initialized');
5653
}
@@ -122,7 +119,7 @@ export function logError(error: Error | unknown, message?: string, context?: Rec
122119
const errObj = error instanceof Error ? error : new Error(String(error));
123120
const logContext = {
124121
...context,
125-
err: errObj
122+
error: errObj
126123
};
127124

128125
logger.error(logContext, message || errObj.message);

0 commit comments

Comments
 (0)