feat: init

This commit is contained in:
Innei
2021-07-23 11:54:10 +08:00
parent 05bdda7049
commit b6766589c9
13 changed files with 1162 additions and 390 deletions

View File

@@ -1,4 +1,5 @@
{
"singleQuote": true,
"trailingComma": "all"
"trailingComma": "all",
"semi": false
}

15
.vscode/settings.json vendored Normal file
View File

@@ -0,0 +1,15 @@
{
"editor.formatOnSave": true,
"[javascript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[javascriptreact]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[typescript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[typescriptreact]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
}

View File

@@ -2,14 +2,26 @@
"name": "server-next",
"version": "0.0.1",
"description": "",
"author": "",
"author": "Innei",
"private": true,
"license": "UNLICENSED",
"license": "MIT",
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"lint-staged": {
"*.{js,jsx,ts,tsx}": [
"prettier --ignore-path ./.prettierignore --write "
]
},
"homepage": "https://github.com/mx-space/server-next#readme",
"scripts": {
"prepare": "husky install",
"prebuild": "rimraf dist",
"build": "nest build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "nest start",
"start": "cross-env NODE_ENV=development nest start -w",
"start:dev": "nest start --watch",
"start:debug": "nest start --debug --watch",
"start:prod": "node dist/main",
@@ -23,10 +35,22 @@
"dependencies": {
"@nestjs/common": "^8.0.4",
"@nestjs/core": "^8.0.4",
"@nestjs/platform-express": "^8.0.4",
"@nestjs/platform-fastify": "^8.0.4",
"@nestjs/swagger": "^5.0.8",
"@typegoose/auto-increment": "^0.6.0",
"@typegoose/typegoose": "^7.6.3",
"fastify-swagger": "^4.8.3",
"husky": "^7.0.1",
"lint-staged": "^11.1.0",
"mongoose": "^5.13.3",
"mongoose-lean-virtuals": "^0.8.0",
"passport": "^0.4.1",
"passport-jwt": "^4.0.0",
"passport-local": "^1.0.0",
"reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2",
"rxjs": "^7.2.0"
"rxjs": "^7.2.0",
"snakecase-keys": "^4.0.2"
},
"devDependencies": {
"@nestjs/cli": "^8.0.2",
@@ -38,6 +62,7 @@
"@types/supertest": "^2.0.11",
"@typescript-eslint/eslint-plugin": "4.28.4",
"@typescript-eslint/parser": "4.28.4",
"cross-env": "^7.0.3",
"eslint": "^7.31.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^3.4.0",
@@ -67,4 +92,4 @@
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}
}
}

1363
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,22 +1,22 @@
import { Test, TestingModule } from '@nestjs/testing';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { Test, TestingModule } from '@nestjs/testing'
import { AppController } from './app.controller'
import { AppService } from './app.service'
describe('AppController', () => {
let appController: AppController;
let appController: AppController
beforeEach(async () => {
const app: TestingModule = await Test.createTestingModule({
controllers: [AppController],
providers: [AppService],
}).compile();
}).compile()
appController = app.get<AppController>(AppController);
});
appController = app.get<AppController>(AppController)
})
describe('root', () => {
it('should return "Hello World!"', () => {
expect(appController.getHello()).toBe('Hello World!');
});
});
});
expect(appController.getHello()).toBe('Hello World!')
})
})
})

View File

@@ -1,5 +1,5 @@
import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';
import { Controller, Get } from '@nestjs/common'
import { AppService } from './app.service'
@Controller()
export class AppController {
@@ -7,6 +7,6 @@ export class AppController {
@Get()
getHello(): string {
return this.appService.getHello();
return this.appService.getHello()
}
}

View File

@@ -1,6 +1,6 @@
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { Module } from '@nestjs/common'
import { AppController } from './app.controller'
import { AppService } from './app.service'
@Module({
imports: [],

View File

@@ -1,8 +1,8 @@
import { Injectable } from '@nestjs/common';
import { Injectable } from '@nestjs/common'
@Injectable()
export class AppService {
getHello(): string {
return 'Hello World!';
return 'Hello World!'
}
}

14
src/core/adapt/fastify.ts Normal file
View File

@@ -0,0 +1,14 @@
import { FastifyAdapter } from '@nestjs/platform-fastify'
export const fastifyApp = new FastifyAdapter({
trustProxy: true,
})
fastifyApp.getInstance().addHook('onRequest', (request, reply, done) => {
const origin = request.headers.origin
if (!origin) {
request.headers.origin = request.headers.host
}
done()
})

View File

@@ -1,8 +1,52 @@
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { NestFactory } from '@nestjs/core'
import { AppModule } from './app.module'
import { NestFastifyApplication } from '@nestjs/platform-fastify'
import { fastifyApp } from './core/adapt/fastify'
import { isDev } from './utils'
import { Logger } from '@nestjs/common'
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'
const PORT = parseInt(process.env.PORT) || 2333
const APIVersion = 1
const Origin = process.env.ORIGIN || ''
async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(2333);
const app = await NestFactory.create<NestFastifyApplication>(
AppModule,
fastifyApp,
)
const hosts = Origin.split(',').map((host) => new RegExp(host, 'i'))
app.enableCors({
origin: (origin, callback) => {
const allow = hosts.some((host) => host.test(origin))
callback(null, allow)
},
credentials: true,
})
app.setGlobalPrefix(isDev ? '' : `api/v${APIVersion}`)
if (isDev) {
const options = new DocumentBuilder()
.setTitle('API')
.setDescription('The blog API description')
.setVersion(`${APIVersion}`)
.addSecurity('bearer', {
type: 'http',
scheme: 'bearer',
})
.addBearerAuth()
.build()
const document = SwaggerModule.createDocument(app, options)
SwaggerModule.setup('api-docs', app, document)
}
await app.listen(PORT, '0.0.0.0', () => {
if (isDev) {
Logger.debug(`http://localhost:${PORT}/api-docs`)
}
Logger.log('Server is up.')
})
}
bootstrap();
bootstrap()

3
src/utils/index.ts Normal file
View File

@@ -0,0 +1,3 @@
export const isDev = process.env.NODE_ENV == 'development'
export * from './ip'

1
src/utils/ip.ts Normal file
View File

@@ -0,0 +1 @@
export {}

View File

@@ -1,24 +1,24 @@
import { Test, TestingModule } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import * as request from 'supertest';
import { AppModule } from './../src/app.module';
import { Test, TestingModule } from '@nestjs/testing'
import { INestApplication } from '@nestjs/common'
import * as request from 'supertest'
import { AppModule } from './../src/app.module'
describe('AppController (e2e)', () => {
let app: INestApplication;
let app: INestApplication
beforeEach(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule],
}).compile();
}).compile()
app = moduleFixture.createNestApplication();
await app.init();
});
app = moduleFixture.createNestApplication()
await app.init()
})
it('/ (GET)', () => {
return request(app.getHttpServer())
.get('/')
.expect(200)
.expect('Hello World!');
});
});
.expect('Hello World!')
})
})