@adonisjs/http-server 7.0.0-0 → 7.0.0-1

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.
Files changed (107) hide show
  1. package/build/factories/http_context.d.ts +25 -0
  2. package/build/factories/http_context.js +51 -0
  3. package/build/factories/http_server.d.ts +8 -0
  4. package/build/factories/http_server.js +26 -0
  5. package/build/factories/main.d.ts +6 -149
  6. package/build/factories/main.js +14 -331
  7. package/build/factories/qs_parser_factory.d.ts +20 -0
  8. package/build/factories/qs_parser_factory.js +44 -0
  9. package/build/factories/request.d.ts +29 -0
  10. package/build/factories/request.js +73 -0
  11. package/build/factories/response.d.ts +29 -0
  12. package/build/factories/response.js +77 -0
  13. package/build/factories/router.d.ts +23 -0
  14. package/build/factories/router.js +45 -0
  15. package/build/factories/server_factory.d.ts +29 -0
  16. package/build/factories/server_factory.js +65 -0
  17. package/build/index.d.ts +14 -272
  18. package/build/index.js +22 -308
  19. package/build/src/cookies/client.d.ts +37 -0
  20. package/build/src/cookies/client.js +84 -0
  21. package/build/src/cookies/drivers/encrypted.d.ts +16 -0
  22. package/build/src/cookies/drivers/encrypted.js +36 -0
  23. package/build/src/cookies/drivers/plain.d.ts +15 -0
  24. package/build/src/cookies/drivers/plain.js +33 -0
  25. package/build/src/cookies/drivers/signed.d.ts +16 -0
  26. package/build/src/cookies/drivers/signed.js +36 -0
  27. package/build/src/cookies/parser.d.ts +37 -0
  28. package/build/src/cookies/parser.js +167 -0
  29. package/build/src/cookies/serializer.d.ts +33 -0
  30. package/build/src/cookies/serializer.js +79 -0
  31. package/build/src/debug.d.ts +3 -0
  32. package/build/src/debug.js +10 -0
  33. package/build/src/define_config.d.ts +9 -0
  34. package/build/src/define_config.js +68 -0
  35. package/build/src/define_middleware.d.ts +11 -0
  36. package/build/src/define_middleware.js +35 -0
  37. package/build/src/exception_handler.d.ts +113 -0
  38. package/build/src/exception_handler.js +306 -0
  39. package/build/src/exceptions.d.ts +84 -0
  40. package/build/src/exceptions.js +38 -0
  41. package/build/src/helpers.d.ts +23 -0
  42. package/build/src/helpers.js +105 -0
  43. package/build/src/http_context/local_storage.d.ts +12 -0
  44. package/build/src/http_context/local_storage.js +39 -0
  45. package/build/src/http_context/main.d.ts +58 -0
  46. package/build/src/http_context/main.js +105 -0
  47. package/build/src/qs.d.ts +11 -0
  48. package/build/src/qs.js +25 -0
  49. package/build/src/redirect.d.ts +42 -0
  50. package/build/src/redirect.js +140 -0
  51. package/build/src/request.d.ts +565 -0
  52. package/build/src/request.js +865 -0
  53. package/build/src/response.d.ts +620 -0
  54. package/build/src/response.js +1208 -0
  55. package/build/src/router/brisk.d.ts +42 -0
  56. package/build/src/router/brisk.js +85 -0
  57. package/build/src/router/executor.d.ts +9 -0
  58. package/build/src/router/executor.js +30 -0
  59. package/build/src/router/factories/use_return_value.d.ts +6 -0
  60. package/build/src/router/factories/use_return_value.js +22 -0
  61. package/build/src/router/group.d.ts +65 -0
  62. package/build/src/router/group.js +207 -0
  63. package/build/src/router/lookup_store/main.d.ts +49 -0
  64. package/build/src/router/lookup_store/main.js +86 -0
  65. package/build/src/router/lookup_store/route_finder.d.ts +21 -0
  66. package/build/src/router/lookup_store/route_finder.js +49 -0
  67. package/build/src/router/lookup_store/url_builder.d.ts +52 -0
  68. package/build/src/router/lookup_store/url_builder.js +209 -0
  69. package/build/src/router/main.d.ts +128 -0
  70. package/build/src/router/main.js +316 -0
  71. package/build/src/router/matchers.d.ts +27 -0
  72. package/build/src/router/matchers.js +36 -0
  73. package/build/src/router/parser.d.ts +5 -0
  74. package/build/src/router/parser.js +17 -0
  75. package/build/src/router/resource.d.ts +54 -0
  76. package/build/src/router/resource.js +216 -0
  77. package/build/src/router/route.d.ts +92 -0
  78. package/build/src/router/route.js +293 -0
  79. package/build/src/router/store.d.ts +66 -0
  80. package/build/src/router/store.js +195 -0
  81. package/build/src/server/factories/final_handler.d.ts +10 -0
  82. package/build/src/server/factories/final_handler.js +30 -0
  83. package/build/src/server/factories/middleware_handler.d.ts +8 -0
  84. package/build/src/server/factories/middleware_handler.js +16 -0
  85. package/build/src/server/factories/write_response.d.ts +6 -0
  86. package/build/src/server/factories/write_response.js +24 -0
  87. package/build/{main-29eaaee4.d.ts → src/server/main.d.ts} +18 -13
  88. package/build/src/server/main.js +292 -0
  89. package/build/src/types/base.d.ts +19 -0
  90. package/build/src/types/base.js +9 -0
  91. package/build/src/types/main.d.ts +7 -14
  92. package/build/src/types/main.js +15 -0
  93. package/build/src/types/middleware.d.ts +35 -0
  94. package/build/src/types/middleware.js +9 -0
  95. package/build/src/types/qs.d.ts +68 -0
  96. package/build/src/types/qs.js +9 -0
  97. package/build/src/types/request.d.ts +39 -0
  98. package/build/src/types/request.js +9 -0
  99. package/build/src/types/response.d.ts +45 -0
  100. package/build/src/types/response.js +9 -0
  101. package/build/src/types/route.d.ts +166 -0
  102. package/build/src/types/route.js +9 -0
  103. package/build/src/types/server.d.ts +72 -0
  104. package/build/src/types/server.js +9 -0
  105. package/package.json +6 -3
  106. package/build/chunk-XX72ATFY.js +0 -4388
  107. package/build/main-e5b46c83.d.ts +0 -2210
