@adonisjs/http-server 6.8.2-5 → 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,11 +1,49 @@
1
+ /*
2
+ * @adonisjs/http-server
3
+ *
4
+ * (c) AdonisJS
5
+ *
6
+ * For the full copyright and license information, please view the LICENSE
7
+ * file that was distributed with this source code.
8
+ */
1
9
  import is from '@sindresorhus/is';
2
10
  import { parseRange } from './helpers.js';
3
11
  import * as errors from './exceptions.js';
12
+ /**
13
+ * The base HTTP exception handler one can inherit from to handle
14
+ * HTTP exceptions.
15
+ *
16
+ * The HTTP exception handler has support for
17
+ *
18
+ * - Ability to render exceptions by calling the render method on the exception.
19
+ * - Rendering status pages
20
+ * - Pretty printing errors during development
21
+ * - Transforming errors to JSON or HTML using content negotiation
22
+ * - Reporting errors
23
+ */
4
24
  export class ExceptionHandler {
25
+ /**
26
+ * Whether or not to render debug info. When set to true, the errors
27
+ * will have the complete error stack.
28
+ */
5
29
  debug = process.env.NODE_ENV !== 'production';
30
+ /**
31
+ * Whether or not to render status pages. When set to true, the unhandled
32
+ * errors with matching status codes will be rendered using a status
33
+ * page.
34
+ */
6
35
  renderStatusPages = process.env.NODE_ENV === 'production';
36
+ /**
37
+ * A collection of error status code range and the view to render.
38
+ */
7
39
  statusPages = {};
40
+ /**
41
+ * Computed from the status pages property
42
+ */
8
43
  #expandedStatusPages;
44
+ /**
45
+ * Renderers for rendering an error.
46
+ */
9
47
  renderers = {
10
48
  html: async (error, ctx) => {
11
49
  if (this.isDebuggingEnabled(ctx)) {
@@ -43,15 +81,33 @@ export class ExceptionHandler {
43
81
  });
44
82
  },
45
83
  };
84
+ /**
85
+ * Enable/disable errors reporting
86
+ */
46
87
  reportErrors = true;
88
+ /**
89
+ * An array of exception classes to ignore when
90
+ * reporting an error
91
+ */
47
92
  ignoreExceptions = [
48
93
  errors.E_HTTP_EXCEPTION,
49
94
  errors.E_ROUTE_NOT_FOUND,
50
95
  errors.E_CANNOT_LOOKUP_ROUTE,
51
96
  errors.E_HTTP_REQUEST_ABORTED,
52
97
  ];
98
+ /**
99
+ * An array of HTTP status codes to ignore when reporting
100
+ * an error
101
+ */
53
102
  ignoreStatuses = [400, 422, 401];
103
+ /**
104
+ * An array of error codes to ignore when reporting
105
+ * an error
106
+ */
54
107
  ignoreCodes = [];
108
+ /**
109
+ * Expands status pages
110
+ */
55
111
  #expandStatusPages() {
56
112
  if (!this.#expandedStatusPages) {
57
113
  this.#expandedStatusPages = Object.keys(this.statusPages).reduce((result, range) => {
@@ -62,12 +118,19 @@ export class ExceptionHandler {
62
118
  }
63
119
  return this.#expandedStatusPages;
64
120
  }
121
+ /**
122
+ * Forcefully tweaking the type of the error object to
123
+ * have known properties.
124
+ */
65
125
  #toHttpError(error) {
66
126
  const httpError = is.object(error) ? error : new Error(String(error));
67
127
  httpError.message = httpError.message || 'Internal server error';
68
128
  httpError.status = httpError.status || 500;
69
129
  return httpError;
70
130
  }
131
+ /**
132
+ * Error reporting context
133
+ */
71
134
  context(ctx) {
72
135
  const requestId = ctx.request.id();
73
136
  return requestId
@@ -76,6 +139,9 @@ export class ExceptionHandler {
76
139
  }
77
140
  : {};
78
141
  }
