mirror of
https://github.com/nestjs/nest.git
synced 2026-02-21 23:11:44 +00:00
feat: use mapToExcludeRoute() when set global prefix
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
import { RequestMethod } from '@nestjs/common';
|
||||
import { HttpServer, RouteInfo, Type } from '@nestjs/common/interfaces';
|
||||
import { isFunction } from '@nestjs/common/utils/shared.utils';
|
||||
import {
|
||||
addLeadingSlash,
|
||||
isFunction,
|
||||
isString,
|
||||
} from '@nestjs/common/utils/shared.utils';
|
||||
import { iterate } from 'iterare';
|
||||
import * as pathToRegexp from 'path-to-regexp';
|
||||
import { v4 as uuid } from 'uuid';
|
||||
@@ -8,13 +12,22 @@ import { ExcludeRouteMetadata } from '../router/interfaces/exclude-route-metadat
|
||||
import { isRouteExcluded } from '../router/utils';
|
||||
|
||||
export const mapToExcludeRoute = (
|
||||
routes: RouteInfo[],
|
||||
routes: (string | RouteInfo)[],
|
||||
): ExcludeRouteMetadata[] => {
|
||||
return routes.map(({ path, method }) => ({
|
||||
path,
|
||||
pathRegex: pathToRegexp(path),
|
||||
requestMethod: method,
|
||||
}));
|
||||
return routes.map(route => {
|
||||
if (isString(route)) {
|
||||
return {
|
||||
path: route,
|
||||
requestMethod: RequestMethod.ALL,
|
||||
pathRegex: pathToRegexp(addLeadingSlash(route)),
|
||||
};
|
||||
}
|
||||
return {
|
||||
path: route.path,
|
||||
requestMethod: route.method,
|
||||
pathRegex: pathToRegexp(addLeadingSlash(route.path)),
|
||||
};
|
||||
});
|
||||
};
|
||||
|
||||
export const filterMiddleware = <T extends Function | Type<any> = any>(
|
||||
|
||||
@@ -7,13 +7,11 @@ import {
|
||||
NestHybridApplicationOptions,
|
||||
NestInterceptor,
|
||||
PipeTransform,
|
||||
RequestMethod,
|
||||
VersioningOptions,
|
||||
VersioningType,
|
||||
WebSocketAdapter,
|
||||
} from '@nestjs/common';
|
||||
import {
|
||||
RouteInfo,
|
||||
GlobalPrefixOptions,
|
||||
NestApplicationOptions,
|
||||
} from '@nestjs/common/interfaces';
|
||||
@@ -31,7 +29,6 @@ import {
|
||||
} from '@nestjs/common/utils/shared.utils';
|
||||
import { iterate } from 'iterare';
|
||||
import { platform } from 'os';
|
||||
import * as pathToRegexp from 'path-to-regexp';
|
||||
import { AbstractHttpAdapter } from './adapters';
|
||||
import { ApplicationConfig } from './application-config';
|
||||
import { MESSAGES } from './constants';
|
||||
@@ -39,8 +36,8 @@ import { optionalRequire } from './helpers/optional-require';
|
||||
import { NestContainer } from './injector/container';
|
||||
import { MiddlewareContainer } from './middleware/container';
|
||||
import { MiddlewareModule } from './middleware/middleware-module';
|
||||
import { mapToExcludeRoute } from './middleware/utils';
|
||||
import { NestApplicationContext } from './nest-application-context';
|
||||
import { ExcludeRouteMetadata } from './router/interfaces/exclude-route-metadata.interface';
|
||||
import { Resolver } from './router/interfaces/resolver.interface';
|
||||
import { RoutePathFactory } from './router/route-path-factory';
|
||||
import { RoutesResolver } from './router/routes-resolver';
|
||||
@@ -336,22 +333,9 @@ export class NestApplication
|
||||
public setGlobalPrefix(prefix: string, options?: GlobalPrefixOptions): this {
|
||||
this.config.setGlobalPrefix(prefix);
|
||||
if (options) {
|
||||
const exclude = options?.exclude.map(
|
||||
(route: string | RouteInfo): ExcludeRouteMetadata => {
|
||||
if (isString(route)) {
|
||||
return {
|
||||
path: route,
|
||||
requestMethod: RequestMethod.ALL,
|
||||
pathRegex: pathToRegexp(addLeadingSlash(route)),
|
||||
};
|
||||
}
|
||||
return {
|
||||
path: route.path,
|
||||
requestMethod: route.method,
|
||||
pathRegex: pathToRegexp(addLeadingSlash(route.path)),
|
||||
};
|
||||
},
|
||||
);
|
||||
const exclude = options?.exclude
|
||||
? mapToExcludeRoute(options.exclude)
|
||||
: [];
|
||||
this.config.setGlobalPrefixOptions({
|
||||
...options,
|
||||
exclude,
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
import { Injectable, VersioningType } from '@nestjs/common';
|
||||
import { addLeadingSlash } from '@nestjs/common/utils/shared.utils';
|
||||
import { RoutePathFactory } from '@nestjs/core/router/route-path-factory';
|
||||
import { expect } from 'chai';
|
||||
import pathToRegexp = require('path-to-regexp');
|
||||
import * as sinon from 'sinon';
|
||||
import { Controller } from '../../../common/decorators/core/controller.decorator';
|
||||
import { RequestMapping } from '../../../common/decorators/http/request-mapping.decorator';
|
||||
@@ -19,6 +17,7 @@ import { MiddlewareContainer } from '../../middleware/container';
|
||||
import { MiddlewareModule } from '../../middleware/middleware-module';
|
||||
import { RouterExceptionFilters } from '../../router/router-exception-filters';
|
||||
import { NoopHttpAdapter } from '../utils/noop-adapter.spec';
|
||||
import { mapToExcludeRoute } from './../../middleware/utils';
|
||||
|
||||
describe('MiddlewareModule', () => {
|
||||
let middlewareModule: MiddlewareModule;
|
||||
@@ -242,13 +241,7 @@ describe('MiddlewareModule', () => {
|
||||
});
|
||||
(middlewareModule as any).config.setGlobalPrefix('api');
|
||||
(middlewareModule as any).config.setGlobalPrefixOptions({
|
||||
exclude: [
|
||||
{
|
||||
path: 'foo',
|
||||
requestMethod: RequestMethod.ALL,
|
||||
pathRegex: pathToRegexp(addLeadingSlash('foo')),
|
||||
},
|
||||
],
|
||||
exclude: mapToExcludeRoute(['foo']),
|
||||
});
|
||||
|
||||
const allPaths = (middlewareModule as any).getPaths({
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { RequestMethod, Type } from '@nestjs/common';
|
||||
import { addLeadingSlash } from '@nestjs/common/utils/shared.utils';
|
||||
import { expect } from 'chai';
|
||||
import * as sinon from 'sinon';
|
||||
import {
|
||||
@@ -10,6 +11,7 @@ import {
|
||||
mapToExcludeRoute,
|
||||
} from '../../middleware/utils';
|
||||
import { NoopHttpAdapter } from '../utils/noop-adapter.spec';
|
||||
import pathToRegexp = require('path-to-regexp');
|
||||
|
||||
describe('middleware utils', () => {
|
||||
const noopAdapter = new NoopHttpAdapter({});
|
||||
@@ -17,6 +19,27 @@ describe('middleware utils', () => {
|
||||
class Test {}
|
||||
function fnMiddleware(req, res, next) {}
|
||||
|
||||
describe('mapToExcludeRoute', () => {
|
||||
it('should return exclude route metadata', () => {
|
||||
const stringRoute = 'foo';
|
||||
const routeInfo = {
|
||||
path: 'bar',
|
||||
method: RequestMethod.GET,
|
||||
};
|
||||
expect(mapToExcludeRoute([stringRoute, routeInfo])).to.eql([
|
||||
{
|
||||
path: stringRoute,
|
||||
requestMethod: RequestMethod.ALL,
|
||||
pathRegex: pathToRegexp(addLeadingSlash(stringRoute)),
|
||||
},
|
||||
{
|
||||
path: routeInfo.path,
|
||||
requestMethod: routeInfo.method,
|
||||
pathRegex: pathToRegexp(addLeadingSlash(routeInfo.path)),
|
||||
},
|
||||
]);
|
||||
});
|
||||
});
|
||||
describe('filterMiddleware', () => {
|
||||
let middleware: any[];
|
||||
beforeEach(() => {
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
import { RequestMethod } from '@nestjs/common';
|
||||
import { addLeadingSlash } from '@nestjs/common/utils/shared.utils';
|
||||
import { expect } from 'chai';
|
||||
import pathToRegexp = require('path-to-regexp');
|
||||
import { ApplicationConfig } from '../application-config';
|
||||
import { NestContainer } from '../injector/container';
|
||||
import { NestApplication } from '../nest-application';
|
||||
import { mapToExcludeRoute } from './../middleware/utils';
|
||||
import { NoopHttpAdapter } from './utils/noop-adapter.spec';
|
||||
|
||||
describe('NestApplication', () => {
|
||||
@@ -68,18 +67,13 @@ describe('NestApplication', () => {
|
||||
exclude: ['foo', { path: 'bar', method: RequestMethod.GET }],
|
||||
});
|
||||
expect((instance as any).config.getGlobalPrefixOptions()).to.eql({
|
||||
exclude: [
|
||||
{
|
||||
path: 'foo',
|
||||
requestMethod: RequestMethod.ALL,
|
||||
pathRegex: pathToRegexp(addLeadingSlash('foo')),
|
||||
},
|
||||
exclude: mapToExcludeRoute([
|
||||
'foo',
|
||||
{
|
||||
path: 'bar',
|
||||
requestMethod: RequestMethod.GET,
|
||||
pathRegex: pathToRegexp(addLeadingSlash('bar')),
|
||||
method: RequestMethod.GET,
|
||||
},
|
||||
],
|
||||
]),
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user