topsyde-utils 1.0.206 → 1.0.208

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,63 @@
1
+ import { AxiosResponse } from "axios";
2
+ declare class BaseAPI {
3
+ private api;
4
+ protected controller: string;
5
+ protected base_url: string;
6
+ constructor(controller: string, base_url?: string);
7
+ private initAxios;
8
+ static Status(response: AxiosResponse<{
9
+ status: boolean;
10
+ }>): void;
11
+ /**
12
+ * @description Axios GET request wrapper
13
+ * @param endpoint string
14
+ * @generic T
15
+ * @returns Promise<AxiosResponse<T>>
16
+ */
17
+ get<T extends {
18
+ status: boolean;
19
+ }>(endpoint: string): Promise<AxiosResponse<T>>;
20
+ /**
21
+ * @description Axios GET request wrapper
22
+ * @param controller string - which controller to use
23
+ * @param endpoint string - controller action to call
24
+ * @generic T
25
+ * @returns Promise<AxiosResponse<T>>
26
+ */
27
+ get<T extends {
28
+ status: boolean;
29
+ }>(controller: string, endpoint: string): Promise<AxiosResponse<T>>;
30
+ /**
31
+ * @description Axios GET request wrapper
32
+ * @param controller string - which controller to use
33
+ * @param endpoint string - controller action to call
34
+ * @param debounceDelay number - debounce delay
35
+ * @generic T
36
+ * @returns Promise<AxiosResponse<T>>
37
+ */
38
+ get<T extends {
39
+ status: boolean;
40
+ }>(controller: string, endpoint: string, debounceDelay?: number): Promise<AxiosResponse<T>>;
41
+ /**
42
+ * @description Axios POST request wrapper
43
+ * @param endpoint string - controller action to call
44
+ * @param payload any
45
+ * @generic T
46
+ * @returns Promise<AxiosResponse<T>>
47
+ */
48
+ post<T extends {
49
+ status: boolean;
50
+ }>(endpoint: string, payload: any): Promise<AxiosResponse<T>>;
51
+ /**
52
+ * @description Axios POST request wrapper
53
+ * @param controller string - which controller to use
54
+ * @param endpoint string - controller action to call
55
+ * @param payload any
56
+ * @generic T
57
+ * @returns Promise<AxiosResponse<T>>
58
+ */
59
+ post<T extends {
60
+ status: boolean;
61
+ }>(controller: string, endpoint: string, payload: any): Promise<AxiosResponse<T>>;
62
+ }
63
+ export default BaseAPI;
@@ -0,0 +1,61 @@
1
+ import Axios, { isAxiosError } from "axios";
2
+ class BaseAPI {
3
+ constructor(controller, base_url) {
4
+ this.controller = controller;
5
+ this.base_url = base_url || import.meta.env.VITE_HOST || "";
6
+ this.api = this.initAxios();
7
+ }
8
+ initAxios() {
9
+ const config = {
10
+ headers: {
11
+ "Content-Type": "application/json",
12
+ },
13
+ };
14
+ const instance = Axios.create(config);
15
+ // Add interceptor to modify request config
16
+ instance.interceptors.request.use((config) => {
17
+ // Ensure the correct content type
18
+ config.headers["Content-Type"] = "application/json";
19
+ return config;
20
+ });
21
+ return instance;
22
+ }
23
+ static Status(response) {
24
+ if (!response.data.status)
25
+ throw new Error("Something went wrong");
26
+ }
27
+ async get(controllerOrEndpoint, endpoint, _debounceDelay) {
28
+ let controller = endpoint ? controllerOrEndpoint : this.controller;
29
+ let action = endpoint ? endpoint : controllerOrEndpoint;
30
+ return this.api.get(`${this.base_url}/${controller}/${action}`).catch((err) => {
31
+ if (isAxiosError(err))
32
+ throw err.response;
33
+ throw err;
34
+ });
35
+ }
36
+ async post(controllerOrEndpoint, endpointOrPayload, payload) {
37
+ let controller;
38
+ let action;
39
+ let data;
40
+ if (payload !== null && payload !== undefined) {
41
+ controller = controllerOrEndpoint;
42
+ action = endpointOrPayload;
43
+ data = payload;
44
+ }
45
+ else {
46
+ controller = this.controller;
47
+ action = controllerOrEndpoint;
48
+ data = endpointOrPayload;
49
+ }
50
+ const response = await this.api.post(`${this.base_url}/${controller}/${action}`, data).catch((err) => {
51
+ if (isAxiosError(err))
52
+ throw err.response;
53
+ throw err;
54
+ });
55
+ if (!response.data.status)
56
+ throw new Error("Something went wrong");
57
+ return response;
58
+ }
59
+ }
60
+ export default BaseAPI;
61
+ //# sourceMappingURL=base.api.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"base.api.js","sourceRoot":"","sources":["../../../src/client/api/base.api.ts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API {\n\tprivate api: AxiosInstance;\n\tprotected controller: string;\n\tprotected base_url: string;\n\n\tconstructor(controller: string, base_url?: string) {\n\t\tthis.controller = controller;\n\t\tthis.base_url = base_url || import.meta.env.VITE_HOST || \"\";\n\t\tthis.api = this.initAxios();\n\t}\n\n\tprivate initAxios(): AxiosInstance {\n\t\tconst config: AxiosRequestConfig = {\n\t\t\theaders: {\n\t\t\t\t\"Content-Type\": \"application/json\",\n\t\t\t},\n\t\t};\n\n\t\tconst instance = Axios.create(config);\n\n\t\t// Add interceptor to modify request config\n\t\tinstance.interceptors.request.use((config) => {\n\t\t\t// Ensure the correct content type\n\t\t\tconfig.headers[\"Content-Type\"] = \"application/json\";\n\n\t\t\treturn config;\n\t\t});\n\n\t\treturn instance;\n\t}\n\n\tpublic static Status(response: AxiosResponse<{ status: boolean }>) {\n\t\tif (!response.data.status) throw new Error(\"Something went wrong\");\n\t}\n\n\t/**\n\t * @description Axios GET request wrapper\n\t * @param endpoint string\n\t * @generic T\n\t * @returns Promise<AxiosResponse<T>>\n\t */\n\tasync get<T extends { status: boolean }>(endpoint: string): Promise<AxiosResponse<T>>;\n\t/**\n\t * @description Axios GET request wrapper\n\t * @param controller string - which controller to use\n\t * @param endpoint string - controller action to call\n\t * @generic T\n\t * @returns Promise<AxiosResponse<T>>\n\t */\n\tasync get<T extends { status: boolean }>(controller: string, endpoint: string): Promise<AxiosResponse<T>>;\n\t/**\n\t * @description Axios GET request wrapper\n\t * @param controller string - which controller to use\n\t * @param endpoint string - controller action to call\n\t * @param debounceDelay number - debounce delay\n\t * @generic T\n\t * @returns Promise<AxiosResponse<T>>\n\t */\n\tasync get<T extends { status: boolean }>(controller: string, endpoint: string, debounceDelay?: number): Promise<AxiosResponse<T>>;\n\tasync get<T extends { status: boolean }>(controllerOrEndpoint: string, endpoint?: string | undefined, _debounceDelay?: number): Promise<AxiosResponse<T>> {\n\t\tlet controller = endpoint ? controllerOrEndpoint : this.controller;\n\t\tlet action = endpoint ? endpoint : controllerOrEndpoint;\n\n\t\treturn this.api.get<T>(`${this.base_url}/${controller}/${action}`).catch((err) => {\n\t\t\tif (isAxiosError(err)) throw err.response;\n\t\t\tthrow err;\n\t\t});\n\t}\n\n\t/**\n\t * @description Axios POST request wrapper\n\t * @param endpoint string - controller action to call\n\t * @param payload any\n\t * @generic T\n\t * @returns Promise<AxiosResponse<T>>\n\t */\n\tasync post<T extends { status: boolean }>(endpoint: string, payload: any): Promise<AxiosResponse<T>>;\n\t/**\n\t * @description Axios POST request wrapper\n\t * @param controller string - which controller to use\n\t * @param endpoint string - controller action to call\n\t * @param payload any\n\t * @generic T\n\t * @returns Promise<AxiosResponse<T>>\n\t */\n\tasync post<T extends { status: boolean }>(controller: string, endpoint: string, payload: any): Promise<AxiosResponse<T>>;\n\tasync post<T extends { status: boolean }>(controllerOrEndpoint: string, endpointOrPayload: string | any, payload?: any): Promise<AxiosResponse<T>> {\n\t\tlet controller: string;\n\t\tlet action: string;\n\t\tlet data: any;\n\n\t\tif (payload !== null && payload !== undefined) {\n\t\t\tcontroller = controllerOrEndpoint;\n\t\t\taction = endpointOrPayload;\n\t\t\tdata = payload;\n\t\t} else {\n\t\t\tcontroller = this.controller;\n\t\t\taction = controllerOrEndpoint;\n\t\t\tdata = endpointOrPayload;\n\t\t}\n\t\tconst response = await this.api.post<T>(`${this.base_url}/${controller}/${action}`, data).catch((err) => {\n\t\t\tif (isAxiosError(err)) throw err.response;\n\t\t\tthrow err;\n\t\t});\n\t\tif (!response.data.status) throw new Error(\"Something went wrong\");\n\t\treturn response;\n\t}\n}\n\nexport default BaseAPI;\n"]}
@@ -0,0 +1,2 @@
1
+ export * from './base.api';
2
+ export { default as BaseAPI } from './base.api';
@@ -0,0 +1,5 @@
1
+ // This file is auto-generated by scripts/generate-indexes.ts
2
+ // Do not edit this file directly
3
+ export * from './base.api.js';
4
+ export { default as BaseAPI } from './base.api.js';
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/client/api/index.ts"],"names":[],"mappings":"AAAA,6DAA6D;AAC7D,iCAAiC;AAEjC,cAAc,YAAY,CAAC;AAC3B,OAAO,EAAE,OAAO,IAAI,OAAO,EAAE,MAAM,YAAY,CAAC","sourcesContent":["// This file is auto-generated by scripts/generate-indexes.ts\n// Do not edit this file directly\n\nexport * from './base.api';\nexport { default as BaseAPI } from './base.api';\n"]}
package/dist/index.d.ts CHANGED
@@ -6,6 +6,7 @@ export * from "./consts";
6
6
  export * from "./types";
