Skip to content

Commit 7197cc1

Browse files
feat: add normalize path to http adapter
1 parent 7f142e9 commit 7197cc1

File tree

4 files changed

+23
-16
lines changed

4 files changed

+23
-16
lines changed

packages/common/interfaces/http/http-server.interface.ts

+1
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,5 @@ export interface HttpServer<
102102
version: VersionValue,
103103
versioningOptions: VersioningOptions,
104104
): (req: TRequest, res: TResponse, next: () => void) => Function;
105+
normalizePath?(path: string): string;
105106
}

packages/core/adapters/http-adapter.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,10 @@ export abstract class AbstractHttpAdapter<
143143
return this.instance as T;
144144
}
145145

146+
public normalizePath(path: string): string {
147+
return path;
148+
}
149+
146150
abstract close();
147151
abstract initHttpServer(options: NestApplicationOptions);
148152
abstract useStaticAssets(...args: any[]);
@@ -158,11 +162,7 @@ export abstract class AbstractHttpAdapter<
158162
abstract setErrorHandler(handler: Function, prefix?: string);
159163
abstract setNotFoundHandler(handler: Function, prefix?: string);
160164
abstract isHeadersSent(response: any);
161-
// TODO remove optional signature (v11)
162-
abstract getHeader?(response: any, name: string);
163165
abstract setHeader(response: any, name: string, value: string);
164-
// TODO remove optional signature (v11)
165-
abstract appendHeader?(response: any, name: string, value: string);
166166
abstract registerParserMiddleware(prefix?: string, rawBody?: boolean);
167167
abstract enableCors(
168168
options: CorsOptions | CorsOptionsDelegate<TRequest>,

packages/core/router/router-explorer.ts

+4-10
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ import { MetadataScanner } from '../metadata-scanner';
3939
import { PipesConsumer, PipesContextCreator } from '../pipes';
4040
import { ExceptionsFilter } from './interfaces/exceptions-filter.interface';
4141
import { RoutePathMetadata } from './interfaces/route-path-metadata.interface';
42-
import { LegacyRouteConverter } from './legacy-route-converter';
4342
import { PathsExplorer } from './paths-explorer';
4443
import { REQUEST_CONTEXT_ID } from './request/request-constants';
4544
import { RouteParamsFactory } from './route-params-factory';
@@ -232,15 +231,10 @@ export class RouterExplorer {
232231
};
233232

234233
this.copyMetadataToCallback(targetCallback, routeHandler);
235-
try {
236-
const convertedPath = LegacyRouteConverter.tryConvert(path);
237-
routerMethodRef(convertedPath, routeHandler);
238-
} catch (e) {
239-
if (e instanceof TypeError) {
240-
LegacyRouteConverter.printError(path);
241-
}
242-
throw e;
243-
}
234+
const normalizedPath = router.normalizePath
235+
? router.normalizePath(path)
236+
: path;
237+
routerMethodRef(normalizedPath, routeHandler);
244238

245239
this.graphInspector.insertEntrypointDefinition<HttpEntrypointMetadata>(
246240
entrypointDefinition,

packages/platform-express/adapters/express-adapter.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -144,18 +144,30 @@ export class ExpressAdapter extends AbstractHttpAdapter<
144144
return response.headersSent;
145145
}
146146

147-
public getHeader?(response: any, name: string) {
147+
public getHeader(response: any, name: string) {
148148
return response.get(name);
149149
}
150150

151151
public setHeader(response: any, name: string, value: string) {
152152
return response.set(name, value);
153153
}
154154

155-
public appendHeader?(response: any, name: string, value: string) {
155+
public appendHeader(response: any, name: string, value: string) {
156156
return response.append(name, value);
157157
}
158158

159+
public normalizePath(path: string): string {
160+
try {
161+
const convertedPath = LegacyRouteConverter.tryConvert(path);
162+
return convertedPath;
163+
} catch (e) {
164+
if (e instanceof TypeError) {
165+
LegacyRouteConverter.printError(path);
166+
}
167+
throw e;
168+
}
169+
}
170+
159171
public listen(port: string | number, callback?: () => void): Server;
160172
public listen(
161173
port: string | number,

0 commit comments

Comments
 (0)