Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type definition for .group params #42

Closed
aimenhamed opened this issue May 14, 2023 · 2 comments
Closed

Type definition for .group params #42

aimenhamed opened this issue May 14, 2023 · 2 comments

Comments

@aimenhamed
Copy link

Hello, really like the framework btw :)

I was wondering if the params for the .group function can be typed so that I can abstract it in a class ?

I want to create a group wrapper class e.g.

interface Group {
  getPrefix: () => string;
  getGroup: () => ElysiaGroup;
}

so that I can do something like:

const group = new Group();
app.group(group.getPrefix(), group.getGroup());

would really appreciate this!!!

Thanks!

@SaltyAom
Copy link
Member

Yes, you can do something like this:

class Group {
	constructor(
		public prefix: string,
		public group: (app: Elysia) => Elysia<any>
	) {}

	getPrefix() {
		return this.prefix
	}

	getGroup() {
		return this.group
	}
}

const group = new Group('/games', (app) => app.get('/blue-archive', () => '😭'))

const app = new Elysia()
	.group(group.getPrefix(), group.getGroup())
	.listen(3000)

However, I recommend inlining variables into the method would have a better type-inference.

May I ask why do you want to separate it into a separate class so I can improve or introduce new features in your regard in the future?

@aimenhamed
Copy link
Author

hey, thanks that works! yeah wanted to avoid the anys for the type inference, but don't think this will be an issue.
basically trying to mimic controller service architecture for my app similar to spring boot.

where my routes are defined at the controller level (group), and then business logic in services.
I use a wrapper class around the group to represent the controller.

hope it makes sense :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants