@adonisjs/http-server 6.8.2-1 → 6.8.2-11

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 (103) hide show
  1. package/LICENSE.md +1 -1
  2. package/README.md +3 -6
  3. package/build/factories/http_context.d.ts +10 -0
  4. package/build/factories/http_context.js +27 -0
  5. package/build/factories/http_server.d.ts +8 -0
  6. package/build/factories/http_server.js +26 -0
  7. package/build/factories/main.js +8 -0
  8. package/build/factories/qs_parser_factory.d.ts +10 -0
  9. package/build/factories/qs_parser_factory.js +18 -0
  10. package/build/factories/request.d.ts +10 -0
  11. package/build/factories/request.js +31 -0
  12. package/build/factories/response.d.ts +10 -0
  13. package/build/factories/response.js +34 -0
  14. package/build/factories/router.d.ts +10 -0
  15. package/build/factories/router.js +25 -0
  16. package/build/factories/server_factory.d.ts +10 -0
  17. package/build/factories/server_factory.js +34 -0
  18. package/build/index.js +8 -0
  19. package/build/src/cookies/client.d.ts +25 -0
  20. package/build/src/cookies/client.js +42 -0
  21. package/build/src/cookies/drivers/encrypted.d.ts +12 -0
  22. package/build/src/cookies/drivers/encrypted.js +20 -0
  23. package/build/src/cookies/drivers/plain.d.ts +12 -0
  24. package/build/src/cookies/drivers/plain.js +20 -0
  25. package/build/src/cookies/drivers/signed.d.ts +12 -0
  26. package/build/src/cookies/drivers/signed.js +20 -0
  27. package/build/src/cookies/parser.d.ts +28 -0
  28. package/build/src/cookies/parser.js +98 -0
  29. package/build/src/cookies/serializer.d.ts +22 -0
  30. package/build/src/cookies/serializer.js +40 -0
  31. package/build/src/debug.js +8 -0
  32. package/build/src/define_config.d.ts +3 -0
  33. package/build/src/define_config.js +11 -0
  34. package/build/src/define_middleware.d.ts +5 -0
  35. package/build/src/define_middleware.js +18 -0
  36. package/build/src/exception_handler.d.ts +93 -6
  37. package/build/src/exception_handler.js +203 -44
  38. package/build/src/exceptions.d.ts +6 -0
  39. package/build/src/exceptions.js +11 -0
  40. package/build/src/helpers.d.ts +14 -0
  41. package/build/src/helpers.js +43 -0
  42. package/build/src/http_context/local_storage.d.ts +3 -0
  43. package/build/src/http_context/local_storage.js +25 -0
  44. package/build/src/http_context/main.d.ts +36 -0
  45. package/build/src/http_context/main.js +54 -0
  46. package/build/src/qs.d.ts +4 -0
  47. package/build/src/qs.js +12 -0
  48. package/build/src/redirect.d.ts +24 -0
  49. package/build/src/redirect.js +60 -0
  50. package/build/src/request.d.ts +466 -0
  51. package/build/src/request.js +542 -0
  52. package/build/src/response.d.ts +427 -2
  53. package/build/src/response.js +611 -11
  54. package/build/src/router/brisk.d.ts +22 -0
  55. package/build/src/router/brisk.js +44 -2
  56. package/build/src/router/executor.d.ts +6 -1
  57. package/build/src/router/executor.js +14 -1
  58. package/build/src/router/factories/use_return_value.d.ts +4 -0
  59. package/build/src/router/factories/use_return_value.js +16 -3
  60. package/build/src/router/group.d.ts +47 -0
  61. package/build/src/router/group.js +88 -0
  62. package/build/src/router/lookup_store/main.d.ts +32 -0
  63. package/build/src/router/lookup_store/main.js +49 -0
  64. package/build/src/router/lookup_store/route_finder.d.ts +13 -0
  65. package/build/src/router/lookup_store/route_finder.js +21 -0
  66. package/build/src/router/lookup_store/url_builder.d.ts +36 -0
  67. package/build/src/router/lookup_store/url_builder.js +99 -2
  68. package/build/src/router/main.d.ts +92 -4
  69. package/build/src/router/main.js +146 -0
  70. package/build/src/router/matchers.d.ts +13 -0
  71. package/build/src/router/matchers.js +21 -0
  72. package/build/src/router/parser.d.ts +5 -0
  73. package/build/src/router/parser.js +17 -0
  74. package/build/src/router/resource.d.ts +30 -1
  75. package/build/src/router/resource.js +93 -1
  76. package/build/src/router/route.d.ts +65 -0
  77. package/build/src/router/route.js +151 -2
  78. package/build/src/router/store.d.ts +54 -0
  79. package/build/src/router/store.js +110 -2
  80. package/build/src/server/factories/final_handler.d.ts +7 -1
  81. package/build/src/server/factories/final_handler.js +15 -2
  82. package/build/src/server/factories/middleware_handler.d.ts +3 -0
  83. package/build/src/server/factories/middleware_handler.js +11 -0
  84. package/build/src/server/factories/write_response.d.ts +4 -0
  85. package/build/src/server/factories/write_response.js +12 -0
  86. package/build/src/server/main.d.ts +48 -0
  87. package/build/src/server/main.js +142 -5
  88. package/build/src/types/base.d.ts +12 -0
  89. package/build/src/types/base.js +8 -0
  90. package/build/src/types/main.js +8 -0
  91. package/build/src/types/middleware.d.ts +18 -0
  92. package/build/src/types/middleware.js +8 -0
  93. package/build/src/types/qs.d.ts +53 -0
  94. package/build/src/types/qs.js +8 -0
  95. package/build/src/types/request.d.ts +32 -0
  96. package/build/src/types/request.js +8 -0
  97. package/build/src/types/response.d.ts +29 -2
  98. package/build/src/types/response.js +8 -0
  99. package/build/src/types/route.d.ts +89 -1
  100. package/build/src/types/route.js +8 -0
  101. package/build/src/types/server.d.ts +36 -1
  102. package/build/src/types/server.js +8 -0
  103. package/package.json +52 -74
