Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Decorator] UseBefore and UserAfter hooks #19

Closed
vincent178 opened this issue Dec 19, 2016 · 4 comments
Closed

[Decorator] UseBefore and UserAfter hooks #19

vincent178 opened this issue Dec 19, 2016 · 4 comments

Comments

@vincent178
Copy link
Contributor

Hi @Romakita and @alexproca ,

I'm thinking about add BeforeAction and AfterAction for this project.

The main reason here is using middleware can add generic logic with request, response next way, but for our controller methods aka endpoints, is there a way to intercept the result around the method? The BeforeAction AfterAction is my proposal.

For example

@Controller("")
export default class HomeController {

    ...
    @Get("/")
    @ResponseView("home/index")
    @AfterAction('renderAssets')
    public async indexAction(request: Express.Request): Promise<Object> {

        const members = await this.memberService.findAll();
        
        return {
            title: 'blog | 博客',
            content: "This is content",
            members
        };
    }

    private renderAssets(result: any,
                         request?: Express.Request,
                         response?: Express.Response,
                         next?: Function) {

        const assets = {};
        result['assets'] = assets;
        return result;
    }
}

With AfterAction, I have a way to change the data before render to the page.

For global Actions, we can register these functions in ServerLoader.

Do you guys think it's a right way to go? or you guys have any idea on this?

Comments are very welcome.

Thanks,
Vincent

@alexproca
Copy link
Contributor

Can we see them as a generalisation of @Authenticated() extends @BeforeAction() and @ResponseView extends @AfterAction() ?

@vincent178
Copy link
Contributor Author

yes, they do

@Romakita
Copy link
Collaborator

Hi @vincent178 ,

Good idea. For your decorator, you can set a function/method as parameters like that:

@Controller("")
export default class HomeController {

    ...
    @Get("/")
    @ResponseView("home/index")
    @AfterAction(this.renderAssets)
    public async indexAction(request: Express.Request): Promise<Object> {

        const members = await this.memberService.findAll();
        
        return {
            title: 'blog | 博客',
            content: "This is content",
            members
        };
    }

    private renderAssets = (result: any,
                         request?: Express.Request,
                         response?: Express.Response,
                         next?: Function)  => {

        const assets = {};
        result['assets'] = assets;
        return result;
    }
}

Isn't necessary to give a name function with typescript :D . Static method work too ^^

I think @ResponseView and #10 response formatting are the same actions like AfterAction (according to @alexproca).

@vincent178
Copy link
Contributor Author

ok, I will try my best to work on this.

@Romakita Romakita added this to the TsExpressDecorators v1.4.0 milestone Jan 15, 2017
@Romakita Romakita changed the title Add BeforeAction and AfterAction decorators and hooks [Decorator] Add BeforeAction and AfterAction decorators and hooks Jan 15, 2017
@Romakita Romakita self-assigned this Jan 25, 2017
@Romakita Romakita changed the title [Decorator] Add BeforeAction and AfterAction decorators and hooks [Decorator] UseBefore and UserAfter hooks Jan 26, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants