Skip to content

Commit

Permalink
feat: implement example client api
Browse files Browse the repository at this point in the history
  • Loading branch information
jbl428 committed Oct 21, 2023
1 parent 7431dfd commit ef4b59e
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 52 deletions.
2 changes: 1 addition & 1 deletion example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
"dependencies": {
"@nestjs/common": "^10.0.0",
"@nestjs/core": "^10.0.0",
"@nestjs/mapped-types": "*",
"@nestjs/platform-express": "^10.0.0",
"@r2don/nest-http-interface": "^1.3.0",
"class-transformer": "^0.5.1",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.8.1"
},
Expand Down
61 changes: 15 additions & 46 deletions example/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion example/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Module } from '@nestjs/common';
import { ServerModule } from './server/server.module';
import { ClientModule } from './client/client.module';
import { HttpInterfaceModule } from '@r2don/nest-http-interface';

@Module({
imports: [ServerModule],
imports: [HttpInterfaceModule.forRoot(), ServerModule, ClientModule],
})
export class AppModule {}
46 changes: 46 additions & 0 deletions example/src/client/client.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import {
Controller,
Post,
Body,
Delete,
Get,
Param,
Patch,
} from '@nestjs/common';
import { ClientHttpService } from './client.http.service';
import { CreateServerDto } from '../server/dto/create-server.dto';
import { UpdateServerDto } from '../server/dto/update-server.dto';
import { Server } from '../server/entities/server.entity';

@Controller('client')
export class ClientController {
constructor(private readonly clientService: ClientHttpService) {}

@Post()
async create(@Body() createClientDto: CreateServerDto): Promise<Server> {
return await this.clientService.create(createClientDto);
}

@Get()
findAll(): Promise<Server[]> {
return this.clientService.findAll();
}

@Get(':id')
findOne(@Param('id') id: string): Promise<Server> {
return this.clientService.findOne(+id);
}

@Patch(':id')
update(
@Param('id') id: string,
@Body() updateServerDto: UpdateServerDto,
): Promise<void> {
return this.clientService.update(+id, updateServerDto);
}

@Delete(':id')
remove(@Param('id') id: string): Promise<void> {
return this.clientService.remove(+id);
}
}
49 changes: 49 additions & 0 deletions example/src/client/client.http.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import {
DeleteExchange,
GetExchange,
HttpInterface,
imitation,
PatchExchange,
PathVariable,
PostExchange,
RequestBody,
ResponseBody,
} from '@r2don/nest-http-interface';
import { CreateServerDto } from '../server/dto/create-server.dto';
import { Server } from '../server/entities/server.entity';
import { UpdateServerDto } from '../server/dto/update-server.dto';

@HttpInterface('http://localhost:3000')
export class ClientHttpService {
@PostExchange('/api/servers')
@ResponseBody(Server) // you don't need to use this decorator if cli plugin is configured (see nest-cli.json)
async create(
@RequestBody() createServerDto: CreateServerDto,
): Promise<Server> {
return imitation(createServerDto);
}

@GetExchange('/api/servers')
async findAll(): Promise<Server[]> {
return imitation();
}

@GetExchange('/api/servers/{id}')
async findOne(@PathVariable('id') id: number): Promise<Server> {
return imitation(id);
}

@PatchExchange('/api/servers/{id}')
async update(
@PathVariable('id') id: number,
@RequestBody()
updateServerDto: UpdateServerDto,
): Promise<void> {
return imitation(id, updateServerDto);
}

@DeleteExchange('/api/servers/{id}')
async remove(@PathVariable('id') id: number): Promise<void> {
return imitation(id);
}
}
9 changes: 9 additions & 0 deletions example/src/client/client.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Module } from '@nestjs/common';
import { ClientHttpService } from './client.http.service';
import { ClientController } from './client.controller';

@Module({
controllers: [ClientController],
providers: [ClientHttpService],
})
export class ClientModule {}
8 changes: 4 additions & 4 deletions example/src/server/dto/update-server.dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PartialType } from '@nestjs/mapped-types';
import { CreateServerDto } from './create-server.dto';

export class UpdateServerDto extends PartialType(CreateServerDto) {}
export class UpdateServerDto {
name?: string;
isOnline?: boolean;
}

0 comments on commit ef4b59e

Please sign in to comment.