@adonisjs/http-server 5.12.0 → 6.0.1-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 (171) 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/main.d.ts +7 -0
  81. package/build/src/types/main.js +7 -0
  82. package/build/src/types/middleware.d.ts +14 -0
  83. package/build/src/types/middleware.js +1 -0
  84. package/build/src/types/qs.d.ts +15 -0
  85. package/build/src/types/qs.js +1 -0
  86. package/build/src/types/request.d.ts +7 -0
  87. package/build/src/types/request.js +1 -0
  88. package/build/src/types/response.d.ts +18 -0
  89. package/build/src/types/response.js +1 -0
  90. package/build/src/types/route.d.ts +74 -0
  91. package/build/src/types/route.js +1 -0
  92. package/build/src/types/server.d.ts +13 -0
  93. package/build/src/types/server.js +1 -0
  94. package/package.json +88 -86
  95. package/build/adonis-typings/container.d.ts +0 -24
  96. package/build/adonis-typings/container.js +0 -8
  97. package/build/adonis-typings/context.d.ts +0 -73
  98. package/build/adonis-typings/context.js +0 -8
  99. package/build/adonis-typings/cookie-client.d.ts +0 -34
  100. package/build/adonis-typings/cookie-client.js +0 -8
  101. package/build/adonis-typings/http-server.d.ts +0 -98
  102. package/build/adonis-typings/http-server.js +0 -8
  103. package/build/adonis-typings/index.d.ts +0 -16
  104. package/build/adonis-typings/index.js +0 -16
  105. package/build/adonis-typings/middleware.d.ts +0 -95
  106. package/build/adonis-typings/middleware.js +0 -8
  107. package/build/adonis-typings/request.d.ts +0 -556
  108. package/build/adonis-typings/request.js +0 -8
  109. package/build/adonis-typings/response.d.ts +0 -440
  110. package/build/adonis-typings/response.js +0 -8
  111. package/build/adonis-typings/route.d.ts +0 -559
  112. package/build/adonis-typings/route.js +0 -8
  113. package/build/exceptions.json +0 -110
  114. package/build/providers/HttpServerProvider.d.ts +0 -46
  115. package/build/providers/HttpServerProvider.js +0 -100
  116. package/build/src/Cookie/Client/index.d.ts +0 -39
  117. package/build/src/Cookie/Client/index.js +0 -108
  118. package/build/src/Cookie/Drivers/Encrypted.d.ts +0 -24
  119. package/build/src/Cookie/Drivers/Encrypted.js +0 -42
  120. package/build/src/Cookie/Drivers/Plain.d.ts +0 -23
  121. package/build/src/Cookie/Drivers/Plain.js +0 -40
  122. package/build/src/Cookie/Drivers/Signed.d.ts +0 -24
  123. package/build/src/Cookie/Drivers/Signed.js +0 -42
  124. package/build/src/Cookie/Parser/index.d.ts +0 -61
  125. package/build/src/Cookie/Parser/index.js +0 -174
  126. package/build/src/Cookie/Serializer/index.d.ts +0 -46
  127. package/build/src/Cookie/Serializer/index.js +0 -88
  128. package/build/src/Exceptions/HttpException.d.ts +0 -20
  129. package/build/src/Exceptions/HttpException.js +0 -36
  130. package/build/src/Exceptions/RouterException.d.ts +0 -36
  131. package/build/src/Exceptions/RouterException.js +0 -76
  132. package/build/src/HttpContext/LocalStorage/index.d.ts +0 -24
  133. package/build/src/HttpContext/LocalStorage/index.js +0 -28
  134. package/build/src/HttpContext/index.d.ts +0 -90
  135. package/build/src/HttpContext/index.js +0 -181
  136. package/build/src/MiddlewareStore/index.d.ts +0 -92
  137. package/build/src/MiddlewareStore/index.js +0 -133
  138. package/build/src/Redirect/index.d.ts +0 -71
  139. package/build/src/Redirect/index.js +0 -139
  140. package/build/src/Request/index.d.ts +0 -619
  141. package/build/src/Request/index.js +0 -862
  142. package/build/src/Response/index.d.ts +0 -414
  143. package/build/src/Response/index.js +0 -1010
  144. package/build/src/Router/BriskRoute.d.ts +0 -53
  145. package/build/src/Router/BriskRoute.js +0 -74
  146. package/build/src/Router/Group.d.ts +0 -101
  147. package/build/src/Router/Group.js +0 -165
  148. package/build/src/Router/LookupStore.d.ts +0 -122
  149. package/build/src/Router/LookupStore.js +0 -264
  150. package/build/src/Router/Matchers.d.ts +0 -31
  151. package/build/src/Router/Matchers.js +0 -43
  152. package/build/src/Router/Resource.d.ts +0 -95
  153. package/build/src/Router/Resource.js +0 -182
  154. package/build/src/Router/Route.d.ts +0 -138
  155. package/build/src/Router/Route.js +0 -204
  156. package/build/src/Router/Store.d.ts +0 -93
  157. package/build/src/Router/Store.js +0 -211
  158. package/build/src/Router/index.d.ts +0 -142
  159. package/build/src/Router/index.js +0 -333
  160. package/build/src/Server/ExceptionManager/index.d.ts +0 -49
  161. package/build/src/Server/ExceptionManager/index.js +0 -96
  162. package/build/src/Server/Hooks/index.d.ts +0 -43
  163. package/build/src/Server/Hooks/index.js +0 -77
  164. package/build/src/Server/PreCompiler/index.d.ts +0 -60
  165. package/build/src/Server/PreCompiler/index.js +0 -143
  166. package/build/src/Server/RequestHandler/index.d.ts +0 -39
  167. package/build/src/Server/RequestHandler/index.js +0 -87
  168. package/build/src/Server/index.d.ts +0 -90
  169. package/build/src/Server/index.js +0 -175
  170. package/build/standalone.d.ts +0 -14
  171. package/build/standalone.js +0 -23
