REST APIs
The Goal
We will discuss how to create a REST API in Nest.
We will talk about modules, controllers, and services.
Create a REST API for our features in Nest
The Commands
Generate a REST resource for each feature.
npx nx g @nx/nest:resource --name=challenges --crud=true --project=challenges-api --type=rest
npx nx g @nx/nest:resource --name=flashcards --crud=true --project=flashcards-api --type=rest
npx nx g @nx/nest:resource --name=notes --crud=true --project=notes-api --type=rest
npx nx g @nx/nest:resource --name=users --crud=true --project=users-api --type=rest
npx nx g @nx/nest:resource --name=features --crud=true --project=features-api --type=rest
The Code
Module
import { Module } from '@nestjs/common';
import { ChallengesService } from './challenges.service';
import { ChallengesController } from './challenges.controller';
@Module({
controllers: [ChallengesController],
providers: [ChallengesService],
})
export class ChallengesModule {}
Controller
import {
Body,
Controller,
Delete,
Get,
Param,
Patch,
Post,
} from '@nestjs/common';
import { Challenge } from '../database/entities/challenge.entity';
import { ChallengesService } from './challenges.service';
@Controller('challenges')
export class ChallengesController {
constructor(private readonly challengesService: ChallengesService) {}
@Post()
create(@Body() challenge: Challenge): Promise<Challenge> {
return this.challengesService.create(challenge);
}
@Get()
findAll(): Promise<Challenge[]> {
return this.challengesService.findAll();
}
@Get(':id')
findOne(@Param('id') id: string): Promise<Challenge> {
return this.challengesService.findOne(id);
}
@Patch(':id')
update(@Param('id') id: string, @Body() challenge: Challenge) {
return this.challengesService.update(challenge);
}
@Delete(':id')
remove(@Param('id') id: string) {
return this.challengesService.remove(id);
}
}
Service
import { Injectable, Inject, NotFoundException } from '@nestjs/common';
import { Repository, DeleteResult } from 'typeorm';
import { Challenge } from '../database/entities/challenge.entity';
@Injectable()
export class ChallengesService {
constructor(
@Inject('CHALLENGE_REPOSITORY')
private challengesRepository: Repository<Challenge>,
) {}
async findAll(): Promise<Challenge[]> {
return await this.challengesRepository.find();
}
async findOne(id: string): Promise<Challenge | undefined> {
return await this.challengesRepository.findOneBy({ id });
}
async get(id: string): Promise<Challenge> {
const challenge = await this.challengesRepository.findOneBy({ id });
if (!challenge) throw new NotFoundException();
return challenge;
}
async create(challenge: Challenge): Promise<Challenge> {
return await this.challengesRepository.save(challenge);
}
async update(challenge: Challenge): Promise<Challenge> {
return await this.challengesRepository.save(challenge);
}
async remove(id: string): Promise<DeleteResult> {
const challenge = await this.challengesRepository.findOneBy({ id });
if (!challenge) throw new NotFoundException();
return await this.challengesRepository.delete(id);
}
}