@noxfly/noxus 2.4.0 → 3.0.0-dev.0
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/README.md +403 -341
- package/dist/app-injector-Bz3Upc0y.d.mts +125 -0
- package/dist/app-injector-Bz3Upc0y.d.ts +125 -0
- package/dist/child.d.mts +48 -22
- package/dist/child.d.ts +48 -22
- package/dist/child.js +1114 -1239
- package/dist/child.mjs +1090 -1193
- package/dist/main.d.mts +304 -261
- package/dist/main.d.ts +304 -261
- package/dist/main.js +1473 -1873
- package/dist/main.mjs +1423 -1791
- package/dist/renderer.d.mts +113 -2
- package/dist/renderer.d.ts +113 -2
- package/dist/renderer.js +144 -132
- package/dist/renderer.mjs +143 -132
- package/dist/request-BlTtiHbi.d.ts +112 -0
- package/dist/request-qJ9EiDZc.d.mts +112 -0
- package/package.json +7 -7
- package/src/DI/app-injector.ts +95 -106
- package/src/DI/injector-explorer.ts +100 -81
- package/src/DI/token.ts +53 -0
- package/src/app.ts +141 -131
- package/src/bootstrap.ts +79 -40
- package/src/decorators/controller.decorator.ts +38 -27
- package/src/decorators/guards.decorator.ts +5 -64
- package/src/decorators/injectable.decorator.ts +68 -15
- package/src/decorators/method.decorator.ts +40 -81
- package/src/decorators/middleware.decorator.ts +5 -72
- package/src/index.ts +3 -0
- package/src/main.ts +4 -11
- package/src/non-electron-process.ts +0 -1
- package/src/preload-bridge.ts +1 -1
- package/src/renderer-client.ts +2 -2
- package/src/renderer-events.ts +1 -1
- package/src/request.ts +3 -3
- package/src/router.ts +221 -369
- package/src/routes.ts +78 -0
- package/src/socket.ts +4 -4
- package/src/window/window-manager.ts +255 -0
- package/tsconfig.json +5 -10
- package/tsup.config.ts +2 -2
- package/dist/app-injector-B3MvgV3k.d.mts +0 -95
- package/dist/app-injector-B3MvgV3k.d.ts +0 -95
- package/dist/index-BxWQVi6C.d.ts +0 -253
- package/dist/index-DQBQQfMw.d.mts +0 -253
- package/src/decorators/inject.decorator.ts +0 -24
- package/src/decorators/injectable.metadata.ts +0 -15
- package/src/decorators/module.decorator.ts +0 -75
package/src/routes.ts
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright 2025 NoxFly
|
|
3
|
+
* @license MIT
|
|
4
|
+
* @author NoxFly
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { Guard } from './decorators/guards.decorator';
|
|
8
|
+
import { Middleware } from './decorators/middleware.decorator';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* A single route entry in the application routing table.
|
|
12
|
+
*/
|
|
13
|
+
export interface RouteDefinition {
|
|
14
|
+
/**
|
|
15
|
+
* The path prefix for this route (e.g. 'users', 'orders').
|
|
16
|
+
* All actions defined in the controller will be prefixed with this path.
|
|
17
|
+
*/
|
|
18
|
+
path: string;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Dynamic import function returning the controller file.
|
|
22
|
+
* The controller is loaded lazily on the first IPC request targeting this prefix.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* load: () => import('./modules/users/users.controller')
|
|
26
|
+
*/
|
|
27
|
+
load: () => Promise<unknown>;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Guards applied to every action in this controller.
|
|
31
|
+
* Merged with action-level guards.
|
|
32
|
+
*/
|
|
33
|
+
guards?: Guard[];
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Middlewares applied to every action in this controller.
|
|
37
|
+
* Merged with action-level middlewares.
|
|
38
|
+
*/
|
|
39
|
+
middlewares?: Middleware[];
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Defines the application routing table.
|
|
44
|
+
* Each entry maps a path prefix to a lazily-loaded controller.
|
|
45
|
+
*
|
|
46
|
+
* This is the single source of truth for routing — no path is declared
|
|
47
|
+
* in @Controller(), preventing duplicate route prefixes across controllers.
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* export const routes = defineRoutes([
|
|
51
|
+
* {
|
|
52
|
+
* path: 'users',
|
|
53
|
+
* load: () => import('./modules/users/users.controller'),
|
|
54
|
+
* guards: [authGuard],
|
|
55
|
+
* },
|
|
56
|
+
* {
|
|
57
|
+
* path: 'orders',
|
|
58
|
+
* load: () => import('./modules/orders/orders.controller'),
|
|
59
|
+
* guards: [authGuard],
|
|
60
|
+
* middlewares: [logMiddleware],
|
|
61
|
+
* },
|
|
62
|
+
* ]);
|
|
63
|
+
*/
|
|
64
|
+
export function defineRoutes(routes: RouteDefinition[]): RouteDefinition[] {
|
|
65
|
+
const paths = routes.map(r => r.path.replace(/^\/+|\/+$/g, ''));
|
|
66
|
+
const duplicates = paths.filter((p, i) => paths.indexOf(p) !== i);
|
|
67
|
+
|
|
68
|
+
if (duplicates.length > 0) {
|
|
69
|
+
throw new Error(
|
|
70
|
+
`[Noxus] Duplicate route prefixes detected: ${[...new Set(duplicates)].map(d => `"${d}"`).join(', ')}`
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return routes.map(r => ({
|
|
75
|
+
...r,
|
|
76
|
+
path: r.path.replace(/^\/+|\/+$/g, ''),
|
|
77
|
+
}));
|
|
78
|
+
}
|
package/src/socket.ts
CHANGED
|
@@ -8,16 +8,16 @@
|
|
|
8
8
|
* Centralizes MessagePort storage for renderer communication and handles
|
|
9
9
|
* push-event delivery back to renderer processes.
|
|
10
10
|
*/
|
|
11
|
-
import { Injectable } from '
|
|
12
|
-
import { createRendererEventMessage } from '
|
|
13
|
-
import { Logger } from '
|
|
11
|
+
import { Injectable } from './decorators/injectable.decorator';
|
|
12
|
+
import { createRendererEventMessage } from './request';
|
|
13
|
+
import { Logger } from './utils/logger';
|
|
14
14
|
|
|
15
15
|
interface RendererChannels {
|
|
16
16
|
request: Electron.MessageChannelMain;
|
|
17
17
|
socket: Electron.MessageChannelMain;
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
@Injectable('singleton')
|
|
20
|
+
@Injectable({ lifetime: 'singleton' })
|
|
21
21
|
export class NoxSocket {
|
|
22
22
|
private readonly channels = new Map<number, RendererChannels>();
|
|
23
23
|
|
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright 2025 NoxFly
|
|
3
|
+
* @license MIT
|
|
4
|
+
* @author NoxFly
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { BrowserWindow, screen } from 'electron/main';
|
|
8
|
+
import { Injectable } from '../decorators/injectable.decorator';
|
|
9
|
+
import { Logger } from '../utils/logger';
|
|
10
|
+
|
|
11
|
+
export interface WindowConfig extends Electron.BrowserWindowConstructorOptions {
|
|
12
|
+
/**
|
|
13
|
+
* If true, the window expands to fill the work area after creation
|
|
14
|
+
* using an animated setBounds. The content is loaded only after
|
|
15
|
+
* the animation completes, preventing the viewbox freeze issue.
|
|
16
|
+
* @default false
|
|
17
|
+
*/
|
|
18
|
+
expandToWorkArea?: boolean;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Duration in ms to wait for the setBounds animation to complete
|
|
22
|
+
* before loading content. Only used when expandToWorkArea is true.
|
|
23
|
+
* @default 600
|
|
24
|
+
*/
|
|
25
|
+
expandAnimationDuration?: number;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface WindowRecord {
|
|
29
|
+
window: BrowserWindow;
|
|
30
|
+
id: number;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* WindowManager is a singleton service that centralizes BrowserWindow lifecycle.
|
|
35
|
+
*
|
|
36
|
+
* Features:
|
|
37
|
+
* - Creates and tracks all application windows.
|
|
38
|
+
* - Handles the animated expand-to-work-area pattern correctly,
|
|
39
|
+
* loading content only after the animation ends to avoid the viewbox freeze.
|
|
40
|
+
* - Provides convenience methods to get windows by id, get the main window, etc.
|
|
41
|
+
* - Automatically removes windows from the registry on close.
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* // In your IApp.onReady():
|
|
45
|
+
* const wm = inject(WindowManager);
|
|
46
|
+
*
|
|
47
|
+
* const win = await wm.create({
|
|
48
|
+
* width: 600, height: 600, center: true,
|
|
49
|
+
* expandToWorkArea: true,
|
|
50
|
+
* webPreferences: { preload: path.join(__dirname, 'preload.js') },
|
|
51
|
+
* });
|
|
52
|
+
*
|
|
53
|
+
* win.loadFile('index.html');
|
|
54
|
+
*/
|
|
55
|
+
@Injectable({ lifetime: 'singleton' })
|
|
56
|
+
export class WindowManager {
|
|
57
|
+
private readonly _windows = new Map<number, BrowserWindow>();
|
|
58
|
+
private _mainWindowId: number | undefined;
|
|
59
|
+
|
|
60
|
+
// -------------------------------------------------------------------------
|
|
61
|
+
// Creation
|
|
62
|
+
// -------------------------------------------------------------------------
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Creates a BrowserWindow, optionally performs an animated expand to the
|
|
66
|
+
* work area, and registers it in the manager.
|
|
67
|
+
*
|
|
68
|
+
* If expandToWorkArea is true:
|
|
69
|
+
* 1. The window is created at the given initial size (defaults to 600×600, centered).
|
|
70
|
+
* 2. An animated setBounds expands it to the full work area.
|
|
71
|
+
* 3. The returned promise resolves only after the animation, so callers
|
|
72
|
+
* can safely call win.loadFile() without the viewbox freeze.
|
|
73
|
+
*
|
|
74
|
+
* @param config Window configuration.
|
|
75
|
+
* @param isMain Mark this window as the main window (accessible via getMain()).
|
|
76
|
+
*/
|
|
77
|
+
public async create(config: WindowConfig, isMain = false): Promise<BrowserWindow> {
|
|
78
|
+
const {
|
|
79
|
+
expandToWorkArea = false,
|
|
80
|
+
expandAnimationDuration = 600,
|
|
81
|
+
...bwOptions
|
|
82
|
+
} = config;
|
|
83
|
+
|
|
84
|
+
// show: false by default during creation — we control visibility
|
|
85
|
+
const win = new BrowserWindow({ show: false, ...bwOptions });
|
|
86
|
+
|
|
87
|
+
this._register(win, isMain);
|
|
88
|
+
|
|
89
|
+
if (expandToWorkArea) {
|
|
90
|
+
await this._expandToWorkArea(win, expandAnimationDuration);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
win.once('ready-to-show', () => win.show());
|
|
94
|
+
|
|
95
|
+
Logger.log(`[WindowManager] Created window #${win.id}${isMain ? ' (main)' : ''}`);
|
|
96
|
+
|
|
97
|
+
return win;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Creates the initial "splash" window that is shown immediately after
|
|
102
|
+
* app.whenReady(). It is displayed instantly (show: true, no preload
|
|
103
|
+
* loading) and then expanded to the work area with animation.
|
|
104
|
+
*
|
|
105
|
+
* After the animation completes you can call win.loadFile() without
|
|
106
|
+
* experiencing the viewbox freeze.
|
|
107
|
+
*
|
|
108
|
+
* This is the recommended way to get pixels on screen as fast as possible.
|
|
109
|
+
*
|
|
110
|
+
* @example
|
|
111
|
+
* const win = await wm.createSplash({
|
|
112
|
+
* webPreferences: { preload: path.join(__dirname, 'preload.js') }
|
|
113
|
+
* });
|
|
114
|
+
* win.loadFile('index.html');
|
|
115
|
+
*/
|
|
116
|
+
public async createSplash(
|
|
117
|
+
options: Electron.BrowserWindowConstructorOptions & { animationDuration?: number } = {},
|
|
118
|
+
): Promise<BrowserWindow> {
|
|
119
|
+
const { animationDuration = 600, ...bwOptions } = options;
|
|
120
|
+
|
|
121
|
+
const win = new BrowserWindow({
|
|
122
|
+
width: 600,
|
|
123
|
+
height: 600,
|
|
124
|
+
center: true,
|
|
125
|
+
frame: false,
|
|
126
|
+
show: true,
|
|
127
|
+
...bwOptions,
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
this._register(win, true);
|
|
131
|
+
|
|
132
|
+
Logger.log(`[WindowManager] Splash window #${win.id} created`);
|
|
133
|
+
|
|
134
|
+
await this._expandToWorkArea(win, animationDuration);
|
|
135
|
+
|
|
136
|
+
return win;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// -------------------------------------------------------------------------
|
|
140
|
+
// Accessors
|
|
141
|
+
// -------------------------------------------------------------------------
|
|
142
|
+
|
|
143
|
+
/** Returns all currently open windows. */
|
|
144
|
+
public getAll(): BrowserWindow[] {
|
|
145
|
+
return [...this._windows.values()];
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** Returns the window designated as main, or undefined. */
|
|
149
|
+
public getMain(): BrowserWindow | undefined {
|
|
150
|
+
return this._mainWindowId !== undefined
|
|
151
|
+
? this._windows.get(this._mainWindowId)
|
|
152
|
+
: undefined;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** Returns a window by its Electron id, or undefined. */
|
|
156
|
+
public getById(id: number): BrowserWindow | undefined {
|
|
157
|
+
return this._windows.get(id);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** Returns the number of open windows. */
|
|
161
|
+
public get count(): number {
|
|
162
|
+
return this._windows.size;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// -------------------------------------------------------------------------
|
|
166
|
+
// Actions
|
|
167
|
+
// -------------------------------------------------------------------------
|
|
168
|
+
|
|
169
|
+
/** Closes and destroys a window by id. */
|
|
170
|
+
public close(id: number): void {
|
|
171
|
+
const win = this._windows.get(id);
|
|
172
|
+
if (!win) {
|
|
173
|
+
Logger.warn(`[WindowManager] Window #${id} not found`);
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
win.destroy();
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/** Closes all windows. */
|
|
180
|
+
public closeAll(): void {
|
|
181
|
+
for (const win of this._windows.values()) {
|
|
182
|
+
win.destroy();
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Sends a message to a specific window via webContents.send.
|
|
188
|
+
* @param id Target window id.
|
|
189
|
+
* @param channel IPC channel name.
|
|
190
|
+
* @param args Payload.
|
|
191
|
+
*/
|
|
192
|
+
public send(id: number, channel: string, ...args: unknown[]): void {
|
|
193
|
+
const win = this._windows.get(id);
|
|
194
|
+
if (!win || win.isDestroyed()) {
|
|
195
|
+
Logger.warn(`[WindowManager] Cannot send to window #${id}: not found or destroyed`);
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
win.webContents.send(channel, ...args);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Broadcasts a message to all open windows.
|
|
203
|
+
*/
|
|
204
|
+
public broadcast(channel: string, ...args: unknown[]): void {
|
|
205
|
+
for (const win of this._windows.values()) {
|
|
206
|
+
if (!win.isDestroyed()) win.webContents.send(channel, ...args);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// -------------------------------------------------------------------------
|
|
211
|
+
// Private
|
|
212
|
+
// -------------------------------------------------------------------------
|
|
213
|
+
|
|
214
|
+
private _register(win: BrowserWindow, isMain: boolean): void {
|
|
215
|
+
this._windows.set(win.id, win);
|
|
216
|
+
|
|
217
|
+
if (isMain && this._mainWindowId === undefined) {
|
|
218
|
+
this._mainWindowId = win.id;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
win.once('closed', () => {
|
|
222
|
+
this._windows.delete(win.id);
|
|
223
|
+
if (this._mainWindowId === win.id) this._mainWindowId = undefined;
|
|
224
|
+
Logger.log(`[WindowManager] Window #${win.id} closed`);
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Animates the window to the full work area of the primary display.
|
|
230
|
+
* Resolves only after the animation is complete, so that content loaded
|
|
231
|
+
* afterward gets the correct surface size (no viewbox freeze).
|
|
232
|
+
*/
|
|
233
|
+
private _expandToWorkArea(win: BrowserWindow, animationDuration: number): Promise<void> {
|
|
234
|
+
return new Promise((resolve) => {
|
|
235
|
+
const { x, y, width, height } = screen.getPrimaryDisplay().workArea;
|
|
236
|
+
|
|
237
|
+
win.setBounds({ x, y, width, height }, true);
|
|
238
|
+
|
|
239
|
+
// Wait for the animation to finish before resolving.
|
|
240
|
+
// We listen to the 'resize' event which fires once the OS
|
|
241
|
+
// animation completes, with a safety timeout as fallback.
|
|
242
|
+
let resolved = false;
|
|
243
|
+
|
|
244
|
+
const done = (): void => {
|
|
245
|
+
if (resolved) return;
|
|
246
|
+
resolved = true;
|
|
247
|
+
win.removeListener('resize', done);
|
|
248
|
+
resolve();
|
|
249
|
+
};
|
|
250
|
+
|
|
251
|
+
win.once('resize', done);
|
|
252
|
+
setTimeout(done, animationDuration + 100); // safety fallback
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
}
|
package/tsconfig.json
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"rootDir": "./src",
|
|
5
5
|
"baseUrl": "./",
|
|
6
6
|
"moduleResolution": "node",
|
|
7
|
-
"target": "
|
|
7
|
+
"target": "ES2020",
|
|
8
8
|
"module": "CommonJS",
|
|
9
9
|
"forceConsistentCasingInFileNames": true,
|
|
10
10
|
"strict": true,
|
|
@@ -13,22 +13,17 @@
|
|
|
13
13
|
"noFallthroughCasesInSwitch": true,
|
|
14
14
|
"sourceMap": true,
|
|
15
15
|
"experimentalDecorators": true,
|
|
16
|
-
"emitDecoratorMetadata":
|
|
16
|
+
"emitDecoratorMetadata": false,
|
|
17
17
|
"importHelpers": false,
|
|
18
18
|
"esModuleInterop": true,
|
|
19
19
|
"useDefineForClassFields": false,
|
|
20
20
|
"noPropertyAccessFromIndexSignature": false,
|
|
21
21
|
"noUncheckedIndexedAccess": true,
|
|
22
|
-
"skipLibCheck": true
|
|
23
|
-
"types": ["reflect-metadata"],
|
|
24
|
-
},
|
|
25
|
-
"tsc-alias": {
|
|
26
|
-
"resolveFullPaths": true,
|
|
27
|
-
"verbose": false,
|
|
22
|
+
"skipLibCheck": true
|
|
28
23
|
},
|
|
29
24
|
"include": [
|
|
30
25
|
"src/**/*.ts",
|
|
31
26
|
"src/**/*.d.ts",
|
|
32
|
-
"node_modules/electron/electron.d.ts"
|
|
33
|
-
]
|
|
27
|
+
"node_modules/electron/electron.d.ts"
|
|
28
|
+
]
|
|
34
29
|
}
|
package/tsup.config.ts
CHANGED
|
@@ -6,7 +6,7 @@ const copyrights = `
|
|
|
6
6
|
* @license MIT
|
|
7
7
|
* @author NoxFly
|
|
8
8
|
*/
|
|
9
|
-
`.trim()
|
|
9
|
+
`.trim();
|
|
10
10
|
|
|
11
11
|
export default defineConfig({
|
|
12
12
|
entry: {
|
|
@@ -30,5 +30,5 @@ export default defineConfig({
|
|
|
30
30
|
treeshake: false,
|
|
31
31
|
banner: {
|
|
32
32
|
js: copyrights,
|
|
33
|
-
}
|
|
33
|
+
},
|
|
34
34
|
});
|
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @copyright 2025 NoxFly
|
|
3
|
-
* @license MIT
|
|
4
|
-
* @author NoxFly
|
|
5
|
-
*/
|
|
6
|
-
interface Type<T> extends Function {
|
|
7
|
-
new (...args: any[]): T;
|
|
8
|
-
}
|
|
9
|
-
/**
|
|
10
|
-
* Represents a generic type that can be either a value or a promise resolving to that value.
|
|
11
|
-
*/
|
|
12
|
-
type MaybeAsync<T> = T | Promise<T>;
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* A function that returns a type.
|
|
17
|
-
* Used for forward references to types that are not yet defined.
|
|
18
|
-
*/
|
|
19
|
-
interface ForwardRefFn<T = any> {
|
|
20
|
-
(): Type<T>;
|
|
21
|
-
}
|
|
22
|
-
/**
|
|
23
|
-
* A wrapper class for forward referenced types.
|
|
24
|
-
*/
|
|
25
|
-
declare class ForwardReference<T = any> {
|
|
26
|
-
readonly forwardRefFn: ForwardRefFn<T>;
|
|
27
|
-
constructor(forwardRefFn: ForwardRefFn<T>);
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* Creates a forward reference to a type.
|
|
31
|
-
* @param fn A function that returns the type.
|
|
32
|
-
* @returns A ForwardReference instance.
|
|
33
|
-
*/
|
|
34
|
-
declare function forwardRef<T = any>(fn: ForwardRefFn<T>): ForwardReference<T>;
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Represents a lifetime of a binding in the dependency injection system.
|
|
39
|
-
* It can be one of the following:
|
|
40
|
-
* - 'singleton': The instance is created once and shared across the application.
|
|
41
|
-
* - 'scope': The instance is created once per scope (e.g., per request).
|
|
42
|
-
* - 'transient': A new instance is created every time it is requested.
|
|
43
|
-
*/
|
|
44
|
-
type Lifetime = 'singleton' | 'scope' | 'transient';
|
|
45
|
-
/**
|
|
46
|
-
* Represents a binding in the dependency injection system.
|
|
47
|
-
* It contains the lifetime of the binding, the implementation type, and optionally an instance.
|
|
48
|
-
*/
|
|
49
|
-
interface IBinding {
|
|
50
|
-
lifetime: Lifetime;
|
|
51
|
-
implementation: Type<unknown>;
|
|
52
|
-
instance?: InstanceType<Type<unknown>>;
|
|
53
|
-
}
|
|
54
|
-
/**
|
|
55
|
-
* AppInjector is the root dependency injection container.
|
|
56
|
-
* It is used to register and resolve dependencies in the application.
|
|
57
|
-
* It supports different lifetimes for dependencies:
|
|
58
|
-
* This should not be manually instantiated, outside of the framework.
|
|
59
|
-
* Use the `RootInjector` instance instead.
|
|
60
|
-
*/
|
|
61
|
-
declare class AppInjector {
|
|
62
|
-
readonly name: string | null;
|
|
63
|
-
bindings: Map<Type<unknown>, IBinding>;
|
|
64
|
-
singletons: Map<Type<unknown>, unknown>;
|
|
65
|
-
scoped: Map<Type<unknown>, unknown>;
|
|
66
|
-
constructor(name?: string | null);
|
|
67
|
-
/**
|
|
68
|
-
* Typically used to create a dependency injection scope
|
|
69
|
-
* at the "scope" level (i.e., per-request lifetime).
|
|
70
|
-
*
|
|
71
|
-
* SHOULD NOT BE USED by anything else than the framework itself.
|
|
72
|
-
*/
|
|
73
|
-
createScope(): AppInjector;
|
|
74
|
-
/**
|
|
75
|
-
* Called when resolving a dependency,
|
|
76
|
-
* i.e., retrieving the instance of a given class.
|
|
77
|
-
*/
|
|
78
|
-
resolve<T>(target: Type<T> | ForwardReference<T>): T;
|
|
79
|
-
/**
|
|
80
|
-
* Instantiates a class, resolving its dependencies.
|
|
81
|
-
*/
|
|
82
|
-
private instantiate;
|
|
83
|
-
}
|
|
84
|
-
/**
|
|
85
|
-
* Injects a type from the dependency injection system.
|
|
86
|
-
* This function is used to retrieve an instance of a type that has been registered in the dependency injection system.
|
|
87
|
-
* It is typically used in the constructor of a class to inject dependencies.
|
|
88
|
-
* @param t - The type to inject.
|
|
89
|
-
* @returns An instance of the type.
|
|
90
|
-
* @throws If the type is not registered in the dependency injection system.
|
|
91
|
-
*/
|
|
92
|
-
declare function inject<T>(t: Type<T> | ForwardReference<T>): T;
|
|
93
|
-
declare const RootInjector: AppInjector;
|
|
94
|
-
|
|
95
|
-
export { AppInjector as A, type ForwardRefFn as F, type IBinding as I, type Lifetime as L, type MaybeAsync as M, RootInjector as R, type Type as T, ForwardReference as a, forwardRef as f, inject as i };
|
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @copyright 2025 NoxFly
|
|
3
|
-
* @license MIT
|
|
4
|
-
* @author NoxFly
|
|
5
|
-
*/
|
|
6
|
-
interface Type<T> extends Function {
|
|
7
|
-
new (...args: any[]): T;
|
|
8
|
-
}
|
|
9
|
-
/**
|
|
10
|
-
* Represents a generic type that can be either a value or a promise resolving to that value.
|
|
11
|
-
*/
|
|
12
|
-
type MaybeAsync<T> = T | Promise<T>;
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* A function that returns a type.
|
|
17
|
-
* Used for forward references to types that are not yet defined.
|
|
18
|
-
*/
|
|
19
|
-
interface ForwardRefFn<T = any> {
|
|
20
|
-
(): Type<T>;
|
|
21
|
-
}
|
|
22
|
-
/**
|
|
23
|
-
* A wrapper class for forward referenced types.
|
|
24
|
-
*/
|
|
25
|
-
declare class ForwardReference<T = any> {
|
|
26
|
-
readonly forwardRefFn: ForwardRefFn<T>;
|
|
27
|
-
constructor(forwardRefFn: ForwardRefFn<T>);
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* Creates a forward reference to a type.
|
|
31
|
-
* @param fn A function that returns the type.
|
|
32
|
-
* @returns A ForwardReference instance.
|
|
33
|
-
*/
|
|
34
|
-
declare function forwardRef<T = any>(fn: ForwardRefFn<T>): ForwardReference<T>;
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Represents a lifetime of a binding in the dependency injection system.
|
|
39
|
-
* It can be one of the following:
|
|
40
|
-
* - 'singleton': The instance is created once and shared across the application.
|
|
41
|
-
* - 'scope': The instance is created once per scope (e.g., per request).
|
|
42
|
-
* - 'transient': A new instance is created every time it is requested.
|
|
43
|
-
*/
|
|
44
|
-
type Lifetime = 'singleton' | 'scope' | 'transient';
|
|
45
|
-
/**
|
|
46
|
-
* Represents a binding in the dependency injection system.
|
|
47
|
-
* It contains the lifetime of the binding, the implementation type, and optionally an instance.
|
|
48
|
-
*/
|
|
49
|
-
interface IBinding {
|
|
50
|
-
lifetime: Lifetime;
|
|
51
|
-
implementation: Type<unknown>;
|
|
52
|
-
instance?: InstanceType<Type<unknown>>;
|
|
53
|
-
}
|
|
54
|
-
/**
|
|
55
|
-
* AppInjector is the root dependency injection container.
|
|
56
|
-
* It is used to register and resolve dependencies in the application.
|
|
57
|
-
* It supports different lifetimes for dependencies:
|
|
58
|
-
* This should not be manually instantiated, outside of the framework.
|
|
59
|
-
* Use the `RootInjector` instance instead.
|
|
60
|
-
*/
|
|
61
|
-
declare class AppInjector {
|
|
62
|
-
readonly name: string | null;
|
|
63
|
-
bindings: Map<Type<unknown>, IBinding>;
|
|
64
|
-
singletons: Map<Type<unknown>, unknown>;
|
|
65
|
-
scoped: Map<Type<unknown>, unknown>;
|
|
66
|
-
constructor(name?: string | null);
|
|
67
|
-
/**
|
|
68
|
-
* Typically used to create a dependency injection scope
|
|
69
|
-
* at the "scope" level (i.e., per-request lifetime).
|
|
70
|
-
*
|
|
71
|
-
* SHOULD NOT BE USED by anything else than the framework itself.
|
|
72
|
-
*/
|
|
73
|
-
createScope(): AppInjector;
|
|
74
|
-
/**
|
|
75
|
-
* Called when resolving a dependency,
|
|
76
|
-
* i.e., retrieving the instance of a given class.
|
|
77
|
-
*/
|
|
78
|
-
resolve<T>(target: Type<T> | ForwardReference<T>): T;
|
|
79
|
-
/**
|
|
80
|
-
* Instantiates a class, resolving its dependencies.
|
|
81
|
-
*/
|
|
82
|
-
private instantiate;
|
|
83
|
-
}
|
|
84
|
-
/**
|
|
85
|
-
* Injects a type from the dependency injection system.
|
|
86
|
-
* This function is used to retrieve an instance of a type that has been registered in the dependency injection system.
|
|
87
|
-
* It is typically used in the constructor of a class to inject dependencies.
|
|
88
|
-
* @param t - The type to inject.
|
|
89
|
-
* @returns An instance of the type.
|
|
90
|
-
* @throws If the type is not registered in the dependency injection system.
|
|
91
|
-
*/
|
|
92
|
-
declare function inject<T>(t: Type<T> | ForwardReference<T>): T;
|
|
93
|
-
declare const RootInjector: AppInjector;
|
|
94
|
-
|
|
95
|
-
export { AppInjector as A, type ForwardRefFn as F, type IBinding as I, type Lifetime as L, type MaybeAsync as M, RootInjector as R, type Type as T, ForwardReference as a, forwardRef as f, inject as i };
|