From a38e782afdcbbc3b6c8b58c78a33ad49c3bf0d25 Mon Sep 17 00:00:00 2001 From: Tine Jozelj Date: Wed, 10 Mar 2021 22:39:17 +0100 Subject: [PATCH] chore: remove forRoot --- README.md | 8 +- lib/morgan.module.ts | 10 +- package-lock.json | 192 +++++++++++++++++++++++++++++++++++++++ package.json | 4 +- test/hello.controller.ts | 31 +++---- test/hello.e2e.spec.ts | 52 +++++------ test/hello.module.ts | 17 ++-- tsconfig.build.json | 7 ++ tsconfig.json | 27 +++--- 9 files changed, 268 insertions(+), 80 deletions(-) create mode 100644 tsconfig.build.json diff --git a/README.md b/README.md index 104eb6c..a23ce86 100644 --- a/README.md +++ b/README.md @@ -8,8 +8,7 @@ NPM Version Package License NPM Downloads -Travis build -Coveralls +Github Actions - Test

## Description @@ -25,6 +24,7 @@ $ npm i --save nest-morgan morgan @types/morgan ### Versions - **2.x** Is for Nest v7.x + - Remove the need to use `MorganModule.forRoot()` [#17](https://github.com/mentos1386/nest-morgan/issues/17). - **1.x** Is for Nest v6.x - **0.x** Is for Nest v5.x @@ -36,7 +36,7 @@ $ npm i --save nest-morgan morgan @types/morgan ```ts @Module({ - imports: [...MorganModule.forRoot()], + imports: [MorganModule], }) export class ApplicationModule {} ``` @@ -55,7 +55,7 @@ import { APP_INTERCEPTOR } from "@nestjs/core"; import { MorganModule, MorganInterceptor } from "nest-morgan"; @Module({ - imports: [MorganModule.forRoot()], + imports: [MorganModule], providers: [ { provide: APP_INTERCEPTOR, diff --git a/lib/morgan.module.ts b/lib/morgan.module.ts index f53d157..ae748ce 100644 --- a/lib/morgan.module.ts +++ b/lib/morgan.module.ts @@ -1,4 +1,4 @@ -import { DynamicModule, Global, Module } from "@nestjs/common"; +import { Global, Module } from "@nestjs/common"; import { morganProviders } from "./morgan.providers"; @Global() @@ -6,10 +6,4 @@ import { morganProviders } from "./morgan.providers"; providers: [...morganProviders], exports: [...morganProviders], }) -export class MorganModule { - static forRoot(): DynamicModule { - return { - module: MorganModule, - }; - } -} +export class MorganModule {} diff --git a/package-lock.json b/package-lock.json index c32bab2..4f5ac31 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15,9 +15,11 @@ "@nestjs/core": "^7.6.13", "@nestjs/platform-express": "^7.6.13", "@nestjs/testing": "^7.6.13", + "@types/express": "^4.17.11", "@types/jest": "^26.0.20", "@types/morgan": "^1.9.2", "@types/node": "^14.14.33", + "@types/supertest": "^2.0.10", "coveralls": "^3.1.0", "jest": "^26.6.3", "reflect-metadata": "^0.1.13", @@ -1063,6 +1065,54 @@ "@babel/types": "^7.3.0" } }, + "node_modules/@types/body-parser": { + "version": "1.19.0", + "resolved": "https://registry.npmjs.org/@types/body-parser/-/body-parser-1.19.0.tgz", + "integrity": "sha512-W98JrE0j2K78swW4ukqMleo8R7h/pFETjM2DQ90MF6XK2i4LO4W3gQ71Lt4w3bfm2EvVSyWHplECvB5sK22yFQ==", + "dev": true, + "dependencies": { + "@types/connect": "*", + "@types/node": "*" + } + }, + "node_modules/@types/connect": { + "version": "3.4.34", + "resolved": "https://registry.npmjs.org/@types/connect/-/connect-3.4.34.tgz", + "integrity": "sha512-ePPA/JuI+X0vb+gSWlPKOY0NdNAie/rPUqX2GUPpbZwiKTkSPhjXWuee47E4MtE54QVzGCQMQkAL6JhV2E1+cQ==", + "dev": true, + "dependencies": { + "@types/node": "*" + } + }, + "node_modules/@types/cookiejar": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/@types/cookiejar/-/cookiejar-2.1.2.tgz", + "integrity": "sha512-t73xJJrvdTjXrn4jLS9VSGRbz0nUY3cl2DMGDU48lKl+HR9dbbjW2A9r3g40VA++mQpy6uuHg33gy7du2BKpog==", + "dev": true + }, + "node_modules/@types/express": { + "version": "4.17.11", + "resolved": "https://registry.npmjs.org/@types/express/-/express-4.17.11.tgz", + "integrity": "sha512-no+R6rW60JEc59977wIxreQVsIEOAYwgCqldrA/vkpCnbD7MqTefO97lmoBe4WE0F156bC4uLSP1XHDOySnChg==", + "dev": true, + "dependencies": { + "@types/body-parser": "*", + "@types/express-serve-static-core": "^4.17.18", + "@types/qs": "*", + "@types/serve-static": "*" + } + }, + "node_modules/@types/express-serve-static-core": { + "version": "4.17.18", + "resolved": "https://registry.npmjs.org/@types/express-serve-static-core/-/express-serve-static-core-4.17.18.tgz", + "integrity": "sha512-m4JTwx5RUBNZvky/JJ8swEJPKFd8si08pPF2PfizYjGZOKr/svUWPcoUmLow6MmPzhasphB7gSTINY67xn3JNA==", + "dev": true, + "dependencies": { + "@types/node": "*", + "@types/qs": "*", + "@types/range-parser": "*" + } + }, "node_modules/@types/graceful-fs": { "version": "4.1.5", "resolved": "https://registry.npmjs.org/@types/graceful-fs/-/graceful-fs-4.1.5.tgz", @@ -1106,6 +1156,12 @@ "pretty-format": "^26.0.0" } }, + "node_modules/@types/mime": { + "version": "1.3.2", + "resolved": "https://registry.npmjs.org/@types/mime/-/mime-1.3.2.tgz", + "integrity": "sha512-YATxVxgRqNH6nHEIsvg6k2Boc1JHI9ZbH5iWFFv/MTkchz3b1ieGDa5T0a9RznNdI0KhVbdbWSN+KWWrQZRxTw==", + "dev": true + }, "node_modules/@types/morgan": { "version": "1.9.2", "resolved": "https://registry.npmjs.org/@types/morgan/-/morgan-1.9.2.tgz", @@ -1133,12 +1189,53 @@ "integrity": "sha512-i99hy7Ki19EqVOl77WplDrvgNugHnsSjECVR/wUrzw2TJXz1zlUfT2ngGckR6xN7yFYaijsMAqPkOLx9HgUqHg==", "dev": true }, + "node_modules/@types/qs": { + "version": "6.9.6", + "resolved": "https://registry.npmjs.org/@types/qs/-/qs-6.9.6.tgz", + "integrity": "sha512-0/HnwIfW4ki2D8L8c9GVcG5I72s9jP5GSLVF0VIXDW00kmIpA6O33G7a8n59Tmh7Nz0WUC3rSb7PTY/sdW2JzA==", + "dev": true + }, + "node_modules/@types/range-parser": { + "version": "1.2.3", + "resolved": "https://registry.npmjs.org/@types/range-parser/-/range-parser-1.2.3.tgz", + "integrity": "sha512-ewFXqrQHlFsgc09MK5jP5iR7vumV/BYayNC6PgJO2LPe8vrnNFyjQjSppfEngITi0qvfKtzFvgKymGheFM9UOA==", + "dev": true + }, + "node_modules/@types/serve-static": { + "version": "1.13.9", + "resolved": "https://registry.npmjs.org/@types/serve-static/-/serve-static-1.13.9.tgz", + "integrity": "sha512-ZFqF6qa48XsPdjXV5Gsz0Zqmux2PerNd3a/ktL45mHpa19cuMi/cL8tcxdAx497yRh+QtYPuofjT9oWw9P7nkA==", + "dev": true, + "dependencies": { + "@types/mime": "^1", + "@types/node": "*" + } + }, "node_modules/@types/stack-utils": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/@types/stack-utils/-/stack-utils-2.0.0.tgz", "integrity": "sha512-RJJrrySY7A8havqpGObOB4W92QXKJo63/jFLLgpvOtsGUqbQZ9Sbgl35KMm1DjC6j7AvmmU2bIno+3IyEaemaw==", "dev": true }, + "node_modules/@types/superagent": { + "version": "4.1.10", + "resolved": "https://registry.npmjs.org/@types/superagent/-/superagent-4.1.10.tgz", + "integrity": "sha512-xAgkb2CMWUMCyVc/3+7iQfOEBE75NvuZeezvmixbUw3nmENf2tCnQkW5yQLTYqvXUQ+R6EXxdqKKbal2zM5V/g==", + "dev": true, + "dependencies": { + "@types/cookiejar": "*", + "@types/node": "*" + } + }, + "node_modules/@types/supertest": { + "version": "2.0.10", + "resolved": "https://registry.npmjs.org/@types/supertest/-/supertest-2.0.10.tgz", + "integrity": "sha512-Xt8TbEyZTnD5Xulw95GLMOkmjGICrOQyJ2jqgkSjAUR3mm7pAIzSR0NFBaMcwlzVvlpCjNwbATcWWwjNiZiFrQ==", + "dev": true, + "dependencies": { + "@types/superagent": "*" + } + }, "node_modules/@types/yargs": { "version": "15.0.13", "resolved": "https://registry.npmjs.org/@types/yargs/-/yargs-15.0.13.tgz", @@ -8372,6 +8469,54 @@ "@babel/types": "^7.3.0" } }, + "@types/body-parser": { + "version": "1.19.0", + "resolved": "https://registry.npmjs.org/@types/body-parser/-/body-parser-1.19.0.tgz", + "integrity": "sha512-W98JrE0j2K78swW4ukqMleo8R7h/pFETjM2DQ90MF6XK2i4LO4W3gQ71Lt4w3bfm2EvVSyWHplECvB5sK22yFQ==", + "dev": true, + "requires": { + "@types/connect": "*", + "@types/node": "*" + } + }, + "@types/connect": { + "version": "3.4.34", + "resolved": "https://registry.npmjs.org/@types/connect/-/connect-3.4.34.tgz", + "integrity": "sha512-ePPA/JuI+X0vb+gSWlPKOY0NdNAie/rPUqX2GUPpbZwiKTkSPhjXWuee47E4MtE54QVzGCQMQkAL6JhV2E1+cQ==", + "dev": true, + "requires": { + "@types/node": "*" + } + }, + "@types/cookiejar": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/@types/cookiejar/-/cookiejar-2.1.2.tgz", + "integrity": "sha512-t73xJJrvdTjXrn4jLS9VSGRbz0nUY3cl2DMGDU48lKl+HR9dbbjW2A9r3g40VA++mQpy6uuHg33gy7du2BKpog==", + "dev": true + }, + "@types/express": { + "version": "4.17.11", + "resolved": "https://registry.npmjs.org/@types/express/-/express-4.17.11.tgz", + "integrity": "sha512-no+R6rW60JEc59977wIxreQVsIEOAYwgCqldrA/vkpCnbD7MqTefO97lmoBe4WE0F156bC4uLSP1XHDOySnChg==", + "dev": true, + "requires": { + "@types/body-parser": "*", + "@types/express-serve-static-core": "^4.17.18", + "@types/qs": "*", + "@types/serve-static": "*" + } + }, + "@types/express-serve-static-core": { + "version": "4.17.18", + "resolved": "https://registry.npmjs.org/@types/express-serve-static-core/-/express-serve-static-core-4.17.18.tgz", + "integrity": "sha512-m4JTwx5RUBNZvky/JJ8swEJPKFd8si08pPF2PfizYjGZOKr/svUWPcoUmLow6MmPzhasphB7gSTINY67xn3JNA==", + "dev": true, + "requires": { + "@types/node": "*", + "@types/qs": "*", + "@types/range-parser": "*" + } + }, "@types/graceful-fs": { "version": "4.1.5", "resolved": "https://registry.npmjs.org/@types/graceful-fs/-/graceful-fs-4.1.5.tgz", @@ -8415,6 +8560,12 @@ "pretty-format": "^26.0.0" } }, + "@types/mime": { + "version": "1.3.2", + "resolved": "https://registry.npmjs.org/@types/mime/-/mime-1.3.2.tgz", + "integrity": "sha512-YATxVxgRqNH6nHEIsvg6k2Boc1JHI9ZbH5iWFFv/MTkchz3b1ieGDa5T0a9RznNdI0KhVbdbWSN+KWWrQZRxTw==", + "dev": true + }, "@types/morgan": { "version": "1.9.2", "resolved": "https://registry.npmjs.org/@types/morgan/-/morgan-1.9.2.tgz", @@ -8442,12 +8593,53 @@ "integrity": "sha512-i99hy7Ki19EqVOl77WplDrvgNugHnsSjECVR/wUrzw2TJXz1zlUfT2ngGckR6xN7yFYaijsMAqPkOLx9HgUqHg==", "dev": true }, + "@types/qs": { + "version": "6.9.6", + "resolved": "https://registry.npmjs.org/@types/qs/-/qs-6.9.6.tgz", + "integrity": "sha512-0/HnwIfW4ki2D8L8c9GVcG5I72s9jP5GSLVF0VIXDW00kmIpA6O33G7a8n59Tmh7Nz0WUC3rSb7PTY/sdW2JzA==", + "dev": true + }, + "@types/range-parser": { + "version": "1.2.3", + "resolved": "https://registry.npmjs.org/@types/range-parser/-/range-parser-1.2.3.tgz", + "integrity": "sha512-ewFXqrQHlFsgc09MK5jP5iR7vumV/BYayNC6PgJO2LPe8vrnNFyjQjSppfEngITi0qvfKtzFvgKymGheFM9UOA==", + "dev": true + }, + "@types/serve-static": { + "version": "1.13.9", + "resolved": "https://registry.npmjs.org/@types/serve-static/-/serve-static-1.13.9.tgz", + "integrity": "sha512-ZFqF6qa48XsPdjXV5Gsz0Zqmux2PerNd3a/ktL45mHpa19cuMi/cL8tcxdAx497yRh+QtYPuofjT9oWw9P7nkA==", + "dev": true, + "requires": { + "@types/mime": "^1", + "@types/node": "*" + } + }, "@types/stack-utils": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/@types/stack-utils/-/stack-utils-2.0.0.tgz", "integrity": "sha512-RJJrrySY7A8havqpGObOB4W92QXKJo63/jFLLgpvOtsGUqbQZ9Sbgl35KMm1DjC6j7AvmmU2bIno+3IyEaemaw==", "dev": true }, + "@types/superagent": { + "version": "4.1.10", + "resolved": "https://registry.npmjs.org/@types/superagent/-/superagent-4.1.10.tgz", + "integrity": "sha512-xAgkb2CMWUMCyVc/3+7iQfOEBE75NvuZeezvmixbUw3nmENf2tCnQkW5yQLTYqvXUQ+R6EXxdqKKbal2zM5V/g==", + "dev": true, + "requires": { + "@types/cookiejar": "*", + "@types/node": "*" + } + }, + "@types/supertest": { + "version": "2.0.10", + "resolved": "https://registry.npmjs.org/@types/supertest/-/supertest-2.0.10.tgz", + "integrity": "sha512-Xt8TbEyZTnD5Xulw95GLMOkmjGICrOQyJ2jqgkSjAUR3mm7pAIzSR0NFBaMcwlzVvlpCjNwbATcWWwjNiZiFrQ==", + "dev": true, + "requires": { + "@types/superagent": "*" + } + }, "@types/yargs": { "version": "15.0.13", "resolved": "https://registry.npmjs.org/@types/yargs/-/yargs-15.0.13.tgz", diff --git a/package.json b/package.json index 462cd9a..2fdf2b4 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "lib": "lib" }, "scripts": { - "build": "tsc -p tsconfig.json && cp *.md dist && cp LICENSE dist && cp package.json dist", + "build": "tsc -p tsconfig.build.json && cp *.md dist && cp LICENSE dist && cp package.json dist", "coverage": "cat ./coverage/lcov.info | coveralls", "test": "jest" }, @@ -35,9 +35,11 @@ "@nestjs/core": "^7.6.13", "@nestjs/platform-express": "^7.6.13", "@nestjs/testing": "^7.6.13", + "@types/express": "^4.17.11", "@types/jest": "^26.0.20", "@types/morgan": "^1.9.2", "@types/node": "^14.14.33", + "@types/supertest": "^2.0.10", "coveralls": "^3.1.0", "jest": "^26.6.3", "reflect-metadata": "^0.1.13", diff --git a/test/hello.controller.ts b/test/hello.controller.ts index 728f241..82f721e 100644 --- a/test/hello.controller.ts +++ b/test/hello.controller.ts @@ -1,30 +1,29 @@ -import { Controller, Get, UseInterceptors } from '@nestjs/common'; -import { MorganInterceptor } from '../lib'; +import { Controller, Get, UseInterceptors } from "@nestjs/common"; +import { MorganInterceptor } from "../lib"; -@Controller('') +@Controller("") export class HelloController { - - @Get('common/works') - @UseInterceptors(MorganInterceptor('common')) + @Get("common/works") + @UseInterceptors(MorganInterceptor("common")) works_common() { - return 'Works'; + return "Works"; } - @Get('common/error') - @UseInterceptors(MorganInterceptor('common')) + @Get("common/error") + @UseInterceptors(MorganInterceptor("common")) error_common() { - throw new Error('Something bad happened'); + throw new Error("Something bad happened"); } - @Get('combined/works') - @UseInterceptors(MorganInterceptor('combined')) + @Get("combined/works") + @UseInterceptors(MorganInterceptor("combined")) works_combined() { - return 'Works'; + return "Works"; } - @Get('combined/error') - @UseInterceptors(MorganInterceptor('combined')) + @Get("combined/error") + @UseInterceptors(MorganInterceptor("combined")) error_combined() { - throw new Error('Something bad happened'); + throw new Error("Something bad happened"); } } diff --git a/test/hello.e2e.spec.ts b/test/hello.e2e.spec.ts index 3fdfe88..92b026a 100644 --- a/test/hello.e2e.spec.ts +++ b/test/hello.e2e.spec.ts @@ -1,60 +1,56 @@ -import * as request from 'supertest'; -import { Test } from '@nestjs/testing'; -import { HelloModule } from './hello.module'; -import { INestApplication } from '@nestjs/common'; -import { MORGAN_PROVIDER } from '../lib'; -import * as express from 'express'; - -describe('Hello', () => { +import * as request from "supertest"; +import { Test } from "@nestjs/testing"; +import { HelloModule } from "./hello.module"; +import { INestApplication } from "@nestjs/common"; +import { MORGAN_PROVIDER } from "../lib"; +import * as express from "express"; + +describe("Hello", () => { let app: INestApplication; - let mockMorganData: { req: express.Request, res: express.Response, next: any}; - const mockMorgan = (format: string, options: any) => (req, res, next) => { - mockMorganData = { req, res, next } + let mockMorganData: { + req: express.Request; + res: express.Response; + next: any; + } | null; + const mockMorgan = () => (req: any, res: any, next: any) => { + mockMorganData = { req, res, next }; }; beforeAll(async () => { const module = await Test.createTestingModule({ imports: [HelloModule], }) - .overrideProvider(MORGAN_PROVIDER) - .useValue(mockMorgan) - .compile(); + .overrideProvider(MORGAN_PROVIDER) + .useValue(mockMorgan) + .compile(); app = module.createNestApplication(); await app.init(); }); - beforeEach(() => mockMorganData = null); + beforeEach(() => (mockMorganData = null)); it(`/GET common/works`, async () => { - await request(app.getHttpServer()) - .get('/common/works') - .expect(200); + await request(app.getHttpServer()).get("/common/works").expect(200); expect(mockMorganData).not.toBeNull(); }); it(`/GET common/error`, async () => { - await request(app.getHttpServer()) - .get('/common/error') - .expect(500); + await request(app.getHttpServer()).get("/common/error").expect(500); expect(mockMorganData).not.toBeNull(); }); it(`/GET combined/works`, async () => { - await request(app.getHttpServer()) - .get('/combined/works') - .expect(200); + await request(app.getHttpServer()).get("/combined/works").expect(200); expect(mockMorganData).not.toBeNull(); }); it(`/GET combined/error`, async () => { - await request(app.getHttpServer()) - .get('/combined/error') - .expect(500); + await request(app.getHttpServer()).get("/combined/error").expect(500); expect(mockMorganData).not.toBeNull(); }); @@ -62,4 +58,4 @@ describe('Hello', () => { afterAll(async () => { await app.close(); }); -}); \ No newline at end of file +}); diff --git a/test/hello.module.ts b/test/hello.module.ts index d1c54a6..a8b560f 100644 --- a/test/hello.module.ts +++ b/test/hello.module.ts @@ -1,14 +1,9 @@ -import { MorganModule } from '../lib'; -import { Module } from '@nestjs/common'; -import { HelloController } from './hello.controller'; +import { MorganModule } from "../lib"; +import { Module } from "@nestjs/common"; +import { HelloController } from "./hello.controller"; @Module({ - imports: [ - MorganModule.forRoot(), - ], - controllers: [ - HelloController - ] + imports: [MorganModule], + controllers: [HelloController], }) -export class HelloModule { -} \ No newline at end of file +export class HelloModule {} diff --git a/tsconfig.build.json b/tsconfig.build.json new file mode 100644 index 0000000..7ea734b --- /dev/null +++ b/tsconfig.build.json @@ -0,0 +1,7 @@ +{ + "extends": "./tsconfig.json", + "exclude": ["node_modules", "example", "test", "dist"], + "compilerOptions": { + "esModuleInterop": true + } +} diff --git a/tsconfig.json b/tsconfig.json index bc13615..8553ba3 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -2,7 +2,7 @@ "compilerOptions": { "module": "commonjs", "declaration": true, - "noImplicitAny": false, + "noImplicitAny": true, "removeComments": true, "noLib": false, "emitDecoratorMetadata": true, @@ -10,14 +10,17 @@ "target": "es6", "sourceMap": false, "outDir": "./dist", - "rootDir": "./lib", - "skipLibCheck": true - }, - "include": [ - "lib/**/*" - ], - "exclude": [ - "node_modules", - "**/*.spec.ts" - ] -} \ No newline at end of file + "skipLibCheck": false, + "noUnusedLocals": true, + "noUnusedParameters": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "strictBindCallApply": true, + "strictPropertyInitialization": true, + "noImplicitThis": true, + "alwaysStrict": true, + "moduleResolution": "node", + "noImplicitReturns": true, + "noFallthroughCasesInSwitch": true + } +}