[WIKI-679] refactor: live server (#7720)

This commit is contained in:
sriram veeraghanta 2025-09-30 19:28:15 +05:30 committed by GitHub
parent 7ce21a6488
commit 5951372555
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
48 changed files with 1690 additions and 1144 deletions

View file

@ -13,7 +13,7 @@
"scripts": {
"build": "tsdown",
"dev": "tsdown --watch",
"check:lint": "eslint . --max-warnings 1",
"check:lint": "eslint . --max-warnings 2",
"check:types": "tsc --noEmit",
"check:format": "prettier --check \"**/*.{ts,tsx,md,json,css,scss}\"",
"fix:lint": "eslint . --fix",

View file

@ -3,46 +3,48 @@ import type { WebSocket } from "ws";
import "reflect-metadata";
type HttpMethod = "get" | "post" | "put" | "delete" | "patch" | "options" | "head" | "ws";
export type HttpMethod = "get" | "post" | "put" | "delete" | "patch" | "options" | "head" | "ws";
interface ControllerInstance {
[key: string]: unknown;
}
type ControllerInstance = {
[key: string]: any;
};
interface ControllerConstructor {
new (...args: unknown[]): ControllerInstance;
export type ControllerConstructor = {
new (...args: any[]): ControllerInstance;
prototype: ControllerInstance;
}
};
export function registerControllers(
export function registerController(
router: Router,
controllers: ControllerConstructor[],
dependencies: any[] = []
Controller: ControllerConstructor,
dependencies: unknown[] = []
): void {
controllers.forEach((Controller) => {
// Create the controller instance with dependencies
const instance = new Controller(...dependencies);
// Create the controller instance with dependencies
const instance = new Controller(...dependencies);
// Determine if it's a WebSocket controller or REST controller by checking
// if it has any methods with the "ws" method metadata
const isWebsocket = Object.getOwnPropertyNames(Controller.prototype).some((methodName) => {
if (methodName === "constructor") return false;
return Reflect.getMetadata("method", instance, methodName) === "ws";
});
if (isWebsocket) {
// Register as WebSocket controller
// Pass the existing instance with dependencies to avoid creating a new instance without them
registerWebSocketController(router, Controller, instance);
} else {
// Register as REST controller - doesn't accept an instance parameter
registerRestController(router, Controller);
}
// Determine if it's a WebSocket controller or REST controller by checking
// if it has any methods with the "ws" method metadata
const isWebsocket = Object.getOwnPropertyNames(Controller.prototype).some((methodName) => {
if (methodName === "constructor") return false;
return Reflect.getMetadata("method", instance, methodName) === "ws";
});
if (isWebsocket) {
// Register as WebSocket controller
// Pass the existing instance with dependencies to avoid creating a new instance without them
registerWebSocketController(router, Controller, instance);
} else {
// Register as REST controller with the existing instance
registerRestController(router, Controller, instance);
}
}
function registerRestController(router: Router, Controller: ControllerConstructor): void {
const instance = new Controller();
function registerRestController(
router: Router,
Controller: ControllerConstructor,
existingInstance?: ControllerInstance
): void {
const instance = existingInstance || new Controller();
const baseRoute = Reflect.getMetadata("baseRoute", Controller) as string;
Object.getOwnPropertyNames(Controller.prototype).forEach((methodName) => {

View file

@ -1,13 +1,3 @@
// Export individual decorators
export { Controller, Middleware } from "./rest";
export { Get, Post, Put, Patch, Delete } from "./rest";
export { WebSocket } from "./websocket";
export { registerControllers } from "./controller";
// Also provide namespaced exports for better organization
import * as RestDecorators from "./rest";
import * as WebSocketDecorators from "./websocket";
// Named namespace exports
export const Rest = RestDecorators;
export const WebSocketNS = WebSocketDecorators;
export * from "./controller";
export * from "./rest";
export * from "./websocket";