@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.
Files changed (208) hide show
  1. package/README.md +3 -6
  2. package/build/factories/http_context.d.ts +10 -1
  3. package/build/factories/http_context.js +27 -0
  4. package/build/factories/http_server.d.ts +8 -0
  5. package/build/factories/http_server.js +26 -0
  6. package/build/factories/main.d.ts +0 -1
  7. package/build/factories/main.js +8 -0
  8. package/build/factories/qs_parser_factory.d.ts +10 -1
  9. package/build/factories/qs_parser_factory.js +18 -0
  10. package/build/factories/request.d.ts +10 -1
  11. package/build/factories/request.js +31 -0
  12. package/build/factories/response.d.ts +10 -1
  13. package/build/factories/response.js +34 -0
  14. package/build/factories/router.d.ts +10 -1
  15. package/build/factories/router.js +25 -0
  16. package/build/factories/server_factory.d.ts +10 -1
  17. package/build/factories/server_factory.js +34 -0
  18. package/build/index.d.ts +0 -1
  19. package/build/index.js +8 -0
  20. package/build/src/cookies/client.d.ts +25 -1
  21. package/build/src/cookies/client.js +42 -0
  22. package/build/src/cookies/drivers/encrypted.d.ts +12 -1
  23. package/build/src/cookies/drivers/encrypted.js +20 -0
  24. package/build/src/cookies/drivers/plain.d.ts +12 -1
  25. package/build/src/cookies/drivers/plain.js +20 -0
  26. package/build/src/cookies/drivers/signed.d.ts +12 -1
  27. package/build/src/cookies/drivers/signed.js +20 -0
  28. package/build/src/cookies/parser.d.ts +28 -1
  29. package/build/src/cookies/parser.js +98 -0
  30. package/build/src/cookies/serializer.d.ts +22 -1
  31. package/build/src/cookies/serializer.js +40 -0
  32. package/build/src/debug.d.ts +0 -1
  33. package/build/src/debug.js +8 -0
  34. package/build/src/define_config.d.ts +3 -1
  35. package/build/src/define_config.js +11 -0
  36. package/build/src/define_middleware.d.ts +5 -1
  37. package/build/src/define_middleware.js +18 -0
  38. package/build/src/exception_handler.d.ts +65 -1
  39. package/build/src/exception_handler.js +95 -0
  40. package/build/src/exceptions.d.ts +6 -1
  41. package/build/src/exceptions.js +11 -0
  42. package/build/src/helpers.d.ts +14 -1
  43. package/build/src/helpers.js +22 -0
  44. package/build/src/http_context/local_storage.d.ts +3 -1
  45. package/build/src/http_context/local_storage.js +25 -0
  46. package/build/src/http_context/main.d.ts +36 -1
  47. package/build/src/http_context/main.js +54 -0
  48. package/build/src/qs.d.ts +4 -1
  49. package/build/src/qs.js +12 -0
  50. package/build/src/redirect.d.ts +24 -1
  51. package/build/src/redirect.js +60 -0
  52. package/build/src/request.d.ts +466 -1
  53. package/build/src/request.js +542 -0
  54. package/build/src/response.d.ts +425 -2
  55. package/build/src/response.js +608 -7
  56. package/build/src/router/brisk.d.ts +22 -1
  57. package/build/src/router/brisk.js +42 -0
  58. package/build/src/router/executor.d.ts +4 -1
  59. package/build/src/router/executor.js +12 -0
  60. package/build/src/router/factories/use_return_value.d.ts +4 -1
  61. package/build/src/router/factories/use_return_value.js +16 -3
  62. package/build/src/router/group.d.ts +47 -1
  63. package/build/src/router/group.js +88 -0
  64. package/build/src/router/lookup_store/main.d.ts +32 -1
  65. package/build/src/router/lookup_store/main.js +49 -0
  66. package/build/src/router/lookup_store/route_finder.d.ts +13 -1
  67. package/build/src/router/lookup_store/route_finder.js +21 -0
  68. package/build/src/router/lookup_store/url_builder.d.ts +36 -1
  69. package/build/src/router/lookup_store/url_builder.js +97 -0
  70. package/build/src/router/main.d.ts +87 -1
  71. package/build/src/router/main.js +142 -0
  72. package/build/src/router/matchers.d.ts +13 -1
  73. package/build/src/router/matchers.js +21 -0
  74. package/build/src/router/parser.d.ts +3 -1
  75. package/build/src/router/parser.js +12 -0
  76. package/build/src/router/resource.d.ts +28 -1
  77. package/build/src/router/resource.js +90 -0
  78. package/build/src/router/route.d.ts +65 -1
  79. package/build/src/router/route.js +151 -2
  80. package/build/src/router/store.d.ts +54 -1
  81. package/build/src/router/store.js +107 -0
  82. package/build/src/server/factories/final_handler.d.ts +5 -1
  83. package/build/src/server/factories/final_handler.js +13 -0
  84. package/build/src/server/factories/middleware_handler.d.ts +3 -1
  85. package/build/src/server/factories/middleware_handler.js +11 -0
  86. package/build/src/server/factories/write_response.d.ts +4 -1
  87. package/build/src/server/factories/write_response.js +12 -0
  88. package/build/src/server/main.d.ts +48 -1
  89. package/build/src/server/main.js +128 -0
  90. package/build/src/types/base.d.ts +12 -1
  91. package/build/src/types/base.js +8 -0
  92. package/build/src/types/main.d.ts +0 -1
  93. package/build/src/types/main.js +8 -0
  94. package/build/src/types/middleware.d.ts +18 -1
  95. package/build/src/types/middleware.js +8 -0
  96. package/build/src/types/qs.d.ts +53 -1
  97. package/build/src/types/qs.js +8 -0
  98. package/build/src/types/request.d.ts +32 -1
  99. package/build/src/types/request.js +8 -0
  100. package/build/src/types/response.d.ts +27 -1
  101. package/build/src/types/response.js +8 -0
  102. package/build/src/types/route.d.ts +87 -1
  103. package/build/src/types/route.js +8 -0
  104. package/build/src/types/server.d.ts +35 -1
  105. package/build/src/types/server.js +8 -0
  106. package/package.json +44 -71
  107. package/build/factories/http_context.d.ts.map +0 -1
  108. package/build/factories/main.d.ts.map +0 -1
  109. package/build/factories/qs_parser_factory.d.ts.map +0 -1
  110. package/build/factories/request.d.ts.map +0 -1
  111. package/build/factories/response.d.ts.map +0 -1
  112. package/build/factories/router.d.ts.map +0 -1
  113. package/build/factories/server_factory.d.ts.map +0 -1
  114. package/build/index.d.ts.map +0 -1
  115. package/build/src/cookies/client.d.ts.map +0 -1
  116. package/build/src/cookies/drivers/encrypted.d.ts.map +0 -1
  117. package/build/src/cookies/drivers/plain.d.ts.map +0 -1
  118. package/build/src/cookies/drivers/signed.d.ts.map +0 -1
  119. package/build/src/cookies/parser.d.ts.map +0 -1
  120. package/build/src/cookies/serializer.d.ts.map +0 -1
  121. package/build/src/debug.d.ts.map +0 -1
  122. package/build/src/define_config.d.ts.map +0 -1
  123. package/build/src/define_middleware.d.ts.map +0 -1
  124. package/build/src/exception_handler.d.ts.map +0 -1
  125. package/build/src/exceptions.d.ts.map +0 -1
  126. package/build/src/helpers.d.ts.map +0 -1
  127. package/build/src/http_context/local_storage.d.ts.map +0 -1
  128. package/build/src/http_context/main.d.ts.map +0 -1
  129. package/build/src/qs.d.ts.map +0 -1
  130. package/build/src/redirect.d.ts.map +0 -1
  131. package/build/src/request.d.ts.map +0 -1
  132. package/build/src/response.d.ts.map +0 -1
  133. package/build/src/router/brisk.d.ts.map +0 -1
  134. package/build/src/router/executor.d.ts.map +0 -1
  135. package/build/src/router/factories/use_return_value.d.ts.map +0 -1
  136. package/build/src/router/group.d.ts.map +0 -1
  137. package/build/src/router/lookup_store/main.d.ts.map +0 -1
  138. package/build/src/router/lookup_store/route_finder.d.ts.map +0 -1
  139. package/build/src/router/lookup_store/url_builder.d.ts.map +0 -1
  140. package/build/src/router/main.d.ts.map +0 -1
  141. package/build/src/router/matchers.d.ts.map +0 -1
  142. package/build/src/router/parser.d.ts.map +0 -1
  143. package/build/src/router/resource.d.ts.map +0 -1
  144. package/build/src/router/route.d.ts.map +0 -1
  145. package/build/src/router/store.d.ts.map +0 -1
  146. package/build/src/server/factories/final_handler.d.ts.map +0 -1
  147. package/build/src/server/factories/middleware_handler.d.ts.map +0 -1
  148. package/build/src/server/factories/write_response.d.ts.map +0 -1
  149. package/build/src/server/main.d.ts.map +0 -1
  150. package/build/src/types/base.d.ts.map +0 -1
  151. package/build/src/types/main.d.ts.map +0 -1
  152. package/build/src/types/middleware.d.ts.map +0 -1
  153. package/build/src/types/qs.d.ts.map +0 -1
  154. package/build/src/types/request.d.ts.map +0 -1
  155. package/build/src/types/response.d.ts.map +0 -1
  156. package/build/src/types/route.d.ts.map +0 -1
  157. package/build/src/types/server.d.ts.map +0 -1
  158. package/factories/http_context.ts +0 -73
  159. package/factories/main.ts +0 -15
  160. package/factories/qs_parser_factory.ts +0 -54
  161. package/factories/request.ts +0 -101
  162. package/factories/response.ts +0 -106
  163. package/factories/router.ts +0 -61
  164. package/factories/server_factory.ts +0 -94
  165. package/index.ts +0 -23
  166. package/src/cookies/client.ts +0 -98
  167. package/src/cookies/drivers/encrypted.ts +0 -42
  168. package/src/cookies/drivers/plain.ts +0 -37
  169. package/src/cookies/drivers/signed.ts +0 -42
  170. package/src/cookies/parser.ts +0 -196
  171. package/src/cookies/serializer.ts +0 -98
  172. package/src/debug.ts +0 -11
  173. package/src/define_config.ts +0 -56
  174. package/src/define_middleware.ts +0 -61
  175. package/src/exception_handler.ts +0 -290
  176. package/src/exceptions.ts +0 -55
  177. package/src/helpers.ts +0 -108
  178. package/src/http_context/local_storage.ts +0 -50
  179. package/src/http_context/main.ts +0 -126
  180. package/src/qs.ts +0 -31
  181. package/src/redirect.ts +0 -181
  182. package/src/request.ts +0 -982
  183. package/src/response.ts +0 -1421
  184. package/src/router/brisk.ts +0 -113
  185. package/src/router/executor.ts +0 -36
  186. package/src/router/factories/use_return_value.ts +0 -26
  187. package/src/router/group.ts +0 -243
  188. package/src/router/lookup_store/main.ts +0 -102
  189. package/src/router/lookup_store/route_finder.ts +0 -60
  190. package/src/router/lookup_store/url_builder.ts +0 -250
  191. package/src/router/main.ts +0 -431
  192. package/src/router/matchers.ts +0 -40
  193. package/src/router/parser.ts +0 -20
  194. package/src/router/resource.ts +0 -277
  195. package/src/router/route.ts +0 -363
  196. package/src/router/store.ts +0 -239
  197. package/src/server/factories/final_handler.ts +0 -38
  198. package/src/server/factories/middleware_handler.ts +0 -23
  199. package/src/server/factories/write_response.ts +0 -26
  200. package/src/server/main.ts +0 -356
  201. package/src/types/base.ts +0 -30
  202. package/src/types/main.ts +0 -16
  203. package/src/types/middleware.ts +0 -59
  204. package/src/types/qs.ts +0 -85
  205. package/src/types/request.ts +0 -52
  206. package/src/types/response.ts +0 -57
  207. package/src/types/route.ts +0 -217
  208. package/src/types/server.ts +0 -92
