Skip to content

Commit

Permalink
feat(express): implements ExpressInterceptorService for OgmaInterceptor
Browse files Browse the repository at this point in the history
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
jmcdo29 committed Mar 27, 2020
1 parent d116da3 commit bbe6335
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 0 deletions.
1 change: 1 addition & 0 deletions commitlint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ module.exports = {
'deps',
'docs',
'release',
'express',
'socket.io',
'fastify',
'ws',
Expand Down
22 changes: 22 additions & 0 deletions packages/platform-express/README.md
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 {}
```
45 changes: 45 additions & 0 deletions packages/platform-express/package.json
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 packages/platform-express/src/express-interceptor.service.ts
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;
}
}
1 change: 1 addition & 0 deletions packages/platform-express/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './express-interceptor.service';
4 changes: 4 additions & 0 deletions packages/platform-express/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "./tsconfig.json",
"exclude": ["test", "./src/**/*spec.ts"]
}
13 changes: 13 additions & 0 deletions packages/platform-express/tsconfig.json
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"
}
]
}

0 comments on commit bbe6335

Please sign in to comment.