@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
@@ -1,181 +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.HttpContext = void 0;
15
- /// <reference path="../../adonis-typings/index.ts" />
16
- const net_1 = require("net");
17
- const util_1 = require("util");
18
- const macroable_1 = require("macroable");
19
- const matchit_1 = __importDefault(require("@poppinss/matchit"));
20
- const utils_1 = require("@poppinss/utils");
21
- const http_1 = require("http");
22
- const Request_1 = require("../Request");
23
- const Response_1 = require("../Response");
24
- const helpers_1 = require("../helpers");
25
- const LocalStorage_1 = require("./LocalStorage");
26
- const exceptions_json_1 = require("../../exceptions.json");
27
- /**
28
- * Http context is passed to all route handlers, middleware,
29
- * error handler and server hooks.
30
- */
31
- class HttpContext extends macroable_1.Macroable {
32
- constructor(request, response, logger, profiler) {
33
- super();
34
- this.request = request;
35
- this.response = response;
36
- this.logger = logger;
37
- this.profiler = profiler;
38
- /**
39
- * Route params
40
- */
41
- this.params = {};
42
- /**
43
- * Route subdomains
44
- */
45
- this.subdomains = {};
46
- /*
47
- * Creating the circular reference. We do this, since request and response
48
- * are meant to be extended and at times people would want to access
49
- * other ctx properties like `logger`, `profiler` inside those
50
- * extended methods.
51
- */
52
- this.request.ctx = this;
53
- this.response.ctx = this;
54
- }
55
- /**
56
- * Find if async localstorage is enabled for HTTP requests
57
- * or not
58
- */
59
- static get usingAsyncLocalStorage() {
60
- return LocalStorage_1.usingAsyncLocalStorage;
61
- }
62
- /**
63
- * Get access to the HTTP context. Available only when
64
- * "usingAsyncLocalStorage" is true
65
- */
66
- static get() {
67
- if (!LocalStorage_1.usingAsyncLocalStorage) {
68
- return null;
69
- }
70
- return LocalStorage_1.httpContextLocalStorage.getStore() || null;
71
- }
72
- /**
73
- * Get the HttpContext instance or raise an exception if not
74
- * available
75
- */
76
- static getOrFail() {
77
- /**
78
- * Localstorage is not enabled
79
- */
80
- if (!LocalStorage_1.usingAsyncLocalStorage) {
81
- const error = new utils_1.Exception(exceptions_json_1.E_INVALID_ALS_ACCESS.message, exceptions_json_1.E_INVALID_ALS_ACCESS.status, exceptions_json_1.E_INVALID_ALS_ACCESS.code);
82
- error.help = exceptions_json_1.E_INVALID_ALS_ACCESS.help.join('\n');
83
- throw error;
84
- }
85
- const store = this.get();
86
- /**
87
- * Store is not accessible
88
- */
89
- if (!store) {
90
- const error = new utils_1.Exception(exceptions_json_1.E_INVALID_ALS_SCOPE.message, exceptions_json_1.E_INVALID_ALS_SCOPE.status, exceptions_json_1.E_INVALID_ALS_SCOPE.code);
91
- error.help = exceptions_json_1.E_INVALID_ALS_SCOPE.help.join('\n');
92
- throw error;
93
- }
94
- return store;
95
- }
96
- /**
97
- * Run a method that doesn't have access to HTTP context from
98
- * the async local storage.
99
- */
100
- static runOutsideContext(callback, ...args) {
101
- return LocalStorage_1.httpContextLocalStorage.exit(callback, ...args);
102
- }
103
- /**
104
- * A helper to see top level properties on the context object
105
- */
106
- inspect() {
107
- return (0, util_1.inspect)(this, false, 1, true);
108
- }
109
- /**
110
- * Creates a new fake context instance for a given route. The method is
111
- * meant to be used inside an AdonisJS application since it relies
112
- * directly on the IoC container.
113
- */
114
- static create(routePattern, routeParams, req, res) {
115
- const Router = HttpContext.app.container.resolveBinding('Adonis/Core/Route');
116
- const Encryption = HttpContext.app.container.resolveBinding('Adonis/Core/Encryption');
117
- const serverConfig = HttpContext.app.container
118
- .resolveBinding('Adonis/Core/Config')
119
- .get('app.http', {});
120
- req = req || new http_1.IncomingMessage(new net_1.Socket());
121
- res = res || new http_1.ServerResponse(req);
122
- /*
123
- * Creating the url from the router pattern and params. Only
124
- * when actual URL isn't defined.
125
- */
126
- req.url = req.url || (0, helpers_1.processPattern)(routePattern, routeParams);
127
- /*
128
- * Creating new request instance
129
- */
130
- const request = new Request_1.Request(req, res, Encryption, {
131
- allowMethodSpoofing: serverConfig.allowMethodSpoofing,
132
- subdomainOffset: serverConfig.subdomainOffset,
133
- trustProxy: serverConfig.trustProxy,
134
- generateRequestId: serverConfig.generateRequestId,
135
- });
136
- /*
137
- * Creating new response instance
138
- */
139
- const response = new Response_1.Response(req, res, Encryption, {
140
- etag: serverConfig.etag,
141
- cookie: serverConfig.cookie,
142
- jsonpCallbackName: serverConfig.jsonpCallbackName,
143
- }, Router);
144
- /*
145
- * Creating new ctx instance
146
- */
147
- const ctx = new HttpContext(request, response, this.app.logger.child({}), this.app.profiler.create('http:context'));
148
- /*
149
- * Attaching route to the ctx
150
- */
151
- ctx.route = {
152
- pattern: routePattern,
153
- middleware: [],
154
- handler: async () => 'handled',
155
- meta: {},
156
- params: matchit_1.default
157
- .parse(routePattern, {})
158
- .filter((token) => [1, 3].includes(token.type))
159
- .map((token) => token.val),
160
- };
161
- /*
162
- * Defining route key
163
- */
164
- ctx.routeKey = `${request.method() || 'GET'}-${ctx.route.pattern}`;
165
- /*
166
- * Attaching params to the ctx
167
- */
168
- ctx.params = routeParams;
169
- /**
170
- * Set params on the request
171
- */
172
- ctx.request.updateParams(routeParams);
173
- return ctx;
174
- }
175
- }
176
- exports.HttpContext = HttpContext;
177
- /**
178
- * Required by macroable
179
- */
180
- HttpContext.macros = {};
181
- HttpContext.getters = {};
@@ -1,92 +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 { IocContract } from '@adonisjs/fold';
11
- import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext';
12
- import { MiddlewareHandler, MiddlewareStoreContract, ResolvedMiddlewareHandler } from '@ioc:Adonis/Core/Middleware';
13
- /**
14
- * Middleware store register and keep all the application middleware at one
15
- * place. Also middleware are resolved during the registration and any
16
- * part of the application can read them without extra overhead.
17
- *
18
- * @example
19
- * ```ts
20
- * const store = new MiddlewareStore()
21
- *
22
- * store.register([
23
- * 'App/Middleware/Auth'
24
- * ])
25
- *
26
- * store.registerNamed({
27
- * auth: 'App/Middleware/Auth'
28
- * })
29
- *
30
- * store.get()
31
- * ```
32
- */
33
- export declare class MiddlewareStore implements MiddlewareStoreContract {
34
- private container;
35
- /**
36
- * A list of global middleware
37
- */
38
- private list;
39
- /**
40
- * A map of named middleware. Named middleware are used as reference
41
- * on the routes
42
- */
43
- private named;
44
- /**
45
- * The resolver to resolve middleware from the IoC container
46
- */
47
- private resolver;
48
- constructor(container: IocContract);
49
- /**
50
- * Resolves the middleware node based upon it's type. If value is a string, then
51
- * we pre-fetch it from the IoC container upfront. On every request, we just
52
- * create a new instance of the class and avoid re-fetching it from the IoC
53
- * container for performance reasons.
54
- *
55
- * The annoying part is that one has to create the middleware before registering
56
- * it, otherwise an exception will be raised.
57
- */
58
- private resolveMiddleware;
59
- /**
60
- * Register an array of global middleware. These middleware are read
61
- * by HTTP server and executed on every request
62
- */
63
- register(middleware: MiddlewareHandler[]): this;
64
- /**
65
- * Register named middleware that can be referenced later on routes
66
- */
67
- registerNamed(middleware: {
68
- [alias: string]: MiddlewareHandler;
69
- }): this;
70
- /**
71
- * Return all middleware registered using [[MiddlewareStore.register]]
72
- * method
73
- */
74
- get(): ResolvedMiddlewareHandler[];
75
- /**
76
- * Returns a single middleware by it's name registered
77
- * using [[MiddlewareStore.registerNamed]] method.
78
- */
79
- getNamed(name: string): null | ResolvedMiddlewareHandler;
80
- /**
81
- * Clears all the global middleware
82
- */
83
- clear(): void;
84
- /**
85
- * Clears all/selected named middleware
86
- */
87
- clearNamed(names?: string[]): void;
88
- /**
89
- * Invokes a resolved middleware.
90
- */
91
- invokeMiddleware(middleware: ResolvedMiddlewareHandler, params: [HttpContextContract, () => Promise<void>]): Promise<void>;
92
- }
@@ -1,133 +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.MiddlewareStore = void 0;
12
- /**
13
- * Middleware store register and keep all the application middleware at one
14
- * place. Also middleware are resolved during the registration and any
15
- * part of the application can read them without extra overhead.
16
- *
17
- * @example
18
- * ```ts
19
- * const store = new MiddlewareStore()
20
- *
21
- * store.register([
22
- * 'App/Middleware/Auth'
23
- * ])
24
- *
25
- * store.registerNamed({
26
- * auth: 'App/Middleware/Auth'
27
- * })
28
- *
29
- * store.get()
30
- * ```
31
- */
32
- class MiddlewareStore {
33
- constructor(container) {
34
- this.container = container;
35
- /**
36
- * A list of global middleware
37
- */
38
- this.list = [];
39
- /**
40
- * A map of named middleware. Named middleware are used as reference
41
- * on the routes
42
- */
43
- this.named = {};
44
- this.resolver = container.getResolver();
45
- }
46
- /**
47
- * Resolves the middleware node based upon it's type. If value is a string, then
48
- * we pre-fetch it from the IoC container upfront. On every request, we just
49
- * create a new instance of the class and avoid re-fetching it from the IoC
50
- * container for performance reasons.
51
- *
52
- * The annoying part is that one has to create the middleware before registering
53
- * it, otherwise an exception will be raised.
54
- */
55
- resolveMiddleware(middleware) {
56
- return typeof middleware === 'function'
57
- ? {
58
- type: 'lazy-import',
59
- value: middleware,
60
- args: [],
61
- }
62
- : Object.assign(this.resolver.resolve(`${middleware}.handle`), { args: [] });
63
- }
64
- /**
65
- * Register an array of global middleware. These middleware are read
66
- * by HTTP server and executed on every request
67
- */
68
- register(middleware) {
69
- this.list = middleware.map(this.resolveMiddleware.bind(this));
70
- return this;
71
- }
72
- /**
73
- * Register named middleware that can be referenced later on routes
74
- */
75
- registerNamed(middleware) {
76
- this.named = Object.keys(middleware).reduce((result, alias) => {
77
- result[alias] = this.resolveMiddleware(middleware[alias]);
78
- return result;
79
- }, {});
80
- return this;
81
- }
82
- /**
83
- * Return all middleware registered using [[MiddlewareStore.register]]
84
- * method
85
- */
86
- get() {
87
- return this.list;
88
- }
89
- /**
90
- * Returns a single middleware by it's name registered
91
- * using [[MiddlewareStore.registerNamed]] method.
92
- */
93
- getNamed(name) {
94
- return this.named[name] || null;
95
- }
96
- /**
97
- * Clears all the global middleware
98
- */
99
- clear() {
100
- this.list = [];
101
- }
102
- /**
103
- * Clears all/selected named middleware
104
- */
105
- clearNamed(names) {
106
- if (names) {
107
- names.forEach((name) => delete this.named[name]);
108
- }
109
- else {
110
- this.named = {};
111
- }
112
- }
113
- /**
114
- * Invokes a resolved middleware.
115
- */
116
- async invokeMiddleware(middleware, params) {
117
- if (middleware.type === 'function') {
118
- return middleware.value(params[0], params[1], middleware.args);
119
- }
120
- const args = [params[0], params[1]];
121
- args.push(middleware.args);
122
- /**
123
- * Handling the lazily imported middleware
124
- */
125
- if (middleware.type === 'lazy-import') {
126
- const middlewareClass = await middleware.value();
127
- const middlewareInstance = await this.container.makeAsync(middlewareClass.default);
128
- return this.container.callAsync(middlewareInstance, 'handle', args);
129
- }
130
- return this.resolver.call(middleware, undefined, args);
131
- }
132
- }
133
- exports.MiddlewareStore = MiddlewareStore;
@@ -1,71 +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
- /// <reference types="node" />
11
- import { IncomingMessage } from 'http';
12
- import { RouterContract, MakeUrlOptions } from '@ioc:Adonis/Core/Route';
13
- import { RedirectContract, ResponseContract } from '@ioc:Adonis/Core/Response';
14
- /**
15
- * Exposes the API to construct redirect routes
16
- */
17
- export declare class Redirect implements RedirectContract {
18
- private request;
19
- private response;
20
- private router;
21
- /**
22
- * A boolean to forward the existing query string
23
- */
24
- private forwardQueryString;
25
- /**
26
- * The status code for the redirect
27
- */
28
- private statusCode;
29
- /**
30
- * A custom query string to forward
31
- */
32
- private queryString;
33
- constructor(request: IncomingMessage, response: ResponseContract, router: RouterContract);
34
- /**
35
- * Sends response by setting require headers
36
- */
37
- private sendResponse;
38
- /**
39
- * Returns the referrer url
40
- */
41
- private getReferrerUrl;
42
- /**
43
- * Set a custom status code.
44
- */
45
- status(statusCode: number): this;
46
- /**
47
- * Clearing query string values added using the
48
- * "withQs" method
49
- */
50
- clearQs(): this;
51
- /**
52
- * Define query string for the redirect. Not passing
53
- * any value will forward the current request query
54
- * string.
55
- */
56
- withQs(): this;
57
- withQs(values: Record<string, any>): this;
58
- withQs(name: string, value: any): this;
59
- /**
60
- * Redirect to the previous path.
61
- */
62
- back(): void;
63
- /**
64
- * Redirect the request using a route identifier.
65
- */
66
- toRoute(routeIdentifier: string, params?: any[] | MakeUrlOptions, options?: MakeUrlOptions): void;
67
- /**
68
- * Redirect the request using a path.
69
- */
70
- toPath(url: string): void;
71
- }
@@ -1,139 +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.Redirect = void 0;
15
- /// <reference path="../../adonis-typings/index.ts" />
16
- const qs_1 = __importDefault(require("qs"));
17
- const url_1 = require("url");
18
- const encodeurl_1 = __importDefault(require("encodeurl"));
19
- /**
20
- * Exposes the API to construct redirect routes
21
- */
22
- class Redirect {
23
- constructor(request, response, router) {
24
- this.request = request;
25
- this.response = response;
26
- this.router = router;
27
- /**
28
- * A boolean to forward the existing query string
29
- */
30
- this.forwardQueryString = false;
31
- /**
32
- * The status code for the redirect
33
- */
34
- this.statusCode = 302;
35
- /**
36
- * A custom query string to forward
37
- */
38
- this.queryString = {};
39
- }
40
- /**
41
- * Sends response by setting require headers
42
- */
43
- sendResponse(url, query) {
44
- const stringified = qs_1.default.stringify(query);
45
- url = stringified ? `${url}?${stringified}` : url;
46
- this.response.location((0, encodeurl_1.default)(url));
47
- this.response.safeStatus(this.statusCode);
48
- this.response.type('text/plain; charset=utf-8');
49
- this.response.send(`Redirecting to ${url}`);
50
- }
51
- /**
52
- * Returns the referrer url
53
- */
54
- getReferrerUrl() {
55
- let url = this.request.headers['referer'] || this.request.headers['referrer'] || '/';
56
- return Array.isArray(url) ? url[0] : url;
57
- }
58
- /**
59
- * Set a custom status code.
60
- */
61
- status(statusCode) {
62
- this.statusCode = statusCode;
63
- return this;
64
- }
65
- /**
66
- * Clearing query string values added using the
67
- * "withQs" method
68
- */
69
- clearQs() {
70
- this.forwardQueryString = false;
71
- this.queryString = {};
72
- return this;
73
- }
74
- withQs(name, value) {
75
- if (typeof name === 'undefined') {
76
- this.forwardQueryString = true;
77
- return this;
78
- }
79
- if (typeof name === 'string') {
80
- this.queryString[name] = value;
81
- return this;
82
- }
83
- Object.assign(this.queryString, name);
84
- return this;
85
- }
86
- /**
87
- * Redirect to the previous path.
88
- */
89
- back() {
90
- let query = {};
91
- const url = (0, url_1.parse)(this.getReferrerUrl());
92
- /**
93
- * Parse query string from the referrer url
94
- */
95
- if (this.forwardQueryString) {
96
- query = qs_1.default.parse(url.query || '');
97
- }
98
- /**
99
- * Append custom query string
100
- */
101
- Object.assign(query, this.queryString);
102
- /**
103
- * Redirect
104
- */
105
- this.sendResponse(url.pathname || '', query);
106
- }
107
- /**
108
- * Redirect the request using a route identifier.
109
- */
110
- toRoute(routeIdentifier, params, options) {
111
- if (options && options.qs) {
112
- this.withQs(options.qs);
113
- options.qs = undefined;
114
- }
115
- const url = this.router.makeUrl(routeIdentifier, params, options);
116
- return this.toPath(url);
117
- }
118
- /**
119
- * Redirect the request using a path.
120
- */
121
- toPath(url) {
122
- let query = {};
123
- /**
124
- * Extract query string from the current URL
125
- */
126
- if (this.forwardQueryString) {
127
- query = qs_1.default.parse((0, url_1.parse)(this.request.url).query || '');
128
- }
129
- /**
130
- * Assign custom query string
131
- */
132
- Object.assign(query, this.queryString);
133
- /**
134
- * Redirect
135
- */
136
- this.sendResponse(url, query);
137
- }
138
- }
139
- exports.Redirect = Redirect;