You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Long and complex sentences, making it harder to read.
Inconsistent terminology (e.g., "ElysiaJs" vs. "Elysia").
Unclear explanations, especially around method chaining and controllers.
Inconsistent code formatting, which could confuse developers.
Key Improvements
Grammar & Clarity
Correct grammatical errors for better readability.
Improve sentence structure for clearer explanations.
Consistency
Use a uniform writing style (e.g., consistently referring to "ElysiaJs" or "Elysia").
Standardize phrasing (e.g., "should" vs. "recommended").
Code Readability
Maintain consistent formatting and indentation.
Add concise comments where necessary for clarity.
Improved Version
Best Practices
Elysia is a pattern-agnostic framework, allowing developers to choose the coding patterns that best suit their needs.
However, implementing the MVC (Model-View-Controller) pattern in Elysia can be challenging due to difficulties in decoupling logic and handling types effectively.
This guide outlines best practices for structuring an Elysia application while maintaining flexibility in adopting other coding styles.
Method Chaining
Elysia code should always follow method chaining to maintain type integrity.
Since Elysia’s type system is dynamic, each method returns a new type reference. Using method chaining ensures proper type inference, preventing type-related issues.
✅ Recommended Approach (Method Chaining):
import{Elysia}from'elysia'newElysia().state('build',1)// The store is strictly typed.get('/',({store: { build }})=>build).listen(3000)
Here, .state('build', 1) modifies the ElysiaInstance type, ensuring build is correctly inferred.
❌ Avoid This (No Method Chaining):
import{Elysia}from'elysia'constapp=newElysia()app.state('build',1)// Type information is lostapp.get('/',({store: { build }})=>build)// ❌ Property 'build' does not existapp.listen(3000)
Why?
Without method chaining, Elysia does not retain new types, leading to incorrect type inference.
Controllers
One Elysia instance should act as one controller.
Passing an entire Context object to a separate controller can cause issues:
Increased complexity: Elysia’s types are dynamic and change based on plugins and method chaining.
Typing difficulties: Elysia’s type system evolves with decorators and state.
Inconsistent type casting: Manually casting types can break type consistency between definitions and runtime behavior.
❌ Avoid Using a Separate Controller Class:
import{Elysia,typeContext}from'elysia'abstractclassController{staticroot(context: Context){returnService.doStuff(context.stuff)}}// ❌ This adds unnecessary complexitynewElysia().get('/',Controller.root)
Why?
Using a full controller method introduces an extra controller layer, which complicates type inference.
✅ Recommended Approach (Treat Elysia as the Controller):
This approach maintains type safety, simplicity, and clarity.
Services
A service in Elysia is a set of utility/helper functions that separate business logic from the main controller.
Elysia supports two types of services:
Non-request dependent services – Functions that do not require request properties.
Request-dependent services – Functions that rely on request-specific data.
✅ Example of a Non-Request Dependent Service:
import{Elysia}from'elysia'abstractclassService{staticfibo(n: number): number{returnn<2 ? n : Service.fibo(n-1)+Service.fibo(n-2)}}newElysia().get('/fibo',({ body })=>Service.fibo(body),{body: t.Numeric()})
If a service does not need stored properties, using an abstract class with static methods prevents unnecessary instantiation.
Where did you find it?
These issues were found in the Elysia documentation, specifically in sections related to:
Best Practices
Method Chaining
Controllers
Services
The text was updated successfully, but these errors were encountered:
What is the type of issue?
Documentation Improvement
What is the issue?
Issue Identified
The original documentation contained:
Key Improvements
Grammar & Clarity
Consistency
Code Readability
Improved Version
Best Practices
Elysia is a pattern-agnostic framework, allowing developers to choose the coding patterns that best suit their needs.
However, implementing the MVC (Model-View-Controller) pattern in Elysia can be challenging due to difficulties in decoupling logic and handling types effectively.
This guide outlines best practices for structuring an Elysia application while maintaining flexibility in adopting other coding styles.
Method Chaining
Elysia code should always follow method chaining to maintain type integrity.
Since Elysia’s type system is dynamic, each method returns a new type reference. Using method chaining ensures proper type inference, preventing type-related issues.
✅ Recommended Approach (Method Chaining):
Here,
.state('build', 1)
modifies theElysiaInstance
type, ensuringbuild
is correctly inferred.❌ Avoid This (No Method Chaining):
Why?
Without method chaining, Elysia does not retain new types, leading to incorrect type inference.
Controllers
One Elysia instance should act as one controller.
Passing an entire
Context
object to a separate controller can cause issues:❌ Avoid Using a Separate Controller Class:
Why?
Using a full controller method introduces an extra controller layer, which complicates type inference.
✅ Recommended Approach (Treat Elysia as the Controller):
This approach maintains type safety, simplicity, and clarity.
Services
A service in Elysia is a set of utility/helper functions that separate business logic from the main controller.
Elysia supports two types of services:
✅ Example of a Non-Request Dependent Service:
If a service does not need stored properties, using an abstract class with static methods prevents unnecessary instantiation.
Where did you find it?
These issues were found in the Elysia documentation, specifically in sections related to:
The text was updated successfully, but these errors were encountered: