Browse Source

refactor: context property `container` renamed to `cont`

e22m4u 3 weeks ago
parent
commit
518e81a40c
7 changed files with 39 additions and 39 deletions
  1. 12 12
      README.md
  2. 2 2
      dist/cjs/index.cjs
  3. 1 1
      src/request-context.d.ts
  4. 2 2
      src/request-context.js
  5. 17 17
      src/request-context.spec.js
  6. 2 2
      src/route.spec.js
  7. 3 3
      src/trie-router.spec.js

+ 12 - 12
README.md

@@ -78,7 +78,7 @@ server.listen(3000, 'localhost');             // прослушивание за
 `RequestContext` с набором свойств, содержащих разобранные
 данные входящего запроса.
 
-- `container: ServiceContainer` экземпляр [сервис-контейнера](https://npmjs.com/package/@e22m4u/js-service)
+- `cont: ServiceContainer` экземпляр [сервис-контейнера](https://npmjs.com/package/@e22m4u/js-service)
 - `req: IncomingMessage` нативный поток входящего запроса
 - `res: ServerResponse` нативный поток ответа сервера
 - `params: ParsedParams` объект ключ-значение с параметрами пути
@@ -101,17 +101,17 @@ router.defineRoute({
   handler(ctx) {
     // GET /users/10?include=city
     // Cookie: foo=bar; baz=qux;
-    console.log(ctx.req);       // IncomingMessage
-    console.log(ctx.res);       // ServerResponse
-    console.log(ctx.params);    // {id: 10}
-    console.log(ctx.query);     // {include: 'city'}
-    console.log(ctx.headers);   // {cookie: 'foo=bar; baz=qux;'}
-    console.log(ctx.cookies);   // {foo: 'bar', baz: 'qux'}
-    console.log(ctx.method);    // "GET"
-    console.log(ctx.path);      // "/users/10?include=city"
-    console.log(ctx.pathname);  // "/users/10"
-    console.log(ctx.meta);      // {prop: 'value'}
-    console.log(ctx.container); // ServiceContainer
+    console.log(ctx.req);      // IncomingMessage
+    console.log(ctx.res);      // ServerResponse
+    console.log(ctx.params);   // {id: 10}
+    console.log(ctx.query);    // {include: 'city'}
+    console.log(ctx.headers);  // {cookie: 'foo=bar; baz=qux;'}
+    console.log(ctx.cookies);  // {foo: 'bar', baz: 'qux'}
+    console.log(ctx.method);   // "GET"
+    console.log(ctx.path);     // "/users/10?include=city"
+    console.log(ctx.pathname); // "/users/10"
+    console.log(ctx.meta);     // {prop: 'value'}
+    console.log(ctx.cont);     // ServiceContainer
     // ...
   },
 });

+ 2 - 2
dist/cjs/index.cjs

@@ -1360,7 +1360,7 @@ var _RequestContext = class _RequestContext {
    *
    * @type {import('@e22m4u/js-service').ServiceContainer}
    */
-  container;
+  cont;
   /**
    * Request.
    *
@@ -1455,7 +1455,7 @@ var _RequestContext = class _RequestContext {
         'The parameter "container" of RequestContext.constructor should be an instance of ServiceContainer, but %v was given.',
         container
       );
-    this.container = container;
+    this.cont = container;
     if (!request || typeof request !== "object" || Array.isArray(request) || !isReadableStream(request)) {
       throw new import_js_format19.Errorf(
         'The parameter "request" of RequestContext.constructor should be an instance of IncomingMessage, but %v was given.',

+ 1 - 1
src/request-context.d.ts

@@ -20,7 +20,7 @@ export declare class RequestContext {
   /**
    * Container.
    */
-  container: ServiceContainer;
+  cont: ServiceContainer;
 
   /**
    * Request.

+ 2 - 2
src/request-context.js

@@ -14,7 +14,7 @@ export class RequestContext {
    *
    * @type {import('@e22m4u/js-service').ServiceContainer}
    */
-  container;
+  cont;
 
   /**
    * Request.
@@ -123,7 +123,7 @@ export class RequestContext {
           'should be an instance of ServiceContainer, but %v was given.',
         container,
       );
-    this.container = container;
+    this.cont = container;
     if (
       !request ||
       typeof request !== 'object' ||

+ 17 - 17
src/request-context.spec.js

@@ -32,8 +32,8 @@ describe('RequestContext', function () {
 
     it('requires the parameter "request" to be the ServiceContainer', function () {
       const res = createResponseMock();
-      const cnt = new ServiceContainer();
-      const throwable = v => () => new RequestContext(cnt, v, res);
+      const cont = new ServiceContainer();
+      const throwable = v => () => new RequestContext(cont, v, res);
       const error = v =>
         format(
           'The parameter "request" of RequestContext.constructor ' +
@@ -55,8 +55,8 @@ describe('RequestContext', function () {
 
     it('requires the parameter "response" to be the ServiceContainer', function () {
       const req = createRequestMock();
-      const cnt = new ServiceContainer();
-      const throwable = v => () => new RequestContext(cnt, req, v);
+      const cont = new ServiceContainer();
+      const throwable = v => () => new RequestContext(cont, req, v);
       const error = v =>
         format(
           'The parameter "response" of RequestContext.constructor ' +
@@ -79,9 +79,9 @@ describe('RequestContext', function () {
     it('sets properties from given arguments', function () {
       const req = createRequestMock();
       const res = createResponseMock();
-      const cnt = new ServiceContainer();
-      const ctx = new RequestContext(cnt, req, res);
-      expect(ctx.container).to.be.eq(cnt);
+      const cont = new ServiceContainer();
+      const ctx = new RequestContext(cont, req, res);
+      expect(ctx.cont).to.be.eq(cont);
       expect(ctx.req).to.be.eq(req);
       expect(ctx.res).to.be.eq(res);
     });
@@ -89,8 +89,8 @@ describe('RequestContext', function () {
     it('sets an empty object to the "meta" property', function () {
       const req = createRequestMock();
       const res = createResponseMock();
-      const cnt = new ServiceContainer();
-      const ctx = new RequestContext(cnt, req, res);
+      const cont = new ServiceContainer();
+      const ctx = new RequestContext(cont, req, res);
       expect(ctx.meta).to.be.eql({});
     });
   });
@@ -99,8 +99,8 @@ describe('RequestContext', function () {
     it('returns the method name in upper case', function () {
       const req = createRequestMock({method: 'post'});
       const res = createResponseMock();
-      const cnt = new ServiceContainer();
-      const ctx = new RequestContext(cnt, req, res);
+      const cont = new ServiceContainer();
+      const ctx = new RequestContext(cont, req, res);
       expect(ctx.method).to.be.eq('POST');
     });
   });
@@ -109,8 +109,8 @@ describe('RequestContext', function () {
     it('returns the request pathname with the query string', function () {
       const req = createRequestMock({path: '/pathname?foo=bar'});
       const res = createResponseMock();
-      const cnt = new ServiceContainer();
-      const ctx = new RequestContext(cnt, req, res);
+      const cont = new ServiceContainer();
+      const ctx = new RequestContext(cont, req, res);
       expect(req.url).to.be.eq('/pathname?foo=bar');
       expect(ctx.path).to.be.eq('/pathname?foo=bar');
     });
@@ -120,8 +120,8 @@ describe('RequestContext', function () {
     it('returns the request pathname without the query string', function () {
       const req = createRequestMock({path: '/pathname?foo=bar'});
       const res = createResponseMock();
-      const cnt = new ServiceContainer();
-      const ctx = new RequestContext(cnt, req, res);
+      const cont = new ServiceContainer();
+      const ctx = new RequestContext(cont, req, res);
       expect(req.url).to.be.eq('/pathname?foo=bar');
       expect(ctx.pathname).to.be.eq('/pathname');
     });
@@ -129,8 +129,8 @@ describe('RequestContext', function () {
     it('sets the cache to the "_pathname" property and uses is for next accesses', function () {
       const req = createRequestMock({path: '/pathname'});
       const res = createResponseMock();
-      const cnt = new ServiceContainer();
-      const ctx = new RequestContext(cnt, req, res);
+      const cont = new ServiceContainer();
+      const ctx = new RequestContext(cont, req, res);
       expect(ctx._pathname).to.be.undefined;
       expect(ctx.pathname).to.be.eq('/pathname');
       expect(ctx._pathname).to.be.eq('/pathname');

+ 2 - 2
src/route.spec.js

@@ -380,8 +380,8 @@ describe('Route', function () {
       });
       const req = createRequestMock();
       const res = createResponseMock();
-      const cnt = new ServiceContainer();
-      const ctx = new RequestContext(cnt, req, res);
+      const cont = new ServiceContainer();
+      const ctx = new RequestContext(cont, req, res);
       const result = route.handle(ctx);
       expect(result).to.be.eq('OK');
     });

+ 3 - 3
src/trie-router.spec.js

@@ -486,7 +486,7 @@ describe('TrieRouter', function () {
         method: HttpMethod.GET,
         path: '/',
         handler(ctx) {
-          const res = ctx.container.getRegistered(RequestContext);
+          const res = ctx.cont.getRegistered(RequestContext);
           expect(res).to.be.eq(ctx);
           expect(res).to.be.not.eq(router.container);
           done();
@@ -505,7 +505,7 @@ describe('TrieRouter', function () {
         method: HttpMethod.GET,
         path: '/',
         handler(ctx) {
-          const result = ctx.container.getRegistered(IncomingMessage);
+          const result = ctx.cont.getRegistered(IncomingMessage);
           expect(result).to.be.eq(req);
           done();
         },
@@ -521,7 +521,7 @@ describe('TrieRouter', function () {
         method: HttpMethod.GET,
         path: '/',
         handler(ctx) {
-          const result = ctx.container.getRegistered(ServerResponse);
+          const result = ctx.cont.getRegistered(ServerResponse);
           expect(result).to.be.eq(res);
           done();
         },