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
    })
)

Groupped Guard

You can create group with a guard scope by adding a guard api in the second parameter, instead of nesting group and guard together.

ts
// Instead of this
app.group('/v1', (app) =>
    app.guard(
        {
            body: t.Literal()
        },
        (app) => app.get('/student', () => 'Rikuhachima Aru')
    )
)

// Do this
app.group(
    '/v1', {
        body: t.Literal('Rikuhachima Aru')
    }, 
    app => app.get('/student', () => 'Rikuhachima Aru')
)
// Instead of this
app.group('/v1', (app) =>
    app.guard(
        {
            body: t.Literal()
        },
        (app) => app.get('/student', () => 'Rikuhachima Aru')
    )
)

// Do this
app.group(
    '/v1', {
        body: t.Literal('Rikuhachima Aru')
    }, 
    app => app.get('/student', () => 'Rikuhachima Aru')
)