elysiajs / elysia

Ergonomic Framework for Humans
https://elysiajs.com
MIT License
9.23k stars 198 forks source link

add returning new instance in group & guard #617

Open PandaWorker opened 3 months ago

PandaWorker commented 3 months ago

What is the problem this feature would solve?

I don't want to use a lot of nesting add returning new instance in group & guard if not callback run param

I want it to work like this

import { Elysia, t } from 'elysia';
import { swagger } from '@elysiajs/swagger';

// default scope (guard return new instance)
const router = new Elysia().guard({
    type: 'application/json',
    detail: {
        tags: ['Public'],
    },
    headers: t.Object({
        'x-device-id': t.String(),
    }),
    cookie: t.Cookie({ at: t.String() }),
    response: {
        401: t.Object({
            code: t.String(),
        }),
    },
});

// links scope (group return new instance )
const links = router
    .group('/links', {
        beforeHandle: [
            (r) => {
                console.log(r);
            },
        ],
    })
    .get('/', () => '');

const catalog = router
    .group('/catalog')
    .get('/', () => [])
    .get('/:productId', () => ({}));

const publicApi = new Elysia()
    .use(links)
    .use(catalog)

const app = new Elysia({ prefix: '/api' }).use(setup).use(publicApi);

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

This solution helps to use a more flexible composition

What alternatives have you considered?

No response

apescione commented 2 months ago

This feature is very useful. Any Update on this?