elysiajs / elysia

Ergonomic Framework for Humans

Home Page:https://elysiajs.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support for route meta

ayZagen opened this issue · comments

What is the problem this feature would solve?

Currently for route specific actions we have to execute them per route.

What is the feature you are proposing to solve the problem?

With the support of this feature we could assign constants to a route and use them in a centralized way.

For example, instead of this

new Elysia()
    .get('/posts', () => 'hi', {
        beforeHandle({ set, headers }) {
            if (!validateToken(headers.authorization, 'read:posts'))
                return (set.status = 'Unauthorized')
        }
    })
// other routes
    .listen(3000)

we could handle it in globalHandler:

new Elysia()
    .onBeforeHandle({ set, headers, meta } => {
       if (!validateToken(headers.authorization, meta.auth))
         return (set.status = 'Unauthorized')
     })
    .get('/posts', () => 'hi', { meta: { auth: 'read:posts'} } )
// other routes
    .listen(3000)

EDIT: Closing in favor of macro feature

What is the problem this feature would solve?

Currently for route specific actions we have to execute them per route.

What is the feature you are proposing to solve the problem?

With the support of this feature we could assign constants to a route and use them in a centralized way.

For example, instead of this

new Elysia()
    .get('/posts', () => 'hi', {
        beforeHandle({ set, headers }) {
            if (!validateToken(headers.authorization, 'read:posts'))
                return (set.status = 'Unauthorized')
        }
    })
// other routes
    .listen(3000)

we could handle it in globalHandler:

new Elysia()
    .onBeforeHandle({ set, headers, meta } => {
       if (!validateToken(headers.authorization, meta.auth))
         return (set.status = 'Unauthorized')
     })
    .get('/posts', () => 'hi', { meta: { auth: 'read:posts'} } )
// other routes
    .listen(3000)

What alternatives have you considered?

No response

Just use macro api