From 8e521876e763b974098674818c90d03f025f128f Mon Sep 17 00:00:00 2001 From: Inga <52715130+inga-lovinde@users.noreply.github.com> Date: Mon, 11 Dec 2023 11:56:43 +0000 Subject: [PATCH] limited swagger docs enabled; minor refactoring --- package-lock.json | 60 ++++++++++++++++++++++++++++++++++++-- package.json | 1 + src/app.module.spec.ts | 7 +++-- src/main.ts | 4 +-- src/utils/nestjsHelpers.ts | 13 +++++++++ test/app.e2e-spec.ts | 6 ++-- 6 files changed, 81 insertions(+), 10 deletions(-) create mode 100644 src/utils/nestjsHelpers.ts diff --git a/package-lock.json b/package-lock.json index d0ef86d..0d66e4c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,6 +13,7 @@ "@nestjs/config": "^3.1.1", "@nestjs/core": "^10.0.0", "@nestjs/platform-express": "^10.0.0", + "@nestjs/swagger": "^7.1.16", "class-transformer": "^0.5.1", "class-validator": "^0.14.0", "node-fetch": "^2.7.0", @@ -1680,6 +1681,25 @@ } } }, + "node_modules/@nestjs/mapped-types": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/@nestjs/mapped-types/-/mapped-types-2.0.3.tgz", + "integrity": "sha512-40Zdqg98lqoF0+7ThWIZFStxgzisK6GG22+1ABO4kZiGF/Tu2FE+DYLw+Q9D94vcFWizJ+MSjNN4ns9r6hIGxw==", + "peerDependencies": { + "@nestjs/common": "^8.0.0 || ^9.0.0 || ^10.0.0", + "class-transformer": "^0.4.0 || ^0.5.0", + "class-validator": "^0.13.0 || ^0.14.0", + "reflect-metadata": "^0.1.12" + }, + "peerDependenciesMeta": { + "class-transformer": { + "optional": true + }, + "class-validator": { + "optional": true + } + } + }, "node_modules/@nestjs/platform-express": { "version": "10.2.10", "resolved": "https://registry.npmjs.org/@nestjs/platform-express/-/platform-express-10.2.10.tgz", @@ -1716,6 +1736,37 @@ "typescript": ">=4.8.2" } }, + "node_modules/@nestjs/swagger": { + "version": "7.1.16", + "resolved": "https://registry.npmjs.org/@nestjs/swagger/-/swagger-7.1.16.tgz", + "integrity": "sha512-f9KBk/BX9MUKPTj7tQNYJ124wV/jP5W2lwWHLGwe/4qQXixuDOo39zP55HIJ44LE7S04B7BOeUOo9GBJD/vRcw==", + "dependencies": { + "@nestjs/mapped-types": "2.0.3", + "js-yaml": "4.1.0", + "lodash": "4.17.21", + "path-to-regexp": "3.2.0", + "swagger-ui-dist": "5.9.1" + }, + "peerDependencies": { + "@fastify/static": "^6.0.0", + "@nestjs/common": "^9.0.0 || ^10.0.0", + "@nestjs/core": "^9.0.0 || ^10.0.0", + "class-transformer": "*", + "class-validator": "*", + "reflect-metadata": "^0.1.12" + }, + "peerDependenciesMeta": { + "@fastify/static": { + "optional": true + }, + "class-transformer": { + "optional": true + }, + "class-validator": { + "optional": true + } + } + }, "node_modules/@nestjs/testing": { "version": "10.2.10", "resolved": "https://registry.npmjs.org/@nestjs/testing/-/testing-10.2.10.tgz", @@ -2682,8 +2733,7 @@ "node_modules/argparse": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/argparse/-/argparse-2.0.1.tgz", - "integrity": "sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==", - "dev": true + "integrity": "sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==" }, "node_modules/array-flatten": { "version": "1.1.1", @@ -6103,7 +6153,6 @@ "version": "4.1.0", "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-4.1.0.tgz", "integrity": "sha512-wpxZs9NoxZaJESJGIZTyDEaYpl0FKSA+FB9aJiyemKhMwkxQg63h4T1KJgUGHpTqPDNRcmmYLugrRjJlBtWvRA==", - "dev": true, "dependencies": { "argparse": "^2.0.1" }, @@ -8040,6 +8089,11 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/swagger-ui-dist": { + "version": "5.9.1", + "resolved": "https://registry.npmjs.org/swagger-ui-dist/-/swagger-ui-dist-5.9.1.tgz", + "integrity": "sha512-5zAx+hUwJb9T3EAntc7TqYkV716CMqG6sZpNlAAMOMWkNXRYxGkN8ADIvD55dQZ10LxN90ZM/TQmN7y1gpICnw==" + }, "node_modules/symbol-observable": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/symbol-observable/-/symbol-observable-4.0.0.tgz", diff --git a/package.json b/package.json index 85a963f..d227add 100644 --- a/package.json +++ b/package.json @@ -24,6 +24,7 @@ "@nestjs/config": "^3.1.1", "@nestjs/core": "^10.0.0", "@nestjs/platform-express": "^10.0.0", + "@nestjs/swagger": "^7.1.16", "class-transformer": "^0.5.1", "class-validator": "^0.14.0", "node-fetch": "^2.7.0", diff --git a/src/app.module.spec.ts b/src/app.module.spec.ts index 90944bc..8c1424b 100644 --- a/src/app.module.spec.ts +++ b/src/app.module.spec.ts @@ -1,8 +1,9 @@ import { Test, type TestingModule } from '@nestjs/testing'; -import { ValidationPipe, type INestApplication } from '@nestjs/common'; +import type { INestApplication } from '@nestjs/common'; import request from 'supertest'; -import { AppModule } from './../src/app.module'; +import { AppModule } from './app.module'; import { createTestOmdbEnrichedDataService } from './utils/testHelpers'; +import { configureApp } from './utils/nestjsHelpers'; describe('AppController (e2e)', () => { let app: INestApplication; @@ -18,7 +19,7 @@ describe('AppController (e2e)', () => { .compile(); app = moduleFixture.createNestApplication(); - app.useGlobalPipes(new ValidationPipe({ transform: true })); + configureApp(app); await app.init(); }); diff --git a/src/main.ts b/src/main.ts index 911176d..2fef652 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,10 +1,10 @@ -import { ValidationPipe } from '@nestjs/common'; import { NestFactory } from '@nestjs/core'; import { AppModule } from './app.module'; +import { configureApp } from './utils/nestjsHelpers'; async function bootstrap() { const app = await NestFactory.create(AppModule); - app.useGlobalPipes(new ValidationPipe({ transform: true })); + configureApp(app); await app.listen(3000); } void bootstrap(); diff --git a/src/utils/nestjsHelpers.ts b/src/utils/nestjsHelpers.ts new file mode 100644 index 0000000..9609a34 --- /dev/null +++ b/src/utils/nestjsHelpers.ts @@ -0,0 +1,13 @@ +import { type INestApplication, ValidationPipe } from '@nestjs/common'; +import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'; + +export const configureApp = (app: INestApplication) => { + app.useGlobalPipes(new ValidationPipe({ transform: true })); + + const config = new DocumentBuilder() + .setTitle('Test joyn assignment') + .setVersion('1.0') + .build(); + const document = SwaggerModule.createDocument(app, config); + SwaggerModule.setup('api', app, document); +}; diff --git a/test/app.e2e-spec.ts b/test/app.e2e-spec.ts index aeffa9a..f635126 100644 --- a/test/app.e2e-spec.ts +++ b/test/app.e2e-spec.ts @@ -1,17 +1,19 @@ import { Test, type TestingModule } from '@nestjs/testing'; import type { INestApplication } from '@nestjs/common'; import request from 'supertest'; -import { AppModule } from './../src/app.module'; +import { AppModule } from '../src/app.module'; +import { configureApp } from '../src/utils/nestjsHelpers'; describe('AppController (e2e)', () => { let app: INestApplication; - beforeEach(async () => { + beforeAll(async () => { const moduleFixture: TestingModule = await Test.createTestingModule({ imports: [AppModule], }).compile(); app = moduleFixture.createNestApplication(); + configureApp(app); await app.init(); });