@@ -1,35 +1,92 @@
1
- import matchit from '@poppinss/matchit';
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
+ */
2
9
  import { RuntimeException } from '@poppinss/utils';
10
+ import { parseRoutePattern } from '../parser.js';
11
+ /**
12
+ * URL builder class is used to create URIs for pre-registered
13
+ * routes.
14
+ *
15
+ * ```ts
16
+ * const builder = new UrlBuilder(encryption, routeFinder)
17
+ *
18
+ * builder
19
+ * .qs({ sort: 'id' })
20
+ * .params([category.id])
21
+ * .make('categories.posts.index')
22
+ * ```
23
+ */
3
24
  export class UrlBuilder {
25
+ /**
26
+ * Query string parser
27
+ */
4
28
  #qsParser;
29
+ /**
30
+ * The parameters to apply on the route
31
+ */
5
32
  #params = {};
33
+ /**
34
+ * Query string to append to the route
35
+ */
6
36
  #qs = {};
37
+ /**
38
+ * Should we perform the route lookup or just build the
39
+ * given pattern as it is.
40
+ */
7
41
  #shouldPerformLookup = true;
42
+ /**
43
+ * BaseURL to append to the constructored URL
44
+ */
8
45
  #baseUrl;
46
+ /**
47
+ * Encryption class for making signed URLs
48
+ */
9
49
  #encryption;
50
+ /**
51
+ * Route finder for finding route pattern
52
+ */
10
53
  #routeFinder;
11
54
  constructor(encryption, routeFinder, qsParser) {
12
55
  this.#qsParser = qsParser;
13
56
  this.#encryption = encryption;
14
57
  this.#routeFinder = routeFinder;
15
58
  }
59
+ /**
60
+ * Raises exception when wildcard values array is missing or
61
+ * has length of zero.
62
+ */
16
63
  #ensureHasWildCardValues(pattern, values) {
17
64
  if (!values || !Array.isArray(values) || !values.length) {
18
65
  throw new RuntimeException(`Cannot make URL for "${pattern}" route. Invalid value provided for wildcard param`);
19
66
  }
20
67
  }
