Skip to content
On this page

Guard ‚Äč

Suppose you have many shared local hooks.

Instead of writing duplicated hook, you can define a shared hook scope using guard.


Guard let you to inject multiple life-cycle event into multiple routes at once. Guard is useful when you have duplicated life-cycle in the multiple route, for example. logging, schema validation, or error handling.

To encapsulate all hooks into the scope, instead of writing:

typescript
app.post('/sign-up', (({ body }) => signUp(body), {
    schema: {
        body: t.Object({
            username: t.String(),
            password: t.String()
        })
    }
})
.post('/sign-in', (({ body }) => signIn(body), {
    beforeHandle: isUserExists,
    schema: {
        body: t.Object({
            username: t.String(),
            password: t.String()
        })
    }
})
app.post('/sign-up', (({ body }) => signUp(body), {
    schema: {
        body: t.Object({
            username: t.String(),
            password: t.String()
        })
    }
})
.post('/sign-in', (({ body }) => signIn(body), {
    beforeHandle: isUserExists,
    schema: {
        body: t.Object({
            username: t.String(),
            password: t.String()
        })
    }
})

You can group hook into guard:

typescript
app.guard({
    schema: {
        body: t.Object({
            username: t.String(),
            password: t.String()
        })
    }
}, app => app
    .post('/sign-up', ({ body }) => signUp(body))
    .post('/sign-in', ({ body }) => signIn(body), {
         beforeHandle: isUserExists
    })
)
app.guard({
    schema: {
        body: t.Object({
            username: t.String(),
            password: t.String()
        })
    }
}, app => app
    .post('/sign-up', ({ body }) => signUp(body))
    .post('/sign-in', ({ body }) => signIn(body), {
         beforeHandle: isUserExists
    })
)