Skip to content

Conversation

@endel
Copy link

@endel endel commented Oct 26, 2025

Hi there,

I'm starting to use better-call for http routes on upcoming version of Colyseus and felt the need for dynamically adding routes to an existing Router.

Proposed API:

const router = createRouter({/* initial routes */});

// dynamically adding new routes
router.addEndpoint(createEndpoint("/dynamic_added", { method: "POST", }, async (c) => {
    return { message: "dynamically added", body: c.body };
}));

I'm happy to update this PR if you have any feedback. Let me know what you think.

All the best!

@Bekacru
Copy link
Collaborator

Bekacru commented Oct 26, 2025

If you can make the type inference work, this is good idea

@endel
Copy link
Author

endel commented Oct 26, 2025

Hi @Bekacru, thanks for your feedback. In order to keep TypeScript type inference, we need to return a new router instead of exposing a public addEndpoint() method.

I've updated the PR to expose an .extend(newEndpoints) instead, so the API now looks like this:

// initial endpoints
const initialEndpoint = createEndpoint("/initial", { method: "GET" }, async () => ({}));
const router = createRouter({ initialEndpoint });

// extending with more endpoints
const dynamicEndpoint = createEndpoint("/dynamic_added", { method: "POST", }, async (c) => {
	return { message: "dynamically added", body: c.body };
});
const extendedRouter = router.extend({ dynamicEndpoint });

// type inference is kept this way
extendedRouter.initialEndpoint./* (...) OK*/
extendedRouter.dynamicEndpoint./* (...) OK*/

Let me know if that works for you, or if you have further feedback :)

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

Successfully merging this pull request may close these issues.

2 participants