refactor(shared): centralize configuration and types
Migrate configuration validation and core moderation types from individual services to the `@bete/shared` package to ensure consistency across the monorepo. - Move `AppConfig` and moderation-related interfaces to `packages/shared`. - Replace service-specific Zod schemas with the centralized shared configuration. - Refactor `services/backend` and `services/discord-gateway` to consume shared config and types. - Remove redundant type definitions and local configuration logic in services. - Update `packages/shared` exports to include new `config` and `moderation-types` modules. - Clean up unused files and deprecated utility functions in `packages/shared`.
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
/**
|
||||
* Global broadcast functions for WebSocket events.
|
||||
* Broadcast functions for WebSocket events.
|
||||
*
|
||||
* These are assigned by ws/server.ts when the WebSocket server initializes.
|
||||
* These are injected by ws/server.ts when the WebSocket server initializes.
|
||||
* Other modules call them to push real-time events to connected frontend clients.
|
||||
*
|
||||
* Usage:
|
||||
@@ -13,38 +13,48 @@ type BroadcastFn = (data: unknown) => void;
|
||||
type BroadcastRawFn = (type: string, data: unknown) => void;
|
||||
type BroadcastBinaryFn = (data: Buffer) => void;
|
||||
|
||||
declare global {
|
||||
// biome-ignore lint/suspicious/noAssignInExpressions: intentional global broadcast registry
|
||||
var __broadcastFns:
|
||||
| {
|
||||
messageCreated: BroadcastFn;
|
||||
messageUpdated: BroadcastFn;
|
||||
messageDeleted: BroadcastFn;
|
||||
attachmentUploaded: BroadcastFn;
|
||||
raw: BroadcastRawFn;
|
||||
binary: BroadcastBinaryFn;
|
||||
}
|
||||
| undefined;
|
||||
export interface BroadcastFunctions {
|
||||
messageCreated: BroadcastFn;
|
||||
messageUpdated: BroadcastFn;
|
||||
messageDeleted: BroadcastFn;
|
||||
attachmentUploaded: BroadcastFn;
|
||||
raw: BroadcastRawFn;
|
||||
binary: BroadcastBinaryFn;
|
||||
}
|
||||
|
||||
const noop: BroadcastFn = () => {};
|
||||
const noopRaw: BroadcastRawFn = () => {};
|
||||
const noopBinary: BroadcastBinaryFn = () => {};
|
||||
|
||||
let _fns: BroadcastFunctions | null = null;
|
||||
|
||||
/**
|
||||
* Inject broadcast functions from the WebSocket server initializer.
|
||||
* Must be called once during server startup before any broadcast is used.
|
||||
*/
|
||||
export function setBroadcastFunctions(fns: BroadcastFunctions): void {
|
||||
_fns = fns;
|
||||
}
|
||||
|
||||
/** Clear injected functions (used during cleanup). */
|
||||
export function clearBroadcastFunctions(): void {
|
||||
_fns = null;
|
||||
}
|
||||
|
||||
export const broadcastMessageCreated: BroadcastFn = (data) =>
|
||||
(globalThis.__broadcastFns?.messageCreated ?? noop)(data);
|
||||
(_fns?.messageCreated ?? noop)(data);
|
||||
|
||||
export const broadcastMessageUpdated: BroadcastFn = (data) =>
|
||||
(globalThis.__broadcastFns?.messageUpdated ?? noop)(data);
|
||||
(_fns?.messageUpdated ?? noop)(data);
|
||||
|
||||
export const broadcastMessageDeleted: BroadcastFn = (data) =>
|
||||
(globalThis.__broadcastFns?.messageDeleted ?? noop)(data);
|
||||
(_fns?.messageDeleted ?? noop)(data);
|
||||
|
||||
export const broadcastAttachmentUploaded: BroadcastFn = (data) =>
|
||||
(globalThis.__broadcastFns?.attachmentUploaded ?? noop)(data);
|
||||
(_fns?.attachmentUploaded ?? noop)(data);
|
||||
|
||||
export const broadcastRaw: BroadcastRawFn = (type, data) =>
|
||||
(globalThis.__broadcastFns?.raw ?? noopRaw)(type, data);
|
||||
(_fns?.raw ?? noopRaw)(type, data);
|
||||
|
||||
export const broadcastBinary: BroadcastBinaryFn = (data) =>
|
||||
(globalThis.__broadcastFns?.binary ?? noopBinary)(data);
|
||||
(_fns?.binary ?? noopBinary)(data);
|
||||
|
||||
Reference in New Issue
Block a user