a library to speed up the development of Fivem scripts and frameworks
fivem-ts - Documentation v0.7.5 • Docs
fivem-ts - Documentation v0.7.5 / Server / DIContainer
A simple Dependency Injection (DI) container for managing dependency injection.
This class allows for the registration and resolution of singletons, transients, and factory-based dependencies. It uses TypeScript decorators and metadata reflection to handle dependency injection.
Example usage:
// Define a service
class MyService {
constructor(private dependency: AnotherService) {}
}
// Register the service
container.registerSingleton(MyService);
// Resolve the service
const myService = container.resolve(MyService);
new DIContainer():
DIContainer
clearRequestScope():
void
Clears the request-scoped instances.
This should be called at the end of a request to clean up the container.
void
container.clearRequestScope();
clearSessionScope(
sessionId
):void
Clears the session-scoped instances for a given session ID.
This should be called at the end of a session to clean up the container.
• sessionId: string
The ID of the session to clear.
void
container.clearSessionScope('sessionId123');
handleHttpRequest(
request
,response
):void
Handles an HTTP request by matching it against the registered routes.
• request: HttpRequest
The HTTP request object.
• response: HttpResponse
The HTTP response object.
void
registerFactory<
T
>(constructor
,factoryFn
):void
Registers a factory function to create an instance of the class.
The factory function will be called to produce the instance.
• T
• constructor
The constructor function of the class to be registered.
• factoryFn
The factory function to create an instance of the class.
void
container.registerFactory(MyService, () => new MyService(new Dependency()));
registerRequestScoped<
T
>(constructor
):void
Registers a class as request-scoped in the container.
A request-scoped instance will be created and shared throughout a single request, and then discarded.
• T
• constructor
The constructor function of the class to be registered.
void
container.registerRequestScoped(MyRequestService);
registerRoute(
method
,path
,handler
):void
Registers a route in the container. This route will handle HTTP requests that match the method and path provided.
• method: string
The HTTP method (GET, POST, etc.).
• path: string
The path of the route.
• handler: Function
The function that handles the route.
void
registerSessionScoped<
T
>(constructor
):void
Registers a class as session-scoped in the container.
A session-scoped instance will be created and shared throughout a user’s session, and then discarded when the session ends.
• T
• constructor
The constructor function of the class to be registered.
void
container.registerSessionScoped(MySessionService);
registerSingleton<
T
>(constructor
):void
Registers a class as a singleton in the container.
A singleton instance will be created once and reused for all future requests.
• T
• constructor
The constructor function of the class to be registered.
void
container.registerSingleton(MyService);
registerTransient<
T
>(constructor
):void
Registers a class as a transient in the container.
A transient instance will be created every time it is resolved.
• T
• constructor
The constructor function of the class to be registered.
void
container.registerTransient(MyTransientService);
resolve<
T
>(constructor
,request
?,sessionId
?):T
Resolves an instance of the specified class.
This method creates an instance of the class and injects its dependencies. If a request or session context is provided, it will resolve accordingly.
• T
• constructor
The constructor function of the class to be resolved.
• request?: unknown
An optional request context for request-scoped dependencies.
• sessionId?: string
An optional session ID for session-scoped dependencies.
T
An instance of the specified class.
const myService = container.resolve(MyService);