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(serve-favicon): add serve-favicon middleware
- Loading branch information
Showing
4 changed files
with
92 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
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 { ServeFaviconMiddleware } from './index'; | ||
import { expect } from 'chai'; | ||
|
||
describe('ServeFaviconMiddleware', () => { | ||
let middleware: ServeFaviconMiddleware; | ||
describe('properly configured', () => { | ||
beforeEach(() => { | ||
ServeFaviconMiddleware.configure('./lerna.json', {}); | ||
middleware = new ServeFaviconMiddleware(); | ||
}); | ||
|
||
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); | ||
}); | ||
afterEach(() => { | ||
ServeFaviconMiddleware.configure(undefined); | ||
}); | ||
}); | ||
|
||
describe('not configured', () => { | ||
middleware = new ServeFaviconMiddleware(); | ||
it('should should return a middleware from calling resolve', () => { | ||
expect(middleware.resolve.bind(middleware)).to.throw(Error); | ||
}); | ||
}); | ||
}); |
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,29 @@ | ||
import * as serveFavicon from 'serve-favicon'; | ||
|
||
import { Middleware, NestMiddleware } from '@nestjs/common'; | ||
|
||
import { RequestHandler } from 'express'; | ||
|
||
@Middleware() | ||
export class ServeFaviconMiddleware implements NestMiddleware { | ||
|
||
public static configure(path: string, opts?: ServeFaviconOptions) { | ||
this.path = path; | ||
this.options = opts; | ||
} | ||
|
||
private static path: string; | ||
private static options: ServeFaviconOptions; | ||
|
||
public resolve(...args: any[]): RequestHandler { | ||
if (ServeFaviconMiddleware.path) { | ||
return serveFavicon(ServeFaviconMiddleware.path, ServeFaviconMiddleware.options); | ||
} else { | ||
throw new Error('ServeFaviconMiddleware requires a path in configure.'); | ||
} | ||
} | ||
} | ||
|
||
export interface ServeFaviconOptions { | ||
maxAge?: number; | ||
} |
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/serve-favicon", | ||
"version": "1.0.0", | ||
"description": "NestJS Middleware for Serve Favicon", | ||
"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": { | ||
"serve-favicon": "^2.4.5" | ||
} | ||
} |