Skip to content
This repository has been archived by the owner on Oct 18, 2022. It is now read-only.

Implement direct data return from controller classes #12

Open
ben-ryder opened this issue May 23, 2022 · 0 comments
Open

Implement direct data return from controller classes #12

ben-ryder opened this issue May 23, 2022 · 0 comments
Assignees
Labels
discovery more information is needed feature New feature or request package/core issues related to @kangojs/core

Comments

@ben-ryder
Copy link
Owner

Package Information: core @ *

Describe your feature
RIght now the controller methods that KangoJS binds to routes have no special handling which means you still have boilerplate code with sending responses and forwarding errors to next():

    @Route({
        httpMethod: HTTPMethods.POST,
        bodyShape: CreateNoteShape
    })
    async add(req: RequestWithUser, res: Response, next: NextFunction) {
        let newNote: NoteDto;

        try {
            newNote = await this.notesService.add(req.user.id, req.bodyDto);
        }
        catch (e) {
            return next(e);
        }

        return res.send(newNote);
    }

It would be great if the framework could absorb some of that repeated handling, so you could just do something like this instead and the returned data is sent in a response automatically and thrown errors are caught are handled:

    @Route({
        httpMethod: HTTPMethods.POST,
        bodyShape: CreateNoteShape
    })
    async add(req: RequestWithUser, res: Response, next: NextFunction) {
        return await this.notesService.add(req.user.id, req.bodyDto);
    }

Users should be able to still send a response manaully themselves though.

This will require a bit of discover work:

  • How will error handling work in terms of unhandled promise rejections etc, will it work fine? (maybe user's should return promises directly, not return await?)
  • Will core need to register the error handler the same way it does currently for auth and validation handlers?
  • How can I make it posisble for users to still manually return a response as well as allowing for automatic response handling?

Is your suggestion related to any problems?
Controllers have a lot of repeated boilerplate code around response & error handling.

Additional context
n/a

@ben-ryder ben-ryder added the feature New feature or request label May 23, 2022
@ben-ryder ben-ryder self-assigned this May 23, 2022
@ben-ryder ben-ryder added discovery more information is needed package/core issues related to @kangojs/core labels May 23, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
discovery more information is needed feature New feature or request package/core issues related to @kangojs/core
Projects
None yet
Development

No branches or pull requests

1 participant