7
7
  export * from "./throwable";
8
8
  export * from "./enums";
9
+ export * from "./client/api/base.api";
9
10
  export * from "./client/rxjs/useRxjs";
10
11
  export * from "./client/vite/plugins/topsydeUtilsVitePlugin";
11
12
  export * from "./server/controller";
@@ -33,13 +34,14 @@ export { default as Singleton } from "./singleton";
33
34
  export { default as Initializable } from "./initializable";
34
35
  export { default as Application } from "./application";
35
36
  export { default as Throwable } from "./throwable";
37
+ export { default as BaseAPI } from "./client/api/base.api";
36
38
  export { default as TopsydeUtilsVitePlugin } from "./client/vite/plugins/topsydeUtilsVitePlugin";
37
39
  export { default as Controller } from "./server/controller";
38
40
  export { default as Service } from "./server/service";
39
41
  export { default as Database } from "./server/base/base.database";
40
42
  export { default as Router } from "./server/bun/router/router";
41
43
  export { default as Router_Internal } from "./server/bun/router/router.internal";
42
- export { default as GameWebsocket } from "./server/bun/websocket/Websocket";
44
+ export { default as Websocket } from "./server/bun/websocket/Websocket";
43
45
  export { default as Message } from "./server/bun/websocket/Message";
44
46
  export { default as Channel } from "./server/bun/websocket/Channel";
45
47
  export { default as Client } from "./server/bun/websocket/Client";
package/dist/index.js CHANGED
@@ -9,6 +9,7 @@ export * from "./consts.js";
9
9
  export * from "./types.js";
10
10
  export * from "./throwable.js";
11
11
  export * from "./enums.js";
12
+ export * from "./client/api/base.api.js";
12
13
  export * from "./client/rxjs/useRxjs.js";
13
14
  export * from "./client/vite/plugins/topsydeUtilsVitePlugin.js";
14
15
  export * from "./server/controller.js";
@@ -37,13 +38,14 @@ export { default as Singleton } from "./singleton.js";
37
38
  export { default as Initializable } from "./initializable.js";
38
39
  export { default as Application } from "./application.js";
39
40
  export { default as Throwable } from "./throwable.js";
41
+ export { default as BaseAPI } from "./client/api/base.api.js";
40
42
  export { default as TopsydeUtilsVitePlugin } from "./client/vite/plugins/topsydeUtilsVitePlugin.js";
41
43
  export { default as Controller } from "./server/controller.js";
42
44
  export { default as Service } from "./server/service.js";
43
45
  export { default as Database } from "./server/base/base.database.js";
44
46
  export { default as Router } from "./server/bun/router/router.js";
45
47
  export { default as Router_Internal } from "./server/bun/router/router.internal.js";
46
- export { default as GameWebsocket } from "./server/bun/websocket/Websocket.js";
48
+ export { default as Websocket } from "./server/bun/websocket/Websocket.js";
47
49
  export { default as Message } from "./server/bun/websocket/Message.js";
48
50
  export { default as Channel } from "./server/bun/websocket/Channel.js";