@@ -0,0 +1,117 @@
1
+ import { Macroable } from '@poppinss/macroable';
2
+ import { BriskRoute } from './brisk.js';
3
+ import { RouteResource } from './resource.js';
4
+ export class RouteGroup extends Macroable {
5
+ routes;
6
+ #middleware = [];
7
+ #middlewareStore;
8
+ constructor(routes, middlewareStore) {
9
+ super();
10
+ this.routes = routes;
11
+ this.#middlewareStore = middlewareStore;
12
+ }
13
+ #shareMiddlewareStackWithRoutes(route) {
14
+ if (route instanceof RouteGroup) {
15
+ route.routes.forEach((child) => this.#shareMiddlewareStackWithRoutes(child));
16
+ return;
17
+ }
18
+ if (route instanceof RouteResource) {
19
+ route.routes.forEach((child) => child.getMiddleware().unshift(this.#middleware));
20
+ return;
21
+ }
22
+ if (route instanceof BriskRoute) {
23
+ route.route.getMiddleware().unshift(this.#middleware);
24
+ return;
25
+ }
26
+ route.getMiddleware().unshift(this.#middleware);
27
+ }
28
+ #updateRouteName(route, name) {
29
+ if (route instanceof RouteGroup) {
30
+ route.routes.forEach((child) => this.#updateRouteName(child, name));
31
+ return;
32
+ }
33
+ if (route instanceof RouteResource) {
34
+ route.routes.forEach((child) => child.as(name, true));
35
+ return;
36
+ }
37
+ if (route instanceof BriskRoute) {
38
+ route.route.as(name, true);
39
+ return;
40
+ }
41
+ route.as(name, true);
42
+ }
43
+ #setRoutePrefix(route, prefix) {
44
+ if (route instanceof RouteGroup) {
45
+ route.routes.forEach((child) => this.#setRoutePrefix(child, prefix));
46
+ return;
47
+ }
48
+ if (route instanceof RouteResource) {
49
+ route.routes.forEach((child) => child.prefix(prefix));
50
+ return;
51
+ }
52
+ if (route instanceof BriskRoute) {
53
+ route.route.prefix(prefix);
54
+ return;
55
+ }
56
+ route.prefix(prefix);
57
+ }
58
+ #updateRouteDomain(route, domain) {
59
+ if (route instanceof RouteGroup) {
60
+ route.routes.forEach((child) => this.#updateRouteDomain(child, domain));
61
+ return;
62
+ }
63
+ if (route instanceof RouteResource) {
64
+ route.routes.forEach((child) => child.domain(domain));
65
+ return;
66
+ }
67
+ if (route instanceof BriskRoute) {
68
+ route.route.domain(domain, false);
69
+ return;
70
+ }
71
+ route.domain(domain, false);
72
+ }
73
+ #updateRouteMatchers(route, param, matcher) {
74
+ if (route instanceof RouteGroup) {
75
+ route.routes.forEach((child) => this.#updateRouteMatchers(child, param, matcher));
76
+ return;
77
+ }
78
+ if (route instanceof RouteResource) {
79
+ route.routes.forEach((child) => child.where(param, matcher));
80
+ return;
81
+ }
82
+ if (route instanceof BriskRoute) {
83
+ route.route.where(param, matcher);
84
+ return;
85
+ }
86
+ route.where(param, matcher);
87
+ }
88
+ where(param, matcher) {
89
+ this.routes.forEach((route) => this.#updateRouteMatchers(route, param, matcher));
90
+ return this;
91
+ }
92
+ prefix(prefix) {
93
+ this.routes.forEach((route) => this.#setRoutePrefix(route, prefix));
94
+ return this;
95
+ }
96
+ domain(domain) {
97
+ this.routes.forEach((route) => this.#updateRouteDomain(route, domain));
98
+ return this;
99
+ }
100
+ as(name) {
101
+ this.routes.forEach((route) => this.#updateRouteName(route, name));
102
+ return this;
103
+ }
104
+ middleware(middleware, args) {
105
+ if (!this.#middleware.length) {
106
+ this.routes.forEach((route) => this.#shareMiddlewareStackWithRoutes(route));
107
+ }
108
+ if (typeof middleware === 'string') {
109
+ this.#middleware.push(this.#middlewareStore.get(middleware, args));
110
+ return this;
111
+ }
112
+ if (typeof middleware === 'function') {
113
+ this.#middleware.push(middleware);
114
+ }
115
+ return this;
116
+ }
117
+ }
@@ -0,0 +1,17 @@
1
+ import type { Encryption } from '@adonisjs/encryption';
2
+ import type { Qs } from '../../qs.js';
3
+ import { UrlBuilder } from './url_builder.js';
4
+ import type { RouteJSON } from '../../types/route.js';
5
+ export declare class LookupStore {
6
+ #private;
7
+ constructor(encryption: Encryption, qsParser: Qs);
8
+ register(route: RouteJSON): void;
9
+ builder(): UrlBuilder;
10
+ builderForDomain(domain: string): UrlBuilder;
11
+ find(routeIdentifier: string, domain?: string): RouteJSON | null;
12
+ findOrFail(routeIdentifier: string, domain?: string): RouteJSON;
13
+ has(routeIdentifier: string, domain?: string): boolean;
14
+ toJSON(): {
15
+ [domain: string]: RouteJSON[];
16
+ };
17
+ }
@@ -0,0 +1,37 @@
1
+ import { UrlBuilder } from './url_builder.js';
2
+ import { RouteFinder } from './route_finder.js';
3
+ export class LookupStore {
4
+ #routes = {};
5
+ #encryption;
6
+ #qsParser;
7
+ constructor(encryption, qsParser) {
8
+ this.#encryption = encryption;
9
+ this.#qsParser = qsParser;
10
+ }
11
+ register(route) {
12
+ this.#routes[route.domain] = this.#routes[route.domain] || [];
13
+ this.#routes[route.domain].push(route);
14
+ }
15
+ builder() {
16
+ return this.builderForDomain('root');
17
+ }
18
+ builderForDomain(domain) {
19
+ const routes = this.#routes[domain];
20
+ return new UrlBuilder(this.#encryption, new RouteFinder(routes || []), this.#qsParser);
21
+ }
22
+ find(routeIdentifier, domain) {
23
+ const routes = this.#routes[domain || 'root'] || [];
24
+ return new RouteFinder(routes).find(routeIdentifier);
25
+ }
26
+ findOrFail(routeIdentifier, domain) {
27
+ const routes = this.#routes[domain || 'root'] || [];
28
+ return new RouteFinder(routes).findOrFail(routeIdentifier);
29
+ }
30
+ has(routeIdentifier, domain) {
31
+ const routes = this.#routes[domain || 'root'] || [];
32
+ return new RouteFinder(routes).has(routeIdentifier);
33
+ }
34
+ toJSON() {
35
+ return this.#routes;
36
+ }
37
+ }
@@ -0,0 +1,8 @@
1
+ import type { RouteJSON } from '../../types/route.js';
2
+ export declare class RouteFinder {
3
+ #private;
4
+ constructor(routes: RouteJSON[]);
5
+ find(routeIdentifier: string): RouteJSON | null;
6
+ findOrFail(routeIdentifier: string): RouteJSON;
7
+ has(routeIdentifier: string): boolean;
8
+ }
@@ -0,0 +1,28 @@
1
+ import { CannotLookupRouteException } from '../../exceptions/cannot_lookup_route.js';
2
+ export class RouteFinder {
3
+ #routes;
4
+ constructor(routes) {
5
+ this.#routes = routes;
6
+ }
7
+ find(routeIdentifier) {
8
+ return (this.#routes.find(({ name, pattern, handler }) => {
9
+ if (name === routeIdentifier || pattern === routeIdentifier) {
10
+ return true;
11
+ }
12
+ if (typeof handler === 'function') {
13
+ return false;
14
+ }
15
+ return handler.name === routeIdentifier;
16
+ }) || null);
17
+ }
18
+ findOrFail(routeIdentifier) {
19
+ const route = this.find(routeIdentifier);
20
+ if (!route) {
21
+ throw new CannotLookupRouteException(`Cannot lookup route "${routeIdentifier}"`);
22
+ }
23
+ return route;
24
+ }
25
+ has(routeIdentifier) {
26
+ return !!this.find(routeIdentifier);
27
+ }
28
+ }
@@ -0,0 +1,16 @@
1
+ import type { Encryption } from '@adonisjs/encryption';
2
+ import type { Qs } from '../../qs.js';
3
+ import type { RouteFinder } from './route_finder.js';
4
+ export declare class UrlBuilder {
5
+ #private;
6
+ constructor(encryption: Encryption, routeFinder: RouteFinder, qsParser: Qs);
7
+ prefixUrl(url: string): this;
8
+ disableRouteLookup(): this;
9
+ qs(queryString?: Record<string, any>): this;
10
+ params(params?: any[] | Record<string, any>): this;
11
+ make(identifier: string): string;
12
+ makeSigned(identifier: string, options?: {
13
+ expiresIn?: string | number;
14
+ purpose?: string;
15
+ }): string;
16
+ }
@@ -0,0 +1,111 @@
1
+ import { RuntimeException } from '@poppinss/utils';
2
+ export class UrlBuilder {
3
+ #qsParser;
4
+ #params = {};
5
+ #qs = {};
6
+ #shouldPerformLookup = true;
7
+ #baseUrl;
8
+ #encryption;
9
+ #routeFinder;
10
+ constructor(encryption, routeFinder, qsParser) {
11
+ this.#qsParser = qsParser;
12
+ this.#encryption = encryption;
13
+ this.#routeFinder = routeFinder;
14
+ }
15
+ #ensureHasWildCardValues(pattern, values) {
16
+ if (!values || !Array.isArray(values) || !values.length) {
17
+ throw new RuntimeException(`Cannot make URL for "${pattern}" route. Invalid value provided for wildcard param`);
18
+ }
19
+ }
20
+ #ensureHasParamValue(pattern, param, value) {
21
+ if (value === undefined || value === null) {
22
+ throw new RuntimeException(`Cannot make URL for "${pattern}" route. Missing value for "${param}" param`);
23
+ }
24
+ }
25
+ #processPattern(pattern) {
26
+ const uriSegments = [];
27
+ const paramsArray = Array.isArray(this.#params) ? this.#params : null;
28
+ const paramsObject = !Array.isArray(this.#params) ? this.#params : {};
29
+ let paramsIndex = 0;
30
+ const tokens = pattern.split('/');
31
+ for (const token of tokens) {
32
+ if (token === '*') {
33
+ const values = paramsArray ? paramsArray.slice(paramsIndex) : paramsObject['*'];
34
+ this.#ensureHasWildCardValues(pattern, values);
35
+ values.forEach((value) => uriSegments.push(value));
36
+ break;
37
+ }
38
+ else if (!token.startsWith(':')) {
39
+ uriSegments.push(token);
40
+ }
41
+ else {
42
+ const paramName = token.replace(/^:/, '').replace(/\?$/, '');
43
+ const value = paramsArray ? paramsArray[paramsIndex] : paramsObject[paramName];
44
+ if (!token.endsWith('?')) {
45
+ this.#ensureHasParamValue(pattern, paramName, value);
46
+ }
47
+ paramsIndex++;
48
+ if (value !== undefined && value !== null) {
49
+ uriSegments.push(value);
50
+ }
51
+ }
52
+ }
53
+ return uriSegments.join('/');
54
+ }
55
+ #suffixQueryString(url, qs) {
56
+ if (qs) {
57
+ const queryString = this.#qsParser.stringify(qs);
58
+ url = queryString ? `${url}?${queryString}` : url;
59
+ }
60
+ return url;
61
+ }
62
+ #prefixBaseUrl(uri) {
63
+ return this.#baseUrl ? `${this.#baseUrl}${uri}` : uri;
64
+ }
65
+ prefixUrl(url) {
66
+ this.#baseUrl = url;
67
+ return this;
68
+ }
69
+ disableRouteLookup() {
70
+ this.#shouldPerformLookup = false;
71
+ return this;
72
+ }
73
+ qs(queryString) {
74
+ if (!queryString) {
75
+ return this;
76
+ }
77
+ this.#qs = queryString;
78
+ return this;
79
+ }
80
+ params(params) {
81
+ if (!params) {
82
+ return this;
83
+ }
84
+ this.#params = params;
85
+ return this;
86
+ }
87
+ make(identifier) {
88
+ let url;
89
+ if (this.#shouldPerformLookup) {
90
+ const route = this.#routeFinder.findOrFail(identifier);
91
+ url = this.#processPattern(route.pattern);
92
+ }
93
+ else {
94
+ url = this.#processPattern(identifier);
95
+ }
96
+ return this.#suffixQueryString(this.#prefixBaseUrl(url), this.#qs);
97
+ }
98
+ makeSigned(identifier, options) {
99
+ let url;
100
+ if (this.#shouldPerformLookup) {
101
+ const route = this.#routeFinder.findOrFail(identifier);
102
+ url = this.#processPattern(route.pattern);
103
+ }
104
+ else {
105
+ url = this.#processPattern(identifier);
106
+ }
107
+ const signature = this.#encryption.verifier.sign(this.#suffixQueryString(url, this.#qs), options?.expiresIn, options?.purpose);
108
+ const qs = Object.assign({}, this.#qs, { signature });
109
+ return this.#suffixQueryString(this.#prefixBaseUrl(url), qs);
110
+ }
111
+ }
@@ -0,0 +1,36 @@
1
+ import type { Encryption } from '@adonisjs/encryption';
2
+ import type { Application } from '@adonisjs/application';
3
+ import type { Qs } from '../qs.js';
4
+ import { Route } from './route.js';
5
+ import { RouteGroup } from './group.js';
6
+ import { BriskRoute } from './brisk.js';
7
+ import { RouteResource } from './resource.js';
8
+ import { LookupStore } from './lookup_store/main.js';
9
+ import { RouteMatchers as Matchers } from './matchers.js';
10
+ import type { LazyImport } from '../types/base.js';
11
+ import { MiddlewareStore } from '../middleware/store.js';
12
+ import type { MiddlewareAsClass } from '../types/middleware.js';
13
+ import type { RouteFn, MatchedRoute, RouteMatcher, MakeUrlOptions, MakeSignedUrlOptions } from '../types/route.js';
14
+ export declare class Router<NamedMiddleware extends Record<string, LazyImport<MiddlewareAsClass>> = any> extends LookupStore {
15
+ #private;
16
+ routes: (Route<NamedMiddleware> | RouteResource<NamedMiddleware> | RouteGroup<NamedMiddleware> | BriskRoute<NamedMiddleware>)[];
17
+ usingDomains: boolean;
18
+ matchers: Matchers;
19
+ constructor(app: Application<any, any, any>, encryption: Encryption, middlewareStore: MiddlewareStore<NamedMiddleware>, qsParser: Qs);
20
+ route(pattern: string, methods: string[], handler: string | RouteFn): Route<NamedMiddleware>;
21
+ any(pattern: string, handler: string | RouteFn): Route<NamedMiddleware>;
22
+ get(pattern: string, handler: string | RouteFn): Route<NamedMiddleware>;
23
+ post(pattern: string, handler: string | RouteFn): Route<NamedMiddleware>;
24
+ put(pattern: string, handler: string | RouteFn): Route<NamedMiddleware>;
25
+ patch(pattern: string, handler: string | RouteFn): Route<NamedMiddleware>;
26
+ delete(pattern: string, handler: string | RouteFn): Route<NamedMiddleware>;
27
+ group(callback: () => void): RouteGroup<NamedMiddleware>;
28
+ resource(resource: string, controller: string): RouteResource<NamedMiddleware>;
29
+ shallowResource(resource: string, controller: string): RouteResource<NamedMiddleware>;
30
+ on(pattern: string): BriskRoute<NamedMiddleware>;
31
+ where(param: string, matcher: RouteMatcher | string | RegExp): this;
32
+ commit(): void;
33
+ match(url: string, method: string, hostname?: string | null): null | MatchedRoute;
34
+ makeUrl(routeIdentifier: string, params?: any[] | Record<string, any>, options?: MakeUrlOptions): string;
35
+ makeSignedUrl(routeIdentifier: string, params?: any[] | Record<string, any>, options?: MakeSignedUrlOptions): string;
36
+ }
@@ -0,0 +1,161 @@
1
+ import is from '@sindresorhus/is';
2
+ import { RuntimeException } from '@poppinss/utils';
3
+ import { Route } from './route.js';
4
+ import { RouteGroup } from './group.js';
5
+ import { BriskRoute } from './brisk.js';
6
+ import { RoutesStore } from './store.js';
7
+ import { toRoutesJSON } from '../helpers.js';
8
+ import { RouteResource } from './resource.js';
9
+ import { LookupStore } from './lookup_store/main.js';
10
+ import { RouteMatchers as Matchers } from './matchers.js';
11
+ export class Router extends LookupStore {
12
+ #app;
13
+ #store = new RoutesStore();
14
+ #globalMatchers = {};
15
+ #middlewareStore;
16
+ #openedGroups = [];
17
+ routes = [];
18
+ usingDomains = false;
19
+ matchers = new Matchers();
20
+ constructor(app, encryption, middlewareStore, qsParser) {
21
+ super(encryption, qsParser);
22
+ this.#app = app;
23
+ this.#middlewareStore = middlewareStore;
24
+ }
25
+ #pushToRoutes(entity) {
26
+ const openedGroup = this.#openedGroups[this.#openedGroups.length - 1];
27
+ if (openedGroup) {
28
+ openedGroup.routes.push(entity);
29
+ return;
30
+ }
31
+ this.routes.push(entity);
32
+ }
33
+ route(pattern, methods, handler) {
34
+ const route = new Route(this.#app, this.#middlewareStore, {
35
+ pattern,
36
+ methods,
37
+ handler,
38
+ globalMatchers: this.#globalMatchers,
39
+ });
40
+ this.#pushToRoutes(route);
41
+ return route;
42
+ }
43
+ any(pattern, handler) {
44
+ return this.route(pattern, ['HEAD', 'OPTIONS', 'GET', 'POST', 'PUT', 'PATCH', 'DELETE'], handler);
45
+ }
46
+ get(pattern, handler) {
47
+ return this.route(pattern, ['GET', 'HEAD'], handler);
48
+ }
49
+ post(pattern, handler) {
50
+ return this.route(pattern, ['POST'], handler);
51
+ }
52
+ put(pattern, handler) {
53
+ return this.route(pattern, ['PUT'], handler);
54
+ }
55
+ patch(pattern, handler) {
56
+ return this.route(pattern, ['PATCH'], handler);
57
+ }
58
+ delete(pattern, handler) {
59
+ return this.route(pattern, ['DELETE'], handler);
60
+ }
61
+ group(callback) {
62
+ const group = new RouteGroup([], this.#middlewareStore);
63
+ this.#pushToRoutes(group);
64
+ this.#openedGroups.push(group);
65
+ callback();
66
+ this.#openedGroups.pop();
67
+ return group;
68
+ }
69
+ resource(resource, controller) {
70
+ const resourceInstance = new RouteResource(this.#app, this.#middlewareStore, {
71
+ resource,
72
+ controller,
73
+ shallow: false,
74
+ globalMatchers: this.#globalMatchers,
75
+ });
76
+ this.#pushToRoutes(resourceInstance);
77
+ return resourceInstance;
78
+ }
79
+ shallowResource(resource, controller) {
80
+ const resourceInstance = new RouteResource(this.#app, this.#middlewareStore, {
81
+ resource,
82
+ controller,
83
+ shallow: true,
84
+ globalMatchers: this.#globalMatchers,
85
+ });
86
+ this.#pushToRoutes(resourceInstance);
87
+ return resourceInstance;
88
+ }
89
+ on(pattern) {
90
+ const briskRoute = new BriskRoute(this.#app, this.#middlewareStore, {
91
+ pattern,
92
+ globalMatchers: this.#globalMatchers,
93
+ });
94
+ this.#pushToRoutes(briskRoute);
95
+ return briskRoute;
96
+ }
97
+ where(param, matcher) {
98
+ if (typeof matcher === 'string') {
99
+ this.#globalMatchers[param] = { match: new RegExp(matcher) };
100
+ }
101
+ else if (is.regExp(matcher)) {
102
+ this.#globalMatchers[param] = { match: matcher };
103
+ }
104
+ else {
105
+ this.#globalMatchers[param] = matcher;
106
+ }
107
+ return this;
108
+ }
109
+ commit() {
110
+ const routeNamesByDomain = new Map();
111
+ toRoutesJSON(this.routes).forEach((route) => {
112
+ if (!routeNamesByDomain.has(route.domain)) {
113
+ routeNamesByDomain.set(route.domain, new Set());
114
+ }
115
+ const routeNames = routeNamesByDomain.get(route.domain);
116
+ if (route.name && routeNames.has(route.name)) {
117
+ throw new RuntimeException(`Route with duplicate name found. A route with name "${route.name}" already exists`);
118
+ }
119
+ if (route.name) {
120
+ routeNames.add(route.name);
121
+ }
122
+ this.register(route);
123
+ this.#store.add(route);
124
+ });
125
+ routeNamesByDomain.clear();
126
+ this.usingDomains = this.#store.usingDomains;
127
+ this.routes = [];
128
+ this.#globalMatchers = {};
129
+ }
130
+ match(url, method, hostname) {
131
+ const matchingDomain = this.#store.matchDomain(hostname);
132
+ return matchingDomain.length
133
+ ? this.#store.match(url, method, {
134
+ tokens: matchingDomain,
135
+ hostname: hostname,
136
+ })
137
+ : this.#store.match(url, method);
138
+ }
139
+ makeUrl(routeIdentifier, params, options) {
140
+ const normalizedOptions = Object.assign({}, options);
141
+ const builder = normalizedOptions.domain
142
+ ? this.builderForDomain(normalizedOptions.domain)
143
+ : this.builder();
144
+ builder.params(params);
145
+ builder.qs(normalizedOptions.qs);
146
+ normalizedOptions.prefixUrl && builder.prefixUrl(normalizedOptions.prefixUrl);
147
+ normalizedOptions.disableRouteLookup && builder.disableRouteLookup();
148
+ return builder.make(routeIdentifier);
149
+ }
150
+ makeSignedUrl(routeIdentifier, params, options) {
151
+ const normalizedOptions = Object.assign({}, options);
152
+ const builder = normalizedOptions.domain
153
+ ? this.builderForDomain(normalizedOptions.domain)
154
+ : this.builder();
155
+ builder.params(params);
156
+ builder.qs(normalizedOptions.qs);
157
+ normalizedOptions.prefixUrl && builder.prefixUrl(normalizedOptions.prefixUrl);
158
+ normalizedOptions.disableRouteLookup && builder.disableRouteLookup();
159
+ return builder.makeSigned(routeIdentifier, normalizedOptions);
160
+ }
161
+ }
@@ -0,0 +1,14 @@
1
+ import { Macroable } from '@poppinss/macroable';
2
+ export declare class RouteMatchers extends Macroable {
3
+ number(): {
4
+ match: RegExp;
5
+ cast: (value: string) => number;
6
+ };
7
+ uuid(): {
8
+ match: RegExp;
9
+ cast: (value: string) => string;
10
+ };
11
+ slug(): {
12
+ match: RegExp;
13
+ };
14
+ }
@@ -0,0 +1,15 @@
1
+ import { Macroable } from '@poppinss/macroable';
2
+ export class RouteMatchers extends Macroable {
3
+ number() {
4
+ return { match: /^[0-9]+$/, cast: (value) => Number(value) };
5
+ }
6
+ uuid() {
7
+ return {
8
+ 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
+ cast: (value) => value.toLowerCase(),
10
+ };
11
+ }
12
+ slug() {
13
+ return { match: /^[^\s-_](?!.*?[-_]{2,})([a-z0-9-\\]{1,})[^\s]*[^-_\s]$/ };
14
+ }
15
+ }
@@ -0,0 +1,27 @@
1
+ import { Macroable } from '@poppinss/macroable';
2
+ import type { Application } from '@adonisjs/application';
3
+ import { Route } from './route.js';
4
+ import type { LazyImport } from '../types/base.js';
5
+ import type { MiddlewareStore } from '../middleware/store.js';
6
+ import type { MiddlewareAsClass } from '../types/middleware.js';
7
+ import type { ResourceActionNames, RouteMatcher, RouteMatchers } from '../types/route.js';
8
+ export declare class RouteResource<NamedMiddleware extends Record<string, LazyImport<MiddlewareAsClass>> = any> extends Macroable {
9
+ #private;
10
+ routes: Route[];
11
+ constructor(app: Application<any, any, any>, middlewareStore: MiddlewareStore<NamedMiddleware>, options: {
12
+ resource: string;
13
+ controller: string;
14
+ globalMatchers: RouteMatchers;
15
+ shallow: boolean;
16
+ });
17
+ only(names: ResourceActionNames[]): this;
18
+ except(names: ResourceActionNames[]): this;
19
+ apiOnly(): this;
20
+ where(key: string, matcher: RouteMatcher | string | RegExp): this;
21
+ tap(callback: (route: Route<NamedMiddleware>) => void): this;
22
+ tap(actions: ResourceActionNames | ResourceActionNames[], callback: (route: Route<NamedMiddleware>) => void): this;
23
+ params(resources: {
24
+ [resource: string]: string;
25
+ }): this;
26
+ as(name: string): this;
27
+ }