142
+ /**
143
+ * The format in which the error should be rendered.
144
+ */
79
145
  getResponseFormat(ctx) {
80
146
  switch (ctx.request.accepts(['html', 'application/vnd.api+json', 'json'])) {
81
147
  case 'application/vnd.api+json':
@@ -87,6 +153,10 @@ export class ExceptionHandler {
87
153
  return 'html';
88
154
  }
89
155
  }
156
+ /**
157
+ * Returns the log level for an error based upon the error
158
+ * status code.
159
+ */
90
160
  getErrorLogLevel(error) {
91
161
  if (error.status >= 500) {
92
162
  return 'error';
@@ -96,9 +166,16 @@ export class ExceptionHandler {
96
166
  }
97
167
  return 'info';
98
168
  }
169
+ /**
170
+ * A boolean to control if errors should be rendered with
171
+ * all the available debugging info.
172
+ */
99
173
  isDebuggingEnabled(_) {
100
174
  return this.debug;
101
175
  }
176
+ /**
177
+ * Returns a boolean by checking if an error should be reported.
178
+ */
102
179
  shouldReport(error) {
103
180
  if (this.reportErrors === false) {
104
181
  return false;
@@ -114,6 +191,9 @@ export class ExceptionHandler {
114
191
  }
115
192
  return true;
116
193
  }
194
+ /**
195
+ * Reports an error during an HTTP request
196
+ */
117
197
  async report(error, ctx) {
118
198
  const httpError = this.#toHttpError(error);
119
199
  if (!this.shouldReport(httpError)) {
@@ -123,21 +203,36 @@ export class ExceptionHandler {
123
203
  httpError.report(httpError, ctx);
124
204
  return;
125
205
  }
206
+ /**
207
+ * Log the error using the logger
208
+ */
126
209
  const level = this.getErrorLogLevel(httpError);
127
210
  ctx.logger.log(level, {
128
211
  ...(level === 'error' || level === 'fatal' ? { err: httpError } : {}),
129
212
  ...this.context(ctx),
130
213
  }, httpError.message);
131
214
  }
215
+ /**
216
+ * Handles the error during the HTTP request.
217
+ */
132
218
  async handle(error, ctx) {
133
219
  const httpError = this.#toHttpError(error);
220
+ /**
221
+ * Self handle exception
222
+ */
134
223
  if (typeof httpError.handle === 'function') {
135
224
  return httpError.handle(httpError, ctx);
136
225
  }
226
+ /**
227
+ * Render status page
228
+ */
137
229
  const statusPages = this.#expandStatusPages();
138
230
  if (this.renderStatusPages && statusPages[httpError.status]) {
139
231
  return statusPages[httpError.status](httpError, ctx);
140
232
  }
233
+ /**
234
+ * Use the format renderers.
235
+ */
141
236
  const responseFormat = this.getResponseFormat(ctx);
142
237
  return this.renderers[responseFormat](httpError, ctx);
143
238
  }
@@ -20,6 +20,9 @@ export declare const E_HTTP_EXCEPTION: {
20
20
  cause?: unknown;
21
21
  };
22
22
  code: string;
23
+ /**
24
+ * This method returns an instance of the exception class
25
+ */
23
26
  invoke(body: any, status: number, code?: string): {
24
27
  body: any;
25
28
  name: string;
@@ -57,6 +60,9 @@ export declare const E_HTTP_REQUEST_ABORTED: {
57
60
  cause?: unknown;
58
61
  };
59
62
  code: string;
63
+ /**
64
+ * This method returns an instance of the exception class
65
+ */
60
66
  invoke(body: any, status: number, code?: string): {
61
67
  body: any;
62
68
  name: string;
@@ -76,4 +82,3 @@ export declare const E_HTTP_REQUEST_ABORTED: {
76
82
  prepareStackTrace?: ((err: Error, stackTraces: NodeJS.CallSite[]) => any) | undefined;
77
83
  stackTraceLimit: number;
78
84
  };
79
- //# sourceMappingURL=exceptions.d.ts.map
@@ -1,9 +1,20 @@
1
+ /*
2
+ * @adonisjs/http-server
3
+ *
4
+ * (c) AdonisJS
5
+ *
6
+ * For the full copyright and license information, please view the LICENSE
7
+ * file that was distributed with this source code.
8
+ */
1
9
  import { createError, Exception } from '@poppinss/utils';
2
10
  export const E_ROUTE_NOT_FOUND = createError('Cannot %s:%s', 'E_ROUTE_NOT_FOUND', 404);
3
11
  export const E_CANNOT_LOOKUP_ROUTE = createError('Cannot lookup route "%s"', 'E_CANNOT_LOOKUP_ROUTE', 500);
4
12
  export const E_HTTP_EXCEPTION = class HttpException extends Exception {
5
13
  body;
6
14
  static code = 'E_HTTP_EXCEPTION';
15
+ /**
16
+ * This method returns an instance of the exception class
17
+ */
7
18
  static invoke(body, status, code = 'E_HTTP_EXCEPTION') {
8
19
  if (body === null || body === undefined) {
9
20
  const error = new this('HTTP Exception', { status, code });
@@ -3,8 +3,21 @@ import { BriskRoute } from './router/brisk.js';
3
3
  import { RouteGroup } from './router/group.js';
4
4
  import type { RouteJSON } from './types/route.js';
5
5
  import { RouteResource } from './router/resource.js';
6
+ /**
7
+ * Makes input string consistent by having only the starting
8
+ * slash
9
+ */
6
10
  export declare function dropSlash(input: string): string;
11
+ /**
12
+ * Returns a flat list of routes from the route groups and resources
13
+ */
7
14
  export declare function toRoutesJSON(routes: (RouteGroup | Route | RouteResource | BriskRoute)[]): RouteJSON[];
15
+ /**
16
+ * Helper to know if the remote address should
17
+ * be trusted.
18
+ */
8
19
  export declare function trustProxy(remoteAddress: string, proxyFn: (addr: string, distance: number) => boolean): boolean;
20
+ /**
21
+ * Parses a range expression to an object filled with the range
22
+ */
9
23
  export declare function parseRange<T>(range: string, value: T): Record<number, T>;
10
- //# sourceMappingURL=helpers.d.ts.map
@@ -1,15 +1,30 @@
1
+ /*
2
+ * @adonisjs/http-server
3
+ *
4
+ * (c) AdonisJS
5
+ *
6
+ * For the full copyright and license information, please view the LICENSE
7
+ * file that was distributed with this source code.
8
+ */
1
9
  import Cache from 'tmp-cache';
2
10
  import { InvalidArgumentsException } from '@poppinss/utils';
3
11
  import { BriskRoute } from './router/brisk.js';
4
12
  import { RouteGroup } from './router/group.js';
5
13
  import { RouteResource } from './router/resource.js';
6
14
  const proxyCache = new Cache({ max: 200 });
15
+ /**
16
+ * Makes input string consistent by having only the starting
17
+ * slash
18
+ */
7
19
  export function dropSlash(input) {
8
20
  if (input === '/') {
9
21
  return '/';
10
22
  }
11
23
  return `/${input.replace(/^\//, '').replace(/\/$/, '')}`;
12
24
  }
25
+ /**
26
+ * Returns a flat list of routes from the route groups and resources
27
+ */
13
28
  export function toRoutesJSON(routes) {
14
29
  return routes.reduce((list, route) => {
15
30
  if (route instanceof RouteGroup) {
@@ -32,6 +47,10 @@ export function toRoutesJSON(routes) {
32
47
  return list;
33
48
  }, []);
34
49
  }
50
+ /**
51
+ * Helper to know if the remote address should
52
+ * be trusted.
53
+ */
35
54
  export function trustProxy(remoteAddress, proxyFn) {
36
55
  if (proxyCache.has(remoteAddress)) {
37
56
  return proxyCache.get(remoteAddress);
@@ -40,6 +59,9 @@ export function trustProxy(remoteAddress, proxyFn) {
40
59
  proxyCache.set(remoteAddress, result);
41
60
  return result;
42
61
  }
62
+ /**
63
+ * Parses a range expression to an object filled with the range
64
+ */
43
65
  export function parseRange(range, value) {
44
66
  const parts = range.split('..');
45
67
  const min = Number(parts[0]);
@@ -1,10 +1,12 @@
1
1
  /// <reference types="node" resolution-mode="require"/>
2
2
  import { AsyncLocalStorage } from 'node:async_hooks';
3
3
  import type { HttpContext } from './main.js';
4
+ /**
5
+ * Async local storage for HTTP context
6
+ */
4
7
  export declare const asyncLocalStorage: {
5
8
  isEnabled: boolean;
6
9
  storage: null | AsyncLocalStorage<HttpContext>;
7
10
  create(): AsyncLocalStorage<HttpContext>;
8
11
  destroy(): void;
9
12
  };
10
- //# sourceMappingURL=local_storage.d.ts.map
@@ -1,12 +1,37 @@
1
+ /*
2
+ * @adonisjs/http-server
3
+ *
4
+ * (c) AdonisJS
5
+ *
6
+ * For the full copyright and license information, please view the LICENSE
7
+ * file that was distributed with this source code.
8
+ */
1
9
  import { AsyncLocalStorage } from 'node:async_hooks';
10
+ /**
11
+ * Async local storage for HTTP context
12
+ */
2
13
  export const asyncLocalStorage = {
14
+ /**
15
+ * Check if the async local storage for the HTTP
16
+ * context is enabled or not
17
+ */
3
18
  isEnabled: false,
19
+ /**
20
+ * HTTP context storage instance for the current scope
21
+ */
4
22
  storage: null,
23
+ /**
24
+ * Create the storage instance. This method must be called only
25
+ * once.
26
+ */
5
27
  create() {
6
28
  this.isEnabled = true;
7
29
  this.storage = new AsyncLocalStorage();
8
30
  return this.storage;
9
31
  },
32
+ /**
33
+ * Destroy the create storage instance
34
+ */
10
35
  destroy() {
11
36
  this.isEnabled = false;
12
37
  this.storage = null;
@@ -4,20 +4,55 @@ import { ContainerResolver } from '@adonisjs/fold';
4
4
  import type { Request } from '../request.js';
5
5
  import type { Response } from '../response.js';
6
6
  import type { StoreRouteNode } from '../types/route.js';
7
+ /**
8
+ * Http context encapsulates properties for a given HTTP request. The
9
+ * context class can be extended using macros and getters.
10
+ */
7
11
  export declare class HttpContext extends Macroable {
8
12
  request: Request;
9
13
  response: Response;
10
14
  logger: Logger;
11
15
  containerResolver: ContainerResolver<any>;
16
+ /**
17
+ * Find if async localstorage is enabled for HTTP requests
18
+ * or not
19
+ */
12
20
  static get usingAsyncLocalStorage(): boolean;
21
+ /**
22
+ * Get access to the HTTP context. Available only when
23
+ * "usingAsyncLocalStorage" is true
24
+ */
13
25
  static get(): HttpContext | null;
26
+ /**
27
+ * Get the HttpContext instance or raise an exception if not
28
+ * available
29
+ */
14
30
  static getOrFail(): HttpContext;
31
+ /**
32
+ * Run a method that doesn't have access to HTTP context from
33
+ * the async local storage.
34
+ */
15
35
  static runOutsideContext<T>(callback: (...args: any[]) => T, ...args: any[]): T;
36
+ /**
37
+ * Reference to the current route. Not available inside
38
+ * server middleware
39
+ */
16
40
  route?: StoreRouteNode;
41
+ /**
42
+ * A unique key for the current route
43
+ */
17
44
  routeKey?: string;
45
+ /**
46
+ * Route params
47
+ */
18
48
  params: Record<string, any>;
49
+ /**
50
+ * Route subdomains
51
+ */
19
52
  subdomains: Record<string, any>;
20
53
  constructor(request: Request, response: Response, logger: Logger, containerResolver: ContainerResolver<any>);
54
+ /**
55
+ * A helper to see top level properties on the context object
56
+ */
21
57
  inspect(): string;
22
58
  }
23
- //# sourceMappingURL=main.d.ts.map
@@ -1,22 +1,49 @@
1
+ /*
2
+ * @adonisjs/http-server
3
+ *
4
+ * (c) AdonisJS
5
+ *
6
+ * For the full copyright and license information, please view the LICENSE
7
+ * file that was distributed with this source code.
8
+ */
1
9
  import { inspect } from 'node:util';
2
10
  import Macroable from '@poppinss/macroable';
3
11
  import { RuntimeException } from '@poppinss/utils';
4
12
  import { asyncLocalStorage } from './local_storage.js';
13
+ /**
14
+ * Http context encapsulates properties for a given HTTP request. The
15
+ * context class can be extended using macros and getters.
16
+ */
5
17
  export class HttpContext extends Macroable {
6
18
  request;
7
19
  response;
8
20
  logger;
9
21
  containerResolver;
22
+ /**
23
+ * Find if async localstorage is enabled for HTTP requests
24
+ * or not
25
+ */
10
26
  static get usingAsyncLocalStorage() {
11
27
  return asyncLocalStorage.isEnabled;
12
28
  }
29
+ /**
30
+ * Get access to the HTTP context. Available only when
31
+ * "usingAsyncLocalStorage" is true
32
+ */
13
33
  static get() {
14
34
  if (!this.usingAsyncLocalStorage || !asyncLocalStorage.storage) {
15
35
  return null;
16
36
  }
17
37
  return asyncLocalStorage.storage.getStore() || null;
18
38
  }
39
+ /**
40
+ * Get the HttpContext instance or raise an exception if not
41
+ * available
42
+ */
19
43
  static getOrFail() {
44
+ /**
45
+ * Localstorage is not enabled
46
+ */
20
47
  if (!this.usingAsyncLocalStorage || !asyncLocalStorage.storage) {
21
48
  throw new RuntimeException('HTTP context is not available. Enable "useAsyncLocalStorage" inside "config/app.ts" file');
22
49
  }
@@ -26,15 +53,32 @@ export class HttpContext extends Macroable {
26
53
  }
27
54
  return store;
28
55
  }
56
+ /**
57
+ * Run a method that doesn't have access to HTTP context from
58
+ * the async local storage.
59
+ */
29
60
  static runOutsideContext(callback, ...args) {
30
61
  if (!asyncLocalStorage.storage) {
31
62
  return callback(...args);
32
63
  }
33
64
  return asyncLocalStorage.storage.exit(callback, ...args);
34
65
  }
66
+ /**
67
+ * Reference to the current route. Not available inside
68
+ * server middleware
69
+ */
35
70
  route;
71
+ /**
72
+ * A unique key for the current route
73
+ */
36
74
  routeKey;
75
+ /**
76
+ * Route params
77
+ */
37
78
  params = {};
79
+ /**
80
+ * Route subdomains
81
+ */
38
82
  subdomains = {};
39
83
  constructor(request, response, logger, containerResolver) {
40
84
  super();
@@ -42,9 +86,19 @@ export class HttpContext extends Macroable {
42
86
  this.response = response;
43
87
  this.logger = logger;
44
88
  this.containerResolver = containerResolver;
89
+ /*
90
+ * Creating the circular reference. We do this, since request and response
91
+ * are meant to be extended and at times people would want to access
92
+ * other ctx properties like `logger`, `profiler` inside those
93
+ * extended methods.
94
+ */
45
95
  this.request.ctx = this;
46
96
  this.response.ctx = this;
47
97
  }
98
+ /**
99
+ * A helper to see top level properties on the context object
100
+ */
101
+ /* c8 ignore next 3 */
48
102
  inspect() {
49
103
  return inspect(this, false, 1, true);
50
104
  }
package/build/src/qs.d.ts CHANGED
@@ -1,8 +1,11 @@
1
1
  import { QSParserConfig } from './types/qs.js';
2
+ /**
3
+ * Query string parser used to parse and stringify query
4
+ * strings.
5
+ */
2
6
  export declare class Qs {
3
7
  #private;
4
8
  constructor(config: QSParserConfig);
5
9
  parse(value: string): import("qs").ParsedQs;
6
10
  stringify(value: any): string;
7
11
  }
8
- //# sourceMappingURL=qs.d.ts.map
package/build/src/qs.js CHANGED
@@ -1,4 +1,16 @@
1
+ /*
2
+ * @adonisjs/http-server
3
+ *
4
+ * (c) AdonisJS
5
+ *
6
+ * For the full copyright and license information, please view the LICENSE
7
+ * file that was distributed with this source code.
8
+ */
1
9
  import { parse, stringify } from 'qs';
10
+ /**
11
+ * Query string parser used to parse and stringify query
12
+ * strings.
13
+ */
2
14
  export class Qs {
3
15
  #config;
4
16
  constructor(config) {
@@ -4,16 +4,39 @@ import type { Qs } from './qs.js';
4
4
  import type { Response } from './response.js';
5
5
  import type { Router } from './router/main.js';
6
6
  import type { MakeUrlOptions } from './types/route.js';
7
+ /**
8
+ * Exposes the API to construct redirect routes
9
+ */
7
10
  export declare class Redirect {
8
11
  #private;
9
12
  constructor(request: IncomingMessage, response: Response, router: Router, qs: Qs);
13
+ /**
14
+ * Set a custom status code.
15
+ */
10
16
  status(statusCode: number): this;
17
+ /**
18
+ * Clearing query string values added using the
19
+ * "withQs" method
20
+ */
11
21
  clearQs(): this;
22
+ /**
23
+ * Define query string for the redirect. Not passing
24
+ * any value will forward the current request query
25
+ * string.
26
+ */
12
27
  withQs(): this;
13
28
  withQs(values: Record<string, any>): this;
14
29
  withQs(name: string, value: any): this;
30
+ /**
31
+ * Redirect to the previous path.
32
+ */
15
33
  back(): void;
34
+ /**
35
+ * Redirect the request using a route identifier.
36
+ */
16
37
  toRoute(routeIdentifier: string, params?: any[] | Record<string, any>, options?: MakeUrlOptions): void;
38
+ /**
39
+ * Redirect the request using a path.
40
+ */
17
41
  toPath(url: string): void;
18
42
  }
19
- //# sourceMappingURL=redirect.d.ts.map