49
51
  export { default as Client } from "./server/bun/websocket/Client.js";
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,6DAA6D;AAC7D,iCAAiC;AAEjC,qBAAqB;AACrB,cAAc,UAAU,CAAC;AACzB,cAAc,aAAa,CAAC;AAC5B,cAAc,iBAAiB,CAAC;AAChC,cAAc,eAAe,CAAC;AAC9B,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC;AACxB,cAAc,aAAa,CAAC;AAC5B,cAAc,SAAS,CAAC;AACxB,cAAc,uBAAuB,CAAC;AACtC,cAAc,8CAA8C,CAAC;AAC7D,cAAc,qBAAqB,CAAC;AACpC,cAAc,kBAAkB,CAAC;AACjC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,0CAA0C,CAAC;AACzD,cAAc,4BAA4B,CAAC;AAC3C,cAAc,4BAA4B,CAAC;AAC3C,cAAc,qCAAqC,CAAC;AACpD,cAAc,kCAAkC,CAAC;AACjD,cAAc,yCAAyC,CAAC;AACx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,IAAI,GAAG,EAAE,MAAM,aAAa,CAAC;AAC7C,OAAO,EAAE,OAAO,IAAI,MAAM,EAAE,MAAM,gBAAgB,CAAC;AACnD,OAAO,EAAE,OAAO,IAAI,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAC3D,OAAO,EAAE,OAAO,IAAI,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAErD,sDAAsD;AACtD,OAAO,EAAE,UAAU,EAAE,eAAe,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAEtE,OAAO,EAAE,aAAa,EAAE,YAAY,EAAE,uBAAuB,EAAE,MAAM,eAAe,CAAC;AACrF,OAAO,EAAE,sBAAsB,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AAEzE,OAAO,EAAE,IAAI,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAK/C,OAAO,EAAE,sBAAsB,EAAE,0BAA0B,EAAE,aAAa,EAAE,MAAM,wCAAwC,CAAC;AAqB3H,OAAO,EAAE,aAAa,EAAiC,YAAY,EAAE,eAAe,EAAE,aAAa,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC","sourcesContent":["// This file is auto-generated by scripts/generate-indexes.ts\n// Do not edit this file directly\n\n// Export all modules\nexport * from \"./errors\";\nexport * from \"./singleton\";\nexport * from \"./initializable\";\nexport * from \"./application\";\nexport * from \"./consts\";\nexport * from \"./types\";\nexport * from \"./throwable\";\nexport * from \"./enums\";\nexport * from \"./client/rxjs/useRxjs\";\nexport * from \"./client/vite/plugins/topsydeUtilsVitePlugin\";\nexport * from \"./server/controller\";\nexport * from \"./server/service\";\nexport * from \"./server/base/base.database\";\nexport * from \"./server/bun/router/controller-discovery\";\nexport * from \"./server/bun/router/routes\";\nexport * from \"./server/bun/router/router\";\nexport * from \"./server/bun/router/router.internal\";\nexport * from \"./server/bun/websocket/Websocket\";\nexport * from \"./server/bun/websocket/websocket.guards\";\nexport * from \"./server/bun/websocket/Message\";\nexport * from \"./server/bun/websocket/Channel\";\nexport * from \"./server/bun/websocket/Client\";\nexport * from \"./server/bun/websocket/websocket.enums\";\nexport * from \"./server/bun/websocket/websocket.types\";\nexport * from \"./utils/Rxjs\";\nexport * from \"./utils/Lib\";\nexport * from \"./utils/Guards\";\nexport * from \"./utils/BaseEntity\";\nexport * from \"./utils/Console\";\nexport * from \"./utils/BaseDto\";\nexport * from \"./utils/dto_validators/IsNumberOrRangeConstraint\";\n\n// Export default classes\nexport { default as Singleton } from \"./singleton\";\nexport { default as Initializable } from \"./initializable\";\nexport { default as Application } from \"./application\";\nexport { default as Throwable } from \"./throwable\";\nexport { default as TopsydeUtilsVitePlugin } from \"./client/vite/plugins/topsydeUtilsVitePlugin\";\nexport { default as Controller } from \"./server/controller\";\nexport { default as Service } from \"./server/service\";\nexport { default as Database } from \"./server/base/base.database\";\nexport { default as Router } from \"./server/bun/router/router\";\nexport { default as Router_Internal } from \"./server/bun/router/router.internal\";\nexport { default as GameWebsocket } from \"./server/bun/websocket/Websocket\";\nexport { default as Message } from \"./server/bun/websocket/Message\";\nexport { default as Channel } from \"./server/bun/websocket/Channel\";\nexport { default as Client } from \"./server/bun/websocket/Client\";\nexport { default as Lib } from \"./utils/Lib\";\nexport { default as Guards } from \"./utils/Guards\";\nexport { default as BaseEntity } from \"./utils/BaseEntity\";\nexport { default as Console } from \"./utils/Console\";\n\n// Re-export specific items for backward compatibility\nexport { ERROR_CODE, HTTP_ERROR_CODE, WS_ERROR_CODE } from \"./errors\";\nexport { InitializableOptions, InitializableEvent } from \"./initializable\";\nexport { RESPONSE_INIT, HEADERS_INIT, RESPONSE_METHOD_OPTIONS } from \"./application\";\nexport { DEFAULT_FALSE_RESPONSE, LOG_COLORS, LOG_ICONS } from \"./consts\";\nexport { ClassConstructor, NonNullableType, ObjectKeys, KVObj, I_ApplicationResponse } from \"./types\";\nexport { E_IS, E_ENVIRONMENTS } from \"./enums\";\nexport { RxjsDataType, NamespaceActions, MultiNamespaceActions } from \"./client/rxjs/useRxjs\";\nexport { ControllerResponse, ControllerAction, ControllerMap, ControllerOptions } from \"./server/controller\";\nexport { Routes } from \"./server/bun/router/routes\";\nexport { WebsocketConstructorOptions, I_WebsocketConstructor } from \"./server/bun/websocket/Websocket\";\nexport { E_WebsocketMessageType, E_WebsocketMessagePriority, E_ClientState } from \"./server/bun/websocket/websocket.enums\";\nexport {\n\tBunWebsocketMessage,\n\tWebsocketChannel,\n\tWebsocketClients,\n\tWebsocketMessageOptions,\n\tWebsocketMessage,\n\tWebsocketStructuredMessage,\n\tWebsocketEntityId,\n\tWebsocketEntityName,\n\tWebsocketEntityData,\n\tI_WebsocketEntity,\n\tI_WebsocketClient,\n\tI_WebsocketChannelEntity,\n\tBroadcastOptions,\n\tAddMemberResult,\n\tAddMemberOptions,\n\tI_WebsocketChannel,\n\tWebsocketInterfaceHandlers,\n\tI_WebsocketInterface,\n} from \"./server/bun/websocket/websocket.types\";\nexport { E_SUBJET_TYPE, I_RxjsPayload, RxjsNamespaces, AsyncSubject, BehaviorSubject, ReplaySubject, Subject, Subscription } from \"./utils/Rxjs\";\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,6DAA6D;AAC7D,iCAAiC;AAEjC,qBAAqB;AACrB,cAAc,UAAU,CAAC;AACzB,cAAc,aAAa,CAAC;AAC5B,cAAc,iBAAiB,CAAC;AAChC,cAAc,eAAe,CAAC;AAC9B,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC;AACxB,cAAc,aAAa,CAAC;AAC5B,cAAc,SAAS,CAAC;AACxB,cAAc,uBAAuB,CAAC;AACtC,cAAc,uBAAuB,CAAC;AACtC,cAAc,8CAA8C,CAAC;AAC7D,cAAc,qBAAqB,CAAC;AACpC,cAAc,kBAAkB,CAAC;AACjC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,0CAA0C,CAAC;AACzD,cAAc,4BAA4B,CAAC;AAC3C,cAAc,4BAA4B,CAAC;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ACpE,OAAO,EAAE,OAAO,IAAI,OAAO,EAAE,MAAM,gCAAgC,CAAC;AACpE,OAAO,EAAE,OAAO,IAAI,MAAM,EAAE,MAAM,+BAA+B,CAAC;AAClE,OAAO,EAAE,OAAO,IAAI,GAAG,EAAE,MAAM,aAAa,CAAC;AAC7C,OAAO,EAAE,OAAO,IAAI,MAAM,EAAE,MAAM,gBAAgB,CAAC;AACnD,OAAO,EAAE,OAAO,IAAI,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAC3D,OAAO,EAAE,OAAO,IAAI,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAErD,sDAAsD;AACtD,OAAO,EAAE,UAAU,EAAE,eAAe,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAEtE,OAAO,EAAE,aAAa,EAAE,YAAY,EAAE,uBAAuB,EAAE,MAAM,eAAe,CAAC;AACrF,OAAO,EAAE,sBAAsB,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AAEzE,OAAO,EAAE,IAAI,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAK/C,OAAO,EAAE,sBAAsB,EAAE,0BAA0B,EAAE,aAAa,EAAE,MAAM,wCAAwC,CAAC;AAqB3H,OAAO,EAAE,aAAa,EAAiC,YAAY,EAAE,eAAe,EAAE,aAAa,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC","sourcesContent":["// This file is auto-generated by scripts/generate-indexes.ts\n// Do not edit this file directly\n\n// Export all modules\nexport * from \"./errors\";\nexport * from \"./singleton\";\nexport * from \"./initializable\";\nexport * from \"./application\";\nexport * from \"./consts\";\nexport * from \"./types\";\nexport * from \"./throwable\";\nexport * from \"./enums\";\nexport * from \"./client/api/base.api\";\nexport * from \"./client/rxjs/useRxjs\";\nexport * from \"./client/vite/plugins/topsydeUtilsVitePlugin\";\nexport * from \"./server/controller\";\nexport * from \"./server/service\";\nexport * from \"./server/base/base.database\";\nexport * from \"./server/bun/router/controller-discovery\";\nexport * from \"./server/bun/router/routes\";\nexport * from \"./server/bun/router/router\";\nexport * from \"./server/bun/router/router.internal\";\nexport * from \"./server/bun/websocket/Websocket\";\nexport * from \"./server/bun/websocket/websocket.guards\";\nexport * from \"./server/bun/websocket/Message\";\nexport * from \"./server/bun/websocket/Channel\";\nexport * from \"./server/bun/websocket/Client\";\nexport * from \"./server/bun/websocket/websocket.enums\";\nexport * from \"./server/bun/websocket/websocket.types\";\nexport * from \"./utils/Rxjs\";\nexport * from \"./utils/Lib\";\nexport * from \"./utils/Guards\";\nexport * from \"./utils/BaseEntity\";\nexport * from \"./utils/Console\";\nexport * from \"./utils/BaseDto\";\nexport * from \"./utils/dto_validators/IsNumberOrRangeConstraint\";\n\n// Export default classes\nexport { default as Singleton } from \"./singleton\";\nexport { default as Initializable } from \"./initializable\";\nexport { default as Application } from \"./application\";\nexport { default as Throwable } from \"./throwable\";\nexport { default as BaseAPI } from \"./client/api/base.api\";\nexport { default as TopsydeUtilsVitePlugin } from \"./client/vite/plugins/topsydeUtilsVitePlugin\";\nexport { default as Controller } from \"./server/controller\";\nexport { default as Service } from \"./server/service\";\nexport { default as Database } from \"./server/base/base.database\";\nexport { default as Router } from \"./server/bun/router/router\";\nexport { default as Router_Internal } from \"./server/bun/router/router.internal\";\nexport { default as Websocket } from \"./server/bun/websocket/Websocket\";\nexport { default as Message } from \"./server/bun/websocket/Message\";\nexport { default as Channel } from \"./server/bun/websocket/Channel\";\nexport { default as Client } from \"./server/bun/websocket/Client\";\nexport { default as Lib } from \"./utils/Lib\";\nexport { default as Guards } from \"./utils/Guards\";\nexport { default as BaseEntity } from \"./utils/BaseEntity\";\nexport { default as Console } from \"./utils/Console\";\n\n// Re-export specific items for backward compatibility\nexport { ERROR_CODE, HTTP_ERROR_CODE, WS_ERROR_CODE } from \"./errors\";\nexport { InitializableOptions, InitializableEvent } from \"./initializable\";\nexport { RESPONSE_INIT, HEADERS_INIT, RESPONSE_METHOD_OPTIONS } from \"./application\";\nexport { DEFAULT_FALSE_RESPONSE, LOG_COLORS, LOG_ICONS } from \"./consts\";\nexport { ClassConstructor, NonNullableType, ObjectKeys, KVObj, I_ApplicationResponse } from \"./types\";\nexport { E_IS, E_ENVIRONMENTS } from \"./enums\";\nexport { RxjsDataType, NamespaceActions, MultiNamespaceActions } from \"./client/rxjs/useRxjs\";\nexport { ControllerResponse, ControllerAction, ControllerMap, ControllerOptions } from \"./server/controller\";\nexport { Routes } from \"./server/bun/router/routes\";\nexport { WebsocketConstructorOptions, I_WebsocketConstructor } from \"./server/bun/websocket/Websocket\";\nexport { E_WebsocketMessageType, E_WebsocketMessagePriority, E_ClientState } from \"./server/bun/websocket/websocket.enums\";\nexport {\n\tBunWebsocketMessage,\n\tWebsocketChannel,\n\tWebsocketClients,\n\tWebsocketMessageOptions,\n\tWebsocketMessage,\n\tWebsocketStructuredMessage,\n\tWebsocketEntityId,\n\tWebsocketEntityName,\n\tWebsocketEntityData,\n\tI_WebsocketEntity,\n\tI_WebsocketClient,\n\tI_WebsocketChannelEntity,\n\tBroadcastOptions,\n\tAddMemberResult,\n\tAddMemberOptions,\n\tI_WebsocketChannel,\n\tWebsocketInterfaceHandlers,\n\tI_WebsocketInterface,\n} from \"./server/bun/websocket/websocket.types\";\nexport { E_SUBJET_TYPE, I_RxjsPayload, RxjsNamespaces, AsyncSubject, BehaviorSubject, ReplaySubject, Subject, Subscription } from \"./utils/Rxjs\";\n"]}
@@ -27,11 +27,10 @@ export interface I_WebsocketConstructor {
27
27
  * Websocket.Broadcast("lobby", { type: "chat", content: { message: "Hi!" } });
28
28
  *
29
29
  * // Extension - override instance methods
30
- * class GameWebsocket extends Websocket {
31
- * protected createClient(entity: I_WebsocketEntity) {
32
- * return new GameClient(entity);
30
+ * MyWebsocket extends Websocket:
31
+ * protected createClient(entity) {
32
+ * return new MyCustomClient(entity);
33
33
  * }
34
- * }
35
34
  */
36
35
  export default class Websocket extends Singleton {
37
36
  protected _channels: WebsocketChannel;
@@ -18,11 +18,10 @@ import { E_WebsocketMessageType } from "./websocket.enums.js";
18
18
  * Websocket.Broadcast("lobby", { type: "chat", content: { message: "Hi!" } });
19
19
  *
20
20
  * // Extension - override instance methods
21
- * class GameWebsocket extends Websocket {
22
- * protected createClient(entity: I_WebsocketEntity) {
23
- * return new GameClient(entity);
21
+ * MyWebsocket extends Websocket:
22
+ * protected createClient(entity) {
23
+ * return new MyCustomClient(entity);
24
24
  * }
25
- * }
26
25
  */
27
26
  export default class Websocket extends Singleton {
28
27
  constructor(options) {
@@ -47,7 +46,7 @@ export default class Websocket extends Singleton {
47
46
  client.send({ type: E_WebsocketMessageType.CLIENT_CONNECTED, content: { message: "Welcome to the server", client: client.whoami() } });
48
47
  // Client handles its own joining logic with rollback support
49
48
  if (!client.joinChannel(global)) {
50
- Lib.Warn(`Failed to add client ${client.id} to global channel`);
49
+ throw new Error("Failed to join global channel");
51
50
  }
52
51
  // Mark as fully connected
53
52
  client.markConnected();
@@ -269,7 +268,7 @@ export default class Websocket extends Singleton {
269
268
  */
270
269
  static GetConnectedClients() {
271
270
  const ws = this.GetInstance();
272
- return Array.from(ws._clients.values()).filter(client => client.state === "connected");
271
+ return Array.from(ws._clients.values()).filter((client) => client.state === "connected");
273
272
  }
274
273
  /**
275
274
  * Get client statistics by state
@@ -1 +1 @@
1
- {"version":3,"file":"Websocket.js","sourceRoot":"","sources":["../../../../src/server/bun/websocket/Websocket.ts"],"names":[],"mappings":"AACA,OAAO,SAAS,MAAM,oBAAoB,CAAC;AAC3C,OAAO,EAAE,GAAG,EAAE,MAAM,gBAAgB,CAAC;AACrC,OAAO,EAAE,OAAO,EAAE,MAAM,wBAAwB,CAAC;AACjD,OAAO,OAAO,MAAM,WAAW,CAAC;AAChC,OAAO,MAAM,MAAM,UAAU,CAAC;AAW9B,OAAO,EAAE,sBAAsB,EAAE,MAAM,mBAAmB,CAAC;AAc3D;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,CAAC,OAAO,OAAO,SAAU,SAAQ,SAA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WAAW;oBAAE,KAAK,CAAC,SAAS,EAAE,CAAC;oBAAC,MAAM;gBAC3C,KAAK,eAAe;oBAAE,KAAK,CAAC,aAAa,EAAE,CAAC;oBAAC,MAAM;gBACnD,KAAK,cAAc;oBAAE,KAAK,CAAC,YAAY,EAAE,CAAC;oBAAC,MAAM;YAClD,CAAC;QACF,CAAC;QAED,OAAO,KAAK,CAAC;IACd,CAAC;CACD","sourcesContent":["import { Server, ServerWebSocket, WebSocketHandler } from \"bun\";\nimport Singleton from \"../../../singleton\";\nimport { Lib } from \"../../../utils\";\nimport { Console } from \"../../../utils/Console\";\nimport Channel from \"./Channel\";\nimport Client from \"./Client\";\nimport type {\n\tI_WebsocketChannel,\n\tI_WebsocketClient,\n\tI_WebsocketEntity,\n\tI_WebsocketInterface,\n\tWebsocketChannel,\n\tWebsocketEntityData,\n\tBunWebsocketMessage,\n\tWebsocketStructuredMessage,\n} from \"./websocket.types\";\nimport { E_WebsocketMessageType } from \"./websocket.enums\";\n\nexport type WebsocketConstructorOptions = {\n\tdebug?: boolean;\n};\n\nexport interface I_WebsocketConstructor {\n\tws_interface?: I_WebsocketInterface;\n\tchannels?: WebsocketChannel;\n\tclientClass?: typeof Client;\n\tchannelClass?: typeof Channel;\n\toptions?: WebsocketConstructorOptions;\n}\n\n/**\n * Websocket - Singleton managing clients, channels, and message routing\n *\n * ## API Design: Static vs Instance\n * - **Static methods**: Use in application code (e.g., `Websocket.Broadcast()`, `Websocket.GetClient()`)\n * - **Instance methods**: Use when extending the class (e.g., `protected createClient()`)\n *\n * Static methods are facades that call the singleton instance internally.\n *\n * @example\n * // Application code - use static methods\n * Websocket.Broadcast(\"lobby\", { type: \"chat\", content: { message: \"Hi!\" } });\n *\n * // Extension - override instance methods\n * class GameWebsocket extends Websocket {\n * protected createClient(entity: I_WebsocketEntity) {\n * return new GameClient(entity);\n * }\n * }\n */\nexport default class Websocket extends Singleton {\n\tprotected _channels: WebsocketChannel;\n\tprotected _clients: Map<string, I_WebsocketClient> = new Map();\n\tprotected _server!: Server;\n\tprotected _channelClass: typeof Channel;\n\tprotected _clientClass: typeof Client;\n\tprotected _ws_interface?: I_WebsocketInterface;\n\tprotected _options: WebsocketConstructorOptions;\n\tprotected _ws_interface_handlers: Partial<WebSocketHandler<WebsocketEntityData>>;\n\n\tprotected constructor(options?: I_WebsocketConstructor) {\n\t\tsuper();\n\t\tthis._ws_interface = options?.ws_interface;\n\t\tthis._channels = options?.channels ?? new Map<string, Channel>();\n\t\tthis._clientClass = options?.clientClass ?? Client;\n\t\tthis._channelClass = options?.channelClass ?? Channel.GetChannelType(options?.channels);\n\t\tthis._options = options?.options ?? { debug: false };\n\t\tthis.createChannel(\"global\", \"Global\", 1000);\n\t\tthis._ws_interface_handlers = this._ws_interface?.handlers(this._channels, this._clients) ?? {};\n\t}\n\n\tprotected set server(value: Server) {\n\t\tthis._server = value;\n\t}\n\n\tpublic get server(): Server {\n\t\treturn this._server;\n\t}\n\n\tpublic set(server: Server) {\n\t\tthis.server = server;\n\t\tConsole.blank();\n\t\tConsole.success(\"Websocket server set\");\n\t}\n\n\t/**\n\t * Create a new channel\n\t * @param id - The id of the channel\n\t * @param name - The name of the channel\n\t * @param limit - The limit of the channel\n\t * @returns The created channel\n\t */\n\tpublic createChannel(id: string, name: string, limit?: number): I_WebsocketChannel {\n\t\tif (this._channels.has(id)) return this._channels.get(id) as I_WebsocketChannel;\n\t\tconst channel = new this._channelClass(id, name, this, limit);\n\t\tthis._channels.set(id, channel);\n\t\treturn channel;\n\t}\n\n\t/**\n\t * Remove a channel\n\t * @param id - The id of the channel\n\t */\n\tpublic removeChannel(id: string) {\n\t\tthis._channels.delete(id);\n\t}\n\n\t/**\n\t * Create a new channel\n\t * @param id - The id of the channel\n\t * @param name - The name of the channel\n\t * @param limit - The limit of the channel\n\t * @returns The created channel\n\t */\n\tpublic static CreateChannel(id: string, name: string, limit?: number) {\n\t\tconst ws = this.GetInstance<Websocket>();\n\t\treturn ws.createChannel(id, name, limit);\n\t}\n\n\tpublic handlers(): WebSocketHandler<WebsocketEntityData> {\n\t\treturn {\n\t\t\topen: this.clientConnected,\n\t\t\tmessage: this.clientMessageReceived,\n\t\t\tclose: this.clientDisconnected,\n\t\t};\n\t}\n\n\tprivate clientMessageReceived = (ws: ServerWebSocket<WebsocketEntityData>, message: BunWebsocketMessage) => {\n\t\tif (Websocket.Heartbeat(ws, message)) return;\n\n\t\tif (this._ws_interface_handlers.message) return this._ws_interface_handlers.message(ws, message);\n\n\t\tws.send(\"This is the message from the server: \" + message);\n\t\tWebsocket.BroadCastAll({ type: \"client.message.received\", content: { message } });\n\t};\n\n\tprivate clientConnected = (ws: ServerWebSocket<WebsocketEntityData>) => {\n\t\tif (this._options.debug) Lib.Log(\"[debug] Client connected\", ws.data);\n\n\t\tconst global = this._channels.get(\"global\");\n\t\tif (!global) throw new Error(\"Global channel not found\");\n\n\t\tconst client = Websocket.CreateClient({ id: ws.data.id, ws: ws, name: ws.data.name });\n\t\tthis._clients.set(client.id, client);\n\n\t\tclient.send({ type: E_WebsocketMessageType.CLIENT_CONNECTED, content: { message: \"Welcome to the server\", client: client.whoami() } });\n\n\t\t// Client handles its own joining logic with rollback support\n\t\tif (!client.joinChannel(global)) {\n\t\t\tLib.Warn(`Failed to add client ${client.id} to global channel`);\n\t\t}\n\n\t\t// Mark as fully connected\n\t\tclient.markConnected();\n\n\t\tif (this._ws_interface_handlers.open) this._ws_interface_handlers.open(ws);\n\t};\n\n\tprivate clientDisconnected = (ws: ServerWebSocket<WebsocketEntityData>, code: number, reason: string) => {\n\t\tif (this._options.debug) Lib.Log(\"Client disconnected\", ws.data);\n\n\t\tconst client = this._clients.get(ws.data.id);\n\t\tif (!client) return;\n\n\t\t// Mark as disconnecting\n\t\tclient.markDisconnecting();\n\n\t\tif (this._ws_interface_handlers.close) this._ws_interface_handlers.close(ws, code, reason);\n\n\t\t// Remove from all channels\n\t\tthis._channels.forEach((channel) => {\n\t\t\tchannel.removeMember(client);\n\t\t});\n\n\t\t// Mark as disconnected\n\t\tclient.markDisconnected();\n\n\t\t// Remove from registry\n\t\tthis._clients.delete(ws.data.id);\n\t};\n\n\tprivate handleHeartbeat = (ws: ServerWebSocket<WebsocketEntityData>, message: BunWebsocketMessage) => {\n\t\tif (message === \"ping\") {\n\t\t\tconst pong: WebsocketStructuredMessage = { type: \"pong\", content: { message: \"pong\" } };\n\t\t\tws.send(JSON.stringify(pong));\n\t\t\treturn true;\n\t\t}\n\t\treturn false;\n\t};\n\n\tprotected createClient(entity: I_WebsocketEntity): I_WebsocketClient {\n\t\treturn new this._clientClass(entity);\n\t}\n\n\t/**\n\t * Handle the heartbeat\n\t * @param ws - The websocket\n\t * @param message - The message\n\t * @returns True if the heartbeat was handled, false otherwise\n\t */\n\tpublic static Heartbeat(ws: ServerWebSocket<WebsocketEntityData>, message: BunWebsocketMessage) {\n\t\tconst self = this.GetInstance<Websocket>();\n\t\treturn self.handleHeartbeat(ws, message);\n\t}\n\n\t/**\n\t * Get the server\n\t * @returns The server\n\t */\n\tpublic static Server() {\n\t\treturn this.GetInstance<Websocket>().server;\n\t}\n\n\t/**\n\t * Broadcast a message to a channel\n\t * @param channel - The channel\n\t * @param message - The message\n\t * @param args - The arguments\n\t */\n\tpublic static Broadcast(channel: string, message: WebsocketStructuredMessage, ...args: any[]) {\n\t\t// Get the server from the singleton instance\n\t\tconst ws = this.GetInstance<Websocket>();\n\t\tif (!ws.server) {\n\t\t\tthrow new Error(\"Websocket server not set\");\n\t\t}\n\t\tws.server.publish(channel, JSON.stringify({ message, args }));\n\t}\n\n\t/**\n\t * Broadcast a message to all channels\n\t * @param message - The message\n\t * @param args - The arguments\n\t */\n\tpublic static BroadCastAll(message: WebsocketStructuredMessage, ...args: any[]) {\n\t\tconst ws = this.GetInstance<Websocket>();\n\t\tws._channels.forEach((channel) => channel.broadcast(message, ...args));\n\t}\n\n\t/**\n\t * Join a channel\n\t * @param channel - The channel\n\t * @param entity - The entity\n\t */\n\tpublic static Join(channel: string, entity: I_WebsocketEntity) {\n\t\tconst ws = this.GetInstance<Websocket>();\n\t\tconst client = ws._clients.get(entity.id);\n\t\tif (!client) return;\n\t\tws._channels.get(channel)?.addMember(client);\n\t}\n\n\t/**\n\t * Leave a channel\n\t * @param channel - The channel\n\t * @param entity - The entity\n\t */\n\tpublic static Leave(channel: string, entity: I_WebsocketEntity) {\n\t\tconst ws = this.GetInstance<Websocket>();\n\t\tconst client = ws._clients.get(entity.id);\n\t\tif (!client) return;\n\t\tws._channels.get(channel)?.removeMember(client);\n\t}\n\n\t/**\n\t * Get a client\n\t * @param id - The id of the client\n\t * @param throw_if_nil - Whether to throw an error if the client is not found\n\t * @returns The client\n\t */\n\tpublic static GetClient(id: string, throw_if_nil?: true): I_WebsocketClient;\n\tpublic static GetClient(id: string, throw_if_nil?: false): I_WebsocketClient | undefined;\n\tpublic static GetClient(id: string, throw_if_nil: boolean = true): I_WebsocketClient | undefined {\n\t\tconst ws = this.GetInstance<Websocket>();\n\t\tconst client = ws._clients.get(id);\n\t\tif (!client && throw_if_nil) throw new Error(`Client with id ${id} not found`);\n\t\treturn client;\n\t}\n\n\t/**\n\t * Get a channel\n\t * @param id - The id of the channel\n\t * @returns The channel\n\t */\n\tpublic static GetChannel(id: string) {\n\t\tconst ws = this.GetInstance<Websocket>();\n\t\treturn ws._channels.get(id);\n\t}\n\n\t/**\n\t * Get all channels\n\t * @returns The channels\n\t */\n\tpublic static GetChannels() {\n\t\tconst ws = this.GetInstance<Websocket>();\n\t\treturn Array.from(ws._channels.values());\n\t}\n\n\t/**\n\t * Get all clients\n\t * @returns The clients\n\t */\n\tpublic static GetClients() {\n\t\tconst ws = this.GetInstance<Websocket>();\n\t\treturn Array.from(ws._clients.values());\n\t}\n\n\t/**\n\t * Get the number of clients\n\t * @returns The number of clients\n\t */\n\tpublic static GetClientCount() {\n\t\tconst ws = this.GetInstance<Websocket>();\n\t\treturn ws._clients.size;\n\t}\n\n\t/**\n\t * Get the number of channels\n\t * @returns The number of channels\n\t */\n\tpublic static GetChannelCount() {\n\t\tconst ws = this.GetInstance<Websocket>();\n\t\treturn ws._channels.size;\n\t}\n\n\t/**\n\t * Create a client\n\t * @param entity - The entity\n\t * @returns The created client\n\t */\n\tpublic static CreateClient(entity: I_WebsocketEntity): I_WebsocketClient {\n\t\tconst ws = this.GetInstance<Websocket>();\n\t\treturn ws.createClient(entity);\n\t}\n\n\t/**\n\t * Get all connected clients (excluding connecting/disconnecting)\n\t * @returns Array of connected clients\n\t */\n\tpublic static GetConnectedClients(): I_WebsocketClient[] {\n\t\tconst ws = this.GetInstance<Websocket>();\n\t\treturn Array.from(ws._clients.values()).filter(\n\t\t\tclient => client.state === \"connected\"\n\t\t);\n\t}\n\n\t/**\n\t * Get client statistics by state\n\t * @returns Object with counts by state\n\t */\n\tpublic static GetClientStats() {\n\t\tconst ws = this.GetInstance<Websocket>();\n\t\tconst stats = {\n\t\t\ttotal: ws._clients.size,\n\t\t\tconnecting: 0,\n\t\t\tconnected: 0,\n\t\t\tdisconnecting: 0,\n\t\t\tdisconnected: 0,\n\t\t};\n\n\t\tfor (const client of ws._clients.values()) {\n\t\t\tswitch (client.state) {\n\t\t\t\tcase \"connecting\": stats.connecting++; break;\n\t\t\t\tcase \"connected\": stats.connected++; break;\n\t\t\t\tcase \"disconnecting\": stats.disconnecting++; break;\n\t\t\t\tcase \"disconnected\": stats.disconnected++; break;\n\t\t\t}\n\t\t}\n\n\t\treturn stats;\n\t}\n}\n"]}
1
+ {"version":3,"file":"Websocket.js","sourceRoot":"","sources":["../../../../src/server/bun/websocket/Websocket.ts"],"names":[],"mappings":"AACA,OAAO,SAAS,MAAM,oBAAoB,CAAC;AAC3C,OAAO,EAAE,GAAG,EAAE,MAAM,gBAAgB,CAAC;AACrC,OAAO,EAAE,OAAO,EAAE,MAAM,wBAAwB,CAAC;AACjD,OAAO,OAAO,MAAM,WAAW,CAAC;AAChC,OAAO,MAAM,MAAM,UAAU,CAAC;AAW9B,OAAO,EAAE,sBAAsB,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AC;gBACtB,KAAK,YAAY;oBAChB,KAAK,CAAC,UAAU,EAAE,CAAC;oBACnB,MAAM;gBACP,KAAK,WAAW;oBACf,KAAK,CAAC,SAAS,EAAE,CAAC;oBAClB,MAAM;gBACP,KAAK,eAAe;oBACnB,KAAK,CAAC,aAAa,EAAE,CAAC;oBACtB,MAAM;gBACP,KAAK,cAAc;oBAClB,KAAK,CAAC,YAAY,EAAE,CAAC;oBACrB,MAAM;YACR,CAAC;QACF,CAAC;QAED,OAAO,KAAK,CAAC;IACd,CAAC;CACD","sourcesContent":["import { Server, ServerWebSocket, WebSocketHandler } from \"bun\";\nimport Singleton from \"../../../singleton\";\nimport { Lib } from \"../../../utils\";\nimport { Console } from \"../../../utils/Console\";\nimport Channel from \"./Channel\";\nimport Client from \"./Client\";\nimport type {\n\tI_WebsocketChannel,\n\tI_WebsocketClient,\n\tI_WebsocketEntity,\n\tI_WebsocketInterface,\n\tWebsocketChannel,\n\tWebsocketEntityData,\n\tBunWebsocketMessage,\n\tWebsocketStructuredMessage,\n} from \"./websocket.types\";\nimport { E_WebsocketMessageType } from \"./websocket.enums\";\n\nexport type WebsocketConstructorOptions = {\n\tdebug?: boolean;\n};\n\nexport interface I_WebsocketConstructor {\n\tws_interface?: I_WebsocketInterface;\n\tchannels?: WebsocketChannel;\n\tclientClass?: typeof Client;\n\tchannelClass?: typeof Channel;\n\toptions?: WebsocketConstructorOptions;\n}\n\n/**\n * Websocket - Singleton managing clients, channels, and message routing\n *\n * ## API Design: Static vs Instance\n * - **Static methods**: Use in application code (e.g., `Websocket.Broadcast()`, `Websocket.GetClient()`)\n * - **Instance methods**: Use when extending the class (e.g., `protected createClient()`)\n *\n * Static methods are facades that call the singleton instance internally.\n *\n * @example\n * // Application code - use static methods\n * Websocket.Broadcast(\"lobby\", { type: \"chat\", content: { message: \"Hi!\" } });\n *\n * // Extension - override instance methods\n * MyWebsocket extends Websocket:\n * protected createClient(entity) {\n * return new MyCustomClient(entity);\n * }\n */\nexport default class Websocket extends Singleton {\n\tprotected _channels: WebsocketChannel;\n\tprotected _clients: Map<string, I_WebsocketClient> = new Map();\n\tprotected _server!: Server;\n\tprotected _channelClass: typeof Channel;\n\tprotected _clientClass: typeof Client;\n\tprotected _ws_interface?: I_WebsocketInterface;\n\tprotected _options: WebsocketConstructorOptions;\n\tprotected _ws_interface_handlers: Partial<WebSocketHandler<WebsocketEntityData>>;\n\n\tprotected constructor(options?: I_WebsocketConstructor) {\n\t\tsuper();\n\t\tthis._ws_interface = options?.ws_interface;\n\t\tthis._channels = options?.channels ?? new Map<string, Channel>();\n\t\tthis._clientClass = options?.clientClass ?? Client;\n\t\tthis._channelClass = options?.channelClass ?? Channel.GetChannelType(options?.channels);\n\t\tthis._options = options?.options ?? { debug: false };\n\t\tthis.createChannel(\"global\", \"Global\", 1000);\n\t\tthis._ws_interface_handlers = this._ws_interface?.handlers(this._channels, this._clients) ?? {};\n\t}\n\n\tprotected set server(value: Server) {\n\t\tthis._server = value;\n\t}\n\n\tpublic get server(): Server {\n\t\treturn this._server;\n\t}\n\n\tpublic set(server: Server) {\n\t\tthis.server = server;\n\t\tConsole.blank();\n\t\tConsole.success(\"Websocket server set\");\n\t}\n\n\t/**\n\t * Create a new channel\n\t * @param id - The id of the channel\n\t * @param name - The name of the channel\n\t * @param limit - The limit of the channel\n\t * @returns The created channel\n\t */\n\tpublic createChannel(id: string, name: string, limit?: number): I_WebsocketChannel {\n\t\tif (this._channels.has(id)) return this._channels.get(id) as I_WebsocketChannel;\n\t\tconst channel = new this._channelClass(id, name, this, limit);\n\t\tthis._channels.set(id, channel);\n\t\treturn channel;\n\t}\n\n\t/**\n\t * Remove a channel\n\t * @param id - The id of the channel\n\t */\n\tpublic removeChannel(id: string) {\n\t\tthis._channels.delete(id);\n\t}\n\n\t/**\n\t * Create a new channel\n\t * @param id - The id of the channel\n\t * @param name - The name of the channel\n\t * @param limit - The limit of the channel\n\t * @returns The created channel\n\t */\n\tpublic static CreateChannel(id: string, name: string, limit?: number) {\n\t\tconst ws = this.GetInstance<Websocket>();\n\t\treturn ws.createChannel(id, name, limit);\n\t}\n\n\tpublic handlers(): WebSocketHandler<WebsocketEntityData> {\n\t\treturn {\n\t\t\topen: this.clientConnected,\n\t\t\tmessage: this.clientMessageReceived,\n\t\t\tclose: this.clientDisconnected,\n\t\t};\n\t}\n\n\tprivate clientMessageReceived = (ws: ServerWebSocket<WebsocketEntityData>, message: BunWebsocketMessage) => {\n\t\tif (Websocket.Heartbeat(ws, message)) return;\n\n\t\tif (this._ws_interface_handlers.message) return this._ws_interface_handlers.message(ws, message);\n\n\t\tws.send(\"This is the message from the server: \" + message);\n\t\tWebsocket.BroadCastAll({ type: \"client.message.received\", content: { message } });\n\t};\n\n\tprivate clientConnected = (ws: ServerWebSocket<WebsocketEntityData>) => {\n\t\tif (this._options.debug) Lib.Log(\"[debug] Client connected\", ws.data);\n\n\t\tconst global = this._channels.get(\"global\");\n\t\tif (!global) throw new Error(\"Global channel not found\");\n\n\t\tconst client = Websocket.CreateClient({ id: ws.data.id, ws: ws, name: ws.data.name });\n\t\tthis._clients.set(client.id, client);\n\n\t\tclient.send({ type: E_WebsocketMessageType.CLIENT_CONNECTED, content: { message: \"Welcome to the server\", client: client.whoami() } });\n\n\t\t// Client handles its own joining logic with rollback support\n\t\tif (!client.joinChannel(global)) {\n\t\t\tthrow new Error(\"Failed to join global channel\");\n\t\t}\n\n\t\t// Mark as fully connected\n\t\tclient.markConnected();\n\n\t\tif (this._ws_interface_handlers.open) this._ws_interface_handlers.open(ws);\n\t};\n\n\tprivate clientDisconnected = (ws: ServerWebSocket<WebsocketEntityData>, code: number, reason: string) => {\n\t\tif (this._options.debug) Lib.Log(\"Client disconnected\", ws.data);\n\n\t\tconst client = this._clients.get(ws.data.id);\n\t\tif (!client) return;\n\n\t\t// Mark as disconnecting\n\t\tclient.markDisconnecting();\n\n\t\tif (this._ws_interface_handlers.close) this._ws_interface_handlers.close(ws, code, reason);\n\n\t\t// Remove from all channels\n\t\tthis._channels.forEach((channel) => {\n\t\t\tchannel.removeMember(client);\n\t\t});\n\n\t\t// Mark as disconnected\n\t\tclient.markDisconnected();\n\n\t\t// Remove from registry\n\t\tthis._clients.delete(ws.data.id);\n\t};\n\n\tprivate handleHeartbeat = (ws: ServerWebSocket<WebsocketEntityData>, message: BunWebsocketMessage) => {\n\t\tif (message === \"ping\") {\n\t\t\tconst pong: WebsocketStructuredMessage = { type: \"pong\", content: { message: \"pong\" } };\n\t\t\tws.send(JSON.stringify(pong));\n\t\t\treturn true;\n\t\t}\n\t\treturn false;\n\t};\n\n\tprotected createClient(entity: I_WebsocketEntity): I_WebsocketClient {\n\t\treturn new this._clientClass(entity);\n\t}\n\n\t/**\n\t * Handle the heartbeat\n\t * @param ws - The websocket\n\t * @param message - The message\n\t * @returns True if the heartbeat was handled, false otherwise\n\t */\n\tpublic static Heartbeat(ws: ServerWebSocket<WebsocketEntityData>, message: BunWebsocketMessage) {\n\t\tconst self = this.GetInstance<Websocket>();\n\t\treturn self.handleHeartbeat(ws, message);\n\t}\n\n\t/**\n\t * Get the server\n\t * @returns The server\n\t */\n\tpublic static Server() {\n\t\treturn this.GetInstance<Websocket>().server;\n\t}\n\n\t/**\n\t * Broadcast a message to a channel\n\t * @param channel - The channel\n\t * @param message - The message\n\t * @param args - The arguments\n\t */\n\tpublic static Broadcast(channel: string, message: WebsocketStructuredMessage, ...args: any[]) {\n\t\t// Get the server from the singleton instance\n\t\tconst ws = this.GetInstance<Websocket>();\n\t\tif (!ws.server) {\n\t\t\tthrow new Error(\"Websocket server not set\");\n\t\t}\n\t\tws.server.publish(channel, JSON.stringify({ message, args }));\n\t}\n\n\t/**\n\t * Broadcast a message to all channels\n\t * @param message - The message\n\t * @param args - The arguments\n\t */\n\tpublic static BroadCastAll(message: WebsocketStructuredMessage, ...args: any[]) {\n\t\tconst ws = this.GetInstance<Websocket>();\n\t\tws._channels.forEach((channel) => channel.broadcast(message, ...args));\n\t}\n\n\t/**\n\t * Join a channel\n\t * @param channel - The channel\n\t * @param entity - The entity\n\t */\n\tpublic static Join(channel: string, entity: I_WebsocketEntity) {\n\t\tconst ws = this.GetInstance<Websocket>();\n\t\tconst client = ws._clients.get(entity.id);\n\t\tif (!client) return;\n\t\tws._channels.get(channel)?.addMember(client);\n\t}\n\n\t/**\n\t * Leave a channel\n\t * @param channel - The channel\n\t * @param entity - The entity\n\t */\n\tpublic static Leave(channel: string, entity: I_WebsocketEntity) {\n\t\tconst ws = this.GetInstance<Websocket>();\n\t\tconst client = ws._clients.get(entity.id);\n\t\tif (!client) return;\n\t\tws._channels.get(channel)?.removeMember(client);\n\t}\n\n\t/**\n\t * Get a client\n\t * @param id - The id of the client\n\t * @param throw_if_nil - Whether to throw an error if the client is not found\n\t * @returns The client\n\t */\n\tpublic static GetClient(id: string, throw_if_nil?: true): I_WebsocketClient;\n\tpublic static GetClient(id: string, throw_if_nil?: false): I_WebsocketClient | undefined;\n\tpublic static GetClient(id: string, throw_if_nil: boolean = true): I_WebsocketClient | undefined {\n\t\tconst ws = this.GetInstance<Websocket>();\n\t\tconst client = ws._clients.get(id);\n\t\tif (!client && throw_if_nil) throw new Error(`Client with id ${id} not found`);\n\t\treturn client;\n\t}\n\n\t/**\n\t * Get a channel\n\t * @param id - The id of the channel\n\t * @returns The channel\n\t */\n\tpublic static GetChannel(id: string) {\n\t\tconst ws = this.GetInstance<Websocket>();\n\t\treturn ws._channels.get(id);\n\t}\n\n\t/**\n\t * Get all channels\n\t * @returns The channels\n\t */\n\tpublic static GetChannels() {\n\t\tconst ws = this.GetInstance<Websocket>();\n\t\treturn Array.from(ws._channels.values());\n\t}\n\n\t/**\n\t * Get all clients\n\t * @returns The clients\n\t */\n\tpublic static GetClients() {\n\t\tconst ws = this.GetInstance<Websocket>();\n\t\treturn Array.from(ws._clients.values());\n\t}\n\n\t/**\n\t * Get the number of clients\n\t * @returns The number of clients\n\t */\n\tpublic static GetClientCount() {\n\t\tconst ws = this.GetInstance<Websocket>();\n\t\treturn ws._clients.size;\n\t}\n\n\t/**\n\t * Get the number of channels\n\t * @returns The number of channels\n\t */\n\tpublic static GetChannelCount() {\n\t\tconst ws = this.GetInstance<Websocket>();\n\t\treturn ws._channels.size;\n\t}\n\n\t/**\n\t * Create a client\n\t * @param entity - The entity\n\t * @returns The created client\n\t */\n\tpublic static CreateClient(entity: I_WebsocketEntity): I_WebsocketClient {\n\t\tconst ws = this.GetInstance<Websocket>();\n\t\treturn ws.createClient(entity);\n\t}\n\n\t/**\n\t * Get all connected clients (excluding connecting/disconnecting)\n\t * @returns Array of connected clients\n\t */\n\tpublic static GetConnectedClients(): I_WebsocketClient[] {\n\t\tconst ws = this.GetInstance<Websocket>();\n\t\treturn Array.from(ws._clients.values()).filter((client) => client.state === \"connected\");\n\t}\n\n\t/**\n\t * Get client statistics by state\n\t * @returns Object with counts by state\n\t */\n\tpublic static GetClientStats() {\n\t\tconst ws = this.GetInstance<Websocket>();\n\t\tconst stats = {\n\t\t\ttotal: ws._clients.size,\n\t\t\tconnecting: 0,\n\t\t\tconnected: 0,\n\t\t\tdisconnecting: 0,\n\t\t\tdisconnected: 0,\n\t\t};\n\n\t\tfor (const client of ws._clients.values()) {\n\t\t\tswitch (client.state) {\n\t\t\t\tcase \"connecting\":\n\t\t\t\t\tstats.connecting++;\n\t\t\t\t\tbreak;\n\t\t\t\tcase \"connected\":\n\t\t\t\t\tstats.connected++;\n\t\t\t\t\tbreak;\n\t\t\t\tcase \"disconnecting\":\n\t\t\t\t\tstats.disconnecting++;\n\t\t\t\t\tbreak;\n\t\t\t\tcase \"disconnected\":\n\t\t\t\t\tstats.disconnected++;\n\t\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\n\t\treturn stats;\n\t}\n}\n"]}
@@ -5,7 +5,7 @@ export * from './Channel';
5
5
  export * from './Client';
6
6
  export * from './websocket.enums';
7
7
  export * from './websocket.types';
8
- export { default as GameWebsocket } from './Websocket';
8
+ export { default as Websocket } from './Websocket';
9
9
  export { default as Message } from './Message';
10
10
  export { default as Channel } from './Channel';
11
11
  export { default as Client } from './Client';
@@ -7,7 +7,7 @@ export * from './Channel.js';
7
7
  export * from './Client.js';
8
8
  export * from './websocket.enums.js';
9
9
  export * from './websocket.types.js';
10
- export { default as GameWebsocket } from './Websocket.js';
10
+ export { default as Websocket } from './Websocket.js';
11
11
  export { default as Message } from './Message.js';
12
12
  export { default as Channel } from './Channel.js';
13
13
  export { default as Client } from './Client.js';
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/server/bun/websocket/index.ts"],"names":[],"mappings":"AAAA,6DAA6D;AAC7D,iCAAiC;AAEjC,cAAc,aAAa,CAAC;AAC5B,cAAc,oBAAoB,CAAC;AACnC,cAAc,WAAW,CAAC;AAC1B,cAAc,WAAW,CAAC;AAC1B,cAAc,UAAU,CAAC;AACzB,cAAc,mBAAmB,CAAC;AAClC,cAAc,mBAAmB,CAAC;AAClC,OAAO,EAAE,OAAO,IAAI,aAAa,EAAE,MAAM,aAAa,CAAC;AACvD,OAAO,EAAE,OAAO,IAAI,OAAO,EAAE,MAAM,WAAW,CAAC;AAC/C,OAAO,EAAE,OAAO,IAAI,OAAO,EAAE,MAAM,WAAW,CAAC;AAC/C,OAAO,EAAE,OAAO,IAAI,MAAM,EAAE,MAAM,UAAU,CAAC","sourcesContent":["// This file is auto-generated by scripts/generate-indexes.ts\n// Do not edit this file directly\n\nexport * from './Websocket';\nexport * from './websocket.guards';\nexport * from './Message';\nexport * from './Channel';\nexport * from './Client';\nexport * from './websocket.enums';\nexport * from './websocket.types';\nexport { default as GameWebsocket } from './Websocket';\nexport { default as Message } from './Message';\nexport { default as Channel } from './Channel';\nexport { default as Client } from './Client';\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/server/bun/websocket/index.ts"],"names":[],"mappings":"AAAA,6DAA6D;AAC7D,iCAAiC;AAEjC,cAAc,aAAa,CAAC;AAC5B,cAAc,oBAAoB,CAAC;AACnC,cAAc,WAAW,CAAC;AAC1B,cAAc,WAAW,CAAC;AAC1B,cAAc,UAAU,CAAC;AACzB,cAAc,mBAAmB,CAAC;AAClC,cAAc,mBAAmB,CAAC;AAClC,OAAO,EAAE,OAAO,IAAI,SAAS,EAAE,MAAM,aAAa,CAAC;AACnD,OAAO,EAAE,OAAO,IAAI,OAAO,EAAE,MAAM,WAAW,CAAC;AAC/C,OAAO,EAAE,OAAO,IAAI,OAAO,EAAE,MAAM,WAAW,CAAC;AAC/C,OAAO,EAAE,OAAO,IAAI,MAAM,EAAE,MAAM,UAAU,CAAC","sourcesContent":["// This file is auto-generated by scripts/generate-indexes.ts\n// Do not edit this file directly\n\nexport * from './Websocket';\nexport * from './websocket.guards';\nexport * from './Message';\nexport * from './Channel';\nexport * from './Client';\nexport * from './websocket.enums';\nexport * from './websocket.types';\nexport { default as Websocket } from './Websocket';\nexport { default as Message } from './Message';\nexport { default as Channel } from './Channel';\nexport { default as Client } from './Client';\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "topsyde-utils",
3
- "version": "1.0.206",
3
+ "version": "1.0.208",
4
4
  "description": "A bundle of TypeScript utility classes and functions",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -32,6 +32,10 @@
32
32
  "types": "./dist/index.d.ts",
33
33
  "default": "./dist/index.js"
34
34
  },
35
+ "./client/api": {
36
+ "types": "./dist/client/api/index.d.ts",
37
+ "default": "./dist/client/api/index.js"
38
+ },
35
39
  "./client/rxjs": {
36
40
  "types": "./dist/client/rxjs/index.d.ts",
37
41
  "default": "./dist/client/rxjs/index.js"
@@ -59,6 +63,9 @@
59
63
  },
60
64
  "typesVersions": {
61
65
  "*": {
66
+ "client/api": [
67
+ "./dist/client/api/index.d.ts"
68
+ ],
62
69
  "client/rxjs": [
63
70
  "./dist/client/rxjs/index.d.ts"
64
71
  ],
@@ -110,6 +117,7 @@
110
117
  "node": ">=14.0.0"
111
118
  },
112
119
  "dependencies": {
120
+ "axios": "^1.12.2",
113
121
  "class-transformer": "^0.5.1",
114
122
  "class-validator": "^0.14.2",
115
123
  "rxjs": "^7.8.2",
@@ -0,0 +1,111 @@
1
+ import Axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse, isAxiosError } from "axios";
2
+ class BaseAPI {
3
+ private api: AxiosInstance;
4
+ protected controller: string;
5
+ protected base_url: string;
6
+
7
+ constructor(controller: string, base_url?: string) {
8
+ this.controller = controller;
9
+ this.base_url = base_url || import.meta.env.VITE_HOST || "";
10
+ this.api = this.initAxios();
11
+ }
12
+
13
+ private initAxios(): AxiosInstance {
14
+ const config: AxiosRequestConfig = {
15
+ headers: {
16
+ "Content-Type": "application/json",
17
+ },
18
+ };
19
+
20
+ const instance = Axios.create(config);
21
+
22
+ // Add interceptor to modify request config
23
+ instance.interceptors.request.use((config) => {
24
+ // Ensure the correct content type
25
+ config.headers["Content-Type"] = "application/json";
26
+
27
+ return config;
28
+ });
29
+
30
+ return instance;
31
+ }
32
+
33
+ public static Status(response: AxiosResponse<{ status: boolean }>) {
34
+ if (!response.data.status) throw new Error("Something went wrong");
35
+ }
36
+
37
+ /**
38
+ * @description Axios GET request wrapper
39
+ * @param endpoint string
40
+ * @generic T
41
+ * @returns Promise<AxiosResponse<T>>
42
+ */
43
+ async get<T extends { status: boolean }>(endpoint: string): Promise<AxiosResponse<T>>;
44
+ /**
45
+ * @description Axios GET request wrapper
46
+ * @param controller string - which controller to use
47
+ * @param endpoint string - controller action to call
48
+ * @generic T
49
+ * @returns Promise<AxiosResponse<T>>
50
+ */
51
+ async get<T extends { status: boolean }>(controller: string, endpoint: string): Promise<AxiosResponse<T>>;
52
+ /**
53
+ * @description Axios GET request wrapper
54
+ * @param controller string - which controller to use
55
+ * @param endpoint string - controller action to call
56
+ * @param debounceDelay number - debounce delay
57
+ * @generic T
58
+ * @returns Promise<AxiosResponse<T>>
59
+ */
60
+ async get<T extends { status: boolean }>(controller: string, endpoint: string, debounceDelay?: number): Promise<AxiosResponse<T>>;
61
+ async get<T extends { status: boolean }>(controllerOrEndpoint: string, endpoint?: string | undefined, _debounceDelay?: number): Promise<AxiosResponse<T>> {
62
+ let controller = endpoint ? controllerOrEndpoint : this.controller;
63
+ let action = endpoint ? endpoint : controllerOrEndpoint;
64
+
65
+ return this.api.get<T>(`${this.base_url}/${controller}/${action}`).catch((err) => {
66
+ if (isAxiosError(err)) throw err.response;
67
+ throw err;
68
+ });
69
+ }
70
+
71
+ /**
72
+ * @description Axios POST request wrapper
73
+ * @param endpoint string - controller action to call
74
+ * @param payload any
75
+ * @generic T
76
+ * @returns Promise<AxiosResponse<T>>
77
+ */
78
+ async post<T extends { status: boolean }>(endpoint: string, payload: any): Promise<AxiosResponse<T>>;
79
+ /**
80
+ * @description Axios POST request wrapper
81
+ * @param controller string - which controller to use
82
+ * @param endpoint string - controller action to call
83
+ * @param payload any
84
+ * @generic T
85
+ * @returns Promise<AxiosResponse<T>>
86
+ */
87
+ async post<T extends { status: boolean }>(controller: string, endpoint: string, payload: any): Promise<AxiosResponse<T>>;
88
+ async post<T extends { status: boolean }>(controllerOrEndpoint: string, endpointOrPayload: string | any, payload?: any): Promise<AxiosResponse<T>> {
89
+ let controller: string;
90
+ let action: string;
91
+ let data: any;
92
+
93
+ if (payload !== null && payload !== undefined) {
94
+ controller = controllerOrEndpoint;
95
+ action = endpointOrPayload;
96
+ data = payload;
97
+ } else {
98
+ controller = this.controller;
99
+ action = controllerOrEndpoint;
100
+ data = endpointOrPayload;
101
+ }
102
+ const response = await this.api.post<T>(`${this.base_url}/${controller}/${action}`, data).catch((err) => {
103
+ if (isAxiosError(err)) throw err.response;
104
+ throw err;
105
+ });
106
+ if (!response.data.status) throw new Error("Something went wrong");
107
+ return response;
108
+ }
109
+ }
110
+
111
+ export default BaseAPI;
@@ -0,0 +1,5 @@
1
+ // This file is auto-generated by scripts/generate-indexes.ts
2
+ // Do not edit this file directly
3
+
4
+ export * from './base.api';
5
+ export { default as BaseAPI } from './base.api';
package/src/index.ts CHANGED
@@ -10,6 +10,7 @@ export * from "./consts";
10
10
  export * from "./types";
11
11
  export * from "./throwable";
12
12
  export * from "./enums";
13
+ export * from "./client/api/base.api";
13
14
  export * from "./client/rxjs/useRxjs";
14
15
  export * from "./client/vite/plugins/topsydeUtilsVitePlugin";
15
16
  export * from "./server/controller";
@@ -39,13 +40,14 @@ export { default as Singleton } from "./singleton";
39
40
  export { default as Initializable } from "./initializable";
40
41
  export { default as Application } from "./application";
41
42
  export { default as Throwable } from "./throwable";
43
+ export { default as BaseAPI } from "./client/api/base.api";
42
44
  export { default as TopsydeUtilsVitePlugin } from "./client/vite/plugins/topsydeUtilsVitePlugin";
43
45
  export { default as Controller } from "./server/controller";
44
46
  export { default as Service } from "./server/service";
45
47
  export { default as Database } from "./server/base/base.database";
46
48
  export { default as Router } from "./server/bun/router/router";
47
49
  export { default as Router_Internal } from "./server/bun/router/router.internal";
48
- export { default as GameWebsocket } from "./server/bun/websocket/Websocket";
50
+ export { default as Websocket } from "./server/bun/websocket/Websocket";
49
51
  export { default as Message } from "./server/bun/websocket/Message";
50
52
  export { default as Channel } from "./server/bun/websocket/Channel";
51
53
  export { default as Client } from "./server/bun/websocket/Client";
@@ -42,11 +42,10 @@ export interface I_WebsocketConstructor {
42
42
  * Websocket.Broadcast("lobby", { type: "chat", content: { message: "Hi!" } });
43
43
  *
44
44
  * // Extension - override instance methods
45
- * class GameWebsocket extends Websocket {
46
- * protected createClient(entity: I_WebsocketEntity) {
47
- * return new GameClient(entity);
45
+ * MyWebsocket extends Websocket:
46
+ * protected createClient(entity) {
47
+ * return new MyCustomClient(entity);
48
48
  * }
49
- * }
50
49
  */
51
50
  export default class Websocket extends Singleton {
52
51
  protected _channels: WebsocketChannel;
@@ -147,7 +146,7 @@ export default class Websocket extends Singleton {
147
146
 
148
147
  // Client handles its own joining logic with rollback support
149
148
  if (!client.joinChannel(global)) {
150
- Lib.Warn(`Failed to add client ${client.id} to global channel`);
149
+ throw new Error("Failed to join global channel");
151
150
  }
152
151
 
153
152
  // Mark as fully connected
@@ -337,9 +336,7 @@ export default class Websocket extends Singleton {
337
336
  */
338
337
  public static GetConnectedClients(): I_WebsocketClient[] {
339
338
  const ws = this.GetInstance<Websocket>();
340
- return Array.from(ws._clients.values()).filter(
341
- client => client.state === "connected"
342
- );
339
+ return Array.from(ws._clients.values()).filter((client) => client.state === "connected");
343
340
  }
344
341
 
345
342
  /**
@@ -358,10 +355,18 @@ export default class Websocket extends Singleton {
358
355
 
359
356
  for (const client of ws._clients.values()) {
360
357
  switch (client.state) {
361
- case "connecting": stats.connecting++; break;
362
- case "connected": stats.connected++; break;
363
- case "disconnecting": stats.disconnecting++; break;
364
- case "disconnected": stats.disconnected++; break;
358
+ case "connecting":
359
+ stats.connecting++;
360
+ break;
361
+ case "connected":
362
+ stats.connected++;
363
+ break;
364
+ case "disconnecting":
365
+ stats.disconnecting++;
366
+ break;
367
+ case "disconnected":
368
+ stats.disconnected++;
369
+ break;
365
370
  }
366
371
  }
367
372
 
@@ -8,7 +8,7 @@ export * from './Channel';
8
8
  export * from './Client';
9
9
  export * from './websocket.enums';
10
10
  export * from './websocket.types';
11
- export { default as GameWebsocket } from './Websocket';
11
+ export { default as Websocket } from './Websocket';
12
12
  export { default as Message } from './Message';
13
13
  export { default as Channel } from './Channel';
14
14
  export { default as Client } from './Client';