@@ -0,0 +1,42 @@
1
+ import Macroable from '@poppinss/macroable';
2
+ import type { Application } from '@adonisjs/application';
3
+ import { Route } from './route.js';
4
+ import type { ParsedGlobalMiddleware } from '../types/middleware.js';
5
+ import type { MakeUrlOptions, RouteFn, RouteMatchers } from '../types/route.js';
6
+ /**
7
+ * Brisk routes exposes the API to configure the route handler by chaining
8
+ * one of the pre-defined methods.
9
+ *
10
+ * For example: Instead of defining the redirect logic as a callback, one can
11
+ * chain the `.redirect` method.
12
+ *
13
+ * Brisk routes are always registered under the `GET` HTTP method.
14
+ */
15
+ export declare class BriskRoute extends Macroable {
16
+ #private;
17
+ /**
18
+ * Reference to route instance. Set after `setHandler` is called
19
+ */
20
+ route: null | Route;
21
+ constructor(app: Application<any>, routerMiddleware: ParsedGlobalMiddleware[], options: {
22
+ pattern: string;
23
+ globalMatchers: RouteMatchers;
24
+ });
25
+ /**
26
+ * Set handler for the brisk route
27
+ */
28
+ setHandler(handler: RouteFn): Route;
29
+ /**
30
+ * Redirects to a given route. Params from the original request will
31
+ * be used when no custom params are defined.
32
+ */
33
+ redirect(identifier: string, params?: any[] | Record<string, any>, options?: MakeUrlOptions & {
34
+ status: number;
35
+ }): Route;
36
+ /**
37
+ * Redirect request to a fixed URL
38
+ */
39
+ redirectToPath(url: string, options?: {
40
+ status: number;
41
+ }): Route;
42
+ }
@@ -0,0 +1,85 @@
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
+ }
@@ -0,0 +1,9 @@
1
+ import type { ContainerResolver } from '@adonisjs/fold';
2
+ import type { StoreRouteNode } from '../types/route.js';
3
+ import type { HttpContext } from '../http_context/main.js';
4
+ import type { ServerErrorHandler } from '../types/server.js';
5
+ /**
6
+ * Executor to execute the route middleware pipeline the route
7
+ * handler
8
+ */
9
+ export declare function execute(route: StoreRouteNode, resolver: ContainerResolver<any>, ctx: HttpContext, errorResponder: ServerErrorHandler['handle']): Promise<void>;
@@ -0,0 +1,30 @@
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
+ }
@@ -0,0 +1,6 @@
1
+ import type { HttpContext } from '../../http_context/main.js';
2
+ /**
3
+ * A factory function that uses the return value of the request
4
+ * pipeline as the response
5
+ */
6
+ export declare function useReturnValue(ctx: HttpContext): (value: any) => void;
@@ -0,0 +1,22 @@
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
+ }
@@ -0,0 +1,65 @@
1
+ import Macroable from '@poppinss/macroable';
2
+ import type { RouteMatcher } from '../types/route.js';
3
+ import type { MiddlewareFn, ParsedNamedMiddleware } from '../types/middleware.js';
4
+ import { Route } from './route.js';
5
+ import { BriskRoute } from './brisk.js';
6
+ import { RouteResource } from './resource.js';
7
+ import { OneOrMore } from '../types/base.js';
8
+ /**
9
+ * Group class exposes the API to take action on a group of routes.
10
+ * The group routes must be pre-defined using the constructor.
11
+ */
12
+ export declare class RouteGroup extends Macroable {
13
+ #private;
14
+ routes: (Route | RouteGroup | RouteResource | BriskRoute)[];
15
+ constructor(routes: (Route | RouteGroup | RouteResource | BriskRoute)[]);
16
+ /**
17
+ * Define route param matcher
18
+ *
19
+ * ```ts
20
+ * Route.group(() => {
21
+ * }).where('id', /^[0-9]+/)
22
+ * ```
23
+ */
24
+ where(param: string, matcher: RouteMatcher | string | RegExp): this;
25
+ /**
26
+ * Define prefix all the routes in the group.
27
+ *
28
+ * ```ts
29
+ * Route.group(() => {
30
+ * }).prefix('v1')
31
+ * ```
32
+ */
33
+ prefix(prefix: string): this;
34
+ /**
35
+ * Define domain for all the routes.
36
+ *
37
+ * ```ts
38
+ * Route.group(() => {
39
+ * }).domain(':name.adonisjs.com')
40
+ * ```
41
+ */
42
+ domain(domain: string): this;
43
+ /**
44
+ * Prepend name to the routes name.
45
+ *
46
+ * ```ts
47
+ * Route.group(() => {
48
+ * }).as('version1')
49
+ * ```
50
+ */
51
+ as(name: string): this;
52
+ /**
53
+ * Prepend an array of middleware to all routes middleware.
54
+ *
55
+ * ```ts
56
+ * Route.group(() => {
57
+ * }).use(middleware.auth())
58
+ * ```
59
+ */
60
+ use(middleware: OneOrMore<MiddlewareFn | ParsedNamedMiddleware>): this;
61
+ /**
62
+ * @alias use
63
+ */
64
+ middleware(middleware: OneOrMore<MiddlewareFn | ParsedNamedMiddleware>): this;
65
+ }
@@ -0,0 +1,207 @@
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
+ }
@@ -0,0 +1,49 @@
1
+ import type { Encryption } from '@adonisjs/encryption';
2
+ import type { Qs } from '../../qs.js';
3
+ import { UrlBuilder } from './url_builder.js';
4
+ import type { RouteJSON } from '../../types/route.js';
5
+ /**
6
+ * Lookup store exposes the API to lookup routes and
7
+ * make URLs for registered routes.
8
+ */
9
+ export declare class LookupStore {
10
+ #private;
11
+ constructor(encryption: Encryption, qsParser: Qs);
12
+ /**
13
+ * Register route JSON payload
14
+ */
15
+ register(route: RouteJSON): void;
16
+ /**
17
+ * Returns an instance of the URL builder for making
18
+ * route URIs
19
+ */
20
+ builder(): UrlBuilder;
21
+ /**
22
+ * Returns an instance of the URL builder for a specific
23
+ * domain.
24
+ */
25
+ builderForDomain(domain: string): UrlBuilder;
26
+ /**
27
+ * Finds a route by its identifier. The identifier can be the
28
+ * route name, controller.method name or the route pattern
29
+ * itself.
30
+ */
31
+ find(routeIdentifier: string, domain?: string): RouteJSON | null;
32
+ /**
33
+ * Finds a route by its identifier. The identifier can be the
34
+ * route name, controller.method name or the route pattern
35
+ * itself.
36
+ *
37
+ * An error is raised when unable to find the route.
38
+ */
39
+ findOrFail(routeIdentifier: string, domain?: string): RouteJSON;
40
+ /**
41
+ * Check if a route exists. The identifier can be the
42
+ * route name, controller.method name or the route pattern
43
+ * itself.
44
+ */
45
+ has(routeIdentifier: string, domain?: string): boolean;
46
+ toJSON(): {
47
+ [domain: string]: RouteJSON[];
48
+ };
49
+ }
@@ -0,0 +1,86 @@
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
+ }
@@ -0,0 +1,21 @@
1
+ import type { RouteJSON } from '../../types/route.js';
2
+ /**
3
+ * Route finder is used to find a route by its name, route pattern
4
+ * or the controller.method name.
5
+ */
6
+ export declare class RouteFinder {
7
+ #private;
8
+ constructor(routes: RouteJSON[]);
9
+ /**
10
+ * Find a route by indentifier
11
+ */
12
+ find(routeIdentifier: string): RouteJSON | null;
13
+ /**
14
+ * Find a route by indentifier or fail
15
+ */
16
+ findOrFail(routeIdentifier: string): RouteJSON;
17
+ /**
18
+ * Find if a route exists
19
+ */
20
+ has(routeIdentifier: string): boolean;
21
+ }
@@ -0,0 +1,49 @@
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
+ }