forked from wbhob/nest-middlewares
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(morgan): add middleware for morgan
- Loading branch information
Showing
3 changed files
with
88 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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { MorganMiddleware } from './index'; | ||
import { expect } from 'chai'; | ||
|
||
describe('MorganMiddleware', () => { | ||
let middleware: MorganMiddleware; | ||
describe('properly configured', () => { | ||
beforeEach(() => { | ||
MorganMiddleware.configure('tiny'); | ||
middleware = new MorganMiddleware(); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(middleware).to.not.be.undefined; | ||
}); | ||
|
||
it('should have a function called resolve', () => { | ||
expect(middleware.resolve).to.be.instanceof(Function); | ||
}); | ||
|
||
it('should should return a middleware from calling resolve', () => { | ||
expect(middleware.resolve()).to.be.an.instanceof(Function); | ||
}); | ||
}); | ||
|
||
describe('not configured', () => { | ||
middleware = new MorganMiddleware(); | ||
it('should should throw when not passed a format', () => { | ||
expect(middleware.resolve.bind(middleware)).to.throw(Error); | ||
}); | ||
}); | ||
afterEach(() => { | ||
MorganMiddleware.configure(undefined); | ||
}); | ||
}); |
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,26 @@ | ||
import * as morgan from 'morgan'; | ||
|
||
import { Middleware, NestMiddleware } from '@nestjs/common'; | ||
|
||
import { RequestHandler } from 'express'; | ||
|
||
@Middleware() | ||
export class MorganMiddleware implements NestMiddleware { | ||
|
||
// DELETE THESE LINES IF MIDDLEWARE DOES NOT TAKE OPTIONS | ||
public static configure(format: string | morgan.FormatFn, opts?: morgan.Options) { | ||
this.format = format; | ||
this.options = opts; | ||
} | ||
|
||
private static options: morgan.Options; | ||
private static format: string | morgan.FormatFn; | ||
|
||
public resolve(...args: any[]): RequestHandler { | ||
if (MorganMiddleware.format) { | ||
return morgan(MorganMiddleware.format as any, MorganMiddleware.options); | ||
} else { | ||
throw new Error('MorganMiddleware must be configured with a logger format.'); | ||
} | ||
} | ||
} |
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,28 @@ | ||
{ | ||
"name": "@nest-middlewares/morgan", | ||
"version": "1.0.0", | ||
"description": "NestJS middleware for Morgan", | ||
"main": "index", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/wbhob/nest-middlewares.git" | ||
}, | ||
"keywords": [ | ||
"nest", | ||
"middlewares", | ||
"express", | ||
"node" | ||
], | ||
"author": "Wilson Hobbs <wilsonhobbs1@gmail.com>", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/wbhob/nest-middlewares/issues" | ||
}, | ||
"homepage": "https://github.com/wbhob/nest-middlewares#readme", | ||
"peerDependencies": { | ||
"@nestjs/common": "^4.0.0" | ||
}, | ||
"dependencies": { | ||
"morgan": "^1.9.0" | ||
} | ||
} |