-
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(express): implements ExpressInterceptorService for OgmaInterceptor
The ExpressInterceptorService is the base class for the OgmaInterceptor to consume Express request logs properly. It will most likely me the most widely used, though it will have to be installed on its own.
- Loading branch information
Showing
7 changed files
with
154 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,7 @@ module.exports = { | |
'deps', | ||
'docs', | ||
'release', | ||
'express', | ||
'socket.io', | ||
'fastify', | ||
'ws', | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# `@ogma/platform-express` | ||
|
||
The `ExpressInterceptorService` parser for the `OgmaInterceptor`. This plugin class parses Express request and response object to be able to successfully log the data about the request. For more information, check out [the @ogma/nestjs-module](packages/nestjs-module/README.md) documentation. | ||
|
||
## Installation | ||
|
||
Nothing special, standard `npm i @ogma/platform-express` or `yarn add @ogma/platform-express` | ||
|
||
## Usage | ||
|
||
This plugin is to be used in the `OgmaInterceptorOptions` portion of the `OgmaModule` during `forRoot` or `forRootAsync` registration. It can be used like so: | ||
|
||
```ts | ||
@Module( | ||
OgmaModule.forRoot({ | ||
interceptor: { | ||
http: ExpressInterceptorService | ||
} | ||
}) | ||
) | ||
export class AppModule {} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
{ | ||
"name": "@ogma/platform-express", | ||
"version": "0.1.0", | ||
"description": "A plugin for the OgmaInterceptor to properly handle HTTP requests from Express", | ||
"keywords": [ | ||
"express", | ||
"logging", | ||
"ogma", | ||
"nestjs", | ||
"interceptor", | ||
"http" | ||
], | ||
"author": "Jay McDoniel <jmcdo29@gmail.com>", | ||
"homepage": "https://github.com/jmcdo29/nestjs-ogma#readme", | ||
"license": "MIT", | ||
"main": "lib/index.js", | ||
"types": "lib/index.d.ts", | ||
"files": [ | ||
"lib" | ||
], | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/jmcdo29/nestjs-ogma.git" | ||
}, | ||
"scripts": { | ||
"build": "tsc -b tsconfig.build.json", | ||
"test": "jest", | ||
"test:cov": "jest --coverage" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/jmcdo29/nestjs-ogma/issues" | ||
}, | ||
"peerDependencies": { | ||
"@ogma/nestjs-module": "^0.1.0", | ||
"@types/express": "^4.17.3" | ||
}, | ||
"devDependencies": { | ||
"@nestjs/platform-express": "^7.0.6", | ||
"@ogma/nestjs-module": "^0.1.0", | ||
"@types/express": "^4.17.3" | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
packages/platform-express/src/express-interceptor.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { ExecutionContext, HttpException, Injectable } from '@nestjs/common'; | ||
import { HTTP_CODE_METADATA } from '@nestjs/common/constants'; | ||
import { AbstractInterceptorService } from '@ogma/nestjs-module'; | ||
import { Request, Response } from 'express'; | ||
|
||
@Injectable() | ||
export class ExpressInterceptorService extends AbstractInterceptorService { | ||
getCallerIp(context: ExecutionContext): string[] | string { | ||
const req = this.getRequest(context); | ||
return req.ips.length ? req.ips : req.ip; | ||
} | ||
|
||
getCallPoint(context: ExecutionContext): string { | ||
const req = this.getRequest(context); | ||
const url = req.url; | ||
return url || ''; | ||
} | ||
|
||
getStatus( | ||
context: ExecutionContext, | ||
inColor: boolean, | ||
error?: Error & HttpException, | ||
): string { | ||
let status; | ||
const res = this.getResponse(context); | ||
status = res.statusCode; | ||
const reflectStatus = this.reflector.get<number>( | ||
HTTP_CODE_METADATA, | ||
context.getHandler(), | ||
); | ||
status = reflectStatus ?? status; | ||
if (error) { | ||
status = this.determineStatusCodeFromError(error); | ||
} | ||
return inColor ? this.wrapInColor(status) : status.toString(); | ||
} | ||
|
||
getMethod(context: ExecutionContext): string { | ||
const req = this.getRequest(context); | ||
const method = req.method; | ||
return method ?? 'GET'; | ||
} | ||
|
||
getProtocol(context: ExecutionContext): string { | ||
const req = this.getRequest(context); | ||
return `HTTP/${this.getHttpMajor(req)}.${this.getHttpMinor(req)}`; | ||
} | ||
|
||
private getRequest(context: ExecutionContext): Request { | ||
return context.switchToHttp().getRequest(); | ||
} | ||
|
||
private getResponse(context: ExecutionContext): Response { | ||
return context.switchToHttp().getResponse(); | ||
} | ||
|
||
private getHttpMajor(req: Request): number { | ||
return req.httpVersionMajor; | ||
} | ||
|
||
private getHttpMinor(req: Request): number { | ||
return req.httpVersionMinor; | ||
} | ||
|
||
private determineStatusCodeFromError(error: HttpException & Error): number { | ||
return (error.getStatus && error.getStatus()) || 500; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './express-interceptor.service'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"exclude": ["test", "./src/**/*spec.ts"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"rootDir": "./src", | ||
"outDir": "./lib" | ||
}, | ||
"include": ["./src"], | ||
"references": [ | ||
{ | ||
"path": "../logger" | ||
} | ||
] | ||
} |