@adonisjs/http-server 6.8.2-6 → 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
@@ -1,20 +0,0 @@
1
- /*
2
- * @adonisjs/http-server
3
- *
4
- * (c) AdonisJS
5
- *
6
- * For the full copyright and license information, please view the LICENSE
7
- * file that was distributed with this source code.
8
- */
9
-
10
- // @ts-expect-error
11
- import matchit from '@poppinss/matchit'
12
- import { MatchItRouteToken, RouteMatchers } from '../types/route.js'
13
-
14
- /**
15
- * Parses the route pattern
16
- */
17
- export function parseRoutePattern(pattern: string, matchers?: RouteMatchers): MatchItRouteToken[] {
18
- const tokens = matchit.parse(pattern, matchers)
19
- return tokens
20
- }
@@ -1,277 +0,0 @@
1
- /*
2
- * @adonisjs/http-server
3
- *
4
- * (c) AdonisJS
5
- *
6
- * For the full copyright and license information, please view the LICENSE
7
- * file that was distributed with this source code.
8
- */
9
-
10
- import string from '@poppinss/utils/string'
11
- import Macroable from '@poppinss/macroable'
12
- import { RuntimeException } from '@poppinss/utils'
13
- import type { Application } from '@adonisjs/application'
14
-
15
- import { Route } from './route.js'
16
- import type { Constructor, LazyImport } from '../types/base.js'
17
- import type { ParsedGlobalMiddleware } from '../types/middleware.js'
18
- import type { ResourceActionNames, RouteMatcher, RouteMatchers } from '../types/route.js'
19
-
20
- /**
21
- * Route resource exposes the API to register multiple routes for a resource.
22
- */
23
- export class RouteResource extends Macroable {
24
- /**
25
- * Resource identifier. Nested resources are separated
26
- * with a dot notation
27
- */
28
- #resource: string
29
-
30
- /**
31
- * The controller to handle resource routing requests
32
- */
33
- #controller: string | LazyImport<Constructor<any>> | Constructor<any>
34
-
35
- /**
36
- * Is it a shallow resource? Shallow resources URLs do not have parent
37
- * resource name and id once they can be identified with the id.
38
- */
39
- #shallow: boolean = false
40
-
41
- /**
42
- * Matchers inherited from the router
43
- */
44
- #globalMatchers: RouteMatchers
45
-
46
- /**
47
- * Reference to the AdonisJS application
48
- */
49
- #app: Application<any>
50
-
51
- /**
52
- * Middleware registered on the router
53
- */
54
- #routerMiddleware: ParsedGlobalMiddleware[]
55
-
56
- /**
57
- * Parameter names for the resources. Defaults to `id` for
58
- * a singular resource and `resource_id` for nested
59
- * resources.
60
- */
61
- #params: Record<string, string> = {}
62
-
63
- /**
64
- * Base name for the routes. We suffix action names
65
- * on top of the base name
66
- */
67
- #routesBaseName: string
68
-
69
- /**
70
- * A collection of routes instances that belongs to this resource
71
- */
72
- routes: Route[] = []
73
-
74
- constructor(
75
- app: Application<any>,
76
- routerMiddleware: ParsedGlobalMiddleware[],
77
- options: {
78
- resource: string
79
- controller: string | LazyImport<Constructor<any>> | Constructor<any>
80
- globalMatchers: RouteMatchers
81
- shallow: boolean
82
- }
83
- ) {
84
- super()
85
- this.#validateResourceName(options.resource)
86
-
87
- this.#app = app
88
- this.#shallow = options.shallow
89
- this.#routerMiddleware = routerMiddleware
90
- this.#controller = options.controller
91
- this.#globalMatchers = options.globalMatchers
92
- this.#resource = this.#normalizeResourceName(options.resource)
93
- this.#routesBaseName = this.#getRoutesBaseName()
94
- this.#buildRoutes()
95
- }
96
-
97
- /**
98
- * Normalizes the resource name to dropping leading and trailing
99
- * slashes.
100
- */
101
- #normalizeResourceName(resource: string) {
102
- return resource.replace(/^\//, '').replace(/\/$/, '')
103
- }
104
-
105
- /**
106
- * Ensure resource name is not an empty string
107
- */
108
- #validateResourceName(resource: string) {
109
- if (!resource || resource === '/') {
110
- throw new RuntimeException(`Invalid resource name "${resource}"`)
111
- }
112
- }
113
-
114
- /**
115
- * Converting segments of a resource to snake case to
116
- * make the route name.
117
- */
118
- #getRoutesBaseName() {
119
- return this.#resource
120
- .split('.')
121
- .map((token) => string.snakeCase(token))
122
- .join('.')
123
- }
124
-
125
- /**
126
- * Create a new route for the given pattern, methods and controller action
127
- */
128
- #createRoute(pattern: string, methods: string[], action: ResourceActionNames) {
129
- const route = new Route(this.#app, this.#routerMiddleware, {
130
- pattern,
131
- methods,
132
- handler:
133
- typeof this.#controller === 'string'
134
- ? `${this.#controller}.${action}`
135
- : [this.#controller, action],
136
- globalMatchers: this.#globalMatchers,
137
- })
138
-
139
- route.as(`${this.#routesBaseName}.${action}`)
140
- this.routes.push(route)
141
- }
142
-
143
- /**
144
- * Returns the `resource_id` name for a given resource. The
145
- * resource name is converted to singular form and
146
- * transformed to snake case.
147
- *
148
- * photos becomes photo_id
149
- * users becomes user_id
150
- */
151
- #getResourceId(resource: string) {
152
- return `${string.snakeCase(string.singular(resource))}_id`
153
- }
154
-
155
- /**
156
- * Build routes for the given resource
157
- */
158
- #buildRoutes() {
159
- const resources = this.#resource.split('.')
160
-
161
- const mainResource = resources.pop()!
162
- this.#params[mainResource] = ':id'
163
-
164
- const baseURI = `${resources
165
- .map((resource) => {
166
- const paramName = `:${this.#getResourceId(resource)}`
167
- this.#params[resource] = paramName
168
-
169
- return `${resource}/${paramName}`
170
- })
171
- .join('/')}/${mainResource}`
172
-
173
- this.#createRoute(baseURI, ['GET', 'HEAD'], 'index')
174
- this.#createRoute(`${baseURI}/create`, ['GET', 'HEAD'], 'create')
175
- this.#createRoute(baseURI, ['POST'], 'store')
176
- this.#createRoute(`${this.#shallow ? mainResource : baseURI}/:id`, ['GET', 'HEAD'], 'show')
177
- this.#createRoute(`${this.#shallow ? mainResource : baseURI}/:id/edit`, ['GET', 'HEAD'], 'edit')
178
- this.#createRoute(`${this.#shallow ? mainResource : baseURI}/:id`, ['PUT', 'PATCH'], 'update')
179
- this.#createRoute(`${this.#shallow ? mainResource : baseURI}/:id`, ['DELETE'], 'destroy')
180
- }
181
-
182
- /**
183
- * Filter the routes based on their partial names
184
- */
185
- #filter(names: ResourceActionNames | ResourceActionNames[], inverse: boolean) {
186
- const actions = Array.isArray(names) ? names : [names]
187
- return this.routes.filter((route) => {
188
- const match = actions.find((name) => route.getName()!.endsWith(name))
189
- return inverse ? !match : match
190
- })
191
- }
192
-
193
- /**
194
- * Register only given routes and remove others
195
- */
196
- only(names: ResourceActionNames[]): this {
197
- this.#filter(names, true).forEach((route) => route.markAsDeleted())
198
- return this
199
- }
200
-
201
- /**
202
- * Register all routes, except the one's defined
203
- */
204
- except(names: ResourceActionNames[]): this {
205
- this.#filter(names, false).forEach((route) => route.markAsDeleted())
206
- return this
207
- }
208
-
209
- /**
210
- * Register api only routes. The `create` and `edit` routes, which
211
- * are meant to show forms will not be registered
212
- */
213
- apiOnly(): this {
214
- return this.except(['create', 'edit'])
215
- }
216
-
217
- /**
218
- * Define matcher for params inside the resource
219
- */
220
- where(key: string, matcher: RouteMatcher | string | RegExp): this {
221
- this.routes.forEach((route) => {
222
- route.where(key, matcher)
223
- })
224
-
225
- return this
226
- }
227
-
228
- /**
229
- * Tap into multiple routes to configure them by their name
230
- */
231
- tap(callback: (route: Route) => void): this
232
- tap(actions: ResourceActionNames | ResourceActionNames[], callback: (route: Route) => void): this
233
- tap(
234
- actions: ((route: Route) => void) | ResourceActionNames | ResourceActionNames[],
235
- callback?: (route: Route) => void
236
- ): this {
237
- if (typeof actions === 'function') {
238
- this.routes.forEach((route) => actions(route))
239
- return this
240
- }
241
-
242
- this.#filter(actions, false).forEach((route) => callback!(route))
243
- return this
244
- }
245
-
246
- /**
247
- * Set the param name for a given resource
248
- */
249
- params(resources: { [resource: string]: string }): this {
250
- Object.keys(resources).forEach((resource) => {
251
- const param = resources[resource]
252
- const existingParam = this.#params[resource]
253
- this.#params[resource] = `:${param}`
254
-
255
- this.routes.forEach((route) => {
256
- route.setPattern(
257
- route.getPattern().replace(`${resource}/${existingParam}`, `${resource}/:${param}`)
258
- )
259
- })
260
- })
261
-
262
- return this
263
- }
264
-
265
- /**
266
- * Prepend name to all the routes
267
- */
268
- as(name: string, normalizeName: boolean = true): this {
269
- name = normalizeName ? string.snakeCase(name) : name
270
- this.routes.forEach((route) => {
271
- route.as(route.getName()!.replace(this.#routesBaseName, name), false)
272
- })
273
-
274
- this.#routesBaseName = name
275
- return this
276
- }
277
- }
@@ -1,363 +0,0 @@
1
- /*
2
- * @adonisjs/http-server
3
- *
4
- * (c) AdonisJS
5
- *
6
- * For the full copyright and license information, please view the LICENSE
7
- * file that was distributed with this source code.
8
- */
9
-
10
- import is from '@sindresorhus/is'
11
- import Macroable from '@poppinss/macroable'
12
- import Middleware from '@poppinss/middleware'
13
- import { RuntimeException } from '@poppinss/utils'
14
- import type { Application } from '@adonisjs/application'
15
- import { moduleCaller, moduleExpression, moduleImporter } from '@adonisjs/fold'
16
-
17
- import { execute } from './executor.js'
18
- import { dropSlash } from '../helpers.js'
19
- import type { Constructor, LazyImport, OneOrMore } from '../types/base.js'
20
-
21
- import type {
22
- MiddlewareFn,
23
- ParsedNamedMiddleware,
24
- ParsedGlobalMiddleware,
25
- } from '../types/middleware.js'
26
-
27
- import type {
28
- GetControllerHandlers,
29
- RouteFn,
30
- RouteJSON,
31
- RouteMatcher,
32
- RouteMatchers,
33
- StoreRouteHandler,
34
- StoreRouteMiddleware,
35
- } from '../types/route.js'
36
-
37
- /**
38
- * The route class exposes the APIs for constructing a route using the
39
- * fluent API.
40
- */
41
- export class Route<Controller extends Constructor<any> = any> extends Macroable {
42
- /**
43
- * Route pattern
44
- */
45
- #pattern: string
46
-
47
- /**
48
- * HTTP Methods for the route
49
- */
50
- #methods: string[]
51
-
52
- /**
53
- * A unique name for the route
54
- */
55
- #name?: string
56
-
57
- /**
58
- * A boolean to prevent route from getting registered within
59
- * the store.
60
- *
61
- * This flag must be set before "Router.commit" method
62
- */
63
- #isDeleted: boolean = false
64
-
65
- /**
66
- * Route handler
67
- */
68
- #handler: StoreRouteHandler
69
-
70
- /**
71
- * Matchers inherited from the router
72
- */
73
- #globalMatchers: RouteMatchers
74
-
75
- /**
76
- * Reference to the AdonisJS application
77
- */
78
- #app: Application<any>
79
-
80
- /**
81
- * Middleware registered on the router
82
- */
83
- #routerMiddleware: ParsedGlobalMiddleware[]
84
-
85
- /**
86
- * By default the route is part of the `root` domain. Root domain is used
87
- * when no domain is defined
88
- */
89
- #routeDomain: string = 'root'
90
-
91
- /**
92
- * An object of matchers to be forwarded to the store. The matchers
93
- * list is populated by calling `where` method
94
- */
95
- #matchers: RouteMatchers = {}
96
-
97
- /**
98
- * Custom prefixes defined on the route or the route parent
99
- * groups
100
- */
101
- #prefixes: string[] = []
102
-
103
- /**
104
- * Middleware defined directly on the route or the route parent
105
- * routes. We mantain an array for each layer of the stack
106
- */
107
- #middleware: StoreRouteMiddleware[][] = []
108
-
109
- constructor(
110
- app: Application<any>,
111
- routerMiddleware: ParsedGlobalMiddleware[],
112
- options: {
113
- pattern: string
114
- methods: string[]
115
- handler:
116
- | RouteFn
117
- | string
118
- | [LazyImport<Controller> | Controller, GetControllerHandlers<Controller>?]
119
- globalMatchers: RouteMatchers
120
- }
121
- ) {
122
- super()
123
- this.#app = app
124
- this.#routerMiddleware = routerMiddleware
125
- this.#pattern = options.pattern
126
- this.#methods = options.methods
127
- this.#handler = this.#resolveRouteHandle(options.handler)
128
- this.#globalMatchers = options.globalMatchers
129
- }
130
-
131
- /**
132
- * Resolves the route handler string expression to a
133
- * handler method object
134
- */
135
- #resolveRouteHandle(
136
- handler:
137
- | RouteFn
138
- | string
139
- | [LazyImport<Controller> | Controller, GetControllerHandlers<Controller>?]
140
- ) {
141
- if (typeof handler === 'string') {
142
- return {
143
- reference: handler,
144
- ...moduleExpression(handler, this.#app.appRoot).toHandleMethod(),
145
- }
146
- }
147
-
148
- /**
149
- * Using a lazily imported controller
150
- */
151
- if (Array.isArray(handler)) {
152
- /**
153
- * The first item of the tuple is a class constructor
154
- */
155
- if (is.class_(handler[0])) {
156
- return {
157
- reference: handler,
158
- ...moduleCaller(handler[0], (handler[1] || 'handle') as string).toHandleMethod(),
159
- }
160
- }
161
-
162
- /**
163
- * The first item of the tuple is a function that lazily
164
- * loads the controller
165
- */
166
- return {
167
- reference: handler,
168
- ...moduleImporter(handler[0], (handler[1] || 'handle') as string).toHandleMethod(),
169
- }
170
- }
171
-
172
- return handler
173
- }
174
-
175
- /**
176
- * Returns an object of param matchers by merging global and local
177
- * matchers. The local copy is given preference over the global
178
- * one's
179
- */
180
- #getMatchers() {
181
- return { ...this.#globalMatchers, ...this.#matchers }
182
- }
183
-
184
- /**
185
- * Returns a normalized pattern string by prefixing the `prefix` (if defined).
186
- */
187
- #computePattern(): string {
188
- const pattern = dropSlash(this.#pattern)
189
- const prefix = this.#prefixes
190
- .slice()
191
- .reverse()
192
- .map((one) => dropSlash(one))
193
- .join('')
194
-
195
- return prefix ? `${prefix}${pattern === '/' ? '' : pattern}` : pattern
196
- }
197
-
198
- /**
199
- * Define matcher for a given param. If a matcher exists, then we do not
200
- * override that, since the routes inside a group will set matchers
201
- * before the group, so they should have priority over the group
202
- * matchers.
203
- *
204
- * ```ts
205
- * Route.group(() => {
206
- * Route.get('/:id', 'handler').where('id', /^[0-9]$/)
207
- * }).where('id', /[^a-z$]/)
208
- * ```
209
- *
210
- * The `/^[0-9]$/` will win over the matcher defined by the group
211
- */
212
- where(param: string, matcher: RouteMatcher | string | RegExp): this {
213
- if (this.#matchers[param]) {
214
- return this
215
- }
216
-
217
- if (typeof matcher === 'string') {
218
- this.#matchers[param] = { match: new RegExp(matcher) }
219
- } else if (is.regExp(matcher)) {
220
- this.#matchers[param] = { match: matcher }
221
- } else {
222
- this.#matchers[param] = matcher
223
- }
224
-
225
- return this
226
- }
227
-
228
- /**
229
- * Define prefix for the route. Calling this method multiple times
230
- * applies multiple prefixes in the reverse order.
231
- */
232
- prefix(prefix: string): this {
233
- this.#prefixes.push(prefix)
234
- return this
235
- }
236
-
237
- /**
238
- * Define a custom domain for the route. We do not overwrite the domain
239
- * unless `overwrite` flag is set to true.
240
- */
241
- domain(domain: string, overwrite: boolean = false): this {
242
- if (this.#routeDomain === 'root' || overwrite) {
243
- this.#routeDomain = domain
244
- }
245
- return this
246
- }
247
-
248
- /**
249
- * Define one or more middleware to be executed before the route
250
- * handler.
251
- *
252
- * Named middleware can be referenced using the name registered with
253
- * the router middleware store.
254
- */
255
- use(middleware: OneOrMore<MiddlewareFn | ParsedNamedMiddleware>): this {
256
- this.#middleware.push(Array.isArray(middleware) ? middleware : [middleware])
257
- return this
258
- }
259
-
260
- /**
261
- * @alias use
262
- */
263
- middleware(middleware: OneOrMore<MiddlewareFn | ParsedNamedMiddleware>): this {
264
- return this.use(middleware)
265
- }
266
-
267
- /**
268
- * Give a unique name to the route. Assinging a new unique removes the
269
- * existing name of the route.
270
- *
271
- * Setting prepends to true prefixes the name to the existing name.
272
- */
273
- as(name: string, prepend = false): this {
274
- if (prepend) {
275
- if (!this.#name) {
276
- throw new RuntimeException(
277
- `Routes inside a group must have names before calling "router.group.as"`
278
- )
279
- }
280
-
281
- this.#name = `${name}.${this.#name}`
282
- return this
283
- }
284
-
285
- this.#name = name
286
- return this
287
- }
288
-
289
- /**
290
- * Check if the route was marked to be deleted
291
- */
292
- isDeleted(): boolean {
293
- return this.#isDeleted
294
- }
295
-
296
- /**
297
- * Mark route as deleted. Deleted routes are not registered
298
- * with the route store
299
- */
300
- markAsDeleted() {
301
- this.#isDeleted = true
302
- }
303
-
304
- /**
305
- * Get the route name
306
- */
307
- getName(): string | undefined {
308
- return this.#name
309
- }
310
-
311
- /**
312
- * Get the route pattern
313
- */
314
- getPattern(): string {
315
- return this.#pattern
316
- }
317
-
318
- /**
319
- * Set the route pattern
320
- */
321
- setPattern(pattern: string): this {
322
- this.#pattern = pattern
323
- return this
324
- }
325
-
326
- /**
327
- * Returns the stack of middleware registered on the route.
328
- * The value is shared by reference.
329
- */
330
- getMiddleware() {
331
- return this.#middleware
332
- }
333
-
334
- /**
335
- * Returns the middleware instance for persistence inside the
336
- * store
337
- */
338
- #getMiddlewareForStore() {
339
- const middleware = new Middleware<StoreRouteMiddleware>()
340
-
341
- this.#routerMiddleware.forEach((one) => middleware.add(one))
342
- this.#middleware.flat().forEach((one) => middleware.add(one))
343
-
344
- return middleware
345
- }
346
-
347
- /**
348
- * Returns JSON representation of the route
349
- */
350
- toJSON(): RouteJSON {
351
- return {
352
- domain: this.#routeDomain,
353
- pattern: this.#computePattern(),
354
- matchers: this.#getMatchers(),
355
- meta: {},
356
- name: this.#name,
357
- handler: this.#handler,
358
- methods: this.#methods,
359
- middleware: this.#getMiddlewareForStore(),
360
- execute: execute,
361
- }
362
- }
363
- }