Skip to content

Commit 7431dfd

Browse files
committed
feat: implement example server api
1 parent c5a5d2e commit 7431dfd

14 files changed

+194
-56
lines changed

example/nest-cli.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33
"collection": "@nestjs/schematics",
44
"sourceRoot": "src",
55
"compilerOptions": {
6-
"deleteOutDir": true
6+
"deleteOutDir": true,
7+
"plugins": [
8+
{
9+
"name": "@r2don/nest-http-interface",
10+
"options": {
11+
"interfaceFilenameSuffix": [".http.service.ts"]
12+
}
13+
}
14+
]
715
}
816
}

example/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"license": "UNLICENSED",
88
"scripts": {
99
"build": "nest build",
10-
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
10+
"format": "prettier --write \"src/**/*.ts\"",
1111
"start": "nest start",
1212
"start:dev": "nest start --watch",
1313
"start:debug": "nest start --debug --watch",
@@ -22,7 +22,9 @@
2222
"dependencies": {
2323
"@nestjs/common": "^10.0.0",
2424
"@nestjs/core": "^10.0.0",
25+
"@nestjs/mapped-types": "*",
2526
"@nestjs/platform-express": "^10.0.0",
27+
"@r2don/nest-http-interface": "^1.3.0",
2628
"reflect-metadata": "^0.1.13",
2729
"rxjs": "^7.8.1"
2830
},

example/pnpm-lock.yaml

Lines changed: 68 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/src/app.controller.spec.ts

Lines changed: 0 additions & 22 deletions
This file was deleted.

example/src/app.controller.ts

Lines changed: 0 additions & 12 deletions
This file was deleted.

example/src/app.module.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import { Module } from '@nestjs/common';
2-
import { AppController } from './app.controller';
3-
import { AppService } from './app.service';
2+
import { ServerModule } from './server/server.module';
43

54
@Module({
6-
imports: [],
7-
controllers: [AppController],
8-
providers: [AppService],
5+
imports: [ServerModule],
96
})
107
export class AppModule {}

example/src/app.service.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export class CreateServerDto {
2+
name: string;
3+
isOnline: boolean;
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { PartialType } from '@nestjs/mapped-types';
2+
import { CreateServerDto } from './create-server.dto';
3+
4+
export class UpdateServerDto extends PartialType(CreateServerDto) {}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export class Server {
2+
id: number;
3+
name: string;
4+
isOnline: boolean;
5+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import {
2+
Controller,
3+
Get,
4+
Post,
5+
Body,
6+
Patch,
7+
Param,
8+
Delete,
9+
} from '@nestjs/common';
10+
import { ServerService } from './server.service';
11+
import { CreateServerDto } from './dto/create-server.dto';
12+
import { UpdateServerDto } from './dto/update-server.dto';
13+
14+
@Controller('/api/servers')
15+
export class ServerController {
16+
constructor(private readonly serverService: ServerService) {}
17+
18+
@Post()
19+
create(@Body() createServerDto: CreateServerDto) {
20+
return this.serverService.create(createServerDto);
21+
}
22+
23+
@Get()
24+
findAll() {
25+
return this.serverService.findAll();
26+
}
27+
28+
@Get(':id')
29+
findOne(@Param('id') id: string) {
30+
return this.serverService.findOne(+id);
31+
}
32+
33+
@Patch(':id')
34+
update(@Param('id') id: string, @Body() updateServerDto: UpdateServerDto) {
35+
return this.serverService.update(+id, updateServerDto);
36+
}
37+
38+
@Delete(':id')
39+
remove(@Param('id') id: string) {
40+
return this.serverService.remove(+id);
41+
}
42+
}

example/src/server/server.module.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Module } from '@nestjs/common';
2+
import { ServerService } from './server.service';
3+
import { ServerController } from './server.controller';
4+
5+
@Module({
6+
controllers: [ServerController],
7+
providers: [ServerService],
8+
})
9+
export class ServerModule {}

example/src/server/server.service.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { Injectable } from '@nestjs/common';
2+
import { CreateServerDto } from './dto/create-server.dto';
3+
import { UpdateServerDto } from './dto/update-server.dto';
4+
import { Server } from './entities/server.entity';
5+
6+
@Injectable()
7+
export class ServerService {
8+
#servers: Server[] = [];
9+
10+
create(createServerDto: CreateServerDto): Server {
11+
const server = new Server();
12+
server.id = this.#servers.length + 1;
13+
server.name = createServerDto.name;
14+
server.isOnline = createServerDto.isOnline;
15+
this.#servers.push(server);
16+
17+
return server;
18+
}
19+
20+
findAll(): Server[] {
21+
return this.#servers;
22+
}
23+
24+
findOne(id: number): Server | undefined {
25+
return this.#servers.find((server) => server.id === id);
26+
}
27+
28+
update(id: number, updateServerDto: UpdateServerDto) {
29+
this.#servers = this.#servers.map((server) => {
30+
if (server.id === id) {
31+
return {
32+
...server,
33+
...updateServerDto,
34+
};
35+
}
36+
37+
return server;
38+
});
39+
}
40+
41+
remove(id: number) {
42+
const index = this.#servers.findIndex((server) => server.id === id);
43+
this.#servers.splice(index, 1);
44+
}
45+
}

example/tsconfig.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@
1111
"outDir": "./dist",
1212
"baseUrl": "./",
1313
"incremental": true,
14+
"strict": true,
15+
"strictPropertyInitialization": false,
1416
"skipLibCheck": true,
15-
"strictNullChecks": false,
16-
"noImplicitAny": false,
17+
"noImplicitAny": true,
1718
"strictBindCallApply": false,
1819
"forceConsistentCasingInFileNames": false,
1920
"noFallthroughCasesInSwitch": false

0 commit comments

Comments
 (0)