elysia icon indicating copy to clipboard operation
elysia copied to clipboard

add returning new instance in group & guard

Open PandaWorker opened this issue 10 months ago • 1 comments

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

PandaWorker avatar Apr 21 '24 10:04 PandaWorker

This feature is very useful. Any Update on this?

apescione avatar May 08 '24 14:05 apescione