topsyde-utils 1.3.2 → 2.0.1
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.
- package/dist/index.d.ts +2 -43
- package/dist/index.js +1 -38
- package/dist/index.js.map +1 -1
- package/dist/utils/Lib.d.ts +0 -12
- package/dist/utils/Lib.js +0 -65
- package/dist/utils/Lib.js.map +1 -1
- package/dist/utils/index.d.ts +0 -3
- package/dist/utils/index.js +0 -3
- package/dist/utils/index.js.map +1 -1
- package/dist/websocket.shared.types.d.ts +25 -0
- package/dist/websocket.shared.types.js +4 -0
- package/dist/websocket.shared.types.js.map +1 -0
- package/package.json +12 -66
- package/src/__tests__/singleton.test.ts +0 -143
- package/src/index.ts +2 -83
- package/src/utils/Lib.ts +0 -77
- package/src/utils/index.ts +0 -3
- package/src/websocket.shared.types.ts +27 -0
- package/dist/application.d.ts +0 -18
- package/dist/application.js +0 -60
- package/dist/application.js.map +0 -1
- package/dist/client/api/base.api.d.ts +0 -63
- package/dist/client/api/base.api.js +0 -61
- package/dist/client/api/base.api.js.map +0 -1
- package/dist/client/api/index.d.ts +0 -2
- package/dist/client/api/index.js +0 -5
- package/dist/client/api/index.js.map +0 -1
- package/dist/client/rxjs/index.d.ts +0 -1
- package/dist/client/rxjs/index.js +0 -4
- package/dist/client/rxjs/index.js.map +0 -1
- package/dist/client/rxjs/useRxjs.d.ts +0 -17
- package/dist/client/rxjs/useRxjs.js +0 -87
- package/dist/client/rxjs/useRxjs.js.map +0 -1
- package/dist/client/vite/plugins/index.d.ts +0 -2
- package/dist/client/vite/plugins/index.js +0 -5
- package/dist/client/vite/plugins/index.js.map +0 -1
- package/dist/client/vite/plugins/topsydeUtilsVitePlugin.d.ts +0 -9
- package/dist/client/vite/plugins/topsydeUtilsVitePlugin.js +0 -74
- package/dist/client/vite/plugins/topsydeUtilsVitePlugin.js.map +0 -1
- package/dist/external/index.d.ts +0 -1
- package/dist/external/index.js +0 -4
- package/dist/external/index.js.map +0 -1
- package/dist/external/re-exports.d.ts +0 -16
- package/dist/external/re-exports.js +0 -24
- package/dist/external/re-exports.js.map +0 -1
- package/dist/server/base/base.database.d.ts +0 -10
- package/dist/server/base/base.database.js +0 -23
- package/dist/server/base/base.database.js.map +0 -1
- package/dist/server/base/index.d.ts +0 -2
- package/dist/server/base/index.js +0 -5
- package/dist/server/base/index.js.map +0 -1
- package/dist/server/bun/index.d.ts +0 -3
- package/dist/server/bun/index.js +0 -6
- package/dist/server/bun/index.js.map +0 -1
- package/dist/server/bun/router/controller-discovery.d.ts +0 -13
- package/dist/server/bun/router/controller-discovery.js +0 -83
- package/dist/server/bun/router/controller-discovery.js.map +0 -1
- package/dist/server/bun/router/index.d.ts +0 -6
- package/dist/server/bun/router/index.js +0 -9
- package/dist/server/bun/router/index.js.map +0 -1
- package/dist/server/bun/router/router.d.ts +0 -12
- package/dist/server/bun/router/router.internal.d.ts +0 -15
- package/dist/server/bun/router/router.internal.js +0 -51
- package/dist/server/bun/router/router.internal.js.map +0 -1
- package/dist/server/bun/router/router.js +0 -38
- package/dist/server/bun/router/router.js.map +0 -1
- package/dist/server/bun/router/routes.d.ts +0 -5
- package/dist/server/bun/router/routes.js +0 -2
- package/dist/server/bun/router/routes.js.map +0 -1
- package/dist/server/bun/websocket/Channel.d.ts +0 -68
- package/dist/server/bun/websocket/Channel.js +0 -263
- package/dist/server/bun/websocket/Channel.js.map +0 -1
- package/dist/server/bun/websocket/Client.d.ts +0 -87
- package/dist/server/bun/websocket/Client.js +0 -193
- package/dist/server/bun/websocket/Client.js.map +0 -1
- package/dist/server/bun/websocket/Message.d.ts +0 -10
- package/dist/server/bun/websocket/Message.js +0 -103
- package/dist/server/bun/websocket/Message.js.map +0 -1
- package/dist/server/bun/websocket/Websocket.d.ts +0 -171
- package/dist/server/bun/websocket/Websocket.js +0 -336
- package/dist/server/bun/websocket/Websocket.js.map +0 -1
- package/dist/server/bun/websocket/index.d.ts +0 -11
- package/dist/server/bun/websocket/index.js +0 -14
- package/dist/server/bun/websocket/index.js.map +0 -1
- package/dist/server/bun/websocket/websocket.enums.d.ts +0 -27
- package/dist/server/bun/websocket/websocket.enums.js +0 -31
- package/dist/server/bun/websocket/websocket.enums.js.map +0 -1
- package/dist/server/bun/websocket/websocket.guards.d.ts +0 -3
- package/dist/server/bun/websocket/websocket.guards.js +0 -17
- package/dist/server/bun/websocket/websocket.guards.js.map +0 -1
- package/dist/server/bun/websocket/websocket.types.d.ts +0 -235
- package/dist/server/bun/websocket/websocket.types.js +0 -2
- package/dist/server/bun/websocket/websocket.types.js.map +0 -1
- package/dist/server/controller.d.ts +0 -62
- package/dist/server/controller.js +0 -55
- package/dist/server/controller.js.map +0 -1
- package/dist/server/index.d.ts +0 -4
- package/dist/server/index.js +0 -7
- package/dist/server/index.js.map +0 -1
- package/dist/server/service.d.ts +0 -5
- package/dist/server/service.js +0 -38
- package/dist/server/service.js.map +0 -1
- package/dist/utils/BaseDto.d.ts +0 -33
- package/dist/utils/BaseDto.js +0 -69
- package/dist/utils/BaseDto.js.map +0 -1
- package/dist/utils/BaseEntity.d.ts +0 -31
- package/dist/utils/BaseEntity.js +0 -37
- package/dist/utils/BaseEntity.js.map +0 -1
- package/dist/utils/dto_validators/IsNumberOrRangeConstraint.d.ts +0 -9
- package/dist/utils/dto_validators/IsNumberOrRangeConstraint.js +0 -85
- package/dist/utils/dto_validators/IsNumberOrRangeConstraint.js.map +0 -1
- package/dist/utils/dto_validators/index.d.ts +0 -1
- package/dist/utils/dto_validators/index.js +0 -4
- package/dist/utils/dto_validators/index.js.map +0 -1
- package/src/__tests__/app.test.ts +0 -206
- package/src/application.ts +0 -73
- package/src/client/api/base.api.ts +0 -111
- package/src/client/api/index.ts +0 -5
- package/src/client/rxjs/index.ts +0 -4
- package/src/client/rxjs/useRxjs.ts +0 -113
- package/src/client/vite/plugins/index.ts +0 -5
- package/src/client/vite/plugins/topsydeUtilsVitePlugin.ts +0 -80
- package/src/external/index.ts +0 -4
- package/src/external/re-exports.ts +0 -54
- package/src/server/base/base.database.ts +0 -31
- package/src/server/base/index.ts +0 -5
- package/src/server/bun/index.ts +0 -6
- package/src/server/bun/router/controller-discovery.ts +0 -94
- package/src/server/bun/router/index.ts +0 -9
- package/src/server/bun/router/router.internal.ts +0 -64
- package/src/server/bun/router/router.ts +0 -51
- package/src/server/bun/router/routes.ts +0 -7
- package/src/server/bun/websocket/Channel.ts +0 -310
- package/src/server/bun/websocket/Client.ts +0 -243
- package/src/server/bun/websocket/ISSUES.md +0 -1175
- package/src/server/bun/websocket/Message.ts +0 -120
- package/src/server/bun/websocket/Websocket.ts +0 -402
- package/src/server/bun/websocket/index.ts +0 -14
- package/src/server/bun/websocket/websocket.enums.ts +0 -29
- package/src/server/bun/websocket/websocket.guards.ts +0 -22
- package/src/server/bun/websocket/websocket.types.ts +0 -252
- package/src/server/controller.ts +0 -121
- package/src/server/index.ts +0 -7
- package/src/server/service.ts +0 -36
- package/src/utils/BaseDto.ts +0 -77
- package/src/utils/BaseEntity.ts +0 -49
- package/src/utils/dto_validators/IsNumberOrRangeConstraint.ts +0 -32
- package/src/utils/dto_validators/index.ts +0 -4
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
import type { Plugin } from "vite";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Creates a Vite plugin that provides compatibility for topsyde-utils in browser environments
|
|
5
|
-
* by disabling sourcemaps and providing mock implementations of Node.js modules.
|
|
6
|
-
*
|
|
7
|
-
* @returns A single Vite plugin with all necessary functionality
|
|
8
|
-
*/
|
|
9
|
-
export function TopsydeUtilsVitePlugin(): Plugin {
|
|
10
|
-
return {
|
|
11
|
-
name: "topsyde-utils-compatibility",
|
|
12
|
-
|
|
13
|
-
// Disable sourcemaps for topsyde-utils
|
|
14
|
-
transform(code, id) {
|
|
15
|
-
if (id.includes("node_modules/topsyde-utils")) {
|
|
16
|
-
return {
|
|
17
|
-
code,
|
|
18
|
-
map: { mappings: "" }, // Return empty sourcemap
|
|
19
|
-
};
|
|
20
|
-
}
|
|
21
|
-
},
|
|
22
|
-
|
|
23
|
-
// Handle virtual modules for Node.js built-ins
|
|
24
|
-
resolveId(id) {
|
|
25
|
-
if (id === "virtual:path" || id === "virtual:fs") {
|
|
26
|
-
return id;
|
|
27
|
-
}
|
|
28
|
-
return null;
|
|
29
|
-
},
|
|
30
|
-
|
|
31
|
-
// Provide virtual module implementations
|
|
32
|
-
load(id) {
|
|
33
|
-
if (id === "virtual:path") {
|
|
34
|
-
return `
|
|
35
|
-
export function join() { return ''; }
|
|
36
|
-
export function resolve() { return ''; }
|
|
37
|
-
export function dirname() { return ''; }
|
|
38
|
-
export function basename() { return ''; }
|
|
39
|
-
export function extname() { return ''; }
|
|
40
|
-
export default { join, resolve, dirname, basename, extname };
|
|
41
|
-
`;
|
|
42
|
-
}
|
|
43
|
-
if (id === "virtual:fs") {
|
|
44
|
-
return `
|
|
45
|
-
export function readFileSync() { return ''; }
|
|
46
|
-
export function existsSync() { return false; }
|
|
47
|
-
export function writeFileSync() { return null; }
|
|
48
|
-
export function readdirSync() { return []; }
|
|
49
|
-
export function statSync() {
|
|
50
|
-
return {
|
|
51
|
-
isDirectory: () => false,
|
|
52
|
-
isFile: () => true
|
|
53
|
-
};
|
|
54
|
-
}
|
|
55
|
-
export default { readFileSync, existsSync, writeFileSync, readdirSync, statSync };
|
|
56
|
-
`;
|
|
57
|
-
}
|
|
58
|
-
return null;
|
|
59
|
-
},
|
|
60
|
-
|
|
61
|
-
// Configure aliases and optimization
|
|
62
|
-
config() {
|
|
63
|
-
return {
|
|
64
|
-
resolve: {
|
|
65
|
-
alias: {
|
|
66
|
-
// Alias Node.js built-ins to virtual modules
|
|
67
|
-
path: "virtual:path",
|
|
68
|
-
fs: "virtual:fs",
|
|
69
|
-
},
|
|
70
|
-
},
|
|
71
|
-
optimizeDeps: {
|
|
72
|
-
exclude: ["topsyde-utils"],
|
|
73
|
-
},
|
|
74
|
-
};
|
|
75
|
-
},
|
|
76
|
-
};
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
// Export as default for compatibility with different import styles
|
|
80
|
-
export default TopsydeUtilsVitePlugin;
|
package/src/external/index.ts
DELETED
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Re-exports of external dependencies for convenience
|
|
3
|
-
*
|
|
4
|
-
* This module re-exports commonly used types, classes, and decorators from
|
|
5
|
-
* class-transformer and class-validator to provide a single import source
|
|
6
|
-
* for projects using topsyde-utils.
|
|
7
|
-
*
|
|
8
|
-
* This prevents the need to separately install these dependencies in
|
|
9
|
-
* consuming projects, as they're already bundled with topsyde-utils.
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
// ============================================================================
|
|
13
|
-
// class-transformer exports
|
|
14
|
-
// ============================================================================
|
|
15
|
-
|
|
16
|
-
// Types
|
|
17
|
-
export type { ClassConstructor, ClassTransformOptions } from "class-transformer";
|
|
18
|
-
|
|
19
|
-
// Core transformation functions
|
|
20
|
-
export { instanceToPlain, plainToInstance } from "class-transformer";
|
|
21
|
-
|
|
22
|
-
// Decorators
|
|
23
|
-
export { Expose } from "class-transformer";
|
|
24
|
-
|
|
25
|
-
// ============================================================================
|
|
26
|
-
// class-validator exports
|
|
27
|
-
// ============================================================================
|
|
28
|
-
|
|
29
|
-
// Types and Classes
|
|
30
|
-
export { ValidationError } from "class-validator";
|
|
31
|
-
|
|
32
|
-
// Validation function
|
|
33
|
-
export { validateSync } from "class-validator";
|
|
34
|
-
|
|
35
|
-
// Property decorators
|
|
36
|
-
export {
|
|
37
|
-
IsArray,
|
|
38
|
-
IsBoolean,
|
|
39
|
-
IsDate,
|
|
40
|
-
IsEnum,
|
|
41
|
-
IsNumber,
|
|
42
|
-
IsObject,
|
|
43
|
-
IsOptional,
|
|
44
|
-
IsString,
|
|
45
|
-
Validate,
|
|
46
|
-
IsAlphanumeric,
|
|
47
|
-
IsBooleanString,
|
|
48
|
-
IsDateString,
|
|
49
|
-
IsDecimal,
|
|
50
|
-
IsDefined,
|
|
51
|
-
IsNumberString,
|
|
52
|
-
IsNotEmpty,
|
|
53
|
-
IsNotEmptyObject,
|
|
54
|
-
} from "class-validator";
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import Singleton from "../../singleton";
|
|
2
|
-
|
|
3
|
-
abstract class Database extends Singleton {
|
|
4
|
-
constructor() {
|
|
5
|
-
super();
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
public abstract connect(): Promise<void>;
|
|
9
|
-
public abstract disconnect(): Promise<void>;
|
|
10
|
-
|
|
11
|
-
protected connected: boolean = false;
|
|
12
|
-
protected processing: boolean = false;
|
|
13
|
-
|
|
14
|
-
public static async Connection<T extends (new (...args: any[]) => Database) & typeof Singleton>(
|
|
15
|
-
this: T,
|
|
16
|
-
...args: ConstructorParameters<T>
|
|
17
|
-
): Promise<InstanceType<T>> {
|
|
18
|
-
try {
|
|
19
|
-
const instance = this.GetInstance(...args) as InstanceType<T>;
|
|
20
|
-
if (!instance.connected && !instance.processing) {
|
|
21
|
-
await instance.connect();
|
|
22
|
-
}
|
|
23
|
-
return instance;
|
|
24
|
-
} catch (e) {
|
|
25
|
-
console.error(e);
|
|
26
|
-
throw e;
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export default Database;
|
package/src/server/base/index.ts
DELETED
package/src/server/bun/index.ts
DELETED
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
import { join } from 'path';
|
|
2
|
-
import { readdirSync, statSync } from 'fs';
|
|
3
|
-
import { Lib } from '../../../utils';
|
|
4
|
-
import { Routes } from './routes';
|
|
5
|
-
|
|
6
|
-
const fallbackRoutes: Routes = {};
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Dynamically discovers and loads controllers from the components directory
|
|
10
|
-
*/
|
|
11
|
-
export class ControllerDiscovery {
|
|
12
|
-
public static async DiscoverRoutes(componentPaths: string[]) {
|
|
13
|
-
try {
|
|
14
|
-
const allDiscoveredRoutes: Routes = {};
|
|
15
|
-
|
|
16
|
-
// Discover controllers in all specified component paths
|
|
17
|
-
for (const path of componentPaths) {
|
|
18
|
-
const discoveredRoutes = await ControllerDiscovery.Find(path);
|
|
19
|
-
|
|
20
|
-
// Merge discovered routes
|
|
21
|
-
Object.assign(allDiscoveredRoutes, discoveredRoutes);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
// Use discovered routes if any were found, otherwise use fallback
|
|
25
|
-
if (Object.keys(allDiscoveredRoutes).length > 0) {
|
|
26
|
-
Lib.Log(`Using auto-discovered routes from paths: ${componentPaths.join(', ')}`);
|
|
27
|
-
return allDiscoveredRoutes;
|
|
28
|
-
} else {
|
|
29
|
-
Lib.Log('No routes discovered, using fallback routes');
|
|
30
|
-
return fallbackRoutes;
|
|
31
|
-
}
|
|
32
|
-
} catch (error) {
|
|
33
|
-
// If auto-discovery fails, use fallback routes
|
|
34
|
-
Lib.Warn('Controller auto-discovery failed, using fallback routes:', error);
|
|
35
|
-
return fallbackRoutes;
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Discovers controllers in the specified directory
|
|
41
|
-
* @param componentsPath Optional custom path to components directory (relative to project root)
|
|
42
|
-
* @returns Routes object with discovered controllers
|
|
43
|
-
*/
|
|
44
|
-
public static async Find(componentsPath?: string): Promise<Routes> {
|
|
45
|
-
const routes: Routes = {};
|
|
46
|
-
|
|
47
|
-
// Get project root - use process.cwd() to get the root of the project using this library
|
|
48
|
-
const projectRoot = process.cwd();
|
|
49
|
-
|
|
50
|
-
// Use provided path or default to components directory
|
|
51
|
-
const componentsDir = componentsPath
|
|
52
|
-
? join(projectRoot, componentsPath) // From project root
|
|
53
|
-
: join(projectRoot, 'src', 'components'); // Default location
|
|
54
|
-
|
|
55
|
-
Lib.Log(`Looking for controllers in: ${componentsDir}`);
|
|
56
|
-
|
|
57
|
-
try {
|
|
58
|
-
// Get all component directories
|
|
59
|
-
const componentFolders = readdirSync(componentsDir).filter(folder =>
|
|
60
|
-
statSync(join(componentsDir, folder)).isDirectory()
|
|
61
|
-
);
|
|
62
|
-
|
|
63
|
-
// Process each component folder
|
|
64
|
-
for (const componentName of componentFolders) {
|
|
65
|
-
const controllerPath = join(componentsDir, componentName, `${componentName}.controller.ts`);
|
|
66
|
-
|
|
67
|
-
try {
|
|
68
|
-
// Check if controller file exists
|
|
69
|
-
const controllerFile = Bun.file(controllerPath);
|
|
70
|
-
if (await controllerFile.exists()) {
|
|
71
|
-
// Import the controller
|
|
72
|
-
try {
|
|
73
|
-
const module = await import(controllerPath);
|
|
74
|
-
const Controller = module.default;
|
|
75
|
-
|
|
76
|
-
if (Controller && typeof Controller === 'function') {
|
|
77
|
-
routes[componentName] = Controller;
|
|
78
|
-
Lib.Log(`Registered controller: ${componentName}`);
|
|
79
|
-
}
|
|
80
|
-
} catch (err) {
|
|
81
|
-
Lib.Warn(`Failed to import controller for ${componentName}:`, err);
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
} catch (err) {
|
|
85
|
-
Lib.Warn(`Error processing component ${componentName}:`, err);
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
} catch (err) {
|
|
89
|
-
Lib.Warn(`Error discovering controllers in ${componentsDir}:`, err);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
return routes;
|
|
93
|
-
}
|
|
94
|
-
}
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
// This file is auto-generated by scripts/generate-indexes.ts
|
|
2
|
-
// Do not edit this file directly
|
|
3
|
-
|
|
4
|
-
export * from './controller-discovery';
|
|
5
|
-
export * from './routes';
|
|
6
|
-
export * from './router';
|
|
7
|
-
export * from './router.internal';
|
|
8
|
-
export { default as Router } from './router';
|
|
9
|
-
export { default as Router_Internal } from './router.internal';
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
import { Throwable } from "../../..";
|
|
2
|
-
import { ERROR_CODE } from "../../../errors";
|
|
3
|
-
import Controller from "../../controller";
|
|
4
|
-
import { Debug } from "../../../utils/Lib";
|
|
5
|
-
import { Routes } from "./routes";
|
|
6
|
-
|
|
7
|
-
class Router_Internal {
|
|
8
|
-
private registry = new Map<string, Controller>();
|
|
9
|
-
private routes: Routes;
|
|
10
|
-
|
|
11
|
-
constructor(routes?: Routes) {
|
|
12
|
-
this.routes = routes ?? {};
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
async post<T>(req: Request): Promise<T> {
|
|
16
|
-
return await this.handleRequest(req);
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
async get<T>(req: Request): Promise<T> {
|
|
20
|
-
return await this.handleRequest(req);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
private async handleRequest<T>(request: Request): Promise<T> {
|
|
24
|
-
const path = this.getPath(request);
|
|
25
|
-
const output = await this.resolve(path).call<T>(request);
|
|
26
|
-
return output;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
private getPath(request: Request): string {
|
|
30
|
-
return new URL(request.url).pathname;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
private resolve(path: string): Controller {
|
|
34
|
-
const controllerKey = path.split("/")[1];
|
|
35
|
-
return this.register(controllerKey, () => this.controllerFactory(controllerKey));
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
private register(controllerKey: string, factory: () => Controller): Controller {
|
|
39
|
-
if (!this.registry.has(controllerKey)) {
|
|
40
|
-
this.registry.set(controllerKey, factory());
|
|
41
|
-
Debug.Log(`Caching controller: /${controllerKey}`);
|
|
42
|
-
}
|
|
43
|
-
return this.registry.get(controllerKey) as Controller;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
public setRoutes(routes: Routes): void {
|
|
47
|
-
this.routes = routes;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
private controllerFactory(controllerKey: string): Controller {
|
|
51
|
-
try {
|
|
52
|
-
if (!(controllerKey in this.routes)) throw new Throwable(`${ERROR_CODE.INVALID_CONTROLLER}: ${controllerKey}`, { logError: false });
|
|
53
|
-
|
|
54
|
-
const ControllerClass = this.routes[controllerKey as keyof typeof this.routes];
|
|
55
|
-
|
|
56
|
-
return new ControllerClass();
|
|
57
|
-
} catch (err) {
|
|
58
|
-
console.error("controllerFactory", err);
|
|
59
|
-
throw err;
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
export default Router_Internal;
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import { ERROR_CODE } from "../../../errors";
|
|
2
|
-
import Singleton from "../../../singleton";
|
|
3
|
-
import Guards from "../../../utils/Guards";
|
|
4
|
-
import Router_Internal from "./router.internal";
|
|
5
|
-
import { Routes } from "./routes";
|
|
6
|
-
|
|
7
|
-
type MethodMap<T> = {
|
|
8
|
-
[method: string]: (req: Request) => Promise<T>;
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
class Router extends Singleton {
|
|
12
|
-
private internal: Router_Internal;
|
|
13
|
-
|
|
14
|
-
public constructor(routes?: Routes) {
|
|
15
|
-
super();
|
|
16
|
-
this.internal = new Router_Internal(routes);
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
private setRoutes(routes: Routes): void {
|
|
20
|
-
this.internal.setRoutes(routes);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
public static async Call<T>(request: Request): Promise<T> {
|
|
24
|
-
if (Guards.IsNil(request)) throw ERROR_CODE.NO_REQUEST;
|
|
25
|
-
const methods: MethodMap<T> = this.getMethodMap();
|
|
26
|
-
const method = methods[request.method];
|
|
27
|
-
|
|
28
|
-
if (Guards.IsNil(method)) throw ERROR_CODE.INVALID_METHOD;
|
|
29
|
-
|
|
30
|
-
return await method(request);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
public static SetRoutes(routes: Routes) {
|
|
34
|
-
this.GetInstance<Router>().setRoutes(routes);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
private static getMethodMap<T>(): MethodMap<T> {
|
|
38
|
-
const router = this.GetInstance<Router>();
|
|
39
|
-
return {
|
|
40
|
-
GET: async (req) => await router.internal.get(req),
|
|
41
|
-
POST: async (req) => await router.internal.post(req),
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
public static GetQueryParams(request: Request): URLSearchParams {
|
|
46
|
-
const url = new URL(request.url);
|
|
47
|
-
return url.searchParams;
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export default Router;
|