1.0.2 • Published 4 years ago

nest-rest-framework v1.0.2

Weekly downloads
58
License
MIT
Repository
github
Last release
4 years ago

Installation

Coming Soon

$ npm install --save nest-rest-framework nest-rest-framework-[typeorm|mongoose]

Nest REST Framework Concepts

Controllers

The foundational belief of the NRF design is that the controller should be as absent of code and logic as possible. The controller's job in this pattern is the wire together pieces that do the real work: ViewSets, Transformers, and Hooks.

ViewSets

The duty of the viewset is to manage the interactions with the data storage layer by implementing the ViewSet base. This logic should be free of mapping logic (transformers) or business logic (hooks). Future state intention is that Nest REST Framework offers quick-start ViewSets for tools such as TypeORM and Mongoose.

Transformers

Transformers are object mappers. NRF has two types of transformers: data transformers and request transformers. Future state intention is that these transformers are optional for use-cases such as prototypes or data-viewer style apps -- this will allow for a very quick spin-up of full REST APIs.

Data Transformers

Data transformers should map the stored data object into a web API-friendly response format. Typically these types of objects are JSON interfaces. Input: database object. Output: web API response.

Request Transformers

Request transformers should map the incoming data request into the stored data object. Input: web API request. Output: database object.

Hooks

Hooks are event registrations for specific use-cases. NRF has three types of hooks: auth hooks, save hooks, and error hooks.

Auth Hooks

Auth hooks are responsible for validating the request for authorization and authentication. For instance, is the user logged in? Does this user have access to take CRUD actions against this particular entity? Can data only be managed on Tuesdays? Auth hooks are agnostic and allow for any sort of validation and verification.

Save Hooks

Save hooks are executed whenever data changes are made.

Error hooks

Error hooks are executed whenever an error occurs during the controller request lifecycle. Creating an error hook allows for functionality such as custom responses, support ticket logging, or process logging.

Creating your first API

  1. Create your data model class
export class Todo {
  id: number;
  details: string;
  complete: boolean;
  1. Create a data transformer class.
export class TodoDataTransformer extends Transformer<Todo, TodoDto> {
  transform(input: Todo): TodoDto {
    const dto = {} as Todo;
    dto.id = input.id;
    dto.details = input.details;
    dto.complete = input.complete;
    return dto;
  }
}
  1. Create a request transformer class.
export class TodoRequestTransformer extends Transformer<TodoDto, Todo> {
  transform(input: TodoDto): Todo {
    const entity = new Todo();
    entity.id = input.id;
    entity.details = input.details;
    entity.complete = input.complete;
    return entity;
  }
}
  1. Create a viewset class.
export class TodoViewSet extends ViewSet<
  number,
  Todo
> {
}
  1. Create your controller.
@Controller('todo')
export class TodoController extends RestController<
  number,
  Todo,
  TodoDto,
  TodoDto
> {
  constructor(
    viewset: TodoViewSet,
    requestTransformer: TodoRequestTransformer,
    dataTransformer: TodoDataTransformer,
  ) {
    super({
      enableBatch: true,
      viewset,
      requestTransformer,
      dataTransformer,
    });
  }
}

Support

Nest REST Framework is an MIT-licensed open source project.

Committing

Committing to this project requires Commit messages to be formatted according to the Angular conventional-changelog

Stay in touch

License

Nest REST Framework is MIT licensed.