-
-
Notifications
You must be signed in to change notification settings - Fork 7.8k
/
Copy pathutils.ts
135 lines (127 loc) · 3.85 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import { RequestMethod } from '@nestjs/common';
import { HttpServer, RouteInfo, Type } from '@nestjs/common/interfaces';
import {
addLeadingSlash,
isFunction,
isString,
} from '@nestjs/common/utils/shared.utils';
import { iterate } from 'iterare';
import { pathToRegexp } from 'path-to-regexp';
import { uid } from 'uid';
import { ExcludeRouteMetadata } from '../router/interfaces/exclude-route-metadata.interface';
import { LegacyRouteConverter } from '../router/legacy-route-converter';
import { isRouteExcluded } from '../router/utils';
export const mapToExcludeRoute = (
routes: (string | RouteInfo)[],
): ExcludeRouteMetadata[] => {
return routes.map(route => {
const originalPath = isString(route) ? route : route.path;
const path = LegacyRouteConverter.tryConvert(originalPath);
try {
if (isString(route)) {
return {
path,
requestMethod: RequestMethod.ALL,
pathRegex: pathToRegexp(addLeadingSlash(path)).regexp,
};
}
return {
path,
requestMethod: route.method,
pathRegex: pathToRegexp(addLeadingSlash(path)).regexp,
};
} catch (e) {
if (e instanceof TypeError) {
LegacyRouteConverter.printError(originalPath);
}
throw e;
}
});
};
export const filterMiddleware = <T extends Function | Type<any> = any>(
middleware: T[],
routes: RouteInfo[],
httpAdapter: HttpServer,
) => {
const excludedRoutes = mapToExcludeRoute(routes);
return iterate([])
.concat(middleware)
.filter(isFunction)
.map((item: T) => mapToClass(item, excludedRoutes, httpAdapter))
.toArray();
};
export const mapToClass = <T extends Function | Type<any>>(
middleware: T,
excludedRoutes: ExcludeRouteMetadata[],
httpAdapter: HttpServer,
) => {
if (isMiddlewareClass(middleware)) {
if (excludedRoutes.length <= 0) {
return middleware;
}
const MiddlewareHost = class extends middleware {
use(...params: unknown[]) {
const [req, _, next] = params as [Record<string, any>, any, Function];
const isExcluded = isMiddlewareRouteExcluded(
req,
excludedRoutes,
httpAdapter,
);
if (isExcluded) {
return next();
}
return super.use(...params);
}
};
return assignToken(MiddlewareHost, middleware.name);
}
return assignToken(
class {
use = (...params: unknown[]) => {
const [req, _, next] = params as [Record<string, any>, any, Function];
const isExcluded = isMiddlewareRouteExcluded(
req,
excludedRoutes,
httpAdapter,
);
if (isExcluded) {
return next();
}
return (middleware as Function)(...params);
};
},
);
};
export function isMiddlewareClass(middleware: any): middleware is Type<any> {
const middlewareStr = middleware.toString();
if (middlewareStr.substring(0, 5) === 'class') {
return true;
}
const middlewareArr = middlewareStr.split(' ');
return (
middlewareArr[0] === 'function' &&
/[A-Z]/.test(middlewareArr[1]?.[0]) &&
isFunction(middleware.prototype?.use)
);
}
export function assignToken(metatype: Type<any>, token = uid(21)): Type<any> {
Object.defineProperty(metatype, 'name', { value: token });
return metatype;
}
export function isMiddlewareRouteExcluded(
req: Record<string, any>,
excludedRoutes: ExcludeRouteMetadata[],
httpAdapter: HttpServer,
): boolean {
if (excludedRoutes.length <= 0) {
return false;
}
const reqMethod = httpAdapter.getRequestMethod!(req);
const originalUrl = httpAdapter.getRequestUrl!(req);
const queryParamsIndex = originalUrl ? originalUrl.indexOf('?') : -1;
const pathname =
queryParamsIndex >= 0
? originalUrl.slice(0, queryParamsIndex)
: originalUrl;
return isRouteExcluded(excludedRoutes, pathname, RequestMethod[reqMethod]);
}