@adonisjs/http-server 6.8.2-1 → 6.8.2-10
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/LICENSE.md +1 -1
- package/README.md +3 -6
- package/build/factories/http_context.d.ts +10 -0
- 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.js +8 -0
- package/build/factories/qs_parser_factory.d.ts +10 -0
- package/build/factories/qs_parser_factory.js +18 -0
- package/build/factories/request.d.ts +10 -0
- package/build/factories/request.js +31 -0
- package/build/factories/response.d.ts +10 -0
- package/build/factories/response.js +34 -0
- package/build/factories/router.d.ts +10 -0
- package/build/factories/router.js +25 -0
- package/build/factories/server_factory.d.ts +10 -0
- package/build/factories/server_factory.js +34 -0
- package/build/index.js +8 -0
- package/build/src/cookies/client.d.ts +25 -0
- package/build/src/cookies/client.js +42 -0
- package/build/src/cookies/drivers/encrypted.d.ts +12 -0
- package/build/src/cookies/drivers/encrypted.js +20 -0
- package/build/src/cookies/drivers/plain.d.ts +12 -0
- package/build/src/cookies/drivers/plain.js +20 -0
- package/build/src/cookies/drivers/signed.d.ts +12 -0
- package/build/src/cookies/drivers/signed.js +20 -0
- package/build/src/cookies/parser.d.ts +28 -0
- package/build/src/cookies/parser.js +98 -0
- package/build/src/cookies/serializer.d.ts +22 -0
- package/build/src/cookies/serializer.js +40 -0
- package/build/src/debug.js +8 -0
- package/build/src/define_config.d.ts +3 -0
- package/build/src/define_config.js +11 -0
- package/build/src/define_middleware.d.ts +5 -0
- package/build/src/define_middleware.js +18 -0
- package/build/src/exception_handler.d.ts +93 -6
- package/build/src/exception_handler.js +203 -44
- package/build/src/exceptions.d.ts +6 -0
- package/build/src/exceptions.js +11 -0
- package/build/src/helpers.d.ts +14 -0
- package/build/src/helpers.js +43 -0
- package/build/src/http_context/local_storage.d.ts +3 -0
- package/build/src/http_context/local_storage.js +25 -0
- package/build/src/http_context/main.d.ts +36 -0
- package/build/src/http_context/main.js +54 -0
- package/build/src/qs.d.ts +4 -0
- package/build/src/qs.js +12 -0
- package/build/src/redirect.d.ts +24 -0
- package/build/src/redirect.js +60 -0
- package/build/src/request.d.ts +466 -0
- package/build/src/request.js +542 -0
- package/build/src/response.d.ts +425 -1
- package/build/src/response.js +611 -11
- package/build/src/router/brisk.d.ts +22 -0
- package/build/src/router/brisk.js +44 -2
- package/build/src/router/executor.d.ts +6 -1
- package/build/src/router/executor.js +14 -1
- package/build/src/router/factories/use_return_value.d.ts +4 -0
- package/build/src/router/factories/use_return_value.js +16 -3
- package/build/src/router/group.d.ts +47 -0
- package/build/src/router/group.js +88 -0
- package/build/src/router/lookup_store/main.d.ts +32 -0
- package/build/src/router/lookup_store/main.js +49 -0
- package/build/src/router/lookup_store/route_finder.d.ts +13 -0
- package/build/src/router/lookup_store/route_finder.js +21 -0
- package/build/src/router/lookup_store/url_builder.d.ts +36 -0
- package/build/src/router/lookup_store/url_builder.js +99 -2
- package/build/src/router/main.d.ts +92 -4
- package/build/src/router/main.js +146 -0
- package/build/src/router/matchers.d.ts +13 -0
- package/build/src/router/matchers.js +21 -0
- package/build/src/router/parser.d.ts +5 -0
- package/build/src/router/parser.js +17 -0
- package/build/src/router/resource.d.ts +30 -1
- package/build/src/router/resource.js +93 -1
- package/build/src/router/route.d.ts +65 -0
- package/build/src/router/route.js +151 -2
- package/build/src/router/store.d.ts +54 -0
- package/build/src/router/store.js +110 -2
- package/build/src/server/factories/final_handler.d.ts +7 -1
- package/build/src/server/factories/final_handler.js +15 -2
- package/build/src/server/factories/middleware_handler.d.ts +3 -0
- package/build/src/server/factories/middleware_handler.js +11 -0
- package/build/src/server/factories/write_response.d.ts +4 -0
- package/build/src/server/factories/write_response.js +12 -0
- package/build/src/server/main.d.ts +48 -0
- package/build/src/server/main.js +142 -5
- package/build/src/types/base.d.ts +12 -0
- package/build/src/types/base.js +8 -0
- package/build/src/types/main.js +8 -0
- package/build/src/types/middleware.d.ts +18 -0
- package/build/src/types/middleware.js +8 -0
- package/build/src/types/qs.d.ts +53 -0
- package/build/src/types/qs.js +8 -0
- package/build/src/types/request.d.ts +32 -0
- package/build/src/types/request.js +8 -0
- package/build/src/types/response.d.ts +27 -0
- package/build/src/types/response.js +8 -0
- package/build/src/types/route.d.ts +89 -1
- package/build/src/types/route.js +8 -0
- package/build/src/types/server.d.ts +36 -1
- package/build/src/types/server.js +8 -0
- package/package.json +52 -74
|
@@ -1,25 +1,54 @@
|
|
|
1
1
|
import Macroable from '@poppinss/macroable';
|
|
2
2
|
import type { Application } from '@adonisjs/application';
|
|
3
3
|
import { Route } from './route.js';
|
|
4
|
+
import type { Constructor, LazyImport } from '../types/base.js';
|
|
4
5
|
import type { ParsedGlobalMiddleware } from '../types/middleware.js';
|
|
5
6
|
import type { ResourceActionNames, RouteMatcher, RouteMatchers } from '../types/route.js';
|
|
7
|
+
/**
|
|
8
|
+
* Route resource exposes the API to register multiple routes for a resource.
|
|
9
|
+
*/
|
|
6
10
|
export declare class RouteResource extends Macroable {
|
|
7
11
|
#private;
|
|
12
|
+
/**
|
|
13
|
+
* A collection of routes instances that belongs to this resource
|
|
14
|
+
*/
|
|
8
15
|
routes: Route[];
|
|
9
16
|
constructor(app: Application<any>, routerMiddleware: ParsedGlobalMiddleware[], options: {
|
|
10
17
|
resource: string;
|
|
11
|
-
controller: string
|
|
18
|
+
controller: string | LazyImport<Constructor<any>> | Constructor<any>;
|
|
12
19
|
globalMatchers: RouteMatchers;
|
|
13
20
|
shallow: boolean;
|
|
14
21
|
});
|
|
22
|
+
/**
|
|
23
|
+
* Register only given routes and remove others
|
|
24
|
+
*/
|
|
15
25
|
only(names: ResourceActionNames[]): this;
|
|
26
|
+
/**
|
|
27
|
+
* Register all routes, except the one's defined
|
|
28
|
+
*/
|
|
16
29
|
except(names: ResourceActionNames[]): this;
|
|
30
|
+
/**
|
|
31
|
+
* Register api only routes. The `create` and `edit` routes, which
|
|
32
|
+
* are meant to show forms will not be registered
|
|
33
|
+
*/
|
|
17
34
|
apiOnly(): this;
|
|
35
|
+
/**
|
|
36
|
+
* Define matcher for params inside the resource
|
|
37
|
+
*/
|
|
18
38
|
where(key: string, matcher: RouteMatcher | string | RegExp): this;
|
|
39
|
+
/**
|
|
40
|
+
* Tap into multiple routes to configure them by their name
|
|
41
|
+
*/
|
|
19
42
|
tap(callback: (route: Route) => void): this;
|
|
20
43
|
tap(actions: ResourceActionNames | ResourceActionNames[], callback: (route: Route) => void): this;
|
|
44
|
+
/**
|
|
45
|
+
* Set the param name for a given resource
|
|
46
|
+
*/
|
|
21
47
|
params(resources: {
|
|
22
48
|
[resource: string]: string;
|
|
23
49
|
}): this;
|
|
50
|
+
/**
|
|
51
|
+
* Prepend name to all the routes
|
|
52
|
+
*/
|
|
24
53
|
as(name: string, normalizeName?: boolean): this;
|
|
25
54
|
}
|
|
@@ -1,16 +1,59 @@
|
|
|
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 string from '@poppinss/utils/string';
|
|
2
10
|
import Macroable from '@poppinss/macroable';
|
|
3
11
|
import { RuntimeException } from '@poppinss/utils';
|
|
4
12
|
import { Route } from './route.js';
|
|
13
|
+
/**
|
|
14
|
+
* Route resource exposes the API to register multiple routes for a resource.
|
|
15
|
+
*/
|
|
5
16
|
export class RouteResource extends Macroable {
|
|
17
|
+
/**
|
|
18
|
+
* Resource identifier. Nested resources are separated
|
|
19
|
+
* with a dot notation
|
|
20
|
+
*/
|
|
6
21
|
#resource;
|
|
22
|
+
/**
|
|
23
|
+
* The controller to handle resource routing requests
|
|
24
|
+
*/
|
|
7
25
|
#controller;
|
|
26
|
+
/**
|
|
27
|
+
* Is it a shallow resource? Shallow resources URLs do not have parent
|
|
28
|
+
* resource name and id once they can be identified with the id.
|
|
29
|
+
*/
|
|
8
30
|
#shallow = false;
|
|
31
|
+
/**
|
|
32
|
+
* Matchers inherited from the router
|
|
33
|
+
*/
|
|
9
34
|
#globalMatchers;
|
|
35
|
+
/**
|
|
36
|
+
* Reference to the AdonisJS application
|
|
37
|
+
*/
|
|
10
38
|
#app;
|
|
39
|
+
/**
|
|
40
|
+
* Middleware registered on the router
|
|
41
|
+
*/
|
|
11
42
|
#routerMiddleware;
|
|
43
|
+
/**
|
|
44
|
+
* Parameter names for the resources. Defaults to `id` for
|
|
45
|
+
* a singular resource and `resource_id` for nested
|
|
46
|
+
* resources.
|
|
47
|
+
*/
|
|
12
48
|
#params = {};
|
|
49
|
+
/**
|
|
50
|
+
* Base name for the routes. We suffix action names
|
|
51
|
+
* on top of the base name
|
|
52
|
+
*/
|
|
13
53
|
#routesBaseName;
|
|
54
|
+
/**
|
|
55
|
+
* A collection of routes instances that belongs to this resource
|
|
56
|
+
*/
|
|
14
57
|
routes = [];
|
|
15
58
|
constructor(app, routerMiddleware, options) {
|
|
16
59
|
super();
|
|
@@ -24,33 +67,60 @@ export class RouteResource extends Macroable {
|
|
|
24
67
|
this.#routesBaseName = this.#getRoutesBaseName();
|
|
25
68
|
this.#buildRoutes();
|
|
26
69
|
}
|
|
70
|
+
/**
|
|
71
|
+
* Normalizes the resource name to dropping leading and trailing
|
|
72
|
+
* slashes.
|
|
73
|
+
*/
|
|
27
74
|
#normalizeResourceName(resource) {
|
|
28
75
|
return resource.replace(/^\//, '').replace(/\/$/, '');
|
|
29
76
|
}
|
|
77
|
+
/**
|
|
78
|
+
* Ensure resource name is not an empty string
|
|
79
|
+
*/
|
|
30
80
|
#validateResourceName(resource) {
|
|
31
81
|
if (!resource || resource === '/') {
|
|
32
82
|
throw new RuntimeException(`Invalid resource name "${resource}"`);
|
|
33
83
|
}
|
|
34
84
|
}
|
|
85
|
+
/**
|
|
86
|
+
* Converting segments of a resource to snake case to
|
|
87
|
+
* make the route name.
|
|
88
|
+
*/
|
|
35
89
|
#getRoutesBaseName() {
|
|
36
90
|
return this.#resource
|
|
37
91
|
.split('.')
|
|
38
92
|
.map((token) => string.snakeCase(token))
|
|
39
93
|
.join('.');
|
|
40
94
|
}
|
|
95
|
+
/**
|
|
96
|
+
* Create a new route for the given pattern, methods and controller action
|
|
97
|
+
*/
|
|
41
98
|
#createRoute(pattern, methods, action) {
|
|
42
99
|
const route = new Route(this.#app, this.#routerMiddleware, {
|
|
43
100
|
pattern,
|
|
44
101
|
methods,
|
|
45
|
-
handler:
|
|
102
|
+
handler: typeof this.#controller === 'string'
|
|
103
|
+
? `${this.#controller}.${action}`
|
|
104
|
+
: [this.#controller, action],
|
|
46
105
|
globalMatchers: this.#globalMatchers,
|
|
47
106
|
});
|
|
48
107
|
route.as(`${this.#routesBaseName}.${action}`);
|
|
49
108
|
this.routes.push(route);
|
|
50
109
|
}
|
|
110
|
+
/**
|
|
111
|
+
* Returns the `resource_id` name for a given resource. The
|
|
112
|
+
* resource name is converted to singular form and
|
|
113
|
+
* transformed to snake case.
|
|
114
|
+
*
|
|
115
|
+
* photos becomes photo_id
|
|
116
|
+
* users becomes user_id
|
|
117
|
+
*/
|
|
51
118
|
#getResourceId(resource) {
|
|
52
119
|
return `${string.snakeCase(string.singular(resource))}_id`;
|
|
53
120
|
}
|
|
121
|
+
/**
|
|
122
|
+
* Build routes for the given resource
|
|
123
|
+
*/
|
|
54
124
|
#buildRoutes() {
|
|
55
125
|
const resources = this.#resource.split('.');
|
|
56
126
|
const mainResource = resources.pop();
|
|
@@ -70,6 +140,9 @@ export class RouteResource extends Macroable {
|
|
|
70
140
|
this.#createRoute(`${this.#shallow ? mainResource : baseURI}/:id`, ['PUT', 'PATCH'], 'update');
|
|
71
141
|
this.#createRoute(`${this.#shallow ? mainResource : baseURI}/:id`, ['DELETE'], 'destroy');
|
|
72
142
|
}
|
|
143
|
+
/**
|
|
144
|
+
* Filter the routes based on their partial names
|
|
145
|
+
*/
|
|
73
146
|
#filter(names, inverse) {
|
|
74
147
|
const actions = Array.isArray(names) ? names : [names];
|
|
75
148
|
return this.routes.filter((route) => {
|
|
@@ -77,17 +150,30 @@ export class RouteResource extends Macroable {
|
|
|
77
150
|
return inverse ? !match : match;
|
|
78
151
|
});
|
|
79
152
|
}
|
|
153
|
+
/**
|
|
154
|
+
* Register only given routes and remove others
|
|
155
|
+
*/
|
|
80
156
|
only(names) {
|
|
81
157
|
this.#filter(names, true).forEach((route) => route.markAsDeleted());
|
|
82
158
|
return this;
|
|
83
159
|
}
|
|
160
|
+
/**
|
|
161
|
+
* Register all routes, except the one's defined
|
|
162
|
+
*/
|
|
84
163
|
except(names) {
|
|
85
164
|
this.#filter(names, false).forEach((route) => route.markAsDeleted());
|
|
86
165
|
return this;
|
|
87
166
|
}
|
|
167
|
+
/**
|
|
168
|
+
* Register api only routes. The `create` and `edit` routes, which
|
|
169
|
+
* are meant to show forms will not be registered
|
|
170
|
+
*/
|
|
88
171
|
apiOnly() {
|
|
89
172
|
return this.except(['create', 'edit']);
|
|
90
173
|
}
|
|
174
|
+
/**
|
|
175
|
+
* Define matcher for params inside the resource
|
|
176
|
+
*/
|
|
91
177
|
where(key, matcher) {
|
|
92
178
|
this.routes.forEach((route) => {
|
|
93
179
|
route.where(key, matcher);
|
|
@@ -102,6 +188,9 @@ export class RouteResource extends Macroable {
|
|
|
102
188
|
this.#filter(actions, false).forEach((route) => callback(route));
|
|
103
189
|
return this;
|
|
104
190
|
}
|
|
191
|
+
/**
|
|
192
|
+
* Set the param name for a given resource
|
|
193
|
+
*/
|
|
105
194
|
params(resources) {
|
|
106
195
|
Object.keys(resources).forEach((resource) => {
|
|
107
196
|
const param = resources[resource];
|
|
@@ -113,6 +202,9 @@ export class RouteResource extends Macroable {
|
|
|
113
202
|
});
|
|
114
203
|
return this;
|
|
115
204
|
}
|
|
205
|
+
/**
|
|
206
|
+
* Prepend name to all the routes
|
|
207
|
+
*/
|
|
116
208
|
as(name, normalizeName = true) {
|
|
117
209
|
name = normalizeName ? string.snakeCase(name) : name;
|
|
118
210
|
this.routes.forEach((route) => {
|
|
@@ -3,6 +3,10 @@ import type { Application } from '@adonisjs/application';
|
|
|
3
3
|
import type { Constructor, LazyImport, OneOrMore } from '../types/base.js';
|
|
4
4
|
import type { MiddlewareFn, ParsedNamedMiddleware, ParsedGlobalMiddleware } from '../types/middleware.js';
|
|
5
5
|
import type { GetControllerHandlers, RouteFn, RouteJSON, RouteMatcher, RouteMatchers, StoreRouteMiddleware } from '../types/route.js';
|
|
6
|
+
/**
|
|
7
|
+
* The route class exposes the APIs for constructing a route using the
|
|
8
|
+
* fluent API.
|
|
9
|
+
*/
|
|
6
10
|
export declare class Route<Controller extends Constructor<any> = any> extends Macroable {
|
|
7
11
|
#private;
|
|
8
12
|
constructor(app: Application<any>, routerMiddleware: ParsedGlobalMiddleware[], options: {
|
|
@@ -11,17 +15,78 @@ export declare class Route<Controller extends Constructor<any> = any> extends Ma
|
|
|
11
15
|
handler: RouteFn | string | [LazyImport<Controller> | Controller, GetControllerHandlers<Controller>?];
|
|
12
16
|
globalMatchers: RouteMatchers;
|
|
13
17
|
});
|
|
18
|
+
/**
|
|
19
|
+
* Define matcher for a given param. If a matcher exists, then we do not
|
|
20
|
+
* override that, since the routes inside a group will set matchers
|
|
21
|
+
* before the group, so they should have priority over the group
|
|
22
|
+
* matchers.
|
|
23
|
+
*
|
|
24
|
+
* ```ts
|
|
25
|
+
* Route.group(() => {
|
|
26
|
+
* Route.get('/:id', 'handler').where('id', /^[0-9]$/)
|
|
27
|
+
* }).where('id', /[^a-z$]/)
|
|
28
|
+
* ```
|
|
29
|
+
*
|
|
30
|
+
* The `/^[0-9]$/` will win over the matcher defined by the group
|
|
31
|
+
*/
|
|
14
32
|
where(param: string, matcher: RouteMatcher | string | RegExp): this;
|
|
33
|
+
/**
|
|
34
|
+
* Define prefix for the route. Calling this method multiple times
|
|
35
|
+
* applies multiple prefixes in the reverse order.
|
|
36
|
+
*/
|
|
15
37
|
prefix(prefix: string): this;
|
|
38
|
+
/**
|
|
39
|
+
* Define a custom domain for the route. We do not overwrite the domain
|
|
40
|
+
* unless `overwrite` flag is set to true.
|
|
41
|
+
*/
|
|
16
42
|
domain(domain: string, overwrite?: boolean): this;
|
|
43
|
+
/**
|
|
44
|
+
* Define one or more middleware to be executed before the route
|
|
45
|
+
* handler.
|
|
46
|
+
*
|
|
47
|
+
* Named middleware can be referenced using the name registered with
|
|
48
|
+
* the router middleware store.
|
|
49
|
+
*/
|
|
17
50
|
use(middleware: OneOrMore<MiddlewareFn | ParsedNamedMiddleware>): this;
|
|
51
|
+
/**
|
|
52
|
+
* @alias use
|
|
53
|
+
*/
|
|
18
54
|
middleware(middleware: OneOrMore<MiddlewareFn | ParsedNamedMiddleware>): this;
|
|
55
|
+
/**
|
|
56
|
+
* Give a unique name to the route. Assinging a new unique removes the
|
|
57
|
+
* existing name of the route.
|
|
58
|
+
*
|
|
59
|
+
* Setting prepends to true prefixes the name to the existing name.
|
|
60
|
+
*/
|
|
19
61
|
as(name: string, prepend?: boolean): this;
|
|
62
|
+
/**
|
|
63
|
+
* Check if the route was marked to be deleted
|
|
64
|
+
*/
|
|
20
65
|
isDeleted(): boolean;
|
|
66
|
+
/**
|
|
67
|
+
* Mark route as deleted. Deleted routes are not registered
|
|
68
|
+
* with the route store
|
|
69
|
+
*/
|
|
21
70
|
markAsDeleted(): void;
|
|
71
|
+
/**
|
|
72
|
+
* Get the route name
|
|
73
|
+
*/
|
|
22
74
|
getName(): string | undefined;
|
|
75
|
+
/**
|
|
76
|
+
* Get the route pattern
|
|
77
|
+
*/
|
|
23
78
|
getPattern(): string;
|
|
79
|
+
/**
|
|
80
|
+
* Set the route pattern
|
|
81
|
+
*/
|
|
24
82
|
setPattern(pattern: string): this;
|
|
83
|
+
/**
|
|
84
|
+
* Returns the stack of middleware registered on the route.
|
|
85
|
+
* The value is shared by reference.
|
|
86
|
+
*/
|
|
25
87
|
getMiddleware(): StoreRouteMiddleware[][];
|
|
88
|
+
/**
|
|
89
|
+
* Returns JSON representation of the route
|
|
90
|
+
*/
|
|
26
91
|
toJSON(): RouteJSON;
|
|
27
92
|
}
|
|
@@ -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 Macroable from '@poppinss/macroable';
|
|
3
11
|
import Middleware from '@poppinss/middleware';
|
|
@@ -5,18 +13,66 @@ import { RuntimeException } from '@poppinss/utils';
|
|
|
5
13
|
import { moduleCaller, moduleExpression, moduleImporter } from '@adonisjs/fold';
|
|
6
14
|
import { execute } from './executor.js';
|
|
7
15
|
import { dropSlash } from '../helpers.js';
|
|
16
|
+
import debug from '../debug.js';
|
|
17
|
+
/**
|
|
18
|
+
* The route class exposes the APIs for constructing a route using the
|
|
19
|
+
* fluent API.
|
|
20
|
+
*/
|
|
8
21
|
export class Route extends Macroable {
|
|
22
|
+
/**
|
|
23
|
+
* Route pattern
|
|
24
|
+
*/
|
|
9
25
|
#pattern;
|
|
26
|
+
/**
|
|
27
|
+
* HTTP Methods for the route
|
|
28
|
+
*/
|
|
10
29
|
#methods;
|
|
30
|
+
/**
|
|
31
|
+
* A unique name for the route
|
|
32
|
+
*/
|
|
11
33
|
#name;
|
|
34
|
+
/**
|
|
35
|
+
* A boolean to prevent route from getting registered within
|
|
36
|
+
* the store.
|
|
37
|
+
*
|
|
38
|
+
* This flag must be set before "Router.commit" method
|
|
39
|
+
*/
|
|
12
40
|
#isDeleted = false;
|
|
41
|
+
/**
|
|
42
|
+
* Route handler
|
|
43
|
+
*/
|
|
13
44
|
#handler;
|
|
45
|
+
/**
|
|
46
|
+
* Matchers inherited from the router
|
|
47
|
+
*/
|
|
14
48
|
#globalMatchers;
|
|
49
|
+
/**
|
|
50
|
+
* Reference to the AdonisJS application
|
|
51
|
+
*/
|
|
15
52
|
#app;
|
|
53
|
+
/**
|
|
54
|
+
* Middleware registered on the router
|
|
55
|
+
*/
|
|
16
56
|
#routerMiddleware;
|
|
57
|
+
/**
|
|
58
|
+
* By default the route is part of the `root` domain. Root domain is used
|
|
59
|
+
* when no domain is defined
|
|
60
|
+
*/
|
|
17
61
|
#routeDomain = 'root';
|
|
62
|
+
/**
|
|
63
|
+
* An object of matchers to be forwarded to the store. The matchers
|
|
64
|
+
* list is populated by calling `where` method
|
|
65
|
+
*/
|
|
18
66
|
#matchers = {};
|
|
67
|
+
/**
|
|
68
|
+
* Custom prefixes defined on the route or the route parent
|
|
69
|
+
* groups
|
|
70
|
+
*/
|
|
19
71
|
#prefixes = [];
|
|
72
|
+
/**
|
|
73
|
+
* Middleware defined directly on the route or the route parent
|
|
74
|
+
* routes. We mantain an array for each layer of the stack
|
|
75
|
+
*/
|
|
20
76
|
#middleware = [];
|
|
21
77
|
constructor(app, routerMiddleware, options) {
|
|
22
78
|
super();
|
|
@@ -27,6 +83,10 @@ export class Route extends Macroable {
|
|
|
27
83
|
this.#handler = this.#resolveRouteHandle(options.handler);
|
|
28
84
|
this.#globalMatchers = options.globalMatchers;
|
|
29
85
|
}
|
|
86
|
+
/**
|
|
87
|
+
* Resolves the route handler string expression to a
|
|
88
|
+
* handler method object
|
|
89
|
+
*/
|
|
30
90
|
#resolveRouteHandle(handler) {
|
|
31
91
|
if (typeof handler === 'string') {
|
|
32
92
|
return {
|
|
@@ -34,13 +94,23 @@ export class Route extends Macroable {
|
|
|
34
94
|
...moduleExpression(handler, this.#app.appRoot).toHandleMethod(),
|
|
35
95
|
};
|
|
36
96
|
}
|
|
97
|
+
/**
|
|
98
|
+
* Using a lazily imported controller
|
|
99
|
+
*/
|
|
37
100
|
if (Array.isArray(handler)) {
|
|
101
|
+
/**
|
|
102
|
+
* The first item of the tuple is a class constructor
|
|
103
|
+
*/
|
|
38
104
|
if (is.class_(handler[0])) {
|
|
39
105
|
return {
|
|
40
106
|
reference: handler,
|
|
41
107
|
...moduleCaller(handler[0], (handler[1] || 'handle')).toHandleMethod(),
|
|
42
108
|
};
|
|
43
109
|
}
|
|
110
|
+
/**
|
|
111
|
+
* The first item of the tuple is a function that lazily
|
|
112
|
+
* loads the controller
|
|
113
|
+
*/
|
|
44
114
|
return {
|
|
45
115
|
reference: handler,
|
|
46
116
|
...moduleImporter(handler[0], (handler[1] || 'handle')).toHandleMethod(),
|
|
@@ -48,9 +118,17 @@ export class Route extends Macroable {
|
|
|
48
118
|
}
|
|
49
119
|
return handler;
|
|
50
120
|
}
|
|
121
|
+
/**
|
|
122
|
+
* Returns an object of param matchers by merging global and local
|
|
123
|
+
* matchers. The local copy is given preference over the global
|
|
124
|
+
* one's
|
|
125
|
+
*/
|
|
51
126
|
#getMatchers() {
|
|
52
127
|
return { ...this.#globalMatchers, ...this.#matchers };
|
|
53
128
|
}
|
|
129
|
+
/**
|
|
130
|
+
* Returns a normalized pattern string by prefixing the `prefix` (if defined).
|
|
131
|
+
*/
|
|
54
132
|
#computePattern() {
|
|
55
133
|
const pattern = dropSlash(this.#pattern);
|
|
56
134
|
const prefix = this.#prefixes
|
|
@@ -60,6 +138,20 @@ export class Route extends Macroable {
|
|
|
60
138
|
.join('');
|
|
61
139
|
return prefix ? `${prefix}${pattern === '/' ? '' : pattern}` : pattern;
|
|
62
140
|
}
|
|
141
|
+
/**
|
|
142
|
+
* Define matcher for a given param. If a matcher exists, then we do not
|
|
143
|
+
* override that, since the routes inside a group will set matchers
|
|
144
|
+
* before the group, so they should have priority over the group
|
|
145
|
+
* matchers.
|
|
146
|
+
*
|
|
147
|
+
* ```ts
|
|
148
|
+
* Route.group(() => {
|
|
149
|
+
* Route.get('/:id', 'handler').where('id', /^[0-9]$/)
|
|
150
|
+
* }).where('id', /[^a-z$]/)
|
|
151
|
+
* ```
|
|
152
|
+
*
|
|
153
|
+
* The `/^[0-9]$/` will win over the matcher defined by the group
|
|
154
|
+
*/
|
|
63
155
|
where(param, matcher) {
|
|
64
156
|
if (this.#matchers[param]) {
|
|
65
157
|
return this;
|
|
@@ -75,23 +167,47 @@ export class Route extends Macroable {
|
|
|
75
167
|
}
|
|
76
168
|
return this;
|
|
77
169
|
}
|
|
170
|
+
/**
|
|
171
|
+
* Define prefix for the route. Calling this method multiple times
|
|
172
|
+
* applies multiple prefixes in the reverse order.
|
|
173
|
+
*/
|
|
78
174
|
prefix(prefix) {
|
|
79
175
|
this.#prefixes.push(prefix);
|
|
80
176
|
return this;
|
|
81
177
|
}
|
|
178
|
+
/**
|
|
179
|
+
* Define a custom domain for the route. We do not overwrite the domain
|
|
180
|
+
* unless `overwrite` flag is set to true.
|
|
181
|
+
*/
|
|
82
182
|
domain(domain, overwrite = false) {
|
|
83
183
|
if (this.#routeDomain === 'root' || overwrite) {
|
|
84
184
|
this.#routeDomain = domain;
|
|
85
185
|
}
|
|
86
186
|
return this;
|
|
87
187
|
}
|
|
188
|
+
/**
|
|
189
|
+
* Define one or more middleware to be executed before the route
|
|
190
|
+
* handler.
|
|
191
|
+
*
|
|
192
|
+
* Named middleware can be referenced using the name registered with
|
|
193
|
+
* the router middleware store.
|
|
194
|
+
*/
|
|
88
195
|
use(middleware) {
|
|
89
196
|
this.#middleware.push(Array.isArray(middleware) ? middleware : [middleware]);
|
|
90
197
|
return this;
|
|
91
198
|
}
|
|
199
|
+
/**
|
|
200
|
+
* @alias use
|
|
201
|
+
*/
|
|
92
202
|
middleware(middleware) {
|
|
93
203
|
return this.use(middleware);
|
|
94
204
|
}
|
|
205
|
+
/**
|
|
206
|
+
* Give a unique name to the route. Assinging a new unique removes the
|
|
207
|
+
* existing name of the route.
|
|
208
|
+
*
|
|
209
|
+
* Setting prepends to true prefixes the name to the existing name.
|
|
210
|
+
*/
|
|
95
211
|
as(name, prepend = false) {
|
|
96
212
|
if (prepend) {
|
|
97
213
|
if (!this.#name) {
|
|
@@ -103,31 +219,64 @@ export class Route extends Macroable {
|
|
|
103
219
|
this.#name = name;
|
|
104
220
|
return this;
|
|
105
221
|
}
|
|
222
|
+
/**
|
|
223
|
+
* Check if the route was marked to be deleted
|
|
224
|
+
*/
|
|
106
225
|
isDeleted() {
|
|
107
226
|
return this.#isDeleted;
|
|
108
227
|
}
|
|
228
|
+
/**
|
|
229
|
+
* Mark route as deleted. Deleted routes are not registered
|
|
230
|
+
* with the route store
|
|
231
|
+
*/
|
|
109
232
|
markAsDeleted() {
|
|
110
233
|
this.#isDeleted = true;
|
|
111
234
|
}
|
|
235
|
+
/**
|
|
236
|
+
* Get the route name
|
|
237
|
+
*/
|
|
112
238
|
getName() {
|
|
113
239
|
return this.#name;
|
|
114
240
|
}
|
|
241
|
+
/**
|
|
242
|
+
* Get the route pattern
|
|
243
|
+
*/
|
|
115
244
|
getPattern() {
|
|
116
245
|
return this.#pattern;
|
|
117
246
|
}
|
|
247
|
+
/**
|
|
248
|
+
* Set the route pattern
|
|
249
|
+
*/
|
|
118
250
|
setPattern(pattern) {
|
|
119
251
|
this.#pattern = pattern;
|
|
120
252
|
return this;
|
|
121
253
|
}
|
|
254
|
+
/**
|
|
255
|
+
* Returns the stack of middleware registered on the route.
|
|
256
|
+
* The value is shared by reference.
|
|
257
|
+
*/
|
|
122
258
|
getMiddleware() {
|
|
123
259
|
return this.#middleware;
|
|
124
260
|
}
|
|
261
|
+
/**
|
|
262
|
+
* Returns the middleware instance for persistence inside the
|
|
263
|
+
* store
|
|
264
|
+
*/
|
|
125
265
|
#getMiddlewareForStore() {
|
|
126
266
|
const middleware = new Middleware();
|
|
127
|
-
this.#routerMiddleware.forEach((one) =>
|
|
128
|
-
|
|
267
|
+
this.#routerMiddleware.forEach((one) => {
|
|
268
|
+
debug('adding global middleware to route %s, %O', this.#pattern, one);
|
|
269
|
+
middleware.add(one);
|
|
270
|
+
});
|
|
271
|
+
this.#middleware.flat().forEach((one) => {
|
|
272
|
+
debug('adding named middleware to route %s, %O', this.#pattern, one);
|
|
273
|
+
middleware.add(one);
|
|
274
|
+
});
|
|
129
275
|
return middleware;
|
|
130
276
|
}
|
|
277
|
+
/**
|
|
278
|
+
* Returns JSON representation of the route
|
|
279
|
+
*/
|
|
131
280
|
toJSON() {
|
|
132
281
|
return {
|
|
133
282
|
domain: this.#routeDomain,
|
|
@@ -1,12 +1,66 @@
|
|
|
1
1
|
import type { RouteJSON, MatchedRoute, StoreRoutesTree, MatchItRouteToken } from '../types/route.js';
|
|
2
|
+
/**
|
|
3
|
+
* Store class is used to store a list of routes, along side with their tokens
|
|
4
|
+
* to match the URLs.
|
|
5
|
+
*
|
|
6
|
+
* ```ts
|
|
7
|
+
* const store = new Store()
|
|
8
|
+
*
|
|
9
|
+
* store.add({
|
|
10
|
+
* pattern: 'posts/:id',
|
|
11
|
+
* handler: function onRoute () {},
|
|
12
|
+
* middleware: [],
|
|
13
|
+
* matchers: {
|
|
14
|
+
* id: '^[0-9]$+'
|
|
15
|
+
* },
|
|
16
|
+
* meta: {},
|
|
17
|
+
* methods: ['GET']
|
|
18
|
+
* })
|
|
19
|
+
*
|
|
20
|
+
* store.match('posts/1', 'GET')
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
2
23
|
export declare class RoutesStore {
|
|
3
24
|
#private;
|
|
25
|
+
/**
|
|
26
|
+
* A flag to know if routes for explicit domains
|
|
27
|
+
* have been registered
|
|
28
|
+
*/
|
|
4
29
|
usingDomains: boolean;
|
|
30
|
+
/**
|
|
31
|
+
* Tree of registered routes and their matchit tokens
|
|
32
|
+
*/
|
|
5
33
|
tree: StoreRoutesTree;
|
|
34
|
+
/**
|
|
35
|
+
* Add a route to the store
|
|
36
|
+
*
|
|
37
|
+
* ```ts
|
|
38
|
+
* store.add({
|
|
39
|
+
* pattern: 'post/:id',
|
|
40
|
+
* methods: ['GET'],
|
|
41
|
+
* matchers: {},
|
|
42
|
+
* meta: {},
|
|
43
|
+
* handler: function handler () {
|
|
44
|
+
* }
|
|
45
|
+
* })
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
6
48
|
add(route: RouteJSON): this;
|
|
49
|
+
/**
|
|
50
|
+
* Matches the url, method and optionally domain to pull the matching
|
|
51
|
+
* route. `null` is returned when unable to match the URL against
|
|
52
|
+
* registered routes.
|
|
53
|
+
*
|
|
54
|
+
* The domain parameter has to be a registered pattern and not the fully
|
|
55
|
+
* qualified runtime domain. You must call `matchDomain` first to fetch
|
|
56
|
+
* the pattern for qualified domain
|
|
57
|
+
*/
|
|
7
58
|
match(url: string, method: string, domain?: {
|
|
8
59
|
tokens: MatchItRouteToken[];
|
|
9
60
|
hostname: string;
|
|
10
61
|
}): null | MatchedRoute;
|
|
62
|
+
/**
|
|
63
|
+
* Match hostname against registered domains.
|
|
64
|
+
*/
|
|
11
65
|
matchDomain(hostname?: string | null): MatchItRouteToken[];
|
|
12
66
|
}
|