68
+ /*
69
+ * Raises exception when value is not defined
70
+ */
21
71
  #ensureHasParamValue(pattern, param, value) {
22
72
  if (value === undefined || value === null) {
23
73
  throw new RuntimeException(`Cannot make URL for "${pattern}" route. Missing value for "${param}" param`);
24
74
  }
25
75
  }
76
+ /**
77
+ * Processes the pattern against the params
78
+ */
26
79
  #processPattern(pattern) {
27
80
  const uriSegments = [];
28
81
  const paramsArray = Array.isArray(this.#params) ? this.#params : null;
29
82
  const paramsObject = !Array.isArray(this.#params) ? this.#params : {};
30
83
  let paramsIndex = 0;
31
- const tokens = matchit.parse(pattern);
84
+ const tokens = parseRoutePattern(pattern);
32
85
  for (const token of tokens) {
86
+ /**
87
+ * Expected wildcard param to be at the end always and hence
88
+ * we must break out from the loop
89
+ */
33
90
  if (token.type === 0) {
34
91
  uriSegments.push(`${token.val}${token.end}`);
35
92
  }
@@ -42,6 +99,9 @@ export class UrlBuilder {
42
99
  else {
43
100
  const paramName = token.val;
44
101
  const value = paramsArray ? paramsArray[paramsIndex] : paramsObject[paramName];
102
+ /**
103
+ * Type = 1 means param is required
104
+ */
45
105
  if (token.type === 1) {
46
106
  this.#ensureHasParamValue(pattern, paramName, value);
47
107
  }
@@ -53,6 +113,9 @@ export class UrlBuilder {
53
113
  }
54
114
  return `/${uriSegments.join('/')}`;
55
115
  }
116
+ /**
117
+ * Suffix the query string to the URL
118
+ */
56
119
  #suffixQueryString(url, qs) {
57
120
  if (qs) {
58
121
  const queryString = this.#qsParser.stringify(qs);
@@ -60,17 +123,30 @@ export class UrlBuilder {
60
123
  }
61
124
  return url;
62
125
  }
126
+ /**
127
+ * Prefixes base URL to the uri string
128
+ */
63
129
  #prefixBaseUrl(uri) {
64
130
  return this.#baseUrl ? `${this.#baseUrl}${uri}` : uri;
65
131
  }
132
+ /**
133
+ * Prefix a custom base URL to the final URI
134
+ */
66
135
  prefixUrl(url) {
67
136
  this.#baseUrl = url;
68
137
  return this;
69
138
  }
139
+ /**
140
+ * Disable route lookup. Calling this method considers
141
+ * the "identifier" as the route pattern
142
+ */
70
143
  disableRouteLookup() {
71
144
  this.#shouldPerformLookup = false;
72
145
  return this;
73
146
  }
147
+ /**
148
+ * Append query string to the final URI
149
+ */
74
150
  qs(queryString) {
75
151
  if (!queryString) {
76
152
  return this;
@@ -78,6 +154,9 @@ export class UrlBuilder {
78
154
  this.#qs = queryString;
79
155
  return this;
80
156
  }
157
+ /**
158
+ * Specify params to apply to the route pattern
159
+ */
81
160
  params(params) {
82
161
  if (!params) {
83
162
  return this;
@@ -85,6 +164,11 @@ export class UrlBuilder {
85
164
  this.#params = params;
86
165
  return this;
87
166
  }
167
+ /**
168
+ * Generate URL for the given route identifier. The identifier can be the
169
+ * route name, controller.method name or the route pattern
170
+ * itself.
171
+ */
88
172
  make(identifier) {
89
173
  let url;
90
174
  if (this.#shouldPerformLookup) {
@@ -96,6 +180,11 @@ export class UrlBuilder {
96
180
  }
97
181
  return this.#suffixQueryString(this.#prefixBaseUrl(url), this.#qs);
98
182
  }
183
+ /**
184
+ * Generate a signed URL for the given route identifier. The identifier can be the
185
+ * route name, controller.method name or the route pattern
186
+ * itself.
187
+ */
99
188
  makeSigned(identifier, options) {
100
189
  let url;
101
190
  if (this.#shouldPerformLookup) {
@@ -105,6 +194,14 @@ export class UrlBuilder {
105
194
  else {
106
195
  url = this.#processPattern(identifier);
107
196
  }
197
+ /*
198
+ * Making the signature from the qualified url. We do not prefix the domain when
199
+ * making signature, since it just makes the signature big.
200
+ *
201
+ * There might be a case, when someone wants to generate signature for the same route
202
+ * on their 2 different domains, but we ignore that case for now and can consider
203
+ * it later (when someone asks for it)
204
+ */
108
205
  const signature = this.#encryption.verifier.sign(this.#suffixQueryString(url, this.#qs), options?.expiresIn, options?.purpose);
109
206
  const qs = Object.assign({}, this.#qs, { signature });
110
207
  return this.#suffixQueryString(this.#prefixBaseUrl(url), qs);
@@ -5,36 +5,124 @@ import { Route } from './route.js';
5
5
  import { RouteGroup } from './group.js';
6
6
  import { BriskRoute } from './brisk.js';
7
7
  import { RouteResource } from './resource.js';
8
- import type { Constructor, LazyImport } from '../types/base.js';
9
8
  import { LookupStore } from './lookup_store/main.js';
10
9
  import { RouteMatchers as Matchers } from './matchers.js';
10
+ import type { Constructor, LazyImport } from '../types/base.js';
11
11
  import type { MiddlewareAsClass, ParsedGlobalMiddleware } from '../types/middleware.js';
12
- import type { RouteFn, MatchedRoute, RouteMatcher, MakeUrlOptions, MakeSignedUrlOptions, GetControllerHandlers } from '../types/route.js';
12
+ import type { RouteFn, MatchedRoute, RouteMatcher, RouteMatchers, MakeUrlOptions, MakeSignedUrlOptions, GetControllerHandlers } from '../types/route.js';
13
+ /**
14
+ * Router class exposes a unified API to register new routes, group them or
15
+ * create route resources.
16
+ *
17
+ * ```ts
18
+ * const router = new Router()
19
+ *
20
+ * router.get('/', async function () {
21
+ * // handle request
22
+ * })
23
+ * ```
24
+ */
13
25
  export declare class Router extends LookupStore {
14
26
  #private;
27
+ /**
28
+ * Collection of routes, including route resource and route
29
+ * group. To get a flat list of routes, call `router.toJSON()`
30
+ */
15
31
  routes: (Route | RouteResource | RouteGroup | BriskRoute)[];
32
+ /**
33
+ * A flag to know if routes for explicit domains have been registered.
34
+ * The boolean is computed after calling the "commit" method.
35
+ */
16
36
  usingDomains: boolean;
37
+ /**
38
+ * Shortcut methods for commonly used route matchers
39
+ */
17
40
  matchers: Matchers;
18
41
  constructor(app: Application<any>, encryption: Encryption, qsParser: Qs);
42
+ /**
43
+ * Parses the route pattern
44
+ */
45
+ parsePattern(pattern: string, matchers?: RouteMatchers): import("../types/route.js").MatchItRouteToken[];
46
+ /**
47
+ * Define an array of middleware to use on all the routes.
48
+ * Calling this method multiple times pushes to the
49
+ * existing list of middleware
50
+ */
19
51
  use(middleware: LazyImport<MiddlewareAsClass>[]): this;
52
+ /**
53
+ * Define a collection of named middleware. The defined collection is
54
+ * not registered anywhere, but instead converted in a new collection
55
+ * of functions you can apply on the routes, or router groups.
56
+ */
20
57
  named<NamedMiddleware extends Record<string, LazyImport<MiddlewareAsClass>>>(collection: NamedMiddleware): { [K in keyof NamedMiddleware]: <Args extends import("../types/middleware.js").GetMiddlewareArgs<import("../types/base.js").UnWrapLazyImport<NamedMiddleware[K]>>>(...args: Args) => {
21
58
  name: K;
22
59
  args: Args[0];
23
60
  } & ParsedGlobalMiddleware; };
61
+ /**
62
+ * Add route for a given pattern and methods
63
+ */
24
64
  route<T extends Constructor<any>>(pattern: string, methods: string[], handler: string | RouteFn | [LazyImport<T> | T, GetControllerHandlers<T>?]): Route<T>;
65
+ /**
66
+ * Define a route that handles all common HTTP methods
67
+ */
25
68
  any<T extends Constructor<any>>(pattern: string, handler: string | RouteFn | [LazyImport<T> | T, GetControllerHandlers<T>?]): Route<T>;
69
+ /**
70
+ * Define `GET` route
71
+ */
26
72
  get<T extends Constructor<any>>(pattern: string, handler: string | RouteFn | [LazyImport<T> | T, GetControllerHandlers<T>?]): Route<T>;
73
+ /**
74
+ * Define `POST` route
75
+ */
27
76
  post<T extends Constructor<any>>(pattern: string, handler: string | RouteFn | [LazyImport<T> | T, GetControllerHandlers<T>?]): Route<T>;
77
+ /**
78
+ * Define `PUT` route
79
+ */
28
80
  put<T extends Constructor<any>>(pattern: string, handler: string | RouteFn | [LazyImport<T> | T, GetControllerHandlers<T>?]): Route<T>;
81
+ /**
82
+ * Define `PATCH` route
83
+ */
29
84
  patch<T extends Constructor<any>>(pattern: string, handler: string | RouteFn | [LazyImport<T> | T, GetControllerHandlers<T>?]): Route<T>;
85
+ /**
86
+ * Define `DELETE` route
87
+ */
30
88
  delete<T extends Constructor<any>>(pattern: string, handler: string | RouteFn | [LazyImport<T> | T, GetControllerHandlers<T>?]): Route<T>;
89
+ /**
90
+ * Creates a group of routes. A route group can apply transforms
91
+ * to routes in bulk
92
+ */
31
93
  group(callback: () => void): RouteGroup;
32
- resource(resource: string, controller: string): RouteResource;
33
- shallowResource(resource: string, controller: string): RouteResource;
94
+ /**
95
+ * Registers a route resource with conventional set of routes
96
+ */
97
+ resource(resource: string, controller: string | LazyImport<Constructor<any>> | Constructor<any>): RouteResource;
98
+ /**
99
+ * Register a route resource with shallow nested routes.
100
+ */
101
+ shallowResource(resource: string, controller: string | LazyImport<Constructor<any>> | Constructor<any>): RouteResource;
102
+ /**
103
+ * Returns a brisk route instance for a given URL pattern
104
+ */
34
105
  on(pattern: string): BriskRoute;
106
+ /**
107
+ * Define matcher for a given param. The global params are applied
108
+ * on all the routes (unless overridden at the route level).
109
+ */
35
110
  where(param: string, matcher: RouteMatcher | string | RegExp): this;
111
+ /**
112
+ * Commit routes to the store. The router is freezed after the
113
+ * commit method is called.
114
+ */
36
115
  commit(): void;
116
+ /**
117
+ * Find route for a given URL, method and optionally domain
118
+ */
37
119
  match(url: string, method: string, hostname?: string | null): null | MatchedRoute;
120
+ /**
121
+ * Make URL to a pre-registered route
122
+ */
38
123
  makeUrl(routeIdentifier: string, params?: any[] | Record<string, any>, options?: MakeUrlOptions): string;
124
+ /**
125
+ * Makes a signed URL to a pre-registered route.
126
+ */
39
127
  makeSignedUrl(routeIdentifier: string, params?: any[] | Record<string, any>, options?: MakeSignedUrlOptions): string;
40
128
  }
@@ -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 { moduleImporter } from '@adonisjs/fold';
3
11
  import { RuntimeException } from '@poppinss/utils';
@@ -10,19 +18,63 @@ import { RouteResource } from './resource.js';
10
18
  import { LookupStore } from './lookup_store/main.js';
11
19
  import { RouteMatchers as Matchers } from './matchers.js';
12
20
  import { defineNamedMiddleware } from '../define_middleware.js';
21
+ import { parseRoutePattern } from './parser.js';
22
+ /**
23
+ * Router class exposes a unified API to register new routes, group them or
24
+ * create route resources.
25
+ *
26
+ * ```ts
27
+ * const router = new Router()
28
+ *
29
+ * router.get('/', async function () {
30
+ * // handle request
31
+ * })
32
+ * ```
33
+ */
13
34
  export class Router extends LookupStore {
35
+ /**
36
+ * Application is needed to resolve string based controller expressions
37
+ */
14
38
  #app;
39
+ /**
40
+ * Store with tokenized routes
41
+ */
15
42
  #store = new RoutesStore();
43
+ /**
44
+ * Global matchers to test route params against regular expressions.
45
+ */
16
46
  #globalMatchers = {};
47
+ /**
48
+ * Middleware store to be shared with the routes
49
+ */
17
50
  #middleware = [];
51
+ /**
52
+ * A boolean to tell the router that a group is in
53
+ * open state right now
54
+ */
18
55
  #openedGroups = [];
56
+ /**
57
+ * Collection of routes, including route resource and route
58
+ * group. To get a flat list of routes, call `router.toJSON()`
59
+ */
19
60
  routes = [];
61
+ /**
62
+ * A flag to know if routes for explicit domains have been registered.
63
+ * The boolean is computed after calling the "commit" method.
64
+ */
20
65
  usingDomains = false;
66
+ /**
67
+ * Shortcut methods for commonly used route matchers
68
+ */
21
69
  matchers = new Matchers();
22
70
  constructor(app, encryption, qsParser) {
23
71
  super(encryption, qsParser);
24
72
  this.#app = app;
25
73
  }
74
+ /**
75
+ * Push a give router entity to the list of routes or the
76
+ * recently opened group.
77
+ */
26
78
  #pushToRoutes(entity) {
27
79
  const openedGroup = this.#openedGroups[this.#openedGroups.length - 1];
28
80
  if (openedGroup) {
@@ -31,13 +83,32 @@ export class Router extends LookupStore {
31
83
  }
32
84
  this.routes.push(entity);
33
85
  }
86
+ /**
87
+ * Parses the route pattern
88
+ */
89
+ parsePattern(pattern, matchers) {
90
+ return parseRoutePattern(pattern, matchers);
91
+ }
92
+ /**
93
+ * Define an array of middleware to use on all the routes.
94
+ * Calling this method multiple times pushes to the
95
+ * existing list of middleware
96
+ */
34
97
  use(middleware) {
35
98
  middleware.forEach((one) => this.#middleware.push(moduleImporter(one, 'handle').toHandleMethod()));
36
99
  return this;
37
100
  }
101
+ /**
102
+ * Define a collection of named middleware. The defined collection is
103
+ * not registered anywhere, but instead converted in a new collection
104
+ * of functions you can apply on the routes, or router groups.
105
+ */
38
106
  named(collection) {
39
107
  return defineNamedMiddleware(collection);
40
108
  }
109
+ /**
110
+ * Add route for a given pattern and methods
111
+ */
41
112
  route(pattern, methods, handler) {
42
113
  const route = new Route(this.#app, this.#middleware, {
43
114
  pattern,
@@ -48,32 +119,74 @@ export class Router extends LookupStore {
48
119
  this.#pushToRoutes(route);
49
120
  return route;
50
121
  }
122
+ /**
123
+ * Define a route that handles all common HTTP methods
124
+ */
51
125
  any(pattern, handler) {
52
126
  return this.route(pattern, ['HEAD', 'OPTIONS', 'GET', 'POST', 'PUT', 'PATCH', 'DELETE'], handler);
53
127
  }
128
+ /**
129
+ * Define `GET` route
130
+ */
54
131
  get(pattern, handler) {
55
132
  return this.route(pattern, ['GET', 'HEAD'], handler);
56
133
  }
134
+ /**
135
+ * Define `POST` route
136
+ */
57
137
  post(pattern, handler) {
58
138
  return this.route(pattern, ['POST'], handler);
59
139
  }
140
+ /**
141
+ * Define `PUT` route
142
+ */
60
143
  put(pattern, handler) {
61
144
  return this.route(pattern, ['PUT'], handler);
62
145
  }
146
+ /**
147
+ * Define `PATCH` route
148
+ */
63
149
  patch(pattern, handler) {
64
150
  return this.route(pattern, ['PATCH'], handler);
65
151
  }
152
+ /**
153
+ * Define `DELETE` route
154
+ */
66
155
  delete(pattern, handler) {
67
156
  return this.route(pattern, ['DELETE'], handler);
68
157
  }
158
+ /**
159
+ * Creates a group of routes. A route group can apply transforms
160
+ * to routes in bulk
161
+ */
69
162
  group(callback) {
163
+ /*
164
+ * Create a new group with empty set of routes
165
+ */
70
166
  const group = new RouteGroup([]);
167
+ /**
168
+ * Track group
169
+ */
71
170
  this.#pushToRoutes(group);
171
+ /*
172
+ * Track the group, so that the upcoming calls inside the callback
173
+ * can use this group
174
+ */
72
175
  this.#openedGroups.push(group);
176
+ /*
177
+ * Execute the callback. Now all registered routes will be
178
+ * collected seperately from the `routes` array
179
+ */
73
180
  callback();
181
+ /*
182
+ * Now the callback is over, get rid of the opened group
183
+ */
74
184
  this.#openedGroups.pop();
75
185
  return group;
76
186
  }
187
+ /**
188
+ * Registers a route resource with conventional set of routes
189
+ */
77
190
  resource(resource, controller) {
78
191
  const resourceInstance = new RouteResource(this.#app, this.#middleware, {
79
192
  resource,
@@ -84,6 +197,9 @@ export class Router extends LookupStore {
84
197
  this.#pushToRoutes(resourceInstance);
85
198
  return resourceInstance;
86
199
  }
200
+ /**
201
+ * Register a route resource with shallow nested routes.
202
+ */
87
203
  shallowResource(resource, controller) {
88
204
  const resourceInstance = new RouteResource(this.#app, this.#middleware, {
89
205
  resource,
@@ -94,6 +210,9 @@ export class Router extends LookupStore {
94
210
  this.#pushToRoutes(resourceInstance);
95
211
  return resourceInstance;
96
212
  }
213
+ /**
214
+ * Returns a brisk route instance for a given URL pattern
215
+ */
97
216
  on(pattern) {
98
217
  const briskRoute = new BriskRoute(this.#app, this.#middleware, {
99
218
  pattern,
@@ -102,6 +221,10 @@ export class Router extends LookupStore {
102
221
  this.#pushToRoutes(briskRoute);
103
222
  return briskRoute;
104
223
  }
224
+ /**
225
+ * Define matcher for a given param. The global params are applied
226
+ * on all the routes (unless overridden at the route level).
227
+ */
105
228
  where(param, matcher) {
106
229
  if (typeof matcher === 'string') {
107
230
  this.#globalMatchers[param] = { match: new RegExp(matcher) };
@@ -114,6 +237,10 @@ export class Router extends LookupStore {
114
237
  }
115
238
  return this;
116
239
  }
240
+ /**
241
+ * Commit routes to the store. The router is freezed after the
242
+ * commit method is called.
243
+ */
117
244
  commit() {
118
245
  const routeNamesByDomain = new Map();
119
246
  toRoutesJSON(this.routes).forEach((route) => {
@@ -121,12 +248,22 @@ export class Router extends LookupStore {
121
248
  routeNamesByDomain.set(route.domain, new Set());
122
249
  }
123
250
  const routeNames = routeNamesByDomain.get(route.domain);
251
+ /*
252
+ * Raise error when route name is already in use. Route names have to be unique
253
+ * to ensure that only one route is returned during lookup.
254
+ */
124
255
  if (route.name && routeNames.has(route.name)) {
125
256
  throw new RuntimeException(`Route with duplicate name found. A route with name "${route.name}" already exists`);
126
257
  }
258
+ /*
259
+ * If route has a unique, then track the name for checking duplicates
260
+ */
127
261
  if (route.name) {
128
262
  routeNames.add(route.name);
129
263
  }
264
+ /**
265
+ * Register the route with the lookup store
266
+ */
130
267
  this.register(route);
131
268
  this.#store.add(route);
132
269
  });
@@ -136,6 +273,9 @@ export class Router extends LookupStore {
136
273
  this.#globalMatchers = {};
137
274
  this.#middleware = [];
138
275
  }
276
+ /**
277
+ * Find route for a given URL, method and optionally domain
278
+ */
139
279
  match(url, method, hostname) {
140
280
  const matchingDomain = this.#store.matchDomain(hostname);
141
281
  return matchingDomain.length
@@ -145,6 +285,9 @@ export class Router extends LookupStore {
145
285
  })
146
286
  : this.#store.match(url, method);
147
287
  }
288
+ /**
289
+ * Make URL to a pre-registered route
290
+ */
148
291
  makeUrl(routeIdentifier, params, options) {
149
292
  const normalizedOptions = Object.assign({}, options);
150
293
  const builder = normalizedOptions.domain
@@ -156,6 +299,9 @@ export class Router extends LookupStore {
156
299
  normalizedOptions.disableRouteLookup && builder.disableRouteLookup();
157
300
  return builder.make(routeIdentifier);
158
301
  }
302
+ /**
303
+ * Makes a signed URL to a pre-registered route.
304
+ */
159
305
  makeSignedUrl(routeIdentifier, params, options) {
160
306
  const normalizedOptions = Object.assign({}, options);
161
307
  const builder = normalizedOptions.domain
@@ -1,13 +1,26 @@
1
1
  import Macroable from '@poppinss/macroable';
2
+ /**
3
+ * Shortcut methods for commonly used route matchers
4
+ */
2
5
  export declare class RouteMatchers extends Macroable {
6
+ /**
7
+ * Enforce value to be a number and also casts it to number data
8
+ * type
9
+ */
3
10
  number(): {
4
11
  match: RegExp;
5
12
  cast: (value: string) => number;
6
13
  };
14
+ /**
15
+ * Enforce value to be formatted as uuid
16
+ */
7
17
  uuid(): {
8
18
  match: RegExp;
9
19
  cast: (value: string) => string;
10
20
  };
21
+ /**
22
+ * Enforce value to be formatted as slug
23
+ */
11
24
  slug(): {
12
25
  match: RegExp;
13
26
  };
@@ -1,14 +1,35 @@
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 Macroable from '@poppinss/macroable';
10
+ /**
11
+ * Shortcut methods for commonly used route matchers
12
+ */
2
13
  export class RouteMatchers extends Macroable {
14
+ /**
15
+ * Enforce value to be a number and also casts it to number data
16
+ * type
17
+ */
3
18
  number() {
4
19
  return { match: /^[0-9]+$/, cast: (value) => Number(value) };
5
20
  }
21
+ /**
22
+ * Enforce value to be formatted as uuid
23
+ */
6
24
  uuid() {
7
25
  return {
8
26
  match: /^[0-9a-zA-F]{8}-[0-9a-zA-F]{4}-[0-9a-zA-F]{4}-[0-9a-zA-F]{4}-[0-9a-zA-F]{12}$/,
9
27
  cast: (value) => value.toLowerCase(),
10
28
  };
11
29
  }
30
+ /**
31
+ * Enforce value to be formatted as slug
32
+ */
12
33
  slug() {
13
34
  return { match: /^[^\s-_](?!.*?[-_]{2,})([a-z0-9-\\]{1,})[^\s]*[^-_\s]$/ };
14
35
  }
@@ -0,0 +1,5 @@
1
+ import { MatchItRouteToken, RouteMatchers } from '../types/route.js';
2
+ /**
3
+ * Parses the route pattern
4
+ */
5
+ export declare function parseRoutePattern(pattern: string, matchers?: RouteMatchers): MatchItRouteToken[];
@@ -0,0 +1,17 @@
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
+ // @ts-expect-error
10
+ import matchit from '@poppinss/matchit';
11
+ /**
12
+ * Parses the route pattern
13
+ */
14
+ export function parseRoutePattern(pattern, matchers) {
15
+ const tokens = matchit.parse(pattern, matchers);
16
+ return tokens;
17
+ }