@adonisjs/http-server 5.12.0 → 6.0.0-0

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 (169) hide show
  1. package/README.md +15 -38
  2. package/build/index.d.ts +16 -0
  3. package/build/index.js +16 -0
  4. package/build/src/cookies/client.d.ts +12 -0
  5. package/build/src/cookies/client.js +42 -0
  6. package/build/src/cookies/drivers/encrypted.d.ts +4 -0
  7. package/build/src/cookies/drivers/encrypted.js +16 -0
  8. package/build/src/cookies/drivers/plain.d.ts +3 -0
  9. package/build/src/cookies/drivers/plain.js +13 -0
  10. package/build/src/cookies/drivers/signed.d.ts +4 -0
  11. package/build/src/cookies/drivers/signed.js +16 -0
  12. package/build/src/cookies/parser.d.ts +9 -0
  13. package/build/src/cookies/parser.js +69 -0
  14. package/build/src/cookies/serializer.d.ts +11 -0
  15. package/build/src/cookies/serializer.js +39 -0
  16. package/build/src/debug.d.ts +3 -0
  17. package/build/src/debug.js +2 -0
  18. package/build/src/define_config.d.ts +2 -0
  19. package/build/src/define_config.js +40 -0
  20. package/build/src/define_middleware.d.ts +4 -0
  21. package/build/src/define_middleware.js +6 -0
  22. package/build/src/exceptions/abort_exception.d.ts +5 -0
  23. package/build/src/exceptions/abort_exception.js +6 -0
  24. package/build/src/exceptions/cannot_lookup_route.d.ts +5 -0
  25. package/build/src/exceptions/cannot_lookup_route.js +5 -0
  26. package/build/src/exceptions/http_exception.d.ts +6 -0
  27. package/build/src/exceptions/http_exception.js +20 -0
  28. package/build/src/exceptions/route_not_found.d.ts +5 -0
  29. package/build/src/exceptions/route_not_found.js +5 -0
  30. package/build/src/helpers.d.ts +6 -67
  31. package/build/src/helpers.js +13 -168
  32. package/build/src/http_context/local_storage.d.ts +9 -0
  33. package/build/src/http_context/local_storage.js +14 -0
  34. package/build/src/http_context/main.d.ts +20 -0
  35. package/build/src/http_context/main.js +49 -0
  36. package/build/src/middleware/store.d.ts +11 -0
  37. package/build/src/middleware/store.js +33 -0
  38. package/build/src/qs.d.ts +7 -0
  39. package/build/src/qs.js +13 -0
  40. package/build/src/redirect.d.ts +18 -0
  41. package/build/src/redirect.js +80 -0
  42. package/build/src/request.d.ts +81 -0
  43. package/build/src/request.js +315 -0
  44. package/build/src/response.d.ts +108 -0
  45. package/build/src/response.js +587 -0
  46. package/build/src/router/brisk.d.ts +18 -0
  47. package/build/src/router/brisk.js +35 -0
  48. package/build/src/router/executor.d.ts +4 -0
  49. package/build/src/router/executor.js +16 -0
  50. package/build/src/router/group.d.ts +19 -0
  51. package/build/src/router/group.js +117 -0
  52. package/build/src/router/lookup_store/main.d.ts +17 -0
  53. package/build/src/router/lookup_store/main.js +37 -0
  54. package/build/src/router/lookup_store/route_finder.d.ts +8 -0
  55. package/build/src/router/lookup_store/route_finder.js +28 -0
  56. package/build/src/router/lookup_store/url_builder.d.ts +16 -0
  57. package/build/src/router/lookup_store/url_builder.js +111 -0
  58. package/build/src/router/main.d.ts +36 -0
  59. package/build/src/router/main.js +161 -0
  60. package/build/src/router/matchers.d.ts +14 -0
  61. package/build/src/router/matchers.js +15 -0
  62. package/build/src/router/resource.d.ts +27 -0
  63. package/build/src/router/resource.js +124 -0
  64. package/build/src/router/route.d.ts +28 -0
  65. package/build/src/router/route.js +135 -0
  66. package/build/src/router/store.d.ts +12 -0
  67. package/build/src/router/store.js +87 -0
  68. package/build/src/server/factories/final_handler.d.ts +4 -0
  69. package/build/src/server/factories/final_handler.js +17 -0
  70. package/build/src/server/factories/middleware_handler.d.ts +5 -0
  71. package/build/src/server/factories/middleware_handler.js +5 -0
  72. package/build/src/server/factories/use_return_value.d.ts +2 -0
  73. package/build/src/server/factories/use_return_value.js +9 -0
  74. package/build/src/server/factories/write_response.d.ts +2 -0
  75. package/build/src/server/factories/write_response.js +12 -0
  76. package/build/src/server/main.d.ts +23 -0
  77. package/build/src/server/main.js +124 -0
  78. package/build/src/types/base.d.ts +5 -0
  79. package/build/src/types/base.js +1 -0
  80. package/build/src/types/middleware.d.ts +14 -0
  81. package/build/src/types/middleware.js +1 -0
  82. package/build/src/types/qs.d.ts +15 -0
  83. package/build/src/types/qs.js +1 -0
  84. package/build/src/types/request.d.ts +7 -0
  85. package/build/src/types/request.js +1 -0
  86. package/build/src/types/response.d.ts +18 -0
  87. package/build/src/types/response.js +1 -0
  88. package/build/src/types/route.d.ts +74 -0
  89. package/build/src/types/route.js +1 -0
  90. package/build/src/types/server.d.ts +13 -0
  91. package/build/src/types/server.js +1 -0
  92. package/package.json +88 -86
  93. package/build/adonis-typings/container.d.ts +0 -24
  94. package/build/adonis-typings/container.js +0 -8
  95. package/build/adonis-typings/context.d.ts +0 -73
  96. package/build/adonis-typings/context.js +0 -8
  97. package/build/adonis-typings/cookie-client.d.ts +0 -34
  98. package/build/adonis-typings/cookie-client.js +0 -8
  99. package/build/adonis-typings/http-server.d.ts +0 -98
  100. package/build/adonis-typings/http-server.js +0 -8
  101. package/build/adonis-typings/index.d.ts +0 -16
  102. package/build/adonis-typings/index.js +0 -16
  103. package/build/adonis-typings/middleware.d.ts +0 -95
  104. package/build/adonis-typings/middleware.js +0 -8
  105. package/build/adonis-typings/request.d.ts +0 -556
  106. package/build/adonis-typings/request.js +0 -8
  107. package/build/adonis-typings/response.d.ts +0 -440
  108. package/build/adonis-typings/response.js +0 -8
  109. package/build/adonis-typings/route.d.ts +0 -559
  110. package/build/adonis-typings/route.js +0 -8
  111. package/build/exceptions.json +0 -110
  112. package/build/providers/HttpServerProvider.d.ts +0 -46
  113. package/build/providers/HttpServerProvider.js +0 -100
  114. package/build/src/Cookie/Client/index.d.ts +0 -39
  115. package/build/src/Cookie/Client/index.js +0 -108
  116. package/build/src/Cookie/Drivers/Encrypted.d.ts +0 -24
  117. package/build/src/Cookie/Drivers/Encrypted.js +0 -42
  118. package/build/src/Cookie/Drivers/Plain.d.ts +0 -23
  119. package/build/src/Cookie/Drivers/Plain.js +0 -40
  120. package/build/src/Cookie/Drivers/Signed.d.ts +0 -24
  121. package/build/src/Cookie/Drivers/Signed.js +0 -42
  122. package/build/src/Cookie/Parser/index.d.ts +0 -61
  123. package/build/src/Cookie/Parser/index.js +0 -174
  124. package/build/src/Cookie/Serializer/index.d.ts +0 -46
  125. package/build/src/Cookie/Serializer/index.js +0 -88
  126. package/build/src/Exceptions/HttpException.d.ts +0 -20
  127. package/build/src/Exceptions/HttpException.js +0 -36
  128. package/build/src/Exceptions/RouterException.d.ts +0 -36
  129. package/build/src/Exceptions/RouterException.js +0 -76
  130. package/build/src/HttpContext/LocalStorage/index.d.ts +0 -24
  131. package/build/src/HttpContext/LocalStorage/index.js +0 -28
  132. package/build/src/HttpContext/index.d.ts +0 -90
  133. package/build/src/HttpContext/index.js +0 -181
  134. package/build/src/MiddlewareStore/index.d.ts +0 -92
  135. package/build/src/MiddlewareStore/index.js +0 -133
  136. package/build/src/Redirect/index.d.ts +0 -71
  137. package/build/src/Redirect/index.js +0 -139
  138. package/build/src/Request/index.d.ts +0 -619
  139. package/build/src/Request/index.js +0 -862
  140. package/build/src/Response/index.d.ts +0 -414
  141. package/build/src/Response/index.js +0 -1010
  142. package/build/src/Router/BriskRoute.d.ts +0 -53
  143. package/build/src/Router/BriskRoute.js +0 -74
  144. package/build/src/Router/Group.d.ts +0 -101
  145. package/build/src/Router/Group.js +0 -165
  146. package/build/src/Router/LookupStore.d.ts +0 -122
  147. package/build/src/Router/LookupStore.js +0 -264
  148. package/build/src/Router/Matchers.d.ts +0 -31
  149. package/build/src/Router/Matchers.js +0 -43
  150. package/build/src/Router/Resource.d.ts +0 -95
  151. package/build/src/Router/Resource.js +0 -182
  152. package/build/src/Router/Route.d.ts +0 -138
  153. package/build/src/Router/Route.js +0 -204
  154. package/build/src/Router/Store.d.ts +0 -93
  155. package/build/src/Router/Store.js +0 -211
  156. package/build/src/Router/index.d.ts +0 -142
  157. package/build/src/Router/index.js +0 -333
  158. package/build/src/Server/ExceptionManager/index.d.ts +0 -49
  159. package/build/src/Server/ExceptionManager/index.js +0 -96
  160. package/build/src/Server/Hooks/index.d.ts +0 -43
  161. package/build/src/Server/Hooks/index.js +0 -77
  162. package/build/src/Server/PreCompiler/index.d.ts +0 -60
  163. package/build/src/Server/PreCompiler/index.js +0 -143
  164. package/build/src/Server/RequestHandler/index.d.ts +0 -39
  165. package/build/src/Server/RequestHandler/index.js +0 -87
  166. package/build/src/Server/index.d.ts +0 -90
  167. package/build/src/Server/index.js +0 -175
  168. package/build/standalone.d.ts +0 -14
  169. package/build/standalone.js +0 -23
