@adonisjs/http-server 6.8.2-6 → 6.8.2-8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -6
- package/build/factories/http_context.d.ts +10 -1
- package/build/factories/http_context.js +27 -0
- package/build/factories/http_server.d.ts +8 -0
- package/build/factories/http_server.js +26 -0
- package/build/factories/main.d.ts +0 -1
- package/build/factories/main.js +8 -0
- package/build/factories/qs_parser_factory.d.ts +10 -1
- package/build/factories/qs_parser_factory.js +18 -0
- package/build/factories/request.d.ts +10 -1
- package/build/factories/request.js +31 -0
- package/build/factories/response.d.ts +10 -1
- package/build/factories/response.js +34 -0
- package/build/factories/router.d.ts +10 -1
- package/build/factories/router.js +25 -0
- package/build/factories/server_factory.d.ts +10 -1
- package/build/factories/server_factory.js +34 -0
- package/build/index.d.ts +0 -1
- package/build/index.js +8 -0
- package/build/src/cookies/client.d.ts +25 -1
- package/build/src/cookies/client.js +42 -0
- package/build/src/cookies/drivers/encrypted.d.ts +12 -1
- package/build/src/cookies/drivers/encrypted.js +20 -0
- package/build/src/cookies/drivers/plain.d.ts +12 -1
- package/build/src/cookies/drivers/plain.js +20 -0
- package/build/src/cookies/drivers/signed.d.ts +12 -1
- package/build/src/cookies/drivers/signed.js +20 -0
- package/build/src/cookies/parser.d.ts +28 -1
- package/build/src/cookies/parser.js +98 -0
- package/build/src/cookies/serializer.d.ts +22 -1
- package/build/src/cookies/serializer.js +40 -0
- package/build/src/debug.d.ts +0 -1
- package/build/src/debug.js +8 -0
- package/build/src/define_config.d.ts +3 -1
- package/build/src/define_config.js +11 -0
- package/build/src/define_middleware.d.ts +5 -1
- package/build/src/define_middleware.js +18 -0
- package/build/src/exception_handler.d.ts +65 -1
- package/build/src/exception_handler.js +95 -0
- package/build/src/exceptions.d.ts +6 -1
- package/build/src/exceptions.js +11 -0
- package/build/src/helpers.d.ts +14 -1
- package/build/src/helpers.js +22 -0
- package/build/src/http_context/local_storage.d.ts +3 -1
- package/build/src/http_context/local_storage.js +25 -0
- package/build/src/http_context/main.d.ts +36 -1
- package/build/src/http_context/main.js +54 -0
- package/build/src/qs.d.ts +4 -1
- package/build/src/qs.js +12 -0
- package/build/src/redirect.d.ts +24 -1
- package/build/src/redirect.js +60 -0
- package/build/src/request.d.ts +466 -1
- package/build/src/request.js +542 -0
- package/build/src/response.d.ts +425 -2
- package/build/src/response.js +608 -7
- package/build/src/router/brisk.d.ts +22 -1
- package/build/src/router/brisk.js +42 -0
- package/build/src/router/executor.d.ts +4 -1
- package/build/src/router/executor.js +12 -0
- package/build/src/router/factories/use_return_value.d.ts +4 -1
- package/build/src/router/factories/use_return_value.js +16 -3
- package/build/src/router/group.d.ts +47 -1
- package/build/src/router/group.js +88 -0
- package/build/src/router/lookup_store/main.d.ts +32 -1
- package/build/src/router/lookup_store/main.js +49 -0
- package/build/src/router/lookup_store/route_finder.d.ts +13 -1
- package/build/src/router/lookup_store/route_finder.js +21 -0
- package/build/src/router/lookup_store/url_builder.d.ts +36 -1
- package/build/src/router/lookup_store/url_builder.js +97 -0
- package/build/src/router/main.d.ts +87 -1
- package/build/src/router/main.js +142 -0
- package/build/src/router/matchers.d.ts +13 -1
- package/build/src/router/matchers.js +21 -0
- package/build/src/router/parser.d.ts +3 -1
- package/build/src/router/parser.js +12 -0
- package/build/src/router/resource.d.ts +28 -1
- package/build/src/router/resource.js +90 -0
- package/build/src/router/route.d.ts +65 -1
- package/build/src/router/route.js +151 -2
- package/build/src/router/store.d.ts +54 -1
- package/build/src/router/store.js +107 -0
- package/build/src/server/factories/final_handler.d.ts +5 -1
- package/build/src/server/factories/final_handler.js +13 -0
- package/build/src/server/factories/middleware_handler.d.ts +3 -1
- package/build/src/server/factories/middleware_handler.js +11 -0
- package/build/src/server/factories/write_response.d.ts +4 -1
- package/build/src/server/factories/write_response.js +12 -0
- package/build/src/server/main.d.ts +48 -1
- package/build/src/server/main.js +128 -0
- package/build/src/types/base.d.ts +12 -1
- package/build/src/types/base.js +8 -0
- package/build/src/types/main.d.ts +0 -1
- package/build/src/types/main.js +8 -0
- package/build/src/types/middleware.d.ts +18 -1
- package/build/src/types/middleware.js +8 -0
- package/build/src/types/qs.d.ts +53 -1
- package/build/src/types/qs.js +8 -0
- package/build/src/types/request.d.ts +32 -1
- package/build/src/types/request.js +8 -0
- package/build/src/types/response.d.ts +27 -1
- package/build/src/types/response.js +8 -0
- package/build/src/types/route.d.ts +87 -1
- package/build/src/types/route.js +8 -0
- package/build/src/types/server.d.ts +35 -1
- package/build/src/types/server.js +8 -0
- package/package.json +44 -71
- package/build/factories/http_context.d.ts.map +0 -1
- package/build/factories/main.d.ts.map +0 -1
- package/build/factories/qs_parser_factory.d.ts.map +0 -1
- package/build/factories/request.d.ts.map +0 -1
- package/build/factories/response.d.ts.map +0 -1
- package/build/factories/router.d.ts.map +0 -1
- package/build/factories/server_factory.d.ts.map +0 -1
- package/build/index.d.ts.map +0 -1
- package/build/src/cookies/client.d.ts.map +0 -1
- package/build/src/cookies/drivers/encrypted.d.ts.map +0 -1
- package/build/src/cookies/drivers/plain.d.ts.map +0 -1
- package/build/src/cookies/drivers/signed.d.ts.map +0 -1
- package/build/src/cookies/parser.d.ts.map +0 -1
- package/build/src/cookies/serializer.d.ts.map +0 -1
- package/build/src/debug.d.ts.map +0 -1
- package/build/src/define_config.d.ts.map +0 -1
- package/build/src/define_middleware.d.ts.map +0 -1
- package/build/src/exception_handler.d.ts.map +0 -1
- package/build/src/exceptions.d.ts.map +0 -1
- package/build/src/helpers.d.ts.map +0 -1
- package/build/src/http_context/local_storage.d.ts.map +0 -1
- package/build/src/http_context/main.d.ts.map +0 -1
- package/build/src/qs.d.ts.map +0 -1
- package/build/src/redirect.d.ts.map +0 -1
- package/build/src/request.d.ts.map +0 -1
- package/build/src/response.d.ts.map +0 -1
- package/build/src/router/brisk.d.ts.map +0 -1
- package/build/src/router/executor.d.ts.map +0 -1
- package/build/src/router/factories/use_return_value.d.ts.map +0 -1
- package/build/src/router/group.d.ts.map +0 -1
- package/build/src/router/lookup_store/main.d.ts.map +0 -1
- package/build/src/router/lookup_store/route_finder.d.ts.map +0 -1
- package/build/src/router/lookup_store/url_builder.d.ts.map +0 -1
- package/build/src/router/main.d.ts.map +0 -1
- package/build/src/router/matchers.d.ts.map +0 -1
- package/build/src/router/parser.d.ts.map +0 -1
- package/build/src/router/resource.d.ts.map +0 -1
- package/build/src/router/route.d.ts.map +0 -1
- package/build/src/router/store.d.ts.map +0 -1
- package/build/src/server/factories/final_handler.d.ts.map +0 -1
- package/build/src/server/factories/middleware_handler.d.ts.map +0 -1
- package/build/src/server/factories/write_response.d.ts.map +0 -1
- package/build/src/server/main.d.ts.map +0 -1
- package/build/src/types/base.d.ts.map +0 -1
- package/build/src/types/main.d.ts.map +0 -1
- package/build/src/types/middleware.d.ts.map +0 -1
- package/build/src/types/qs.d.ts.map +0 -1
- package/build/src/types/request.d.ts.map +0 -1
- package/build/src/types/response.d.ts.map +0 -1
- package/build/src/types/route.d.ts.map +0 -1
- package/build/src/types/server.d.ts.map +0 -1
- package/factories/http_context.ts +0 -73
- package/factories/main.ts +0 -15
- package/factories/qs_parser_factory.ts +0 -54
- package/factories/request.ts +0 -101
- package/factories/response.ts +0 -106
- package/factories/router.ts +0 -61
- package/factories/server_factory.ts +0 -94
- package/index.ts +0 -23
- package/src/cookies/client.ts +0 -98
- package/src/cookies/drivers/encrypted.ts +0 -42
- package/src/cookies/drivers/plain.ts +0 -37
- package/src/cookies/drivers/signed.ts +0 -42
- package/src/cookies/parser.ts +0 -196
- package/src/cookies/serializer.ts +0 -98
- package/src/debug.ts +0 -11
- package/src/define_config.ts +0 -56
- package/src/define_middleware.ts +0 -61
- package/src/exception_handler.ts +0 -290
- package/src/exceptions.ts +0 -55
- package/src/helpers.ts +0 -108
- package/src/http_context/local_storage.ts +0 -50
- package/src/http_context/main.ts +0 -126
- package/src/qs.ts +0 -31
- package/src/redirect.ts +0 -181
- package/src/request.ts +0 -982
- package/src/response.ts +0 -1421
- package/src/router/brisk.ts +0 -113
- package/src/router/executor.ts +0 -36
- package/src/router/factories/use_return_value.ts +0 -26
- package/src/router/group.ts +0 -243
- package/src/router/lookup_store/main.ts +0 -102
- package/src/router/lookup_store/route_finder.ts +0 -60
- package/src/router/lookup_store/url_builder.ts +0 -250
- package/src/router/main.ts +0 -431
- package/src/router/matchers.ts +0 -40
- package/src/router/parser.ts +0 -20
- package/src/router/resource.ts +0 -277
- package/src/router/route.ts +0 -363
- package/src/router/store.ts +0 -239
- package/src/server/factories/final_handler.ts +0 -38
- package/src/server/factories/middleware_handler.ts +0 -23
- package/src/server/factories/write_response.ts +0 -26
- package/src/server/main.ts +0 -356
- package/src/types/base.ts +0 -30
- package/src/types/main.ts +0 -16
- package/src/types/middleware.ts +0 -59
- package/src/types/qs.ts +0 -85
- package/src/types/request.ts +0 -52
- package/src/types/response.ts +0 -57
- package/src/types/route.ts +0 -217
- package/src/types/server.ts +0 -92
|
@@ -1,28 +1,81 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @adonisjs/http-server
|
|
3
|
+
*
|
|
4
|
+
* (c) AdonisJS
|
|
5
|
+
*
|
|
6
|
+
* For the full copyright and license information, please view the LICENSE
|
|
7
|
+
* file that was distributed with this source code.
|
|
8
|
+
*/
|
|
1
9
|
import { RuntimeException } from '@poppinss/utils';
|
|
2
10
|
import { parseRoutePattern } from '../parser.js';
|
|
11
|
+
/**
|
|
12
|
+
* URL builder class is used to create URIs for pre-registered
|
|
13
|
+
* routes.
|
|
14
|
+
*
|
|
15
|
+
* ```ts
|
|
16
|
+
* const builder = new UrlBuilder(encryption, routeFinder)
|
|
17
|
+
*
|
|
18
|
+
* builder
|
|
19
|
+
* .qs({ sort: 'id' })
|
|
20
|
+
* .params([category.id])
|
|
21
|
+
* .make('categories.posts.index')
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
3
24
|
export class UrlBuilder {
|
|
25
|
+
/**
|
|
26
|
+
* Query string parser
|
|
27
|
+
*/
|
|
4
28
|
#qsParser;
|
|
29
|
+
/**
|
|
30
|
+
* The parameters to apply on the route
|
|
31
|
+
*/
|
|
5
32
|
#params = {};
|
|
33
|
+
/**
|
|
34
|
+
* Query string to append to the route
|
|
35
|
+
*/
|
|
6
36
|
#qs = {};
|
|
37
|
+
/**
|
|
38
|
+
* Should we perform the route lookup or just build the
|
|
39
|
+
* given pattern as it is.
|
|
40
|
+
*/
|
|
7
41
|
#shouldPerformLookup = true;
|
|
42
|
+
/**
|
|
43
|
+
* BaseURL to append to the constructored URL
|
|
44
|
+
*/
|
|
8
45
|
#baseUrl;
|
|
46
|
+
/**
|
|
47
|
+
* Encryption class for making signed URLs
|
|
48
|
+
*/
|
|
9
49
|
#encryption;
|
|
50
|
+
/**
|
|
51
|
+
* Route finder for finding route pattern
|
|
52
|
+
*/
|
|
10
53
|
#routeFinder;
|
|
11
54
|
constructor(encryption, routeFinder, qsParser) {
|
|
12
55
|
this.#qsParser = qsParser;
|
|
13
56
|
this.#encryption = encryption;
|
|
14
57
|
this.#routeFinder = routeFinder;
|
|
15
58
|
}
|
|
59
|
+
/**
|
|
60
|
+
* Raises exception when wildcard values array is missing or
|
|
61
|
+
* has length of zero.
|
|
62
|
+
*/
|
|
16
63
|
#ensureHasWildCardValues(pattern, values) {
|
|
17
64
|
if (!values || !Array.isArray(values) || !values.length) {
|
|
18
65
|
throw new RuntimeException(`Cannot make URL for "${pattern}" route. Invalid value provided for wildcard param`);
|
|
19
66
|
}
|
|
20
67
|
}
|
|
68
|
+
/*
|
|
69
|
+
* Raises exception when value is not defined
|
|
70
|
+
*/
|
|
21
71
|
#ensureHasParamValue(pattern, param, value) {
|
|
22
72
|
if (value === undefined || value === null) {
|
|
23
73
|
throw new RuntimeException(`Cannot make URL for "${pattern}" route. Missing value for "${param}" param`);
|
|
24
74
|
}
|
|
25
75
|
}
|
|
76
|
+
/**
|
|
77
|
+
* Processes the pattern against the params
|
|
78
|
+
*/
|
|
26
79
|
#processPattern(pattern) {
|
|
27
80
|
const uriSegments = [];
|
|
28
81
|
const paramsArray = Array.isArray(this.#params) ? this.#params : null;
|
|
@@ -30,6 +83,10 @@ export class UrlBuilder {
|
|
|
30
83
|
let paramsIndex = 0;
|
|
31
84
|
const tokens = parseRoutePattern(pattern);
|
|
32
85
|
for (const token of tokens) {
|
|
86
|
+
/**
|
|
87
|
+
* Expected wildcard param to be at the end always and hence
|
|
88
|
+
* we must break out from the loop
|
|
89
|
+
*/
|
|
33
90
|
if (token.type === 0) {
|
|
34
91
|
uriSegments.push(`${token.val}${token.end}`);
|
|
35
92
|
}
|
|
@@ -42,6 +99,9 @@ export class UrlBuilder {
|
|
|
42
99
|
else {
|
|
43
100
|
const paramName = token.val;
|
|
44
101
|
const value = paramsArray ? paramsArray[paramsIndex] : paramsObject[paramName];
|
|
102
|
+
/**
|
|
103
|
+
* Type = 1 means param is required
|
|
104
|
+
*/
|
|
45
105
|
if (token.type === 1) {
|
|
46
106
|
this.#ensureHasParamValue(pattern, paramName, value);
|
|
47
107
|
}
|
|
@@ -53,6 +113,9 @@ export class UrlBuilder {
|
|
|
53
113
|
}
|
|
54
114
|
return `/${uriSegments.join('/')}`;
|
|
55
115
|
}
|
|
116
|
+
/**
|
|
117
|
+
* Suffix the query string to the URL
|
|
118
|
+
*/
|
|
56
119
|
#suffixQueryString(url, qs) {
|
|
57
120
|
if (qs) {
|
|
58
121
|
const queryString = this.#qsParser.stringify(qs);
|
|
@@ -60,17 +123,30 @@ export class UrlBuilder {
|
|
|
60
123
|
}
|
|
61
124
|
return url;
|
|
62
125
|
}
|
|
126
|
+
/**
|
|
127
|
+
* Prefixes base URL to the uri string
|
|
128
|
+
*/
|
|
63
129
|
#prefixBaseUrl(uri) {
|
|
64
130
|
return this.#baseUrl ? `${this.#baseUrl}${uri}` : uri;
|
|
65
131
|
}
|
|
132
|
+
/**
|
|
133
|
+
* Prefix a custom base URL to the final URI
|
|
134
|
+
*/
|
|
66
135
|
prefixUrl(url) {
|
|
67
136
|
this.#baseUrl = url;
|
|
68
137
|
return this;
|
|
69
138
|
}
|
|
139
|
+
/**
|
|
140
|
+
* Disable route lookup. Calling this method considers
|
|
141
|
+
* the "identifier" as the route pattern
|
|
142
|
+
*/
|
|
70
143
|
disableRouteLookup() {
|
|
71
144
|
this.#shouldPerformLookup = false;
|
|
72
145
|
return this;
|
|
73
146
|
}
|
|
147
|
+
/**
|
|
148
|
+
* Append query string to the final URI
|
|
149
|
+
*/
|
|
74
150
|
qs(queryString) {
|
|
75
151
|
if (!queryString) {
|
|
76
152
|
return this;
|
|
@@ -78,6 +154,9 @@ export class UrlBuilder {
|
|
|
78
154
|
this.#qs = queryString;
|
|
79
155
|
return this;
|
|
80
156
|
}
|
|
157
|
+
/**
|
|
158
|
+
* Specify params to apply to the route pattern
|
|
159
|
+
*/
|
|
81
160
|
params(params) {
|
|
82
161
|
if (!params) {
|
|
83
162
|
return this;
|
|
@@ -85,6 +164,11 @@ export class UrlBuilder {
|
|
|
85
164
|
this.#params = params;
|
|
86
165
|
return this;
|
|
87
166
|
}
|
|
167
|
+
/**
|
|
168
|
+
* Generate URL for the given route identifier. The identifier can be the
|
|
169
|
+
* route name, controller.method name or the route pattern
|
|
170
|
+
* itself.
|
|
171
|
+
*/
|
|
88
172
|
make(identifier) {
|
|
89
173
|
let url;
|
|
90
174
|
if (this.#shouldPerformLookup) {
|
|
@@ -96,6 +180,11 @@ export class UrlBuilder {
|
|
|
96
180
|
}
|
|
97
181
|
return this.#suffixQueryString(this.#prefixBaseUrl(url), this.#qs);
|
|
98
182
|
}
|
|
183
|
+
/**
|
|
184
|
+
* Generate a signed URL for the given route identifier. The identifier can be the
|
|
185
|
+
* route name, controller.method name or the route pattern
|
|
186
|
+
* itself.
|
|
187
|
+
*/
|
|
99
188
|
makeSigned(identifier, options) {
|
|
100
189
|
let url;
|
|
101
190
|
if (this.#shouldPerformLookup) {
|
|
@@ -105,6 +194,14 @@ export class UrlBuilder {
|
|
|
105
194
|
else {
|
|
106
195
|
url = this.#processPattern(identifier);
|
|
107
196
|
}
|
|
197
|
+
/*
|
|
198
|
+
* Making the signature from the qualified url. We do not prefix the domain when
|
|
199
|
+
* making signature, since it just makes the signature big.
|
|
200
|
+
*
|
|
201
|
+
* There might be a case, when someone wants to generate signature for the same route
|
|
202
|
+
* on their 2 different domains, but we ignore that case for now and can consider
|
|
203
|
+
* it later (when someone asks for it)
|
|
204
|
+
*/
|
|
108
205
|
const signature = this.#encryption.verifier.sign(this.#suffixQueryString(url, this.#qs), options?.expiresIn, options?.purpose);
|
|
109
206
|
const qs = Object.assign({}, this.#qs, { signature });
|
|
110
207
|
return this.#suffixQueryString(this.#prefixBaseUrl(url), qs);
|
|
@@ -10,33 +10,119 @@ import { RouteMatchers as Matchers } from './matchers.js';
|
|
|
10
10
|
import type { Constructor, LazyImport } from '../types/base.js';
|
|
11
11
|
import type { MiddlewareAsClass, ParsedGlobalMiddleware } from '../types/middleware.js';
|
|
12
12
|
import type { RouteFn, MatchedRoute, RouteMatcher, RouteMatchers, MakeUrlOptions, MakeSignedUrlOptions, GetControllerHandlers } from '../types/route.js';
|
|
13
|
+
/**
|
|
14
|
+
* Router class exposes a unified API to register new routes, group them or
|
|
15
|
+
* create route resources.
|
|
16
|
+
*
|
|
17
|
+
* ```ts
|
|
18
|
+
* const router = new Router()
|
|
19
|
+
*
|
|
20
|
+
* router.get('/', async function () {
|
|
21
|
+
* // handle request
|
|
22
|
+
* })
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
13
25
|
export declare class Router extends LookupStore {
|
|
14
26
|
#private;
|
|
27
|
+
/**
|
|
28
|
+
* Collection of routes, including route resource and route
|
|
29
|
+
* group. To get a flat list of routes, call `router.toJSON()`
|
|
30
|
+
*/
|
|
15
31
|
routes: (Route | RouteResource | RouteGroup | BriskRoute)[];
|
|
32
|
+
/**
|
|
33
|
+
* A flag to know if routes for explicit domains have been registered.
|
|
34
|
+
* The boolean is computed after calling the "commit" method.
|
|
35
|
+
*/
|
|
16
36
|
usingDomains: boolean;
|
|
37
|
+
/**
|
|
38
|
+
* Shortcut methods for commonly used route matchers
|
|
39
|
+
*/
|
|
17
40
|
matchers: Matchers;
|
|
18
41
|
constructor(app: Application<any>, encryption: Encryption, qsParser: Qs);
|
|
42
|
+
/**
|
|
43
|
+
* Parses the route pattern
|
|
44
|
+
*/
|
|
19
45
|
parsePattern(pattern: string, matchers?: RouteMatchers): import("../types/route.js").MatchItRouteToken[];
|
|
46
|
+
/**
|
|
47
|
+
* Define an array of middleware to use on all the routes.
|
|
48
|
+
* Calling this method multiple times pushes to the
|
|
49
|
+
* existing list of middleware
|
|
50
|
+
*/
|
|
20
51
|
use(middleware: LazyImport<MiddlewareAsClass>[]): this;
|
|
52
|
+
/**
|
|
53
|
+
* Define a collection of named middleware. The defined collection is
|
|
54
|
+
* not registered anywhere, but instead converted in a new collection
|
|
55
|
+
* of functions you can apply on the routes, or router groups.
|
|
56
|
+
*/
|
|
21
57
|
named<NamedMiddleware extends Record<string, LazyImport<MiddlewareAsClass>>>(collection: NamedMiddleware): { [K in keyof NamedMiddleware]: <Args extends import("../types/middleware.js").GetMiddlewareArgs<import("../types/base.js").UnWrapLazyImport<NamedMiddleware[K]>>>(...args: Args) => {
|
|
22
58
|
name: K;
|
|
23
59
|
args: Args[0];
|
|
24
60
|
} & ParsedGlobalMiddleware; };
|
|
61
|
+
/**
|
|
62
|
+
* Add route for a given pattern and methods
|
|
63
|
+
*/
|
|
25
64
|
route<T extends Constructor<any>>(pattern: string, methods: string[], handler: string | RouteFn | [LazyImport<T> | T, GetControllerHandlers<T>?]): Route<T>;
|
|
65
|
+
/**
|
|
66
|
+
* Define a route that handles all common HTTP methods
|
|
67
|
+
*/
|
|
26
68
|
any<T extends Constructor<any>>(pattern: string, handler: string | RouteFn | [LazyImport<T> | T, GetControllerHandlers<T>?]): Route<T>;
|
|
69
|
+
/**
|
|
70
|
+
* Define `GET` route
|
|
71
|
+
*/
|
|
27
72
|
get<T extends Constructor<any>>(pattern: string, handler: string | RouteFn | [LazyImport<T> | T, GetControllerHandlers<T>?]): Route<T>;
|
|
73
|
+
/**
|
|
74
|
+
* Define `POST` route
|
|
75
|
+
*/
|
|
28
76
|
post<T extends Constructor<any>>(pattern: string, handler: string | RouteFn | [LazyImport<T> | T, GetControllerHandlers<T>?]): Route<T>;
|
|
77
|
+
/**
|
|
78
|
+
* Define `PUT` route
|
|
79
|
+
*/
|
|
29
80
|
put<T extends Constructor<any>>(pattern: string, handler: string | RouteFn | [LazyImport<T> | T, GetControllerHandlers<T>?]): Route<T>;
|
|
81
|
+
/**
|
|
82
|
+
* Define `PATCH` route
|
|
83
|
+
*/
|
|
30
84
|
patch<T extends Constructor<any>>(pattern: string, handler: string | RouteFn | [LazyImport<T> | T, GetControllerHandlers<T>?]): Route<T>;
|
|
85
|
+
/**
|
|
86
|
+
* Define `DELETE` route
|
|
87
|
+
*/
|
|
31
88
|
delete<T extends Constructor<any>>(pattern: string, handler: string | RouteFn | [LazyImport<T> | T, GetControllerHandlers<T>?]): Route<T>;
|
|
89
|
+
/**
|
|
90
|
+
* Creates a group of routes. A route group can apply transforms
|
|
91
|
+
* to routes in bulk
|
|
92
|
+
*/
|
|
32
93
|
group(callback: () => void): RouteGroup;
|
|
94
|
+
/**
|
|
95
|
+
* Registers a route resource with conventional set of routes
|
|
96
|
+
*/
|
|
33
97
|
resource(resource: string, controller: string | LazyImport<Constructor<any>> | Constructor<any>): RouteResource;
|
|
98
|
+
/**
|
|
99
|
+
* Register a route resource with shallow nested routes.
|
|
100
|
+
*/
|
|
34
101
|
shallowResource(resource: string, controller: string | LazyImport<Constructor<any>> | Constructor<any>): RouteResource;
|
|
102
|
+
/**
|
|
103
|
+
* Returns a brisk route instance for a given URL pattern
|
|
104
|
+
*/
|
|
35
105
|
on(pattern: string): BriskRoute;
|
|
106
|
+
/**
|
|
107
|
+
* Define matcher for a given param. The global params are applied
|
|
108
|
+
* on all the routes (unless overridden at the route level).
|
|
109
|
+
*/
|
|
36
110
|
where(param: string, matcher: RouteMatcher | string | RegExp): this;
|
|
111
|
+
/**
|
|
112
|
+
* Commit routes to the store. The router is freezed after the
|
|
113
|
+
* commit method is called.
|
|
114
|
+
*/
|
|
37
115
|
commit(): void;
|
|
116
|
+
/**
|
|
117
|
+
* Find route for a given URL, method and optionally domain
|
|
118
|
+
*/
|
|
38
119
|
match(url: string, method: string, hostname?: string | null): null | MatchedRoute;
|
|
120
|
+
/**
|
|
121
|
+
* Make URL to a pre-registered route
|
|
122
|
+
*/
|
|
39
123
|
makeUrl(routeIdentifier: string, params?: any[] | Record<string, any>, options?: MakeUrlOptions): string;
|
|
124
|
+
/**
|
|
125
|
+
* Makes a signed URL to a pre-registered route.
|
|
126
|
+
*/
|
|
40
127
|
makeSignedUrl(routeIdentifier: string, params?: any[] | Record<string, any>, options?: MakeSignedUrlOptions): string;
|
|
41
128
|
}
|
|
42
|
-
//# sourceMappingURL=main.d.ts.map
|
package/build/src/router/main.js
CHANGED
|
@@ -1,3 +1,11 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @adonisjs/http-server
|
|
3
|
+
*
|
|
4
|
+
* (c) AdonisJS
|
|
5
|
+
*
|
|
6
|
+
* For the full copyright and license information, please view the LICENSE
|
|
7
|
+
* file that was distributed with this source code.
|
|
8
|
+
*/
|
|
1
9
|
import is from '@sindresorhus/is';
|
|
2
10
|
import { moduleImporter } from '@adonisjs/fold';
|
|
3
11
|
import { RuntimeException } from '@poppinss/utils';
|
|
@@ -11,19 +19,62 @@ import { LookupStore } from './lookup_store/main.js';
|
|
|
11
19
|
import { RouteMatchers as Matchers } from './matchers.js';
|
|
12
20
|
import { defineNamedMiddleware } from '../define_middleware.js';
|
|
13
21
|
import { parseRoutePattern } from './parser.js';
|
|
22
|
+
/**
|
|
23
|
+
* Router class exposes a unified API to register new routes, group them or
|
|
24
|
+
* create route resources.
|
|
25
|
+
*
|
|
26
|
+
* ```ts
|
|
27
|
+
* const router = new Router()
|
|
28
|
+
*
|
|
29
|
+
* router.get('/', async function () {
|
|
30
|
+
* // handle request
|
|
31
|
+
* })
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
14
34
|
export class Router extends LookupStore {
|
|
35
|
+
/**
|
|
36
|
+
* Application is needed to resolve string based controller expressions
|
|
37
|
+
*/
|
|
15
38
|
#app;
|
|
39
|
+
/**
|
|
40
|
+
* Store with tokenized routes
|
|
41
|
+
*/
|
|
16
42
|
#store = new RoutesStore();
|
|
43
|
+
/**
|
|
44
|
+
* Global matchers to test route params against regular expressions.
|
|
45
|
+
*/
|
|
17
46
|
#globalMatchers = {};
|
|
47
|
+
/**
|
|
48
|
+
* Middleware store to be shared with the routes
|
|
49
|
+
*/
|
|
18
50
|
#middleware = [];
|
|
51
|
+
/**
|
|
52
|
+
* A boolean to tell the router that a group is in
|
|
53
|
+
* open state right now
|
|
54
|
+
*/
|
|
19
55
|
#openedGroups = [];
|
|
56
|
+
/**
|
|
57
|
+
* Collection of routes, including route resource and route
|
|
58
|
+
* group. To get a flat list of routes, call `router.toJSON()`
|
|
59
|
+
*/
|
|
20
60
|
routes = [];
|
|
61
|
+
/**
|
|
62
|
+
* A flag to know if routes for explicit domains have been registered.
|
|
63
|
+
* The boolean is computed after calling the "commit" method.
|
|
64
|
+
*/
|
|
21
65
|
usingDomains = false;
|
|
66
|
+
/**
|
|
67
|
+
* Shortcut methods for commonly used route matchers
|
|
68
|
+
*/
|
|
22
69
|
matchers = new Matchers();
|
|
23
70
|
constructor(app, encryption, qsParser) {
|
|
24
71
|
super(encryption, qsParser);
|
|
25
72
|
this.#app = app;
|
|
26
73
|
}
|
|
74
|
+
/**
|
|
75
|
+
* Push a give router entity to the list of routes or the
|
|
76
|
+
* recently opened group.
|
|
77
|
+
*/
|
|
27
78
|
#pushToRoutes(entity) {
|
|
28
79
|
const openedGroup = this.#openedGroups[this.#openedGroups.length - 1];
|
|
29
80
|
if (openedGroup) {
|
|
@@ -32,16 +83,32 @@ export class Router extends LookupStore {
|
|
|
32
83
|
}
|
|
33
84
|
this.routes.push(entity);
|
|
34
85
|
}
|
|
86
|
+
/**
|
|
87
|
+
* Parses the route pattern
|
|
88
|
+
*/
|
|
35
89
|
parsePattern(pattern, matchers) {
|
|
36
90
|
return parseRoutePattern(pattern, matchers);
|
|
37
91
|
}
|
|
92
|
+
/**
|
|
93
|
+
* Define an array of middleware to use on all the routes.
|
|
94
|
+
* Calling this method multiple times pushes to the
|
|
95
|
+
* existing list of middleware
|
|
96
|
+
*/
|
|
38
97
|
use(middleware) {
|
|
39
98
|
middleware.forEach((one) => this.#middleware.push(moduleImporter(one, 'handle').toHandleMethod()));
|
|
40
99
|
return this;
|
|
41
100
|
}
|
|
101
|
+
/**
|
|
102
|
+
* Define a collection of named middleware. The defined collection is
|
|
103
|
+
* not registered anywhere, but instead converted in a new collection
|
|
104
|
+
* of functions you can apply on the routes, or router groups.
|
|
105
|
+
*/
|
|
42
106
|
named(collection) {
|
|
43
107
|
return defineNamedMiddleware(collection);
|
|
44
108
|
}
|
|
109
|
+
/**
|
|
110
|
+
* Add route for a given pattern and methods
|
|
111
|
+
*/
|
|
45
112
|
route(pattern, methods, handler) {
|
|
46
113
|
const route = new Route(this.#app, this.#middleware, {
|
|
47
114
|
pattern,
|
|
@@ -52,32 +119,74 @@ export class Router extends LookupStore {
|
|
|
52
119
|
this.#pushToRoutes(route);
|
|
53
120
|
return route;
|
|
54
121
|
}
|
|
122
|
+
/**
|
|
123
|
+
* Define a route that handles all common HTTP methods
|
|
124
|
+
*/
|
|
55
125
|
any(pattern, handler) {
|
|
56
126
|
return this.route(pattern, ['HEAD', 'OPTIONS', 'GET', 'POST', 'PUT', 'PATCH', 'DELETE'], handler);
|
|
57
127
|
}
|
|
128
|
+
/**
|
|
129
|
+
* Define `GET` route
|
|
130
|
+
*/
|
|
58
131
|
get(pattern, handler) {
|
|
59
132
|
return this.route(pattern, ['GET', 'HEAD'], handler);
|
|
60
133
|
}
|
|
134
|
+
/**
|
|
135
|
+
* Define `POST` route
|
|
136
|
+
*/
|
|
61
137
|
post(pattern, handler) {
|
|
62
138
|
return this.route(pattern, ['POST'], handler);
|
|
63
139
|
}
|
|
140
|
+
/**
|
|
141
|
+
* Define `PUT` route
|
|
142
|
+
*/
|
|
64
143
|
put(pattern, handler) {
|
|
65
144
|
return this.route(pattern, ['PUT'], handler);
|
|
66
145
|
}
|
|
146
|
+
/**
|
|
147
|
+
* Define `PATCH` route
|
|
148
|
+
*/
|
|
67
149
|
patch(pattern, handler) {
|
|
68
150
|
return this.route(pattern, ['PATCH'], handler);
|
|
69
151
|
}
|
|
152
|
+
/**
|
|
153
|
+
* Define `DELETE` route
|
|
154
|
+
*/
|
|
70
155
|
delete(pattern, handler) {
|
|
71
156
|
return this.route(pattern, ['DELETE'], handler);
|
|
72
157
|
}
|
|
158
|
+
/**
|
|
159
|
+
* Creates a group of routes. A route group can apply transforms
|
|
160
|
+
* to routes in bulk
|
|
161
|
+
*/
|
|
73
162
|
group(callback) {
|
|
163
|
+
/*
|
|
164
|
+
* Create a new group with empty set of routes
|
|
165
|
+
*/
|
|
74
166
|
const group = new RouteGroup([]);
|
|
167
|
+
/**
|
|
168
|
+
* Track group
|
|
169
|
+
*/
|
|
75
170
|
this.#pushToRoutes(group);
|
|
171
|
+
/*
|
|
172
|
+
* Track the group, so that the upcoming calls inside the callback
|
|
173
|
+
* can use this group
|
|
174
|
+
*/
|
|
76
175
|
this.#openedGroups.push(group);
|
|
176
|
+
/*
|
|
177
|
+
* Execute the callback. Now all registered routes will be
|
|
178
|
+
* collected seperately from the `routes` array
|
|
179
|
+
*/
|
|
77
180
|
callback();
|
|
181
|
+
/*
|
|
182
|
+
* Now the callback is over, get rid of the opened group
|
|
183
|
+
*/
|
|
78
184
|
this.#openedGroups.pop();
|
|
79
185
|
return group;
|
|
80
186
|
}
|
|
187
|
+
/**
|
|
188
|
+
* Registers a route resource with conventional set of routes
|
|
189
|
+
*/
|
|
81
190
|
resource(resource, controller) {
|
|
82
191
|
const resourceInstance = new RouteResource(this.#app, this.#middleware, {
|
|
83
192
|
resource,
|
|
@@ -88,6 +197,9 @@ export class Router extends LookupStore {
|
|
|
88
197
|
this.#pushToRoutes(resourceInstance);
|
|
89
198
|
return resourceInstance;
|
|
90
199
|
}
|
|
200
|
+
/**
|
|
201
|
+
* Register a route resource with shallow nested routes.
|
|
202
|
+
*/
|
|
91
203
|
shallowResource(resource, controller) {
|
|
92
204
|
const resourceInstance = new RouteResource(this.#app, this.#middleware, {
|
|
93
205
|
resource,
|
|
@@ -98,6 +210,9 @@ export class Router extends LookupStore {
|
|
|
98
210
|
this.#pushToRoutes(resourceInstance);
|
|
99
211
|
return resourceInstance;
|
|
100
212
|
}
|
|
213
|
+
/**
|
|
214
|
+
* Returns a brisk route instance for a given URL pattern
|
|
215
|
+
*/
|
|
101
216
|
on(pattern) {
|
|
102
217
|
const briskRoute = new BriskRoute(this.#app, this.#middleware, {
|
|
103
218
|
pattern,
|
|
@@ -106,6 +221,10 @@ export class Router extends LookupStore {
|
|
|
106
221
|
this.#pushToRoutes(briskRoute);
|
|
107
222
|
return briskRoute;
|
|
108
223
|
}
|
|
224
|
+
/**
|
|
225
|
+
* Define matcher for a given param. The global params are applied
|
|
226
|
+
* on all the routes (unless overridden at the route level).
|
|
227
|
+
*/
|
|
109
228
|
where(param, matcher) {
|
|
110
229
|
if (typeof matcher === 'string') {
|
|
111
230
|
this.#globalMatchers[param] = { match: new RegExp(matcher) };
|
|
@@ -118,6 +237,10 @@ export class Router extends LookupStore {
|
|
|
118
237
|
}
|
|
119
238
|
return this;
|
|
120
239
|
}
|
|
240
|
+
/**
|
|
241
|
+
* Commit routes to the store. The router is freezed after the
|
|
242
|
+
* commit method is called.
|
|
243
|
+
*/
|
|
121
244
|
commit() {
|
|
122
245
|
const routeNamesByDomain = new Map();
|
|
123
246
|
toRoutesJSON(this.routes).forEach((route) => {
|
|
@@ -125,12 +248,22 @@ export class Router extends LookupStore {
|
|
|
125
248
|
routeNamesByDomain.set(route.domain, new Set());
|
|
126
249
|
}
|
|
127
250
|
const routeNames = routeNamesByDomain.get(route.domain);
|
|
251
|
+
/*
|
|
252
|
+
* Raise error when route name is already in use. Route names have to be unique
|
|
253
|
+
* to ensure that only one route is returned during lookup.
|
|
254
|
+
*/
|
|
128
255
|
if (route.name && routeNames.has(route.name)) {
|
|
129
256
|
throw new RuntimeException(`Route with duplicate name found. A route with name "${route.name}" already exists`);
|
|
130
257
|
}
|
|
258
|
+
/*
|
|
259
|
+
* If route has a unique, then track the name for checking duplicates
|
|
260
|
+
*/
|
|
131
261
|
if (route.name) {
|
|
132
262
|
routeNames.add(route.name);
|
|
133
263
|
}
|
|
264
|
+
/**
|
|
265
|
+
* Register the route with the lookup store
|
|
266
|
+
*/
|
|
134
267
|
this.register(route);
|
|
135
268
|
this.#store.add(route);
|
|
136
269
|
});
|
|
@@ -140,6 +273,9 @@ export class Router extends LookupStore {
|
|
|
140
273
|
this.#globalMatchers = {};
|
|
141
274
|
this.#middleware = [];
|
|
142
275
|
}
|
|
276
|
+
/**
|
|
277
|
+
* Find route for a given URL, method and optionally domain
|
|
278
|
+
*/
|
|
143
279
|
match(url, method, hostname) {
|
|
144
280
|
const matchingDomain = this.#store.matchDomain(hostname);
|
|
145
281
|
return matchingDomain.length
|
|
@@ -149,6 +285,9 @@ export class Router extends LookupStore {
|
|
|
149
285
|
})
|
|
150
286
|
: this.#store.match(url, method);
|
|
151
287
|
}
|
|
288
|
+
/**
|
|
289
|
+
* Make URL to a pre-registered route
|
|
290
|
+
*/
|
|
152
291
|
makeUrl(routeIdentifier, params, options) {
|
|
153
292
|
const normalizedOptions = Object.assign({}, options);
|
|
154
293
|
const builder = normalizedOptions.domain
|
|
@@ -160,6 +299,9 @@ export class Router extends LookupStore {
|
|
|
160
299
|
normalizedOptions.disableRouteLookup && builder.disableRouteLookup();
|
|
161
300
|
return builder.make(routeIdentifier);
|
|
162
301
|
}
|
|
302
|
+
/**
|
|
303
|
+
* Makes a signed URL to a pre-registered route.
|
|
304
|
+
*/
|
|
163
305
|
makeSignedUrl(routeIdentifier, params, options) {
|
|
164
306
|
const normalizedOptions = Object.assign({}, options);
|
|
165
307
|
const builder = normalizedOptions.domain
|
|
@@ -1,15 +1,27 @@
|
|
|
1
1
|
import Macroable from '@poppinss/macroable';
|
|
2
|
+
/**
|
|
3
|
+
* Shortcut methods for commonly used route matchers
|
|
4
|
+
*/
|
|
2
5
|
export declare class RouteMatchers extends Macroable {
|
|
6
|
+
/**
|
|
7
|
+
* Enforce value to be a number and also casts it to number data
|
|
8
|
+
* type
|
|
9
|
+
*/
|
|
3
10
|
number(): {
|
|
4
11
|
match: RegExp;
|
|
5
12
|
cast: (value: string) => number;
|
|
6
13
|
};
|
|
14
|
+
/**
|
|
15
|
+
* Enforce value to be formatted as uuid
|
|
16
|
+
*/
|
|
7
17
|
uuid(): {
|
|
8
18
|
match: RegExp;
|
|
9
19
|
cast: (value: string) => string;
|
|
10
20
|
};
|
|
21
|
+
/**
|
|
22
|
+
* Enforce value to be formatted as slug
|
|
23
|
+
*/
|
|
11
24
|
slug(): {
|
|
12
25
|
match: RegExp;
|
|
13
26
|
};
|
|
14
27
|
}
|
|
15
|
-
//# sourceMappingURL=matchers.d.ts.map
|
|
@@ -1,14 +1,35 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @adonisjs/http-server
|
|
3
|
+
*
|
|
4
|
+
* (c) AdonisJS
|
|
5
|
+
*
|
|
6
|
+
* For the full copyright and license information, please view the LICENSE
|
|
7
|
+
* file that was distributed with this source code.
|
|
8
|
+
*/
|
|
1
9
|
import Macroable from '@poppinss/macroable';
|
|
10
|
+
/**
|
|
11
|
+
* Shortcut methods for commonly used route matchers
|
|
12
|
+
*/
|
|
2
13
|
export class RouteMatchers extends Macroable {
|
|
14
|
+
/**
|
|
15
|
+
* Enforce value to be a number and also casts it to number data
|
|
16
|
+
* type
|
|
17
|
+
*/
|
|
3
18
|
number() {
|
|
4
19
|
return { match: /^[0-9]+$/, cast: (value) => Number(value) };
|
|
5
20
|
}
|
|
21
|
+
/**
|
|
22
|
+
* Enforce value to be formatted as uuid
|
|
23
|
+
*/
|
|
6
24
|
uuid() {
|
|
7
25
|
return {
|
|
8
26
|
match: /^[0-9a-zA-F]{8}-[0-9a-zA-F]{4}-[0-9a-zA-F]{4}-[0-9a-zA-F]{4}-[0-9a-zA-F]{12}$/,
|
|
9
27
|
cast: (value) => value.toLowerCase(),
|
|
10
28
|
};
|
|
11
29
|
}
|
|
30
|
+
/**
|
|
31
|
+
* Enforce value to be formatted as slug
|
|
32
|
+
*/
|
|
12
33
|
slug() {
|
|
13
34
|
return { match: /^[^\s-_](?!.*?[-_]{2,})([a-z0-9-\\]{1,})[^\s]*[^-_\s]$/ };
|
|
14
35
|
}
|
|
@@ -1,4 +1,16 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @adonisjs/http-server
|
|
3
|
+
*
|
|
4
|
+
* (c) AdonisJS
|
|
5
|
+
*
|
|
6
|
+
* For the full copyright and license information, please view the LICENSE
|
|
7
|
+
* file that was distributed with this source code.
|
|
8
|
+
*/
|
|
9
|
+
// @ts-expect-error
|
|
1
10
|
import matchit from '@poppinss/matchit';
|
|
11
|
+
/**
|
|
12
|
+
* Parses the route pattern
|
|
13
|
+
*/
|
|
2
14
|
export function parseRoutePattern(pattern, matchers) {
|
|
3
15
|
const tokens = matchit.parse(pattern, matchers);
|
|
4
16
|
return tokens;
|