@adonisjs/http-server 6.8.2-5 → 6.8.2-7

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 +142 -0
  80. package/build/src/router/store.d.ts +54 -1
  81. package/build/src/router/store.js +102 -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 +43 -70
  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,65 @@ 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
+ /**
17
+ * The route class exposes the APIs for constructing a route using the
18
+ * fluent API.
19
+ */
8
20
  export class Route extends Macroable {
21
+ /**
22
+ * Route pattern
23
+ */
9
24
  #pattern;
25
+ /**
26
+ * HTTP Methods for the route
27
+ */
10
28
  #methods;
29
+ /**
30
+ * A unique name for the route
31
+ */
11
32
  #name;
33
+ /**
34
+ * A boolean to prevent route from getting registered within
35
+ * the store.
36
+ *
37
+ * This flag must be set before "Router.commit" method
38
+ */
12
39
  #isDeleted = false;
40
+ /**
41
+ * Route handler
42
+ */
13
43
  #handler;
44
+ /**
45
+ * Matchers inherited from the router
46
+ */
14
47
  #globalMatchers;
48
+ /**
49
+ * Reference to the AdonisJS application
50
+ */
15
51
  #app;
52
+ /**
53
+ * Middleware registered on the router
54
+ */
16
55
  #routerMiddleware;
56
+ /**
57
+ * By default the route is part of the `root` domain. Root domain is used
58
+ * when no domain is defined
59
+ */
17
60
  #routeDomain = 'root';
61
+ /**
62
+ * An object of matchers to be forwarded to the store. The matchers
63
+ * list is populated by calling `where` method
64
+ */
18
65
  #matchers = {};
66
+ /**
67
+ * Custom prefixes defined on the route or the route parent
68
+ * groups
69
+ */
19
70
  #prefixes = [];
71
+ /**
72
+ * Middleware defined directly on the route or the route parent
73
+ * routes. We mantain an array for each layer of the stack
74
+ */
20
75
  #middleware = [];
21
76
  constructor(app, routerMiddleware, options) {
22
77
  super();
@@ -27,6 +82,10 @@ export class Route extends Macroable {
27
82
  this.#handler = this.#resolveRouteHandle(options.handler);
28
83
  this.#globalMatchers = options.globalMatchers;
29
84
  }
85
+ /**
86
+ * Resolves the route handler string expression to a
87
+ * handler method object
88
+ */
30
89
  #resolveRouteHandle(handler) {
31
90
  if (typeof handler === 'string') {
32
91
  return {
@@ -34,13 +93,23 @@ export class Route extends Macroable {
34
93
  ...moduleExpression(handler, this.#app.appRoot).toHandleMethod(),
35
94
  };
36
95
  }
96
+ /**
97
+ * Using a lazily imported controller
98
+ */
37
99
  if (Array.isArray(handler)) {
100
+ /**
101
+ * The first item of the tuple is a class constructor
102
+ */
38
103
  if (is.class_(handler[0])) {
39
104
  return {
40
105
  reference: handler,
41
106
  ...moduleCaller(handler[0], (handler[1] || 'handle')).toHandleMethod(),
42
107
  };
43
108
  }
109
+ /**
110
+ * The first item of the tuple is a function that lazily
111
+ * loads the controller
112
+ */
44
113
  return {
45
114
  reference: handler,
46
115
  ...moduleImporter(handler[0], (handler[1] || 'handle')).toHandleMethod(),
@@ -48,9 +117,17 @@ export class Route extends Macroable {
48
117
  }
49
118
  return handler;
50
119
  }
120
+ /**
121
+ * Returns an object of param matchers by merging global and local
122
+ * matchers. The local copy is given preference over the global
123
+ * one's
124
+ */
51
125
  #getMatchers() {
52
126
  return { ...this.#globalMatchers, ...this.#matchers };
53
127
  }
128
+ /**
129
+ * Returns a normalized pattern string by prefixing the `prefix` (if defined).
130
+ */
54
131
  #computePattern() {
55
132
  const pattern = dropSlash(this.#pattern);
56
133
  const prefix = this.#prefixes
@@ -60,6 +137,20 @@ export class Route extends Macroable {
60
137
  .join('');
61
138
  return prefix ? `${prefix}${pattern === '/' ? '' : pattern}` : pattern;
62
139
  }
140
+ /**
141
+ * Define matcher for a given param. If a matcher exists, then we do not
142
+ * override that, since the routes inside a group will set matchers
143
+ * before the group, so they should have priority over the group
144
+ * matchers.
145
+ *
146
+ * ```ts
147
+ * Route.group(() => {
148
+ * Route.get('/:id', 'handler').where('id', /^[0-9]$/)
149
+ * }).where('id', /[^a-z$]/)
150
+ * ```
151
+ *
152
+ * The `/^[0-9]$/` will win over the matcher defined by the group
153
+ */
63
154
  where(param, matcher) {
64
155
  if (this.#matchers[param]) {
65
156
  return this;
@@ -75,23 +166,47 @@ export class Route extends Macroable {
75
166
  }
76
167
  return this;
77
168
  }
169
+ /**
170
+ * Define prefix for the route. Calling this method multiple times
171
+ * applies multiple prefixes in the reverse order.
172
+ */
78
173
  prefix(prefix) {
79
174
  this.#prefixes.push(prefix);
80
175
  return this;
81
176
  }
177
+ /**
178
+ * Define a custom domain for the route. We do not overwrite the domain
179
+ * unless `overwrite` flag is set to true.
180
+ */
82
181
  domain(domain, overwrite = false) {
83
182
  if (this.#routeDomain === 'root' || overwrite) {
84
183
  this.#routeDomain = domain;
85
184
  }
86
185
  return this;
87
186
  }
187
+ /**
188
+ * Define one or more middleware to be executed before the route
189
+ * handler.
190
+ *
191
+ * Named middleware can be referenced using the name registered with
192
+ * the router middleware store.
193
+ */
88
194
  use(middleware) {
89
195
  this.#middleware.push(Array.isArray(middleware) ? middleware : [middleware]);
90
196
  return this;
91
197
  }
198
+ /**
199
+ * @alias use
200
+ */
92
201
  middleware(middleware) {
93
202
  return this.use(middleware);
94
203
  }
204
+ /**
205
+ * Give a unique name to the route. Assinging a new unique removes the
206
+ * existing name of the route.
207
+ *
208
+ * Setting prepends to true prefixes the name to the existing name.
209
+ */
95
210
  as(name, prepend = false) {
96
211
  if (prepend) {
97
212
  if (!this.#name) {
@@ -103,31 +218,58 @@ export class Route extends Macroable {
103
218
  this.#name = name;
104
219
  return this;
105
220
  }
221
+ /**
222
+ * Check if the route was marked to be deleted
223
+ */
106
224
  isDeleted() {
107
225
  return this.#isDeleted;
108
226
  }
227
+ /**
228
+ * Mark route as deleted. Deleted routes are not registered
229
+ * with the route store
230
+ */
109
231
  markAsDeleted() {
110
232
  this.#isDeleted = true;
111
233
  }
234
+ /**
235
+ * Get the route name
236
+ */
112
237
  getName() {
113
238
  return this.#name;
114
239
  }
240
+ /**
241
+ * Get the route pattern
242
+ */
115
243
  getPattern() {
116
244
  return this.#pattern;
117
245
  }
246
+ /**
247
+ * Set the route pattern
248
+ */
118
249
  setPattern(pattern) {
119
250
  this.#pattern = pattern;
120
251
  return this;
121
252
  }
253
+ /**
254
+ * Returns the stack of middleware registered on the route.
255
+ * The value is shared by reference.
256
+ */
122
257
  getMiddleware() {
123
258
  return this.#middleware;
124
259
  }
260
+ /**
261
+ * Returns the middleware instance for persistence inside the
262
+ * store
263
+ */
125
264
  #getMiddlewareForStore() {
126
265
  const middleware = new Middleware();
127
266
  this.#routerMiddleware.forEach((one) => middleware.add(one));
128
267
  this.#middleware.flat().forEach((one) => middleware.add(one));
129
268
  return middleware;
130
269
  }
270
+ /**
271
+ * Returns JSON representation of the route
272
+ */
131
273
  toJSON() {
132
274
  return {
133
275
  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