@@ -4,8 +4,14 @@ import { Route } from './route.js';
4
4
  import type { Constructor, LazyImport } from '../types/base.js';
5
5
  import type { ParsedGlobalMiddleware } from '../types/middleware.js';
6
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
+ */
7
10
  export declare class RouteResource extends Macroable {
8
11
  #private;
12
+ /**
13
+ * A collection of routes instances that belongs to this resource
14
+ */
9
15
  routes: Route[];
10
16
  constructor(app: Application<any>, routerMiddleware: ParsedGlobalMiddleware[], options: {
11
17
  resource: string;
@@ -13,15 +19,36 @@ export declare class RouteResource extends Macroable {
13
19
  globalMatchers: RouteMatchers;
14
20
  shallow: boolean;
15
21
  });
22
+ /**
23
+ * Register only given routes and remove others
24
+ */
16
25
  only(names: ResourceActionNames[]): this;
26
+ /**
27
+ * Register all routes, except the one's defined
28
+ */
17
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
+ */
18
34
  apiOnly(): this;
35
+ /**
36
+ * Define matcher for params inside the resource
37
+ */
19
38
  where(key: string, matcher: RouteMatcher | string | RegExp): this;
39
+ /**
40
+ * Tap into multiple routes to configure them by their name
41
+ */
20
42
  tap(callback: (route: Route) => void): this;
21
43
  tap(actions: ResourceActionNames | ResourceActionNames[], callback: (route: Route) => void): this;
44
+ /**
45
+ * Set the param name for a given resource
46
+ */
22
47
  params(resources: {
23
48
  [resource: string]: string;
24
49
  }): this;
50
+ /**
51
+ * Prepend name to all the routes
52
+ */
25
53
  as(name: string, normalizeName?: boolean): this;
26
54
  }
27
- //# sourceMappingURL=resource.d.ts.map
@@ -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,20 +67,34 @@ 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,
@@ -50,9 +107,20 @@ export class RouteResource extends Macroable {
50
107
  route.as(`${this.#routesBaseName}.${action}`);
51
108
  this.routes.push(route);
52
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
+ */
53
118
  #getResourceId(resource) {
54
119
  return `${string.snakeCase(string.singular(resource))}_id`;
55
120
  }
121
+ /**
122
+ * Build routes for the given resource
123
+ */
56
124
  #buildRoutes() {
57
125
  const resources = this.#resource.split('.');
58
126
  const mainResource = resources.pop();
@@ -72,6 +140,9 @@ export class RouteResource extends Macroable {
72
140
  this.#createRoute(`${this.#shallow ? mainResource : baseURI}/:id`, ['PUT', 'PATCH'], 'update');
73
141
  this.#createRoute(`${this.#shallow ? mainResource : baseURI}/:id`, ['DELETE'], 'destroy');
74
142
  }
143
+ /**
144
+ * Filter the routes based on their partial names
145
+ */
75
146
  #filter(names, inverse) {
76
147
  const actions = Array.isArray(names) ? names : [names];
77
148
  return this.routes.filter((route) => {
@@ -79,17 +150,30 @@ export class RouteResource extends Macroable {
79
150
  return inverse ? !match : match;
80
151
  });
81
152
  }
153
+ /**
154
+ * Register only given routes and remove others
155
+ */
82
156
  only(names) {
83
157
  this.#filter(names, true).forEach((route) => route.markAsDeleted());
84
158
  return this;
85
159
  }
160
+ /**
161
+ * Register all routes, except the one's defined
162
+ */
86
163
  except(names) {
87
164
  this.#filter(names, false).forEach((route) => route.markAsDeleted());
88
165
  return this;
89
166
  }
167
+ /**
168
+ * Register api only routes. The `create` and `edit` routes, which
169
+ * are meant to show forms will not be registered
170
+ */
90
171
  apiOnly() {
91
172
  return this.except(['create', 'edit']);
92
173
  }
174
+ /**
175
+ * Define matcher for params inside the resource
176
+ */
93
177
  where(key, matcher) {
94
178
  this.routes.forEach((route) => {
95
179
  route.where(key, matcher);
@@ -104,6 +188,9 @@ export class RouteResource extends Macroable {
104
188
  this.#filter(actions, false).forEach((route) => callback(route));
105
189
  return this;
106
190
  }
191
+ /**
192
+ * Set the param name for a given resource
193
+ */
107
194
  params(resources) {
108
195
  Object.keys(resources).forEach((resource) => {
109
196
  const param = resources[resource];
@@ -115,6 +202,9 @@ export class RouteResource extends Macroable {
115
202
  });
116
203
  return this;
117
204
  }
205
+ /**
206
+ * Prepend name to all the routes
207
+ */
118
208
  as(name, normalizeName = true) {
119
209
  name = normalizeName ? string.snakeCase(name) : name;
120
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,18 +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
  }
28
- //# sourceMappingURL=route.d.ts.map
@@ -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) => middleware.add(one));
128
- this.#middleware.flat().forEach((one) => middleware.add(one));
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,13 +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
  }
13
- //# sourceMappingURL=store.d.ts.map