5.0.0 • Published 9 months ago
@aging/rest-controller v5.0.0
rest-controller
Class: RestController
- This class provides a helper method to dynamically generate RESTful API controllers for a specific database entity. It is intended to simplify the creation and management of endpoints for CRUD (Create, Read, Update, Delete) operations and other entity-specific routes by following REST conventions as well as authentication and authorization metadata.
Features:
- Path Generation: Automatically constructs API paths for various operations such as retrieving, creating, updating, and deleting resources based on the entity's base URL.
- Consistency: Ensures uniform and predictable API paths across the application.
- Customization: Allows for the definition of custom routes or variations in paths for specific actions.
Usage:
import { NotImplementedException } from '@nestjs/common';
import { RestController } from './rest-controller';
import { RestPath } from '@aging/rest-path';
export class SampleEntity {}
export class CreateSampleDto {}
export class UpdateSampleDto {}
export class QuerySampleDto {}
const P = new RestPath({
name: 'sample',
pluralName: 'samples',
prefix: '',
});
const C = new RestController({
entity: SampleEntity,
path: P,
isPublic: false,
createDto: CreateSampleDto,
updateDto: UpdateSampleDto,
queryDto: QuerySampleDto,
});
@C.Controller()
export class SampleController {
@C.Save()
Save() {
throw new NotImplementedException();
}
@C.SaveMany()
SaveMany() {
throw new NotImplementedException();
}
@C.FindAll()
findAll() {
throw new NotImplementedException();
}
@C.FindOneById()
FindOneById() {
throw new NotImplementedException();
}
@C.Delete()
Delete() {
throw new NotImplementedException();
}
@C.Update()
Update() {
throw new NotImplementedException();
}
@C.Count()
Count() {
throw new NotImplementedException();
}
@C.AddRelation()
AddRelation() {
throw new NotImplementedException();
}
@C.SetRelation()
SetRelation() {
throw new NotImplementedException();
}
@C.RemoveRelation()
RemoveRelation() {
throw new NotImplementedException();
}
@C.UnsetRelation()
UnsetRelation() {
throw new NotImplementedException();
}
}
Benefits:
- Scalability: As the number of entities in your application grows, managing API routes becomes easier with a centralized, reusable approach.
- Maintainability: Reduces redundancy and potential for errors in hardcoded paths.
- Flexibility: Adaptable to different naming conventions or route structures.