Skip to content

Commit

Permalink
feature(@nestjs/core) FastifyAdapter matches URL and request method
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Jun 22, 2018
1 parent f4817ee commit 7023905
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
16 changes: 13 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"object-hash": "^1.3.0",
"opencollective": "^1.0.3",
"optional": "^0.1.4",
"path-to-regexp": "^2.2.1",
"pump": "^3.0.0",
"redis": "^2.7.1",
"reflect-metadata": "^0.1.12",
Expand Down
13 changes: 11 additions & 2 deletions packages/core/adapters/fastify-adapter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Logger, RequestMethod } from '@nestjs/common';
import { ErrorHandler, RequestHandler } from '@nestjs/common/interfaces';
import { loadPackage } from '@nestjs/common/utils/load-package.util';
import * as pathToRegexp from 'path-to-regexp';

export class FastifyAdapter {
private readonly logger = new Logger(FastifyAdapter.name);
Expand Down Expand Up @@ -134,12 +135,20 @@ export class FastifyAdapter {
createMiddlewareFactory(
requestMethod: RequestMethod,
): (path: string, callback: Function) => any {
return (path: string, callback: Function) =>
return (path: string, callback: Function) => {
const re = pathToRegexp(path);
this.instance.use(path, (req, res, next) => {
if (req.method === RequestMethod[requestMethod]) {
if (!re.exec(req.originalUrl + '/')) {
return next();
}
if (
requestMethod === RequestMethod.ALL ||
req.method === RequestMethod[requestMethod]
) {
return callback(req, res, next);
}
next();
});
};
}
}

0 comments on commit 7023905

Please sign in to comment.