Enhance WebSocket server with new HTTP server implementation and type assertions
* Updated package.json to specify the entry point for the development script and added morgan as a dependency. * Introduced HttpServer class for handling HTTP requests with Express and integrated logging. * Added new assertion methods in the Assert class for better type validation. * Created IRoutes interface to define route handling structure. * Added optional and nullable type definitions for improved type safety. * Implemented initial server setup in src/index.ts.
This commit is contained in:
@@ -11,7 +11,7 @@ export default class Assert {
|
||||
}
|
||||
}
|
||||
|
||||
public static isDefined(name: string, value: unknown): asserts value is undefined | null {
|
||||
public static isDefined(name: string, value: unknown): asserts value is NonNullable<typeof value> {
|
||||
if (value === undefined || value === null) {
|
||||
throw new Error(`[${name}] must be defined instead received [${typeof value}]`);
|
||||
}
|
||||
@@ -73,6 +73,37 @@ export default class Assert {
|
||||
}
|
||||
}
|
||||
|
||||
public static isFunction(name: string, value: unknown): asserts value is Function {
|
||||
if (typeof value !== 'function') {
|
||||
throw new Error(`[${name}] must be a function, instead received [${typeof value}]`);
|
||||
}
|
||||
}
|
||||
public static satisfiesInterface<T>(name: string, obj: unknown, requiredProps: (keyof T)[]): asserts obj is T {
|
||||
Assert.isObject(name, obj);
|
||||
|
||||
for (const prop of requiredProps) {
|
||||
if (!(prop in (obj as any))) {
|
||||
throw new Error(`[${name}] missing required property: ${String(prop)}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static isArray<T>(name: string, value: unknown): asserts value is T[] {
|
||||
if (!Array.isArray(value)) {
|
||||
throw new Error(`[${name}] must be an array, instead received [${typeof value}]`);
|
||||
}
|
||||
}
|
||||
|
||||
public static isStringArray(name: string, value: unknown): asserts value is string[] {
|
||||
if (!Array.isArray(value)) {
|
||||
throw new Error(`[${name}] must be an array, instead received [${typeof value}]`);
|
||||
}
|
||||
|
||||
for (const item of value) {
|
||||
Assert.isString(name, item);
|
||||
}
|
||||
}
|
||||
|
||||
public static isObject<T>(name: string, value: unknown): asserts value is T {
|
||||
if (value === null || typeof value !== 'object') {
|
||||
throw new Error(`[${name}] must be an object, instead received [${typeof value}]`);
|
||||
@@ -85,6 +116,16 @@ export default class Assert {
|
||||
}
|
||||
}
|
||||
|
||||
public static isInstance<T>(name: string, parentClass: new (...args: any[]) => T, object: unknown): asserts object is T {
|
||||
if (object === null || object === undefined || typeof object !== 'object') {
|
||||
throw new Error(`[${name}] must be an instance of [${parentClass.constructor.name}], instead received [${typeof object}]`);
|
||||
}
|
||||
|
||||
if (!(object instanceof parentClass)) {
|
||||
throw new Error(`[${name}] must be an instance of [${parentClass.constructor.name}], instead received [${object.constructor.name}]`);
|
||||
}
|
||||
}
|
||||
|
||||
private static _isBoolean(value: unknown): value is boolean {
|
||||
return typeof value === 'boolean';
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user