-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathhttp-route-path-match.ts
243 lines (215 loc) · 8.34 KB
/
http-route-path-match.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import { CfnGatewayRoute, CfnRoute } from './appmesh.generated';
// keep this import separate from other imports to reduce chance for merge conflicts with v2-main
// eslint-disable-next-line no-duplicate-imports, import/order
import { Construct } from '@aws-cdk/core';
/**
* The type returned from the `bind()` method in {@link HttpRoutePathMatch}.
*/
export interface HttpRoutePathMatchConfig {
/**
* Route configuration for matching on the complete URL path of the request.
*
* @default - no matching will be performed on the complete URL path
*/
readonly wholePathMatch?: CfnRoute.HttpPathMatchProperty;
/**
* Route configuration for matching on the prefix of the URL path of the request.
*
* @default - no matching will be performed on the prefix of the URL path
*/
readonly prefixPathMatch?: string;
}
/**
* Defines HTTP route matching based on the URL path of the request.
*/
export abstract class HttpRoutePathMatch {
/**
* The value of the path must match the specified value exactly.
* The provided `path` must start with the '/' character.
*
* @param path the exact path to match on
*/
public static exactly(path: string): HttpRoutePathMatch {
return new HttpRouteWholePathMatch({ exact: path });
}
/**
* The value of the path must match the specified regex.
*
* @param regex the regex used to match the path
*/
public static regex(regex: string): HttpRoutePathMatch {
return new HttpRouteWholePathMatch({ regex: regex });
}
/**
* The value of the path must match the specified prefix.
*
* @param prefix the value to use to match the beginning of the path part of the URL of the request.
* It must start with the '/' character. If provided as "/", matches all requests.
* For example, if your virtual service name is "my-service.local"
* and you want the route to match requests to "my-service.local/metrics", your prefix should be "/metrics".
*/
public static startsWith(prefix: string): HttpRoutePathMatch {
return new HttpRoutePrefixPathMatch(prefix);
}
/**
* Returns the route path match configuration.
*/
public abstract bind(scope: Construct): HttpRoutePathMatchConfig;
}
class HttpRoutePrefixPathMatch extends HttpRoutePathMatch {
constructor(private readonly prefix: string) {
super();
if (prefix && prefix[0] !== '/') {
throw new Error(`Prefix Path for the match must start with \'/\', got: ${prefix}`);
}
}
bind(_scope: Construct): HttpRoutePathMatchConfig {
return {
prefixPathMatch: this.prefix,
};
}
}
class HttpRouteWholePathMatch extends HttpRoutePathMatch {
constructor(private readonly match: CfnRoute.HttpPathMatchProperty) {
super();
if (match.exact && match.exact[0] !== '/') {
throw new Error(`Exact Path for the match must start with \'/\', got: ${match.exact}`);
}
}
bind(_scope: Construct): HttpRoutePathMatchConfig {
return {
wholePathMatch: this.match,
};
}
}
/**
* The type returned from the `bind()` method in {@link HttpGatewayRoutePathMatch}.
*/
export interface HttpGatewayRoutePathMatchConfig {
/**
* Gateway route configuration for matching on the complete URL path of the request.
*
* @default - no matching will be performed on the complete URL path
*/
readonly wholePathMatch?: CfnGatewayRoute.HttpPathMatchProperty;
/**
* Gateway route configuration for matching on the prefix of the URL path of the request.
*
* @default - no matching will be performed on the prefix of the URL path
*/
readonly prefixPathMatch?: string;
/**
* Gateway route configuration for rewriting the complete URL path of the request..
*
* @default - no rewrite will be performed on the request's complete URL path
*/
readonly wholePathRewrite?: CfnGatewayRoute.HttpGatewayRoutePathRewriteProperty;
/**
* Gateway route configuration for rewriting the prefix of the URL path of the request.
*
* @default - rewrites the request's URL path to '/'
*/
readonly prefixPathRewrite?: CfnGatewayRoute.HttpGatewayRoutePrefixRewriteProperty;
}
/**
* Defines HTTP gateway route matching based on the URL path of the request.
*/
export abstract class HttpGatewayRoutePathMatch {
/**
* The value of the path must match the specified prefix.
*
* @param prefix the value to use to match the beginning of the path part of the URL of the request.
* It must start with the '/' character.
* When `rewriteTo` is provided, it must also end with the '/' character.
* If provided as "/", matches all requests.
* For example, if your virtual service name is "my-service.local"
* and you want the route to match requests to "my-service.local/metrics", your prefix should be "/metrics".
* @param rewriteTo Specify either disabling automatic rewrite or rewriting to specified prefix path.
* To disable automatic rewrite, provide `''`.
* As a default, request's URL path is automatically rewritten to '/'.
*/
public static startsWith(prefix: string, rewriteTo?: string): HttpGatewayRoutePathMatch {
return new HttpGatewayRoutePrefixPathMatch(prefix, rewriteTo);
}
/**
* The value of the path must match the specified value exactly.
* The provided `path` must start with the '/' character.
*
* @param path the exact path to match on
* @param rewriteTo the value to substitute for the matched part of the path of the gateway request URL
* As a default, retains original request's URL path.
*/
public static exactly(path: string, rewriteTo?: string): HttpGatewayRoutePathMatch {
return new HttpGatewayRouteWholePathMatch({ exact: path }, rewriteTo);
}
/**
* The value of the path must match the specified regex.
*
* @param regex the regex used to match the path
* @param rewriteTo the value to substitute for the matched part of the path of the gateway request URL
* As a default, retains original request's URL path.
*/
public static regex(regex: string, rewriteTo?: string): HttpGatewayRoutePathMatch {
return new HttpGatewayRouteWholePathMatch({ regex }, rewriteTo);
}
/**
* Returns the gateway route path match configuration.
*/
public abstract bind(scope: Construct): HttpGatewayRoutePathMatchConfig;
}
class HttpGatewayRoutePrefixPathMatch extends HttpGatewayRoutePathMatch {
constructor(
private readonly prefixPathMatch: string,
private readonly rewriteTo?: string,
) {
super();
if (prefixPathMatch[0] !== '/') {
throw new Error('Prefix path for the match must start with \'/\', '
+ `got: ${prefixPathMatch}`);
}
if (rewriteTo) {
if (prefixPathMatch[prefixPathMatch.length - 1] !== '/') {
throw new Error('When prefix path for the rewrite is specified, prefix path for the match must end with \'/\', '
+ `got: ${prefixPathMatch}`);
}
if (rewriteTo[0] !== '/' || rewriteTo[rewriteTo.length - 1] !== '/') {
throw new Error('Prefix path for the rewrite must start and end with \'/\', '
+ `got: ${rewriteTo}`);
}
}
}
bind(_scope: Construct): HttpGatewayRoutePathMatchConfig {
return {
prefixPathMatch: this.prefixPathMatch,
prefixPathRewrite: this.rewriteTo === undefined
? undefined
: {
defaultPrefix: this.rewriteTo === '' ? 'DISABLED' : undefined,
value: this.rewriteTo === '' ? undefined : this.rewriteTo,
},
};
}
}
class HttpGatewayRouteWholePathMatch extends HttpGatewayRoutePathMatch {
constructor(
private readonly wholePathMatch: CfnGatewayRoute.HttpPathMatchProperty,
private readonly exactPathRewrite?: string | undefined,
) {
super();
if (wholePathMatch.exact && wholePathMatch.exact[0] !== '/') {
throw new Error(`Exact Path for the match must start with \'/\', got: ${ wholePathMatch.exact }`);
}
if (exactPathRewrite === '') {
throw new Error('Exact Path for the rewrite cannot be empty. Unlike startsWith() method, no automatic rewrite on whole path match');
}
if (exactPathRewrite && exactPathRewrite[0] !== '/') {
throw new Error(`Exact Path for the rewrite must start with \'/\', got: ${ exactPathRewrite }`);
}
}
bind(_scope: Construct): HttpGatewayRoutePathMatchConfig {
return {
wholePathMatch: this.wholePathMatch,
wholePathRewrite: this.exactPathRewrite === undefined ? undefined : { exact: this.exactPathRewrite },
};
}
}