@adonisjs/http-server 7.0.0-1 → 7.0.0-3
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/build/chunk-NC6OWANS.js +4437 -0
- package/build/chunk-NC6OWANS.js.map +1 -0
- package/build/factories/http_server.d.ts +1 -0
- package/build/factories/main.js +332 -14
- package/build/factories/main.js.map +1 -0
- package/build/index.js +309 -22
- package/build/index.js.map +1 -0
- package/build/src/define_middleware.d.ts +2 -1
- package/build/src/router/lookup_store/main.d.ts +1 -3
- package/build/src/router/lookup_store/route_finder.d.ts +5 -1
- package/build/src/router/main.d.ts +5 -4
- package/build/src/router/resource.d.ts +19 -7
- package/build/src/types/main.js +1 -15
- package/build/src/types/main.js.map +1 -0
- package/build/src/types/middleware.d.ts +3 -1
- package/package.json +60 -59
- package/build/factories/http_context.js +0 -51
- package/build/factories/http_server.js +0 -26
- package/build/factories/qs_parser_factory.js +0 -44
- package/build/factories/request.js +0 -73
- package/build/factories/response.js +0 -77
- package/build/factories/router.js +0 -45
- package/build/factories/server_factory.js +0 -65
- package/build/src/cookies/client.js +0 -84
- package/build/src/cookies/drivers/encrypted.js +0 -36
- package/build/src/cookies/drivers/plain.js +0 -33
- package/build/src/cookies/drivers/signed.js +0 -36
- package/build/src/cookies/parser.js +0 -167
- package/build/src/cookies/serializer.js +0 -79
- package/build/src/debug.js +0 -10
- package/build/src/define_config.js +0 -68
- package/build/src/define_middleware.js +0 -35
- package/build/src/exception_handler.js +0 -306
- package/build/src/exceptions.js +0 -38
- package/build/src/helpers.js +0 -105
- package/build/src/http_context/local_storage.js +0 -39
- package/build/src/http_context/main.js +0 -105
- package/build/src/qs.js +0 -25
- package/build/src/redirect.js +0 -140
- package/build/src/request.js +0 -865
- package/build/src/response.js +0 -1208
- package/build/src/router/brisk.js +0 -85
- package/build/src/router/executor.js +0 -30
- package/build/src/router/factories/use_return_value.js +0 -22
- package/build/src/router/group.js +0 -207
- package/build/src/router/lookup_store/main.js +0 -86
- package/build/src/router/lookup_store/route_finder.js +0 -49
- package/build/src/router/lookup_store/url_builder.js +0 -209
- package/build/src/router/main.js +0 -316
- package/build/src/router/matchers.js +0 -36
- package/build/src/router/parser.js +0 -17
- package/build/src/router/resource.js +0 -216
- package/build/src/router/route.js +0 -293
- package/build/src/router/store.js +0 -195
- package/build/src/server/factories/final_handler.js +0 -30
- package/build/src/server/factories/middleware_handler.js +0 -16
- package/build/src/server/factories/write_response.js +0 -24
- package/build/src/server/main.js +0 -292
- package/build/src/types/base.js +0 -9
- package/build/src/types/middleware.js +0 -9
- package/build/src/types/qs.js +0 -9
- package/build/src/types/request.js +0 -9
- package/build/src/types/response.js +0 -9
- package/build/src/types/route.js +0 -9
- package/build/src/types/server.js +0 -9
|
@@ -1,85 +0,0 @@
|
|
|
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
|
-
import Macroable from '@poppinss/macroable';
|
|
10
|
-
import { Route } from './route.js';
|
|
11
|
-
/**
|
|
12
|
-
* Brisk routes exposes the API to configure the route handler by chaining
|
|
13
|
-
* one of the pre-defined methods.
|
|
14
|
-
*
|
|
15
|
-
* For example: Instead of defining the redirect logic as a callback, one can
|
|
16
|
-
* chain the `.redirect` method.
|
|
17
|
-
*
|
|
18
|
-
* Brisk routes are always registered under the `GET` HTTP method.
|
|
19
|
-
*/
|
|
20
|
-
export class BriskRoute extends Macroable {
|
|
21
|
-
/**
|
|
22
|
-
* Route pattern
|
|
23
|
-
*/
|
|
24
|
-
#pattern;
|
|
25
|
-
/**
|
|
26
|
-
* Matchers inherited from the router
|
|
27
|
-
*/
|
|
28
|
-
#globalMatchers;
|
|
29
|
-
/**
|
|
30
|
-
* Reference to the AdonisJS application
|
|
31
|
-
*/
|
|
32
|
-
#app;
|
|
33
|
-
/**
|
|
34
|
-
* Middleware registered on the router
|
|
35
|
-
*/
|
|
36
|
-
#routerMiddleware;
|
|
37
|
-
/**
|
|
38
|
-
* Reference to route instance. Set after `setHandler` is called
|
|
39
|
-
*/
|
|
40
|
-
route = null;
|
|
41
|
-
constructor(app, routerMiddleware, options) {
|
|
42
|
-
super();
|
|
43
|
-
this.#app = app;
|
|
44
|
-
this.#routerMiddleware = routerMiddleware;
|
|
45
|
-
this.#pattern = options.pattern;
|
|
46
|
-
this.#globalMatchers = options.globalMatchers;
|
|
47
|
-
}
|
|
48
|
-
/**
|
|
49
|
-
* Set handler for the brisk route
|
|
50
|
-
*/
|
|
51
|
-
setHandler(handler) {
|
|
52
|
-
this.route = new Route(this.#app, this.#routerMiddleware, {
|
|
53
|
-
pattern: this.#pattern,
|
|
54
|
-
globalMatchers: this.#globalMatchers,
|
|
55
|
-
methods: ['GET', 'HEAD'],
|
|
56
|
-
handler: handler,
|
|
57
|
-
});
|
|
58
|
-
return this.route;
|
|
59
|
-
}
|
|
60
|
-
/**
|
|
61
|
-
* Redirects to a given route. Params from the original request will
|
|
62
|
-
* be used when no custom params are defined.
|
|
63
|
-
*/
|
|
64
|
-
redirect(identifier, params, options) {
|
|
65
|
-
return this.setHandler(async function redirectsToRoute(ctx) {
|
|
66
|
-
const redirector = ctx.response.redirect();
|
|
67
|
-
if (options?.status) {
|
|
68
|
-
redirector.status(options.status);
|
|
69
|
-
}
|
|
70
|
-
return redirector.toRoute(identifier, params || ctx.params, options);
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
/**
|
|
74
|
-
* Redirect request to a fixed URL
|
|
75
|
-
*/
|
|
76
|
-
redirectToPath(url, options) {
|
|
77
|
-
return this.setHandler(async function redirectsToPath(ctx) {
|
|
78
|
-
const redirector = ctx.response.redirect();
|
|
79
|
-
if (options?.status) {
|
|
80
|
-
redirector.status(options.status);
|
|
81
|
-
}
|
|
82
|
-
return redirector.toPath(url);
|
|
83
|
-
});
|
|
84
|
-
}
|
|
85
|
-
}
|
|
@@ -1,30 +0,0 @@
|
|
|
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
|
-
import { useReturnValue } from './factories/use_return_value.js';
|
|
10
|
-
/**
|
|
11
|
-
* Executor to execute the route middleware pipeline the route
|
|
12
|
-
* handler
|
|
13
|
-
*/
|
|
14
|
-
export function execute(route, resolver, ctx, errorResponder) {
|
|
15
|
-
return route.middleware
|
|
16
|
-
.runner()
|
|
17
|
-
.errorHandler((error) => errorResponder(error, ctx))
|
|
18
|
-
.finalHandler(async () => {
|
|
19
|
-
if (typeof route.handler === 'function') {
|
|
20
|
-
return Promise.resolve(route.handler(ctx)).then(useReturnValue(ctx));
|
|
21
|
-
}
|
|
22
|
-
return route.handler.handle(resolver, ctx).then(useReturnValue(ctx));
|
|
23
|
-
})
|
|
24
|
-
.run(async (middleware, next) => {
|
|
25
|
-
if (typeof middleware === 'function') {
|
|
26
|
-
return middleware(ctx, next);
|
|
27
|
-
}
|
|
28
|
-
return middleware.handle(resolver, ctx, next, middleware.args);
|
|
29
|
-
});
|
|
30
|
-
}
|
|
@@ -1,22 +0,0 @@
|
|
|
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
|
-
/**
|
|
10
|
-
* A factory function that uses the return value of the request
|
|
11
|
-
* pipeline as the response
|
|
12
|
-
*/
|
|
13
|
-
export function useReturnValue(ctx) {
|
|
14
|
-
return function (value) {
|
|
15
|
-
if (value !== undefined && // Return value is explicitly defined
|
|
16
|
-
!ctx.response.hasLazyBody && // Lazy body is not set
|
|
17
|
-
value !== ctx.response // Return value is not the instance of response object
|
|
18
|
-
) {
|
|
19
|
-
ctx.response.send(value);
|
|
20
|
-
}
|
|
21
|
-
};
|
|
22
|
-
}
|
|
@@ -1,207 +0,0 @@
|
|
|
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
|
-
import Macroable from '@poppinss/macroable';
|
|
10
|
-
import { BriskRoute } from './brisk.js';
|
|
11
|
-
import { RouteResource } from './resource.js';
|
|
12
|
-
/**
|
|
13
|
-
* Group class exposes the API to take action on a group of routes.
|
|
14
|
-
* The group routes must be pre-defined using the constructor.
|
|
15
|
-
*/
|
|
16
|
-
export class RouteGroup extends Macroable {
|
|
17
|
-
routes;
|
|
18
|
-
/**
|
|
19
|
-
* Array of middleware registered on the group.
|
|
20
|
-
*/
|
|
21
|
-
#middleware = [];
|
|
22
|
-
constructor(routes) {
|
|
23
|
-
super();
|
|
24
|
-
this.routes = routes;
|
|
25
|
-
}
|
|
26
|
-
/**
|
|
27
|
-
* Shares midldeware stack with the routes. The method is invoked recursively
|
|
28
|
-
* to only register middleware with the route class and not with the
|
|
29
|
-
* resource or the child group
|
|
30
|
-
*/
|
|
31
|
-
#shareMiddlewareStackWithRoutes(route) {
|
|
32
|
-
if (route instanceof RouteGroup) {
|
|
33
|
-
route.routes.forEach((child) => this.#shareMiddlewareStackWithRoutes(child));
|
|
34
|
-
return;
|
|
35
|
-
}
|
|
36
|
-
if (route instanceof RouteResource) {
|
|
37
|
-
route.routes.forEach((child) => child.getMiddleware().unshift(this.#middleware));
|
|
38
|
-
return;
|
|
39
|
-
}
|
|
40
|
-
if (route instanceof BriskRoute) {
|
|
41
|
-
route.route.getMiddleware().unshift(this.#middleware);
|
|
42
|
-
return;
|
|
43
|
-
}
|
|
44
|
-
route.getMiddleware().unshift(this.#middleware);
|
|
45
|
-
}
|
|
46
|
-
/**
|
|
47
|
-
* Updates the route name. The method is invoked recursively to only update
|
|
48
|
-
* the name with the route class and not with the resource or the child
|
|
49
|
-
* group.
|
|
50
|
-
*/
|
|
51
|
-
#updateRouteName(route, name) {
|
|
52
|
-
if (route instanceof RouteGroup) {
|
|
53
|
-
route.routes.forEach((child) => this.#updateRouteName(child, name));
|
|
54
|
-
return;
|
|
55
|
-
}
|
|
56
|
-
if (route instanceof RouteResource) {
|
|
57
|
-
route.routes.forEach((child) => child.as(name, true));
|
|
58
|
-
return;
|
|
59
|
-
}
|
|
60
|
-
if (route instanceof BriskRoute) {
|
|
61
|
-
route.route.as(name, true);
|
|
62
|
-
return;
|
|
63
|
-
}
|
|
64
|
-
route.as(name, true);
|
|
65
|
-
}
|
|
66
|
-
/**
|
|
67
|
-
* Sets prefix on the route. The method is invoked recursively to only set
|
|
68
|
-
* the prefix with the route class and not with the resource or the
|
|
69
|
-
* child group.
|
|
70
|
-
*/
|
|
71
|
-
#setRoutePrefix(route, prefix) {
|
|
72
|
-
if (route instanceof RouteGroup) {
|
|
73
|
-
route.routes.forEach((child) => this.#setRoutePrefix(child, prefix));
|
|
74
|
-
return;
|
|
75
|
-
}
|
|
76
|
-
if (route instanceof RouteResource) {
|
|
77
|
-
route.routes.forEach((child) => child.prefix(prefix));
|
|
78
|
-
return;
|
|
79
|
-
}
|
|
80
|
-
if (route instanceof BriskRoute) {
|
|
81
|
-
route.route.prefix(prefix);
|
|
82
|
-
return;
|
|
83
|
-
}
|
|
84
|
-
route.prefix(prefix);
|
|
85
|
-
}
|
|
86
|
-
/**
|
|
87
|
-
* Updates domain on the route. The method is invoked recursively to only update
|
|
88
|
-
* the domain with the route class and not with the resource or the child
|
|
89
|
-
* group.
|
|
90
|
-
*/
|
|
91
|
-
#updateRouteDomain(route, domain) {
|
|
92
|
-
if (route instanceof RouteGroup) {
|
|
93
|
-
route.routes.forEach((child) => this.#updateRouteDomain(child, domain));
|
|
94
|
-
return;
|
|
95
|
-
}
|
|
96
|
-
if (route instanceof RouteResource) {
|
|
97
|
-
route.routes.forEach((child) => child.domain(domain));
|
|
98
|
-
return;
|
|
99
|
-
}
|
|
100
|
-
if (route instanceof BriskRoute) {
|
|
101
|
-
route.route.domain(domain, false);
|
|
102
|
-
return;
|
|
103
|
-
}
|
|
104
|
-
route.domain(domain, false);
|
|
105
|
-
}
|
|
106
|
-
/**
|
|
107
|
-
* Updates matchers on the route. The method is invoked recursively to only update
|
|
108
|
-
* the matchers with the route class and not with the resource or the child
|
|
109
|
-
* group.
|
|
110
|
-
*/
|
|
111
|
-
#updateRouteMatchers(route, param, matcher) {
|
|
112
|
-
if (route instanceof RouteGroup) {
|
|
113
|
-
route.routes.forEach((child) => this.#updateRouteMatchers(child, param, matcher));
|
|
114
|
-
return;
|
|
115
|
-
}
|
|
116
|
-
if (route instanceof RouteResource) {
|
|
117
|
-
route.routes.forEach((child) => child.where(param, matcher));
|
|
118
|
-
return;
|
|
119
|
-
}
|
|
120
|
-
if (route instanceof BriskRoute) {
|
|
121
|
-
route.route.where(param, matcher);
|
|
122
|
-
return;
|
|
123
|
-
}
|
|
124
|
-
route.where(param, matcher);
|
|
125
|
-
}
|
|
126
|
-
/**
|
|
127
|
-
* Define route param matcher
|
|
128
|
-
*
|
|
129
|
-
* ```ts
|
|
130
|
-
* Route.group(() => {
|
|
131
|
-
* }).where('id', /^[0-9]+/)
|
|
132
|
-
* ```
|
|
133
|
-
*/
|
|
134
|
-
where(param, matcher) {
|
|
135
|
-
this.routes.forEach((route) => this.#updateRouteMatchers(route, param, matcher));
|
|
136
|
-
return this;
|
|
137
|
-
}
|
|
138
|
-
/**
|
|
139
|
-
* Define prefix all the routes in the group.
|
|
140
|
-
*
|
|
141
|
-
* ```ts
|
|
142
|
-
* Route.group(() => {
|
|
143
|
-
* }).prefix('v1')
|
|
144
|
-
* ```
|
|
145
|
-
*/
|
|
146
|
-
prefix(prefix) {
|
|
147
|
-
this.routes.forEach((route) => this.#setRoutePrefix(route, prefix));
|
|
148
|
-
return this;
|
|
149
|
-
}
|
|
150
|
-
/**
|
|
151
|
-
* Define domain for all the routes.
|
|
152
|
-
*
|
|
153
|
-
* ```ts
|
|
154
|
-
* Route.group(() => {
|
|
155
|
-
* }).domain(':name.adonisjs.com')
|
|
156
|
-
* ```
|
|
157
|
-
*/
|
|
158
|
-
domain(domain) {
|
|
159
|
-
this.routes.forEach((route) => this.#updateRouteDomain(route, domain));
|
|
160
|
-
return this;
|
|
161
|
-
}
|
|
162
|
-
/**
|
|
163
|
-
* Prepend name to the routes name.
|
|
164
|
-
*
|
|
165
|
-
* ```ts
|
|
166
|
-
* Route.group(() => {
|
|
167
|
-
* }).as('version1')
|
|
168
|
-
* ```
|
|
169
|
-
*/
|
|
170
|
-
as(name) {
|
|
171
|
-
this.routes.forEach((route) => this.#updateRouteName(route, name));
|
|
172
|
-
return this;
|
|
173
|
-
}
|
|
174
|
-
/**
|
|
175
|
-
* Prepend an array of middleware to all routes middleware.
|
|
176
|
-
*
|
|
177
|
-
* ```ts
|
|
178
|
-
* Route.group(() => {
|
|
179
|
-
* }).use(middleware.auth())
|
|
180
|
-
* ```
|
|
181
|
-
*/
|
|
182
|
-
use(middleware) {
|
|
183
|
-
/**
|
|
184
|
-
* Register middleware with children. We share the group middleware
|
|
185
|
-
* array by reference, therefore have to register it only for the
|
|
186
|
-
* first time.
|
|
187
|
-
*/
|
|
188
|
-
if (!this.#middleware.length) {
|
|
189
|
-
this.routes.forEach((route) => this.#shareMiddlewareStackWithRoutes(route));
|
|
190
|
-
}
|
|
191
|
-
if (Array.isArray(middleware)) {
|
|
192
|
-
for (let one of middleware) {
|
|
193
|
-
this.#middleware.push(one);
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
else {
|
|
197
|
-
this.#middleware.push(middleware);
|
|
198
|
-
}
|
|
199
|
-
return this;
|
|
200
|
-
}
|
|
201
|
-
/**
|
|
202
|
-
* @alias use
|
|
203
|
-
*/
|
|
204
|
-
middleware(middleware) {
|
|
205
|
-
return this.use(middleware);
|
|
206
|
-
}
|
|
207
|
-
}
|
|
@@ -1,86 +0,0 @@
|
|
|
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
|
-
import { UrlBuilder } from './url_builder.js';
|
|
10
|
-
import { RouteFinder } from './route_finder.js';
|
|
11
|
-
/**
|
|
12
|
-
* Lookup store exposes the API to lookup routes and
|
|
13
|
-
* make URLs for registered routes.
|
|
14
|
-
*/
|
|
15
|
-
export class LookupStore {
|
|
16
|
-
/**
|
|
17
|
-
* List of routes grouped by domain
|
|
18
|
-
*/
|
|
19
|
-
#routes = {};
|
|
20
|
-
/**
|
|
21
|
-
* Encryption for making URLs
|
|
22
|
-
*/
|
|
23
|
-
#encryption;
|
|
24
|
-
/**
|
|
25
|
-
* Query string parser for making URLs
|
|
26
|
-
*/
|
|
27
|
-
#qsParser;
|
|
28
|
-
constructor(encryption, qsParser) {
|
|
29
|
-
this.#encryption = encryption;
|
|
30
|
-
this.#qsParser = qsParser;
|
|
31
|
-
}
|
|
32
|
-
/**
|
|
33
|
-
* Register route JSON payload
|
|
34
|
-
*/
|
|
35
|
-
register(route) {
|
|
36
|
-
this.#routes[route.domain] = this.#routes[route.domain] || [];
|
|
37
|
-
this.#routes[route.domain].push(route);
|
|
38
|
-
}
|
|
39
|
-
/**
|
|
40
|
-
* Returns an instance of the URL builder for making
|
|
41
|
-
* route URIs
|
|
42
|
-
*/
|
|
43
|
-
builder() {
|
|
44
|
-
return this.builderForDomain('root');
|
|
45
|
-
}
|
|
46
|
-
/**
|
|
47
|
-
* Returns an instance of the URL builder for a specific
|
|
48
|
-
* domain.
|
|
49
|
-
*/
|
|
50
|
-
builderForDomain(domain) {
|
|
51
|
-
const routes = this.#routes[domain];
|
|
52
|
-
return new UrlBuilder(this.#encryption, new RouteFinder(routes || []), this.#qsParser);
|
|
53
|
-
}
|
|
54
|
-
/**
|
|
55
|
-
* Finds a route by its identifier. The identifier can be the
|
|
56
|
-
* route name, controller.method name or the route pattern
|
|
57
|
-
* itself.
|
|
58
|
-
*/
|
|
59
|
-
find(routeIdentifier, domain) {
|
|
60
|
-
const routes = this.#routes[domain || 'root'] || [];
|
|
61
|
-
return new RouteFinder(routes).find(routeIdentifier);
|
|
62
|
-
}
|
|
63
|
-
/**
|
|
64
|
-
* Finds a route by its identifier. The identifier can be the
|
|
65
|
-
* route name, controller.method name or the route pattern
|
|
66
|
-
* itself.
|
|
67
|
-
*
|
|
68
|
-
* An error is raised when unable to find the route.
|
|
69
|
-
*/
|
|
70
|
-
findOrFail(routeIdentifier, domain) {
|
|
71
|
-
const routes = this.#routes[domain || 'root'] || [];
|
|
72
|
-
return new RouteFinder(routes).findOrFail(routeIdentifier);
|
|
73
|
-
}
|
|
74
|
-
/**
|
|
75
|
-
* Check if a route exists. The identifier can be the
|
|
76
|
-
* route name, controller.method name or the route pattern
|
|
77
|
-
* itself.
|
|
78
|
-
*/
|
|
79
|
-
has(routeIdentifier, domain) {
|
|
80
|
-
const routes = this.#routes[domain || 'root'] || [];
|
|
81
|
-
return new RouteFinder(routes).has(routeIdentifier);
|
|
82
|
-
}
|
|
83
|
-
toJSON() {
|
|
84
|
-
return this.#routes;
|
|
85
|
-
}
|
|
86
|
-
}
|
|
@@ -1,49 +0,0 @@
|
|
|
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
|
-
import * as errors from '../../exceptions.js';
|
|
10
|
-
/**
|
|
11
|
-
* Route finder is used to find a route by its name, route pattern
|
|
12
|
-
* or the controller.method name.
|
|
13
|
-
*/
|
|
14
|
-
export class RouteFinder {
|
|
15
|
-
#routes;
|
|
16
|
-
constructor(routes) {
|
|
17
|
-
this.#routes = routes;
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* Find a route by indentifier
|
|
21
|
-
*/
|
|
22
|
-
find(routeIdentifier) {
|
|
23
|
-
return (this.#routes.find(({ name, pattern, handler }) => {
|
|
24
|
-
if (name === routeIdentifier || pattern === routeIdentifier) {
|
|
25
|
-
return true;
|
|
26
|
-
}
|
|
27
|
-
if (typeof handler === 'function') {
|
|
28
|
-
return false;
|
|
29
|
-
}
|
|
30
|
-
return handler.reference === routeIdentifier;
|
|
31
|
-
}) || null);
|
|
32
|
-
}
|
|
33
|
-
/**
|
|
34
|
-
* Find a route by indentifier or fail
|
|
35
|
-
*/
|
|
36
|
-
findOrFail(routeIdentifier) {
|
|
37
|
-
const route = this.find(routeIdentifier);
|
|
38
|
-
if (!route) {
|
|
39
|
-
throw new errors.E_CANNOT_LOOKUP_ROUTE([routeIdentifier]);
|
|
40
|
-
}
|
|
41
|
-
return route;
|
|
42
|
-
}
|
|
43
|
-
/**
|
|
44
|
-
* Find if a route exists
|
|
45
|
-
*/
|
|
46
|
-
has(routeIdentifier) {
|
|
47
|
-
return !!this.find(routeIdentifier);
|
|
48
|
-
}
|
|
49
|
-
}
|
|
@@ -1,209 +0,0 @@
|
|
|
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
|
-
import { RuntimeException } from '@poppinss/utils';
|
|
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
|
-
*/
|
|
24
|
-
export class UrlBuilder {
|
|
25
|
-
/**
|
|
26
|
-
* Query string parser
|
|
27
|
-
*/
|
|
28
|
-
#qsParser;
|
|
29
|
-
/**
|
|
30
|
-
* The parameters to apply on the route
|
|
31
|
-
*/
|
|
32
|
-
#params = {};
|
|
33
|
-
/**
|
|
34
|
-
* Query string to append to the route
|
|
35
|
-
*/
|
|
36
|
-
#qs = {};
|
|
37
|
-
/**
|
|
38
|
-
* Should we perform the route lookup or just build the
|
|
39
|
-
* given pattern as it is.
|
|
40
|
-
*/
|
|
41
|
-
#shouldPerformLookup = true;
|
|
42
|
-
/**
|
|
43
|
-
* BaseURL to append to the constructored URL
|
|
44
|
-
*/
|
|
45
|
-
#baseUrl;
|
|
46
|
-
/**
|
|
47
|
-
* Encryption class for making signed URLs
|
|
48
|
-
*/
|
|
49
|
-
#encryption;
|
|
50
|
-
/**
|
|
51
|
-
* Route finder for finding route pattern
|
|
52
|
-
*/
|
|
53
|
-
#routeFinder;
|
|
54
|
-
constructor(encryption, routeFinder, qsParser) {
|
|
55
|
-
this.#qsParser = qsParser;
|
|
56
|
-
this.#encryption = encryption;
|
|
57
|
-
this.#routeFinder = routeFinder;
|
|
58
|
-
}
|
|
59
|
-
/**
|
|
60
|
-
* Raises exception when wildcard values array is missing or
|
|
61
|
-
* has length of zero.
|
|
62
|
-
*/
|
|
63
|
-
#ensureHasWildCardValues(pattern, values) {
|
|
64
|
-
if (!values || !Array.isArray(values) || !values.length) {
|
|
65
|
-
throw new RuntimeException(`Cannot make URL for "${pattern}" route. Invalid value provided for wildcard param`);
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
/*
|
|
69
|
-
* Raises exception when value is not defined
|
|
70
|
-
*/
|
|
71
|
-
#ensureHasParamValue(pattern, param, value) {
|
|
72
|
-
if (value === undefined || value === null) {
|
|
73
|
-
throw new RuntimeException(`Cannot make URL for "${pattern}" route. Missing value for "${param}" param`);
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
/**
|
|
77
|
-
* Processes the pattern against the params
|
|
78
|
-
*/
|
|
79
|
-
#processPattern(pattern) {
|
|
80
|
-
const uriSegments = [];
|
|
81
|
-
const paramsArray = Array.isArray(this.#params) ? this.#params : null;
|
|
82
|
-
const paramsObject = !Array.isArray(this.#params) ? this.#params : {};
|
|
83
|
-
let paramsIndex = 0;
|
|
84
|
-
const tokens = parseRoutePattern(pattern);
|
|
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
|
-
*/
|
|
90
|
-
if (token.type === 0) {
|
|
91
|
-
uriSegments.push(`${token.val}${token.end}`);
|
|
92
|
-
}
|
|
93
|
-
else if (token.type === 2) {
|
|
94
|
-
const values = paramsArray ? paramsArray.slice(paramsIndex) : paramsObject['*'];
|
|
95
|
-
this.#ensureHasWildCardValues(pattern, values);
|
|
96
|
-
uriSegments.push(`${values.join('/')}${token.end}`);
|
|
97
|
-
break;
|
|
98
|
-
}
|
|
99
|
-
else {
|
|
100
|
-
const paramName = token.val;
|
|
101
|
-
const value = paramsArray ? paramsArray[paramsIndex] : paramsObject[paramName];
|
|
102
|
-
/**
|
|
103
|
-
* Type = 1 means param is required
|
|
104
|
-
*/
|
|
105
|
-
if (token.type === 1) {
|
|
106
|
-
this.#ensureHasParamValue(pattern, paramName, value);
|
|
107
|
-
}
|
|
108
|
-
paramsIndex++;
|
|
109
|
-
if (value !== undefined && value !== null) {
|
|
110
|
-
uriSegments.push(`${value}${token.end}`);
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
return `/${uriSegments.join('/')}`;
|
|
115
|
-
}
|
|
116
|
-
/**
|
|
117
|
-
* Suffix the query string to the URL
|
|
118
|
-
*/
|
|
119
|
-
#suffixQueryString(url, qs) {
|
|
120
|
-
if (qs) {
|
|
121
|
-
const queryString = this.#qsParser.stringify(qs);
|
|
122
|
-
url = queryString ? `${url}?${queryString}` : url;
|
|
123
|
-
}
|
|
124
|
-
return url;
|
|
125
|
-
}
|
|
126
|
-
/**
|
|
127
|
-
* Prefixes base URL to the uri string
|
|
128
|
-
*/
|
|
129
|
-
#prefixBaseUrl(uri) {
|
|
130
|
-
return this.#baseUrl ? `${this.#baseUrl}${uri}` : uri;
|
|
131
|
-
}
|
|
132
|
-
/**
|
|
133
|
-
* Prefix a custom base URL to the final URI
|
|
134
|
-
*/
|
|
135
|
-
prefixUrl(url) {
|
|
136
|
-
this.#baseUrl = url;
|
|
137
|
-
return this;
|
|
138
|
-
}
|
|
139
|
-
/**
|
|
140
|
-
* Disable route lookup. Calling this method considers
|
|
141
|
-
* the "identifier" as the route pattern
|
|
142
|
-
*/
|
|
143
|
-
disableRouteLookup() {
|
|
144
|
-
this.#shouldPerformLookup = false;
|
|
145
|
-
return this;
|
|
146
|
-
}
|
|
147
|
-
/**
|
|
148
|
-
* Append query string to the final URI
|
|
149
|
-
*/
|
|
150
|
-
qs(queryString) {
|
|
151
|
-
if (!queryString) {
|
|
152
|
-
return this;
|
|
153
|
-
}
|
|
154
|
-
this.#qs = queryString;
|
|
155
|
-
return this;
|
|
156
|
-
}
|
|
157
|
-
/**
|
|
158
|
-
* Specify params to apply to the route pattern
|
|
159
|
-
*/
|
|
160
|
-
params(params) {
|
|
161
|
-
if (!params) {
|
|
162
|
-
return this;
|
|
163
|
-
}
|
|
164
|
-
this.#params = params;
|
|
165
|
-
return this;
|
|
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
|
-
*/
|
|
172
|
-
make(identifier) {
|
|
173
|
-
let url;
|
|
174
|
-
if (this.#shouldPerformLookup) {
|
|
175
|
-
const route = this.#routeFinder.findOrFail(identifier);
|
|
176
|
-
url = this.#processPattern(route.pattern);
|
|
177
|
-
}
|
|
178
|
-
else {
|
|
179
|
-
url = this.#processPattern(identifier);
|
|
180
|
-
}
|
|
181
|
-
return this.#suffixQueryString(this.#prefixBaseUrl(url), this.#qs);
|
|
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
|
-
*/
|
|
188
|
-
makeSigned(identifier, options) {
|
|
189
|
-
let url;
|
|
190
|
-
if (this.#shouldPerformLookup) {
|
|
191
|
-
const route = this.#routeFinder.findOrFail(identifier);
|
|
192
|
-
url = this.#processPattern(route.pattern);
|
|
193
|
-
}
|
|
194
|
-
else {
|
|
195
|
-
url = this.#processPattern(identifier);
|
|
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
|
-
*/
|
|
205
|
-
const signature = this.#encryption.verifier.sign(this.#suffixQueryString(url, this.#qs), options?.expiresIn, options?.purpose);
|
|
206
|
-
const qs = Object.assign({}, this.#qs, { signature });
|
|
207
|
-
return this.#suffixQueryString(this.#prefixBaseUrl(url), qs);
|
|
208
|
-
}
|
|
209
|
-
}
|