| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- import {Route} from './route.js';
- import {Errorf} from '@e22m4u/js-format';
- import {ServiceContainer, isServiceContainer} from '@e22m4u/js-service';
- import {
- isReadableStream,
- isWritableStream,
- getRequestPathname,
- } from './utils/index.js';
- /**
- * Request context.
- */
- export class RequestContext {
- /**
- * Service container.
- *
- * @type {ServiceContainer}
- */
- _container;
- /**
- * Getter of service container.
- *
- * @type {ServiceContainer}
- */
- get container() {
- return this._container;
- }
- /**
- * Request.
- *
- * @type {import('http').IncomingMessage}
- */
- _request;
- /**
- * Getter of request.
- *
- * @type {import('http').IncomingMessage}
- */
- get request() {
- return this._request;
- }
- /**
- * Response.
- *
- * @type {import('http').ServerResponse}
- */
- _response;
- /**
- * Getter of response.
- *
- * @type {import('http').ServerResponse}
- */
- get response() {
- return this._response;
- }
- /**
- * Route
- *
- * @type {Route}
- */
- _route;
- /**
- * Getter of route.
- *
- * @type {Route}
- */
- get route() {
- return this._route;
- }
- /**
- * Query.
- *
- * @type {object}
- */
- query = {};
- /**
- * Path parameters.
- *
- * @type {object}
- */
- params = {};
- /**
- * Headers.
- *
- * @type {object}
- */
- headers = {};
- /**
- * Parsed cookies.
- *
- * @type {object}
- */
- cookies = {};
- /**
- * Parsed body.
- *
- * @type {*}
- */
- body;
- /**
- * State.
- *
- * @type {object}
- */
- state = {};
- /**
- * Route meta.
- *
- * @type {import('./route.js').RouteMeta}
- */
- get meta() {
- return this.route.meta;
- }
- /**
- * Method.
- *
- * @returns {string}
- */
- get method() {
- return this.request.method.toUpperCase();
- }
- /**
- * Path.
- *
- * @returns {string}
- */
- get path() {
- return this.request.url;
- }
- /**
- * Pathname.
- *
- * @type {string|undefined}
- * @private
- */
- _pathname = undefined;
- /**
- * Pathname.
- *
- * @returns {string}
- */
- get pathname() {
- if (this._pathname != null) return this._pathname;
- this._pathname = getRequestPathname(this.request);
- return this._pathname;
- }
- /**
- * Constructor.
- *
- * @param {ServiceContainer} container
- * @param {import('http').IncomingMessage} request
- * @param {import('http').ServerResponse} response
- * @param {Route} route
- */
- constructor(container, request, response, route) {
- if (!isServiceContainer(container))
- throw new Errorf(
- 'The parameter "container" of RequestContext.constructor ' +
- 'should be an instance of ServiceContainer, but %v was given.',
- container,
- );
- this._container = container;
- if (
- !request ||
- typeof request !== 'object' ||
- Array.isArray(request) ||
- !isReadableStream(request)
- ) {
- throw new Errorf(
- 'The parameter "request" of RequestContext.constructor ' +
- 'should be an instance of IncomingMessage, but %v was given.',
- request,
- );
- }
- this._request = request;
- if (
- !response ||
- typeof response !== 'object' ||
- Array.isArray(response) ||
- !isWritableStream(response)
- ) {
- throw new Errorf(
- 'The parameter "response" of RequestContext.constructor ' +
- 'should be an instance of ServerResponse, but %v was given.',
- response,
- );
- }
- this._response = response;
- if (!(route instanceof Route)) {
- throw new Errorf(
- 'The parameter "route" of RequestContext.constructor ' +
- 'should be an instance of Route, but %v was given.',
- route,
- );
- }
- this._route = route;
- }
- }
|