request-context.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. import {Route} from './route.js';
  2. import {Errorf} from '@e22m4u/js-format';
  3. import {ServiceContainer, isServiceContainer} from '@e22m4u/js-service';
  4. import {
  5. isReadableStream,
  6. isWritableStream,
  7. getRequestPathname,
  8. } from './utils/index.js';
  9. /**
  10. * Request context.
  11. */
  12. export class RequestContext {
  13. /**
  14. * Service container.
  15. *
  16. * @type {ServiceContainer}
  17. */
  18. _container;
  19. /**
  20. * Getter of service container.
  21. *
  22. * @type {ServiceContainer}
  23. */
  24. get container() {
  25. return this._container;
  26. }
  27. /**
  28. * Request.
  29. *
  30. * @type {import('http').IncomingMessage}
  31. */
  32. _request;
  33. /**
  34. * Getter of request.
  35. *
  36. * @type {import('http').IncomingMessage}
  37. */
  38. get request() {
  39. return this._request;
  40. }
  41. /**
  42. * Response.
  43. *
  44. * @type {import('http').ServerResponse}
  45. */
  46. _response;
  47. /**
  48. * Getter of response.
  49. *
  50. * @type {import('http').ServerResponse}
  51. */
  52. get response() {
  53. return this._response;
  54. }
  55. /**
  56. * Route
  57. *
  58. * @type {Route}
  59. */
  60. _route;
  61. /**
  62. * Getter of route.
  63. *
  64. * @type {Route}
  65. */
  66. get route() {
  67. return this._route;
  68. }
  69. /**
  70. * Query.
  71. *
  72. * @type {object}
  73. */
  74. query = {};
  75. /**
  76. * Path parameters.
  77. *
  78. * @type {object}
  79. */
  80. params = {};
  81. /**
  82. * Headers.
  83. *
  84. * @type {object}
  85. */
  86. headers = {};
  87. /**
  88. * Parsed cookies.
  89. *
  90. * @type {object}
  91. */
  92. cookies = {};
  93. /**
  94. * Parsed body.
  95. *
  96. * @type {*}
  97. */
  98. body;
  99. /**
  100. * Route meta.
  101. *
  102. * @type {import('./route.js').RouteMeta}
  103. */
  104. get meta() {
  105. return this.route.meta;
  106. }
  107. /**
  108. * Method.
  109. *
  110. * @returns {string}
  111. */
  112. get method() {
  113. return this.request.method.toUpperCase();
  114. }
  115. /**
  116. * Path.
  117. *
  118. * @returns {string}
  119. */
  120. get path() {
  121. return this.request.url;
  122. }
  123. /**
  124. * Pathname.
  125. *
  126. * @type {string|undefined}
  127. * @private
  128. */
  129. _pathname = undefined;
  130. /**
  131. * Pathname.
  132. *
  133. * @returns {string}
  134. */
  135. get pathname() {
  136. if (this._pathname != null) return this._pathname;
  137. this._pathname = getRequestPathname(this.request);
  138. return this._pathname;
  139. }
  140. /**
  141. * Constructor.
  142. *
  143. * @param {ServiceContainer} container
  144. * @param {import('http').IncomingMessage} request
  145. * @param {import('http').ServerResponse} response
  146. * @param {Route} route
  147. */
  148. constructor(container, request, response, route) {
  149. if (!isServiceContainer(container))
  150. throw new Errorf(
  151. 'The parameter "container" of RequestContext.constructor ' +
  152. 'should be an instance of ServiceContainer, but %v was given.',
  153. container,
  154. );
  155. this._container = container;
  156. if (
  157. !request ||
  158. typeof request !== 'object' ||
  159. Array.isArray(request) ||
  160. !isReadableStream(request)
  161. ) {
  162. throw new Errorf(
  163. 'The parameter "request" of RequestContext.constructor ' +
  164. 'should be an instance of IncomingMessage, but %v was given.',
  165. request,
  166. );
  167. }
  168. this._request = request;
  169. if (
  170. !response ||
  171. typeof response !== 'object' ||
  172. Array.isArray(response) ||
  173. !isWritableStream(response)
  174. ) {
  175. throw new Errorf(
  176. 'The parameter "response" of RequestContext.constructor ' +
  177. 'should be an instance of ServerResponse, but %v was given.',
  178. response,
  179. );
  180. }
  181. this._response = response;
  182. if (!(route instanceof Route)) {
  183. throw new Errorf(
  184. 'The parameter "route" of RequestContext.constructor ' +
  185. 'should be an instance of Route, but %v was given.',
  186. route,
  187. );
  188. }
  189. this._route = route;
  190. }
  191. }