From e6b2bfb418f2133abd90a40676a60304b0b582ce Mon Sep 17 00:00:00 2001 From: Walidoux Date: Mon, 31 Jul 2023 11:05:20 +0100 Subject: [PATCH] init(migration): setup boilerplate code --- nest-cli.json | 5 +++ package.json | 68 +++++++++++++++++++++++++++++++++++ src/app.module.ts | 10 ++++++ src/gateway/gateway.module.ts | 8 +++++ src/gateway/gateway.ts | 26 ++++++++++++++ src/main.ts | 10 ++++++ test/app.e2e-spec.ts | 23 ++++++++++++ test/jest-e2e.json | 9 +++++ tsconfig.json | 14 ++++++++ 9 files changed, 173 insertions(+) create mode 100644 nest-cli.json create mode 100644 package.json create mode 100644 src/app.module.ts create mode 100644 src/gateway/gateway.module.ts create mode 100644 src/gateway/gateway.ts create mode 100644 src/main.ts create mode 100644 test/app.e2e-spec.ts create mode 100644 test/jest-e2e.json create mode 100644 tsconfig.json diff --git a/nest-cli.json b/nest-cli.json new file mode 100644 index 0000000..2566481 --- /dev/null +++ b/nest-cli.json @@ -0,0 +1,5 @@ +{ + "$schema": "https://json.schemastore.org/nest-cli", + "collection": "@nestjs/schematics", + "sourceRoot": "src" +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..f421dca --- /dev/null +++ b/package.json @@ -0,0 +1,68 @@ +{ + "name": "@habbilfr/server", + "main": "./dist/index.js", + "types": "./dist/index.d.ts", + "scripts": { + "prebuild": "rm -rf dist", + "build": "nest build", + "start": "nest start", + "start:dev": "nest start --watch", + "start:debug": "nest start --debug --watch", + "start:prod": "node dist/main", + "test": "jest", + "test:watch": "jest --watch", + "test:cov": "jest --coverage", + "test:e2e": "jest --config ./test/jest-e2e.json", + "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand" + }, + "dependencies": { + "@nestjs/common": "^10.1.0", + "@nestjs/core": "^10.1.0", + "@nestjs/platform-socket.io": "10.1.0", + "@nestjs/platform-express": "^9.0.0", + "@nestjs/websockets": "10.1.0", + "reflect-metadata": "^0.1.13", + "rimraf": "^3.0.2", + "rxjs": "^7.2.0" + }, + "devDependencies": { + "@nestjs/cli": "^10.1.10", + "@nestjs/schematics": "^10.0.1", + "@nestjs/testing": "^10.1.0", + "@types/node": "^20.4.2", + "@types/supertest": "^2.0.12", + "@types/jest": "^29.5.2", + "@types/express": "^4.17.13", + "jest": "^29.5.0", + "supertest": "^6.3.3", + "source-map-support": "^0.5.20", + "ts-jest": "^29.1.1", + "ts-loader": "^9.4.4", + "ts-node": "^10.9.1", + "tsconfig-paths": "^4.2.0", + "typescript": "^5.1.6" + }, + "peerDependencies": { + "socket.io": "4.7.1" + }, + "jest": { + "moduleFileExtensions": [ + "js", + "json", + "ts" + ], + "rootDir": "src", + "testRegex": ".*\\.spec\\.ts$", + "transform": { + "^.+\\.(t|j)s$": "ts-jest" + }, + "collectCoverageFrom": [ + "**/*.(t|j)s" + ], + "coverageDirectory": "../coverage", + "testEnvironment": "node" + }, + "files": [ + "dist" + ] +} diff --git a/src/app.module.ts b/src/app.module.ts new file mode 100644 index 0000000..811db8b --- /dev/null +++ b/src/app.module.ts @@ -0,0 +1,10 @@ +import { Module } from '@nestjs/common' + +import { GatewayModule } from './gateway/gateway.module' + +@Module({ + imports: [GatewayModule], + controllers: [], + providers: [] +}) +export class AppModule {} diff --git a/src/gateway/gateway.module.ts b/src/gateway/gateway.module.ts new file mode 100644 index 0000000..7b3f14f --- /dev/null +++ b/src/gateway/gateway.module.ts @@ -0,0 +1,8 @@ +import { Module } from '@nestjs/common' + +import { MyGateway } from './gateway' + +@Module({ + providers: [MyGateway] +}) +export class GatewayModule {} diff --git a/src/gateway/gateway.ts b/src/gateway/gateway.ts new file mode 100644 index 0000000..d8a31a8 --- /dev/null +++ b/src/gateway/gateway.ts @@ -0,0 +1,26 @@ +import type { OnModuleInit } from '@nestjs/common' +import { ConnectedSocket, MessageBody, SubscribeMessage, WebSocketGateway, WebSocketServer } from '@nestjs/websockets' +import type { Server, Socket } from 'socket.io' + +@WebSocketGateway({ cors: { origin: '*' } }) +export class MyGateway implements OnModuleInit { + @WebSocketServer() + server: Server + + onModuleInit(): void { + this.server.on('connection', (socket) => { + console.log(socket.id) + console.log('Connected') + }) + } + + @SubscribeMessage('newMessage') + onNewMessage(@MessageBody() data: string, @ConnectedSocket() client: Socket): void { + console.log(data, client) + this.server.emit('onMessage', { + msg: 'New Message', + content: data, + client + }) + } +} diff --git a/src/main.ts b/src/main.ts new file mode 100644 index 0000000..005d71d --- /dev/null +++ b/src/main.ts @@ -0,0 +1,10 @@ +import { NestFactory } from '@nestjs/core' + +import { AppModule } from './app.module' + +const bootstrap = async (): Promise => { + const app = await NestFactory.create(AppModule) + await app.listen(3000, () => console.log('Running on Port 3000')) +} + +bootstrap() diff --git a/test/app.e2e-spec.ts b/test/app.e2e-spec.ts new file mode 100644 index 0000000..96728c4 --- /dev/null +++ b/test/app.e2e-spec.ts @@ -0,0 +1,23 @@ +import type { TestingModule } from '@nestjs/testing' +import { Test } from '@nestjs/testing' +import type { INestApplication } from '@nestjs/common' +import * as request from 'supertest' + +import { AppModule } from '../src/app.module' + +describe('AppController (e2e)', () => { + let app: INestApplication + + beforeEach(async () => { + const moduleFixture: TestingModule = await Test.createTestingModule({ + imports: [AppModule] + }).compile() + + app = moduleFixture.createNestApplication() + await app.init() + }) + + it('/ (GET)', () => { + return request(app.getHttpServer()).get('/').expect(200).expect('Hello World!') + }) +}) diff --git a/test/jest-e2e.json b/test/jest-e2e.json new file mode 100644 index 0000000..e9d912f --- /dev/null +++ b/test/jest-e2e.json @@ -0,0 +1,9 @@ +{ + "moduleFileExtensions": ["js", "json", "ts"], + "rootDir": ".", + "testEnvironment": "node", + "testRegex": ".e2e-spec.ts$", + "transform": { + "^.+\\.(t|j)s$": "ts-jest" + } +} diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..c2ac0eb --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,14 @@ +{ + "compilerOptions": { + "module": "commonjs", + "declaration": true, + "removeComments": true, + "emitDecoratorMetadata": true, + "experimentalDecorators": true, + "allowSyntheticDefaultImports": true, + "target": "es2017", + "sourceMap": true, + "outDir": "./dist", + "incremental": true + } +}