@@ -1,264 +0,0 @@
1
- "use strict";
2
- /*
3
- * @adonisjs/http-server
4
- *
5
- * (c) Harminder Virk <virk@adonisjs.com>
6
- *
7
- * For the full copyright and license information, please view the LICENSE
8
- * file that was distributed with this source code.
9
- */
10
- var __importDefault = (this && this.__importDefault) || function (mod) {
11
- return (mod && mod.__esModule) ? mod : { "default": mod };
12
- };
13
- Object.defineProperty(exports, "__esModule", { value: true });
14
- exports.LookupStore = exports.UrlBuilder = void 0;
15
- const qs_1 = __importDefault(require("qs"));
16
- const encodeurl_1 = __importDefault(require("encodeurl"));
17
- const RouterException_1 = require("../Exceptions/RouterException");
18
- /**
19
- * A class to encapsulate finding routes
20
- */
21
- class Routes {
22
- constructor(routes) {
23
- this.routes = routes;
24
- }
25
- /**
26
- * Find a route by indentifier
27
- */
28
- find(routeIdentifier) {
29
- return (this.routes.find(({ name, pattern, handler }) => {
30
- return (name === routeIdentifier || pattern === routeIdentifier || handler === routeIdentifier);
31
- }) || null);
32
- }
33
- /**
34
- * Find a route by indentifier or fail
35
- */
36
- findOrFail(routeIdentifier) {
37
- const route = this.find(routeIdentifier);
38
- if (!route) {
39
- throw RouterException_1.RouterException.cannotLookupRoute(routeIdentifier);
40
- }
41
- return route;
42
- }
43
- /**
44
- * Find if a route exists
45
- */
46
- has(routeIdentifier) {
47
- return !!this.find(routeIdentifier);
48
- }
49
- }
50
- /**
51
- * Url builder is responsible for building the URLs
52
- */
53
- class UrlBuilder {
54
- constructor(encryption, routes) {
55
- this.encryption = encryption;
56
- this.routes = routes;
57
- /**
58
- * A custom query string to append to the URL
59
- */
60
- this.queryString = {};
61
- /**
62
- * A boolean to know if the route should be looked
63
- * up inside the route store or not
64
- */
65
- this.lookupRoute = true;
66
- }
67
- /**
68
- * Processes the pattern against the params
69
- */
70
- processPattern(pattern) {
71
- let url = [];
72
- const isParamsAnArray = Array.isArray(this.routeParams);
73
- /*
74
- * Split pattern when route has dynamic segments
75
- */
76
- const tokens = pattern.split('/');
77
- let paramsIndex = 0;
78
- for (const token of tokens) {
79
- /**
80
- * Expected wildcard param to be at the end always and hence
81
- * we must break out from the loop
82
- */
83
- if (token === '*') {
84
- const wildcardParams = isParamsAnArray
85
- ? this.routeParams.slice(paramsIndex)
86
- : this.routeParams['*'];
87
- if (!wildcardParams || !Array.isArray(wildcardParams) || !wildcardParams.length) {
88
- throw RouterException_1.RouterException.cannotMakeRoute('*', pattern);
89
- }
90
- url = url.concat(wildcardParams);
91
- break;
92
- }
93
- /**
94
- * Token is a static value
95
- */
96
- if (!token.startsWith(':')) {
97
- url.push(token);
98
- }
99
- else {
100
- const isOptional = token.endsWith('?');
101
- const paramName = token.replace(/^:/, '').replace(/\?$/, '');
102
- const param = isParamsAnArray ? this.routeParams[paramsIndex] : this.routeParams[paramName];
103
- paramsIndex++;
104
- /*
105
- * A required param is always required to make the complete URL
106
- */
107
- if (!param && !isOptional) {
108
- throw RouterException_1.RouterException.cannotMakeRoute(paramName, pattern);
109
- }
110
- url.push(param);
111
- }
112
- }
113
- return url.join('/');
114
- }
115
- /**
116
- * Suffix the query string to the URL
117
- */
118
- suffixQueryString(url) {
119
- if (this.queryString) {
120
- const encoded = qs_1.default.stringify(this.queryString);
121
- url = encoded ? `${url}?${(0, encodeurl_1.default)(encoded)}` : url;
122
- }
123
- return url;
124
- }
125
- /**
126
- * Prefix a custom url to the final URI
127
- */
128
- prefixUrl(url) {
129
- this.baseUrl = url;
130
- return this;
131
- }
132
- /**
133
- * Disable route lookup. Calling this method considers
134
- * the "identifier" as the route pattern
135
- */
136
- disableRouteLookup() {
137
- this.lookupRoute = false;
138
- return this;
139
- }
140
- /**
141
- * Append query string to the final URI
142
- */
143
- qs(queryString) {
144
- if (!queryString) {
145
- return this;
146
- }
147
- this.queryString = queryString;
148
- return this;
149
- }
150
- /**
151
- * Define required params to resolve the route
152
- */
153
- params(params) {
154
- if (!params) {
155
- return this;
156
- }
157
- this.routeParams = params;
158
- return this;
159
- }
160
- /**
161
- * Generate url for the given route identifier
162
- */
163
- make(identifier) {
164
- let url;
165
- if (this.lookupRoute) {
166
- const route = this.routes.findOrFail(identifier);
167
- url = this.processPattern(route.pattern);
168
- }
169
- else {
170
- url = this.processPattern(identifier);
171
- }
172
- return this.suffixQueryString(this.baseUrl ? `${this.baseUrl}${url}` : url);
173
- }
174
- /**
175
- * Generate url for the given route identifier
176
- */
177
- makeSigned(identifier, options) {
178
- let url;
179
- if (this.lookupRoute) {
180
- const route = this.routes.findOrFail(identifier);
181
- url = this.processPattern(route.pattern);
182
- }
183
- else {
184
- url = this.processPattern(identifier);
185
- }
186
- /*
187
- * Making the signature from the qualified url. We do not prefix the domain when
188
- * making signature, since it just makes the signature big.
189
- *
190
- * There might be a case, when someone wants to generate signature for the same route
191
- * on their 2 different domains, but we ignore that case for now and can consider
192
- * it later (when someone asks for it)
193
- */
194
- const signature = this.encryption.verifier.sign(this.suffixQueryString(url), options?.expiresIn, options?.purpose);
195
- /*
196
- * Adding signature to the query string and re-making the url again
197
- */
198
- Object.assign(this.queryString, { signature });
199
- return this.suffixQueryString(this.baseUrl ? `${this.baseUrl}${url}` : url);
200
- }
201
- }
202
- exports.UrlBuilder = UrlBuilder;
203
- /**
204
- * The look up store to make URLs for a given route by looking
205
- * it by its name, route handler or the pattern directly.
206
- */
207
- class LookupStore {
208
- constructor(encryption) {
209
- this.encryption = encryption;
210
- /**
211
- * Shape of the registered routes. Optimized for lookups
212
- */
213
- this.tree = {};
214
- }
215
- /**
216
- * Register a route for lookups
217
- */
218
- register(route) {
219
- const domain = route.domain || 'root';
220
- this.tree[domain] = this.tree[domain] || [];
221
- this.tree[domain].push(route);
222
- }
223
- /**
224
- * Returns the route builder for the root domain
225
- */
226
- builder() {
227
- return this.builderForDomain('root');
228
- }
229
- /**
230
- * Returns the route builder a given domain.
231
- */
232
- builderForDomain(domainPattern) {
233
- const domainRoutes = this.tree[domainPattern];
234
- if (!domainRoutes && domainPattern !== 'root') {
235
- throw RouterException_1.RouterException.cannotLookupDomain(domainPattern);
236
- }
237
- return new UrlBuilder(this.encryption, new Routes(domainRoutes || []));
238
- }
239
- /**
240
- * Find a route by indentifier. Optionally one can find routes inside
241
- * a given domain
242
- */
243
- find(routeIdentifier, domainPattern) {
244
- const routes = this.tree[domainPattern || 'root'] || [];
245
- return new Routes(routes || []).find(routeIdentifier);
246
- }
247
- /**
248
- * Find a route by indentifier or fail. Optionally one can find routes inside
249
- * a given domain
250
- */
251
- findOrFail(routeIdentifier, domainPattern) {
252
- const routes = this.tree[domainPattern || 'root'] || [];
253
- return new Routes(routes || []).findOrFail(routeIdentifier);
254
- }
255
- /**
256
- * Find if a route for given identifier exists. Optionally one can find routes inside
257
- * a given domain
258
- */
259
- has(routeIdentifier, domainPattern) {
260
- const routes = this.tree[domainPattern || 'root'] || [];
261
- return new Routes(routes || []).has(routeIdentifier);
262
- }
263
- }
264
- exports.LookupStore = LookupStore;
@@ -1,31 +0,0 @@
1
- /// <reference path="../../adonis-typings/index.d.ts" />
2
- import { Macroable } from 'macroable';
3
- import { RouteMatchersContract } from '@ioc:Adonis/Core/Route';
4
- /**
5
- * Shortcut methods for commonly used route matchers
6
- */
7
- export declare class RouteMatchers extends Macroable implements RouteMatchersContract {
8
- protected static macros: {};
9
- protected static getters: {};
10
- /**
11
- * Enforce value to be a number and also casts it to number data
12
- * type
13
- */
14
- number(): {
15
- match: RegExp;
16
- cast: (value: string) => number;
17
- };
18
- /**
19
- * Enforce value to be formatted as uuid
20
- */
21
- uuid(): {
22
- match: RegExp;
23
- cast: (value: string) => string;
24
- };
25
- /**
26
- * Enforce value to be formatted as slug
27
- */
28
- slug(): {
29
- match: RegExp;
30
- };
31
- }
@@ -1,43 +0,0 @@
1
- "use strict";
2
- /*
3
- * @adonisjs/http-server
4
- *
5
- * (c) Harminder Virk <virk@adonisjs.com>
6
- *
7
- * For the full copyright and license information, please view the LICENSE
8
- * file that was distributed with this source code.
9
- */
10
- Object.defineProperty(exports, "__esModule", { value: true });
11
- exports.RouteMatchers = void 0;
12
- /// <reference path="../../adonis-typings/index.ts" />
13
- const macroable_1 = require("macroable");
14
- /**
15
- * Shortcut methods for commonly used route matchers
16
- */
17
- class RouteMatchers extends macroable_1.Macroable {
18
- /**
19
- * Enforce value to be a number and also casts it to number data
20
- * type
21
- */
22
- number() {
23
- return { match: /^[0-9]+$/, cast: (value) => Number(value) };
24
- }
25
- /**
26
- * Enforce value to be formatted as uuid
27
- */
28
- uuid() {
29
- return {
30
- 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}$/,
31
- cast: (value) => value.toLowerCase(),
32
- };
33
- }
34
- /**
35
- * Enforce value to be formatted as slug
36
- */
37
- slug() {
38
- return { match: /^[^\s-_](?!.*?[-_]{2,})([a-z0-9-\\]{1,})[^\s]*[^-_\s]$/ };
39
- }
40
- }
41
- exports.RouteMatchers = RouteMatchers;
42
- RouteMatchers.macros = {};
43
- RouteMatchers.getters = {};
@@ -1,95 +0,0 @@
1
- /**
2
- * @adonisjs/http-server
3
- *
4
- * (c) Harminder Virk <virk@adonisjs.com>
5
- *
6
- * For the full copyright and license information, please view the LICENSE
7
- * file that was distributed with this source code.
8
- */
9
- /// <reference path="../../adonis-typings/index.d.ts" />
10
- import { Macroable } from 'macroable';
11
- import { RouteParamMatcher, ResourceRouteNames, RouteMatchersNode, RouteResourceContract, RouteMiddlewareHandler } from '@ioc:Adonis/Core/Route';
12
- import { Route } from './Route';
13
- /**
14
- * Resource route helps in defining multiple conventional routes. The support
15
- * for shallow routes makes it super easy to avoid deeply nested routes.
16
- * Learn more http://weblog.jamisbuck.org/2007/2/5/nesting-resources.
17
- *
18
- * @example
19
- * ```ts
20
- * const resource = new RouteResource('articles', 'ArticlesController')
21
- * ```
22
- */
23
- export declare class RouteResource extends Macroable implements RouteResourceContract {
24
- private resource;
25
- private controller;
26
- private globalMatchers;
27
- private shallow;
28
- protected static macros: {};
29
- protected static getters: {};
30
- /**
31
- * The param names used to create the resource URLs.
32
- *
33
- * We need these later when someone explicitly wants to remap
34
- * param name for a given resource using the "paramFor" method.
35
- */
36
- private resourceParamNames;
37
- /**
38
- * A copy of routes that belongs to this resource
39
- */
40
- routes: Route[];
41
- /**
42
- * Resource name
43
- */
44
- private resourceName;
45
- constructor(resource: string, controller: string, globalMatchers: RouteMatchersNode, shallow?: boolean);
46
- /**
47
- * Add a new route for the given pattern, methods and controller action
48
- */
49
- private makeRoute;
50
- /**
51
- * Build routes for the given resource
52
- */
53
- private buildRoutes;
54
- /**
55
- * Filter the routes based on their partial names
56
- */
57
- private filter;
58
- /**
59
- * Register only given routes and remove others
60
- */
61
- only(names: ResourceRouteNames[]): this;
62
- /**
63
- * Register all routes, except the one's defined
64
- */
65
- except(names: ResourceRouteNames[]): this;
66
- /**
67
- * Register api only routes. The `create` and `edit` routes, which
68
- * are meant to show forms will not be registered
69
- */
70
- apiOnly(): this;
71
- /**
72
- * Add middleware to routes inside the resource
73
- */
74
- middleware(middleware: {
75
- [P in ResourceRouteNames]?: RouteMiddlewareHandler | RouteMiddlewareHandler[];
76
- } & {
77
- '*'?: RouteMiddlewareHandler | RouteMiddlewareHandler[];
78
- }): this;
79
- /**
80
- * Define matcher for params inside the resource
81
- */
82
- where(key: string, matcher: RouteParamMatcher): this;
83
- /**
84
- * Define namespace for all the routes inside a given resource
85
- */
86
- namespace(namespace: string): this;
87
- /**
88
- * Set the param name for a given resource
89
- */
90
- paramFor(resource: string, param: string): this;
91
- /**
92
- * Prepend name to the routes names
93
- */
94
- as(name: string): this;
95
- }
@@ -1,182 +0,0 @@
1
- "use strict";
2
- /**
3
- * @adonisjs/http-server
4
- *
5
- * (c) Harminder Virk <virk@adonisjs.com>
6
- *
7
- * For the full copyright and license information, please view the LICENSE
8
- * file that was distributed with this source code.
9
- */
10
- Object.defineProperty(exports, "__esModule", { value: true });
11
- exports.RouteResource = void 0;
12
- /// <reference path="../../adonis-typings/index.ts" />
13
- const pluralize_1 = require("pluralize");
14
- const macroable_1 = require("macroable");
15
- const helpers_1 = require("@poppinss/utils/build/helpers");
16
- const Route_1 = require("./Route");
17
- /**
18
- * Resource route helps in defining multiple conventional routes. The support
19
- * for shallow routes makes it super easy to avoid deeply nested routes.
20
- * Learn more http://weblog.jamisbuck.org/2007/2/5/nesting-resources.
21
- *
22
- * @example
23
- * ```ts
24
- * const resource = new RouteResource('articles', 'ArticlesController')
25
- * ```
26
- */
27
- class RouteResource extends macroable_1.Macroable {
28
- constructor(resource, controller, globalMatchers, shallow = false) {
29
- super();
30
- this.resource = resource;
31
- this.controller = controller;
32
- this.globalMatchers = globalMatchers;
33
- this.shallow = shallow;
34
- /**
35
- * The param names used to create the resource URLs.
36
- *
37
- * We need these later when someone explicitly wants to remap
38
- * param name for a given resource using the "paramFor" method.
39
- */
40
- this.resourceParamNames = {};
41
- /**
42
- * A copy of routes that belongs to this resource
43
- */
44
- this.routes = [];
45
- /**
46
- * Resource name
47
- */
48
- this.resourceName = this.resource
49
- .split('.')
50
- .map((token) => helpers_1.string.snakeCase(token))
51
- .join('.');
52
- this.buildRoutes();
53
- }
54
- /**
55
- * Add a new route for the given pattern, methods and controller action
56
- */
57
- makeRoute(pattern, methods, action) {
58
- const route = new Route_1.Route(pattern, methods, `${this.controller}.${action}`, this.globalMatchers);
59
- route.as(`${this.resourceName}.${action}`);
60
- this.routes.push(route);
61
- }
62
- /**
63
- * Build routes for the given resource
64
- */
65
- buildRoutes() {
66
- this.resource = this.resource.replace(/^\//, '').replace(/\/$/, '');
67
- const resourceTokens = this.resource.split('.');
68
- const mainResource = resourceTokens.pop();
69
- /**
70
- * The main resource always uses ids
71
- */
72
- this.resourceParamNames[mainResource] = ':id';
73
- const fullUrl = `${resourceTokens
74
- .map((token) => {
75
- const paramName = `:${helpers_1.string.snakeCase((0, pluralize_1.singular)(token))}_id`;
76
- this.resourceParamNames[token] = paramName;
77
- return `${token}/${paramName}`;
78
- })
79
- .join('/')}/${mainResource}`;
80
- this.makeRoute(fullUrl, ['GET', 'HEAD'], 'index');
81
- this.makeRoute(`${fullUrl}/create`, ['GET', 'HEAD'], 'create');
82
- this.makeRoute(fullUrl, ['POST'], 'store');
83
- this.makeRoute(`${this.shallow ? mainResource : fullUrl}/:id`, ['GET', 'HEAD'], 'show');
84
- this.makeRoute(`${this.shallow ? mainResource : fullUrl}/:id/edit`, ['GET', 'HEAD'], 'edit');
85
- this.makeRoute(`${this.shallow ? mainResource : fullUrl}/:id`, ['PUT', 'PATCH'], 'update');
86
- this.makeRoute(`${this.shallow ? mainResource : fullUrl}/:id`, ['DELETE'], 'destroy');
87
- }
88
- /**
89
- * Filter the routes based on their partial names
90
- */
91
- filter(names, inverse) {
92
- return this.routes.filter((route) => {
93
- const match = names.find((name) => route.name.endsWith(name));
94
- return inverse ? !match : match;
95
- });
96
- }
97
- /**
98
- * Register only given routes and remove others
99
- */
100
- only(names) {
101
- this.filter(names, true).forEach((route) => (route.deleted = true));
102
- return this;
103
- }
104
- /**
105
- * Register all routes, except the one's defined
106
- */
107
- except(names) {
108
- this.filter(names, false).forEach((route) => (route.deleted = true));
109
- return this;
110
- }
111
- /**
112
- * Register api only routes. The `create` and `edit` routes, which
113
- * are meant to show forms will not be registered
114
- */
115
- apiOnly() {
116
- return this.except(['create', 'edit']);
117
- }
118
- /**
119
- * Add middleware to routes inside the resource
120
- */
121
- middleware(middleware) {
122
- for (let name in middleware) {
123
- if (name === '*') {
124
- this.routes.forEach((one) => one.middleware(middleware[name]));
125
- }
126
- else {
127
- const route = this.routes.find((one) => one.name.endsWith(name));
128
- /* istanbul ignore else */
129
- if (route) {
130
- route.middleware(middleware[name]);
131
- }
132
- }
133
- }
134
- return this;
135
- }
136
- /**
137
- * Define matcher for params inside the resource
138
- */
139
- where(key, matcher) {
140
- this.routes.forEach((route) => {
141
- route.where(key, matcher);
142
- });
143
- return this;
144
- }
145
- /**
146
- * Define namespace for all the routes inside a given resource
147
- */
148
- namespace(namespace) {
149
- this.routes.forEach((route) => {
150
- route.namespace(namespace);
151
- });
152
- return this;
153
- }
154
- /**
155
- * Set the param name for a given resource
156
- */
157
- paramFor(resource, param) {
158
- const existingParam = this.resourceParamNames[resource];
159
- this.resourceParamNames[resource] = `:${param}`;
160
- this.routes.forEach((route) => {
161
- /**
162
- * Update the pattern for the route with the new param name
163
- */
164
- route.setPattern(route.getPattern().replace(`${resource}/${existingParam}`, `${resource}/:${param}`));
165
- });
166
- return this;
167
- }
168
- /**
169
- * Prepend name to the routes names
170
- */
171
- as(name) {
172
- name = helpers_1.string.snakeCase(name);
173
- this.routes.forEach((route) => {
174
- route.as(route.name.replace(this.resourceName, name), false);
175
- });
176
- this.resourceName = name;
177
- return this;
178
- }
179
- }
180
- exports.RouteResource = RouteResource;
181
- RouteResource.macros = {};
182
- RouteResource.getters = {};