Skip to content

Commit f55b6ee

Browse files
committed
feat(adapter-nestjs): 🚀 实现 nestjs 适配器,完善 nestjs 示例项目
1 parent d2c846d commit f55b6ee

12 files changed

Lines changed: 585 additions & 48 deletions

File tree

examples/nestjs/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
/dist
33
/node_modules
44
/build
5+
/cert
56

67
# Logs
78
logs

examples/nestjs/package.json

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,20 @@
2020
"test:e2e": "jest --config ./test/jest-e2e.json"
2121
},
2222
"dependencies": {
23-
"@nestjs/common": "^11.0.1",
24-
"@nestjs/core": "^11.0.1",
25-
"@nestjs/platform-express": "^11.0.1",
23+
"@https-enable/adapter-nestjs": "workspace:*",
24+
"@https-enable/core": "workspace:*",
25+
"@nestjs/common": "11.0.1",
26+
"@nestjs/core": "11.0.1",
27+
"@nestjs/platform-express": "11.0.1",
28+
"@nestjs/platform-fastify": "11.0.1",
2629
"reflect-metadata": "^0.2.2",
2730
"rxjs": "^7.8.1"
2831
},
2932
"devDependencies": {
3033
"@eslint/eslintrc": "^3.2.0",
3134
"@eslint/js": "^9.18.0",
32-
"@nestjs/cli": "^11.0.0",
33-
"@nestjs/schematics": "^11.0.0",
35+
"@nestjs/cli": "^11.0.1",
36+
"@nestjs/schematics": "^11.0.1",
3437
"@nestjs/testing": "^11.0.1",
3538
"@swc/cli": "^0.6.0",
3639
"@swc/core": "^1.10.7",

examples/nestjs/src/app.controller.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { TestingModule } from '@nestjs/testing';
1+
import { TestingModule } from '@nestjs/testing';
22
import { Test } from '@nestjs/testing';
33
import { AppController } from './app.controller';
44
import { AppService } from './app.service';

examples/nestjs/src/app.controller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { AppService } from './app.service';
1+
import { AppService } from './app.service';
22
import { Controller, Get } from '@nestjs/common';
33

44
@Controller()

examples/nestjs/src/main.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,34 @@
1+
/* eslint-disable @typescript-eslint/no-unused-vars */
12
import { NestFactory } from '@nestjs/core';
23
import { AppModule } from './app.module';
4+
import { HttpsEnabler } from '@https-enable/core';
5+
import { NestExpressApplication } from '@nestjs/platform-express';
6+
import {
7+
FastifyAdapter,
8+
NestFastifyApplication,
9+
} from '@nestjs/platform-fastify';
10+
import {
11+
NestJsFastifyHttpsAdapter,
12+
NestJsExpressHttpsAdapter,
13+
} from '@https-enable/adapter-nestjs';
314

415
async function bootstrap() {
5-
const app = await NestFactory.create(AppModule);
6-
await app.listen(process.env.PORT ?? 3000);
16+
// const app = await NestFactory.create<NestExpressApplication>(AppModule);
17+
const app = await NestFactory.create<NestFastifyApplication>(
18+
AppModule,
19+
new FastifyAdapter(),
20+
);
21+
22+
const adapter = new NestJsFastifyHttpsAdapter(app);
23+
24+
const enabler = new HttpsEnabler({
25+
adapter,
26+
options: { host: '127.0.0.1', port: process.env.PORT ?? 3000 },
27+
certificateOptions: { validity: 1, domains: '127.0.0.1', base: 'cert' },
28+
});
29+
30+
await enabler.startServer().then((res) => {
31+
console.log(`Server running in http://${res.host}:${res.port}`);
32+
});
733
}
834
void bootstrap();

examples/nestjs/test/app.e2e-spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import type { INestApplication } from '@nestjs/common';
2-
import type { TestingModule } from '@nestjs/testing';
3-
import type { App } from 'supertest/types';
1+
import { INestApplication } from '@nestjs/common';
2+
import { TestingModule } from '@nestjs/testing';
3+
import { App } from 'supertest/types';
44
import { Test } from '@nestjs/testing';
55
import * as request from 'supertest';
66
import { AppModule } from './../src/app.module';
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { defineBuildConfig } from 'unbuild'
2+
3+
export default defineBuildConfig({
4+
entries: ['src/index'],
5+
declaration: true,
6+
clean: true,
7+
rollup: {
8+
emitCJS: true,
9+
// inlineDependencies: false,
10+
},
11+
failOnWarn: false,
12+
externals: [
13+
'express',
14+
],
15+
})
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"name": "@https-enable/adapter-nestjs",
3+
"version": "0.1.0",
4+
"license": "MIT",
5+
"exports": {
6+
".": {
7+
"import": "./dist/index.mjs",
8+
"require": "./dist/index.cjs"
9+
}
10+
},
11+
"main": "dist/index.cjs",
12+
"module": "dist/index.mjs",
13+
"types": "dist/index.d.ts",
14+
"files": [
15+
"dist"
16+
],
17+
"publishConfig": {
18+
"access": "public",
19+
"registry": "https://registry.npmjs.org/"
20+
},
21+
"scripts": {
22+
"build": "unbuild",
23+
"dev": "unbuild --stub",
24+
"release": "npm run build && bumpp",
25+
"prepublishOnly": "npm run build"
26+
},
27+
"peerDependencies": {
28+
"@https-enable/core": "workspace:*",
29+
"@nestjs/common": "*",
30+
"@nestjs/core": "*",
31+
"@nestjs/platform-express": "*",
32+
"@nestjs/platform-fastify": "*",
33+
"@types/express": "*"
34+
},
35+
"dependencies": {
36+
"@https-enable/mkcert": "workspace:*"
37+
},
38+
"devDependencies": {
39+
"@https-enable/tsconfig": "workspace:*",
40+
"@https-enable/types": "workspace:*",
41+
"@types/express": "^5.0.0",
42+
"@types/node": "^22.13.1",
43+
"unbuild": "^3.3.1"
44+
}
45+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import type { ServerOptions } from '@https-enable/core'
2+
import type { NestExpressApplication } from '@nestjs/platform-express'
3+
import type { NestFastifyApplication } from '@nestjs/platform-fastify'
4+
import { HttpsAdapter } from '@https-enable/core'
5+
6+
type ExpressApp = ReturnType<ReturnType<NestExpressApplication['getHttpAdapter']>['getInstance']>
7+
export class NestJsExpressHttpsAdapter extends HttpsAdapter<ExpressApp> {
8+
constructor(public nestApp: NestExpressApplication) {
9+
super()
10+
}
11+
12+
init = async () => {
13+
await this.nestApp.init()
14+
return this.nestApp.getHttpAdapter().getInstance()
15+
}
16+
17+
createMiddleware?: (options: ServerOptions) => any
18+
onCertRenewed?: any
19+
}
20+
21+
type FastifyApp = ReturnType<ReturnType<NestFastifyApplication['getHttpAdapter']>['getInstance']>['routing']
22+
export class NestJsFastifyHttpsAdapter extends HttpsAdapter<FastifyApp, any> {
23+
constructor(public nestApp: NestFastifyApplication) {
24+
super()
25+
}
26+
27+
init = async () => {
28+
await this.nestApp.init()
29+
const fastifyApp = this.nestApp.getHttpAdapter().getInstance()
30+
await fastifyApp.ready()
31+
return fastifyApp.routing
32+
}
33+
34+
createMiddleware?: (options: ServerOptions) => any
35+
onCertRenewed?: any
36+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"extends": "@https-enable/tsconfig/tsconfig.node.json",
3+
"compilerOptions": {
4+
"incremental": false,
5+
"composite": false,
6+
"module": "ESNext",
7+
"moduleResolution": "Bundler"
8+
},
9+
"include": ["src/**/*.ts", "src/**/*.d.ts"],
10+
"exclude": ["dist", "build", "node_modules"]
11+
}

0 commit comments

Comments
 (0)