Overview โ
Nust is a nuxt module that allows NestJS like backend structure in nuxt, Standardizing your backend with CRUD structure, powering nuxt backend with features like:
- ๐ฎ ย Controllers
- ๐๏ธ ย Decorators
- ๐๏ธ ย Injectable providers/services
- ๐ช๏ธ ย Parameter extraction
- โ
๏ธ ย Body/DTO Validation (using
class-validator
) - ๐๏ธ ย Transformers (using
class-transformer
) - ๐๏ธ ย Guards
- ๐๏ธ ย OpenAPI documentation support, Nestjs like Api documentation decorators for better Swagger and Scalar support
Turn your server structure from this:
From this: โ
js
server/
โโโ api/
โ โโโ cat/
โ โ โโโ index.get.ts //Find all
โ โ โโโ index.post.ts //Create
โ โ โโโ [id].get.ts //Find one
โ โ โโโ [id].patch.ts //Update
โ โ โโโ [id].delete.ts //delete
โ โโโ dog/
โ โโโ index.get.ts //Find all
โ โโโ index.post.ts //Create
โ โโโ [id].get.ts //Find one
โ โโโ [id].patch.ts //Update
โ โโโ [id].delete.ts //delete
โโโ utils/
โโโ catUtilss.ts
โโโ dogUtils.ts
To this: (Just and example, structure can be however you like) โ
js
server/
โโโ nust/
โ โโโ cat/
โ โ โโโ dto/ // For example, you can add your CRUD dto's here
โ โ โ โโโ CreateCat.dto.ts
โ โ โ โโโ UpdateCat.dto.ts
โ โ โโโ entity/ // For example, you can add your resource relevent types here
โ โ โโโ cat.controller.ts // Has all the CRUD methods
โ โ โโโ cat.service.ts // cat service provider, can be used to hold all logic, allowing it to be injected to any controller and reuse the logic
โ โโโ dog/
โ โ โโโ dog.controller.ts // Has all the CRUD methods
โ โ โโโ dog.service.ts // dog service provider
โโโ index.ts // controllersFile, a file that exports an object of all controllers
If you've worked with other backend focused frameworks you'd realise this structure is familiar, where the logic for a CRUD resource all sits under one folder/module, helps keep the backend code organised and its logic reusable.
Resource controllers โ
You're event handler changes from this:
typescript
//index.get.ts
export default defineEventHandler((event)=>{
//...
return //
})
//index.post.ts
export default defineEventHandler((event)=>{
//...
return //
})
//[id].get.ts
export default defineEventHandler((event)=>{
//...
return //
})
//[id].post.ts
export default defineEventHandler((event)=>{
//...
return //
})
//[id].delete.ts
export default defineEventHandler((event)=>{
//...
return //
})
To this:
typescript
import {Controller, Get, Post, Delete, Body, Param} from '#nust'
import {EventObject} from "./route-params.decorator";
@Controller('cat') // Prefix can be defind here or you can just add it to each method
export class CatController {
// Get all
@Get('')
findAll() {
//...
}
// POST Create
@Post('')
create(@Body(CreateCatDto) dto: CreateCatDto) {
//...
}
// Get one
@Get(':id')
findOne(@Param('id') id: string): CatEntity {
//..
}
@Patch(':id')
update(
@Param('id') id: string,
@Body(UpdateCatDto) dto: UpdateCatDto
) {
//...
}
@Delete(':id')
delete(@Param('id') id: string) {
//...
}
@Any('get-random-cat')
otherNoneStandardCRUDmethod() {
//...
}
}