@noxfly/noxus 1.0.4 → 1.1.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/.editorconfig +16 -0
- package/README.md +108 -6
- package/dist/noxus.d.mts +108 -12
- package/dist/noxus.d.ts +108 -12
- package/dist/noxus.js +287 -85
- package/dist/noxus.mjs +280 -85
- package/dist/noxus.mjs.map +1 -1
- package/eslint.config.js +1 -0
- package/package.json +3 -2
- package/scripts/postbuild.js +26 -0
- package/src/DI/app-injector.ts +15 -2
- package/src/DI/injector-explorer.ts +6 -0
- package/src/app.ts +153 -0
- package/src/bootstrap.ts +13 -112
- package/src/decorators/controller.decorator.ts +6 -0
- package/src/decorators/guards.decorator.ts +8 -2
- package/src/decorators/injectable.decorator.ts +6 -0
- package/src/decorators/method.decorator.ts +6 -0
- package/src/decorators/middleware.decorator.ts +54 -0
- package/src/decorators/module.decorator.ts +5 -1
- package/src/exceptions.ts +48 -26
- package/src/index.ts +7 -0
- package/src/request.ts +9 -7
- package/src/router.ts +103 -19
- package/src/utils/logger.ts +6 -0
- package/src/utils/radix-tree.ts +6 -0
- package/src/utils/types.ts +6 -0
- package/tsup.config.ts +11 -0
- package/dist/noxus.js.map +0 -1
- package/images/screenshot-requests.png +0 -0
- package/images/screenshot-startup.png +0 -0
package/.editorconfig
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# Editor configuration, see https://editorconfig.org
|
|
2
|
+
root = true
|
|
3
|
+
|
|
4
|
+
[*]
|
|
5
|
+
charset = utf-8
|
|
6
|
+
indent_style = space
|
|
7
|
+
indent_size = 4
|
|
8
|
+
insert_final_newline = true
|
|
9
|
+
trim_trailing_whitespace = true
|
|
10
|
+
|
|
11
|
+
[*.ts]
|
|
12
|
+
quote_type = single
|
|
13
|
+
|
|
14
|
+
[*.md]
|
|
15
|
+
max_line_length = off
|
|
16
|
+
trim_trailing_whitespace = false
|
package/README.md
CHANGED
|
@@ -62,10 +62,15 @@ However, you can feel free to keep both merged, this won't change anything, but
|
|
|
62
62
|
// main/index.ts
|
|
63
63
|
|
|
64
64
|
import { bootstrapApplication } from '@noxfly/noxus';
|
|
65
|
+
import { AppModule } from './modules/app.module.ts';
|
|
65
66
|
import { Application } from './modules/app.service.ts';
|
|
66
67
|
|
|
67
|
-
async function main() {
|
|
68
|
-
const
|
|
68
|
+
async function main(): Promise<void> {
|
|
69
|
+
const noxApp = await bootstrapApplication(AppModule);
|
|
70
|
+
|
|
71
|
+
noxApp.configure(Application);
|
|
72
|
+
|
|
73
|
+
noxApp.start();
|
|
69
74
|
}
|
|
70
75
|
|
|
71
76
|
main();
|
|
@@ -205,9 +210,9 @@ type fn = (...args: any[]) => void;
|
|
|
205
210
|
contextBridge.exposeInMainWorld('ipcRenderer', {
|
|
206
211
|
requestPort: () => ipcRenderer.send('gimme-my-port'),
|
|
207
212
|
|
|
208
|
-
hereIsMyPort: () => ipcRenderer.once('port', (e) => {
|
|
213
|
+
hereIsMyPort: () => ipcRenderer.once('port', (e, message) => {
|
|
209
214
|
e.ports[0]?.start();
|
|
210
|
-
window.postMessage({ type: 'init-port' }, '*', [e.ports[0]!]);
|
|
215
|
+
window.postMessage({ type: 'init-port', senderId: message.senderId }, '*', [e.ports[0]!]);
|
|
211
216
|
}),
|
|
212
217
|
});
|
|
213
218
|
```
|
|
@@ -240,6 +245,7 @@ class ElectronService {
|
|
|
240
245
|
private readonly ipcRenderer: any; // if you know how to get a type, tell me
|
|
241
246
|
|
|
242
247
|
private port: MessagePort | undefined;
|
|
248
|
+
private senderId: number | undefined;
|
|
243
249
|
private readonly pendingRequests = new Map<string, PendingRequestsHandlers<any>>();
|
|
244
250
|
|
|
245
251
|
constructor() {
|
|
@@ -250,6 +256,7 @@ class ElectronService {
|
|
|
250
256
|
window.addEventListener('message', (event: MessageEvent) => {
|
|
251
257
|
if(event.data?.type === 'init-port' && event.ports.length > 0) {
|
|
252
258
|
this.port = event.ports[0]!;
|
|
259
|
+
this.senderId = event.data.senderId;
|
|
253
260
|
this.port.onmessage = onResponse;
|
|
254
261
|
}
|
|
255
262
|
});
|
|
@@ -297,14 +304,15 @@ class ElectronService {
|
|
|
297
304
|
/**
|
|
298
305
|
* Initiate a request to the main process
|
|
299
306
|
*/
|
|
300
|
-
public request<T>(request: Omit<IRequest, 'requestId'>): Promise<T> {
|
|
307
|
+
public request<T>(request: Omit<IRequest, 'requestId' | 'senderId'>): Promise<T> {
|
|
301
308
|
return new Promise<T>((resolve, reject) => {
|
|
302
|
-
if(!this.port) {
|
|
309
|
+
if(!this.port || !this.senderId) {
|
|
303
310
|
return reject(new Error("MessagePort is not available"));
|
|
304
311
|
}
|
|
305
312
|
|
|
306
313
|
const req: IRequest = {
|
|
307
314
|
requestId: /* Create a random ID with the function of your choice */,
|
|
315
|
+
senderId: this.senderId,
|
|
308
316
|
...request,
|
|
309
317
|
};
|
|
310
318
|
|
|
@@ -379,6 +387,100 @@ throw new UnavailableException();
|
|
|
379
387
|
| 599 | NetworkConnectTimeoutException |
|
|
380
388
|
|
|
381
389
|
|
|
390
|
+
## Advanced
|
|
391
|
+
|
|
392
|
+
### Injection
|
|
393
|
+
|
|
394
|
+
You can decide to inject an Injectable without passing by the constructor, as follow :
|
|
395
|
+
|
|
396
|
+
```ts
|
|
397
|
+
import { inject } from '@noxfly/noxus';
|
|
398
|
+
import { MyClass } from 'src/myclass';
|
|
399
|
+
|
|
400
|
+
const instance: MyClass = inject(MyClass);
|
|
401
|
+
```
|
|
402
|
+
|
|
403
|
+
### Middlewares
|
|
404
|
+
|
|
405
|
+
Declare middlewares as follow :
|
|
406
|
+
|
|
407
|
+
```ts
|
|
408
|
+
// renderer/middlewares.ts
|
|
409
|
+
|
|
410
|
+
import { IMiddleware, Injectable, Request, IResponse, NextFunction } from '@noxfly/noxus';
|
|
411
|
+
|
|
412
|
+
@Injectable()
|
|
413
|
+
export class MiddlewareA implements IMiddleware {
|
|
414
|
+
public async invoke(request: Request, response: IResponse, next: NextFunction): Promise<void> {
|
|
415
|
+
console.log(`[Middleware A] before next()`);
|
|
416
|
+
await next();
|
|
417
|
+
console.log(`[Middleware A] after next()`);
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
@Injectable()
|
|
422
|
+
export class MiddlewareB implements IMiddleware {
|
|
423
|
+
public async invoke(request: Request, response: IResponse, next: NextFunction): Promise<void> {
|
|
424
|
+
console.log(`[Middleware B] before next()`);
|
|
425
|
+
await next();
|
|
426
|
+
console.log(`[Middleware B] after next()`);
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
```
|
|
430
|
+
|
|
431
|
+
It is highly recommended to `await` the call of the `next` function.
|
|
432
|
+
|
|
433
|
+
Register these by 3 possible ways :
|
|
434
|
+
|
|
435
|
+
1. For a root scope. Will be present for each routes.
|
|
436
|
+
|
|
437
|
+
```ts
|
|
438
|
+
const noxApp = bootstrapApplication(AppModule);
|
|
439
|
+
|
|
440
|
+
noxApp.configure(Application);
|
|
441
|
+
|
|
442
|
+
noxApp.use(MiddlewareA);
|
|
443
|
+
noxApp.use(MiddlewareB);
|
|
444
|
+
|
|
445
|
+
noxApp.start();
|
|
446
|
+
```
|
|
447
|
+
|
|
448
|
+
2. Or for a controller or action's scope :
|
|
449
|
+
|
|
450
|
+
```ts
|
|
451
|
+
@Controller('user')
|
|
452
|
+
@UseMiddlewares([MiddlewareA, MiddlewareB])
|
|
453
|
+
export class UserController {
|
|
454
|
+
@Get('all')
|
|
455
|
+
@UseMiddlewares([MiddlewareA, MiddlewareB])
|
|
456
|
+
public getAll(): Promise<void> {
|
|
457
|
+
// ...
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
```
|
|
461
|
+
|
|
462
|
+
Note that if, for a given action, it has registered multiples times the same middleware, only the first registration will be saved.
|
|
463
|
+
|
|
464
|
+
For instance, registering MiddlewareA for root, on the controller and on the action is useless.
|
|
465
|
+
|
|
466
|
+
The order of declaration of use of middlewares is important.
|
|
467
|
+
|
|
468
|
+
assume we do this :
|
|
469
|
+
1. Use Middleware A for root
|
|
470
|
+
2. Use Middleware B for root just after MiddlewareA
|
|
471
|
+
3. Use Middleware C for controller
|
|
472
|
+
4. Use Middleware D for action
|
|
473
|
+
5. Use AuthGuard on the controller
|
|
474
|
+
6. Use RoleGuard on the action
|
|
475
|
+
|
|
476
|
+
Then the executing pipeline will be as follow :
|
|
477
|
+
|
|
478
|
+
```r
|
|
479
|
+
A -> B -> C -> D -> AuthGuard -> RoleGuard -> [action] -> D -> C -> B -> A.
|
|
480
|
+
```
|
|
481
|
+
|
|
482
|
+
if a middleware throw any exception or put the response status higher or equal to 400, the pipeline immediatly stops and the response is returned, weither it is done before or after the call to the next function.
|
|
483
|
+
|
|
382
484
|
## Contributing
|
|
383
485
|
|
|
384
486
|
1. Clone the repo
|
package/dist/noxus.d.mts
CHANGED
|
@@ -1,8 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright 2025 NoxFly
|
|
3
|
+
* @license MIT
|
|
4
|
+
* @author NoxFly
|
|
5
|
+
*/
|
|
1
6
|
interface Type<T> extends Function {
|
|
2
7
|
new (...args: any[]): T;
|
|
3
8
|
}
|
|
4
9
|
type MaybeAsync<T> = T | Promise<T>;
|
|
5
10
|
|
|
11
|
+
|
|
6
12
|
type Lifetime = 'singleton' | 'scope' | 'transient';
|
|
7
13
|
interface IBinding {
|
|
8
14
|
lifetime: Lifetime;
|
|
@@ -31,11 +37,8 @@ declare class AppInjector {
|
|
|
31
37
|
private instantiate;
|
|
32
38
|
}
|
|
33
39
|
declare const RootInjector: AppInjector;
|
|
40
|
+
declare function inject<T>(t: Type<T>): T;
|
|
34
41
|
|
|
35
|
-
interface IApp {
|
|
36
|
-
dispose(): Promise<void>;
|
|
37
|
-
onReady(): Promise<void>;
|
|
38
|
-
}
|
|
39
42
|
|
|
40
43
|
interface IRouteMetadata {
|
|
41
44
|
method: HttpMethod;
|
|
@@ -52,18 +55,19 @@ declare const Delete: (path: string) => MethodDecorator;
|
|
|
52
55
|
declare const ROUTE_METADATA_KEY: unique symbol;
|
|
53
56
|
declare function getRouteMetadata(target: Type<unknown>): IRouteMetadata[];
|
|
54
57
|
|
|
58
|
+
|
|
55
59
|
declare class Request {
|
|
56
|
-
readonly app: IApp;
|
|
57
60
|
readonly event: Electron.MessageEvent;
|
|
58
61
|
readonly id: string;
|
|
59
62
|
readonly method: HttpMethod;
|
|
60
63
|
readonly path: string;
|
|
61
64
|
readonly body: any;
|
|
62
|
-
readonly context:
|
|
65
|
+
readonly context: AppInjector;
|
|
63
66
|
readonly params: Record<string, string>;
|
|
64
|
-
constructor(
|
|
67
|
+
constructor(event: Electron.MessageEvent, id: string, method: HttpMethod, path: string, body: any);
|
|
65
68
|
}
|
|
66
69
|
interface IRequest<T = any> {
|
|
70
|
+
senderId: number;
|
|
67
71
|
requestId: string;
|
|
68
72
|
path: string;
|
|
69
73
|
method: HttpMethod;
|
|
@@ -76,6 +80,7 @@ interface IResponse<T = any> {
|
|
|
76
80
|
error?: string;
|
|
77
81
|
}
|
|
78
82
|
|
|
83
|
+
|
|
79
84
|
interface IGuard {
|
|
80
85
|
canActivate(request: Request): MaybeAsync<boolean>;
|
|
81
86
|
}
|
|
@@ -87,32 +92,117 @@ declare function Authorize(...guardClasses: Type<IGuard>[]): MethodDecorator & C
|
|
|
87
92
|
declare function getGuardForController(controllerName: string): Type<IGuard>[];
|
|
88
93
|
declare function getGuardForControllerAction(controllerName: string, actionName: string): Type<IGuard>[];
|
|
89
94
|
|
|
95
|
+
|
|
96
|
+
type NextFunction = () => Promise<void>;
|
|
97
|
+
interface IMiddleware {
|
|
98
|
+
invoke(request: Request, response: IResponse, next: NextFunction): MaybeAsync<void>;
|
|
99
|
+
}
|
|
100
|
+
declare function UseMiddlewares(mdlw: Type<IMiddleware>[]): ClassDecorator & MethodDecorator;
|
|
101
|
+
declare function getMiddlewaresForController(controllerName: string): Type<IMiddleware>[];
|
|
102
|
+
declare function getMiddlewaresForControllerAction(controllerName: string, actionName: string): Type<IMiddleware>[];
|
|
103
|
+
|
|
104
|
+
|
|
90
105
|
interface IRouteDefinition {
|
|
91
106
|
method: string;
|
|
92
107
|
path: string;
|
|
93
108
|
controller: Type<any>;
|
|
94
109
|
handler: string;
|
|
95
110
|
guards: Type<IGuard>[];
|
|
111
|
+
middlewares: Type<IMiddleware>[];
|
|
96
112
|
}
|
|
97
113
|
type ControllerAction = (request: Request, response: IResponse) => any;
|
|
98
114
|
declare class Router {
|
|
99
115
|
private readonly routes;
|
|
116
|
+
private readonly rootMiddlewares;
|
|
117
|
+
/**
|
|
118
|
+
*
|
|
119
|
+
*/
|
|
100
120
|
registerController(controllerClass: Type<unknown>): Router;
|
|
121
|
+
/**
|
|
122
|
+
*
|
|
123
|
+
*/
|
|
124
|
+
defineRootMiddleware(middleware: Type<IMiddleware>): Router;
|
|
125
|
+
/**
|
|
126
|
+
*
|
|
127
|
+
*/
|
|
101
128
|
handle(request: Request): Promise<IResponse>;
|
|
129
|
+
/**
|
|
130
|
+
*
|
|
131
|
+
*/
|
|
102
132
|
private findRoute;
|
|
133
|
+
/**
|
|
134
|
+
*
|
|
135
|
+
*/
|
|
103
136
|
private resolveController;
|
|
104
|
-
|
|
137
|
+
/**
|
|
138
|
+
*
|
|
139
|
+
*/
|
|
140
|
+
private runRequestPipeline;
|
|
141
|
+
/**
|
|
142
|
+
*
|
|
143
|
+
*/
|
|
144
|
+
private runMiddleware;
|
|
145
|
+
/**
|
|
146
|
+
*
|
|
147
|
+
*/
|
|
148
|
+
private runGuard;
|
|
149
|
+
/**
|
|
150
|
+
*
|
|
151
|
+
*/
|
|
105
152
|
private extractParams;
|
|
106
153
|
}
|
|
107
154
|
|
|
155
|
+
|
|
156
|
+
interface IApp {
|
|
157
|
+
dispose(): Promise<void>;
|
|
158
|
+
onReady(): Promise<void>;
|
|
159
|
+
onActivated(): Promise<void>;
|
|
160
|
+
}
|
|
161
|
+
declare class NoxApp {
|
|
162
|
+
private readonly router;
|
|
163
|
+
private readonly messagePorts;
|
|
164
|
+
private app;
|
|
165
|
+
constructor(router: Router);
|
|
166
|
+
/**
|
|
167
|
+
*
|
|
168
|
+
*/
|
|
169
|
+
init(): Promise<NoxApp>;
|
|
170
|
+
/**
|
|
171
|
+
*
|
|
172
|
+
*/
|
|
173
|
+
private giveTheRendererAPort;
|
|
174
|
+
/**
|
|
175
|
+
* Electron specific message handling.
|
|
176
|
+
* Replaces HTTP calls by using Electron's IPC mechanism.
|
|
177
|
+
*/
|
|
178
|
+
private onRendererMessage;
|
|
179
|
+
/**
|
|
180
|
+
* MacOS specific behavior.
|
|
181
|
+
*/
|
|
182
|
+
private onAppActivated;
|
|
183
|
+
private shutdownChannel;
|
|
184
|
+
/**
|
|
185
|
+
*
|
|
186
|
+
*/
|
|
187
|
+
private onAllWindowsClosed;
|
|
188
|
+
configure(app: Type<IApp>): NoxApp;
|
|
189
|
+
use(middleware: Type<IMiddleware>): NoxApp;
|
|
190
|
+
/**
|
|
191
|
+
* Should be called after the bootstrapApplication function is called.
|
|
192
|
+
*/
|
|
193
|
+
start(): NoxApp;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
|
|
108
197
|
/**
|
|
109
198
|
*
|
|
110
199
|
*/
|
|
111
|
-
declare function bootstrapApplication(
|
|
200
|
+
declare function bootstrapApplication(rootModule: Type<any>): Promise<NoxApp>;
|
|
112
201
|
|
|
113
|
-
declare
|
|
114
|
-
|
|
202
|
+
declare class ResponseException extends Error {
|
|
203
|
+
readonly status: number;
|
|
115
204
|
constructor(message?: string);
|
|
205
|
+
constructor(statusCode?: number, message?: string);
|
|
116
206
|
}
|
|
117
207
|
declare class BadRequestException extends ResponseException {
|
|
118
208
|
readonly status = 400;
|
|
@@ -120,6 +210,9 @@ declare class BadRequestException extends ResponseException {
|
|
|
120
210
|
declare class UnauthorizedException extends ResponseException {
|
|
121
211
|
readonly status = 401;
|
|
122
212
|
}
|
|
213
|
+
declare class PaymentRequiredException extends ResponseException {
|
|
214
|
+
readonly status = 402;
|
|
215
|
+
}
|
|
123
216
|
declare class ForbiddenException extends ResponseException {
|
|
124
217
|
readonly status = 403;
|
|
125
218
|
}
|
|
@@ -181,6 +274,7 @@ declare class NetworkConnectTimeoutException extends ResponseException {
|
|
|
181
274
|
readonly status = 599;
|
|
182
275
|
}
|
|
183
276
|
|
|
277
|
+
|
|
184
278
|
interface IControllerMetadata {
|
|
185
279
|
path: string;
|
|
186
280
|
guards: Type<IGuard>[];
|
|
@@ -189,10 +283,12 @@ declare function Controller(path: string): ClassDecorator;
|
|
|
189
283
|
declare const CONTROLLER_METADATA_KEY: unique symbol;
|
|
190
284
|
declare function getControllerMetadata(target: Type<unknown>): IControllerMetadata | undefined;
|
|
191
285
|
|
|
286
|
+
|
|
192
287
|
declare function Injectable(lifetime?: Lifetime): ClassDecorator;
|
|
193
288
|
declare const INJECTABLE_METADATA_KEY: unique symbol;
|
|
194
289
|
declare function getInjectableMetadata(target: Type<unknown>): Lifetime | undefined;
|
|
195
290
|
|
|
291
|
+
|
|
196
292
|
declare function Module(metadata: IModuleMetadata): ClassDecorator;
|
|
197
293
|
declare const MODULE_METADATA_KEY: unique symbol;
|
|
198
294
|
interface IModuleMetadata {
|
|
@@ -231,4 +327,4 @@ declare namespace Logger {
|
|
|
231
327
|
function debug(...args: any[]): void;
|
|
232
328
|
}
|
|
233
329
|
|
|
234
|
-
export { Authorize, BadGatewayException, BadRequestException, CONTROLLER_METADATA_KEY, ConflictException, Controller, type ControllerAction, Delete, ForbiddenException, GatewayTimeoutException, Get, type HttpMethod, HttpVersionNotSupportedException, type IApp, type IBinding, type IControllerMetadata, type IGuard, type IModuleMetadata, INJECTABLE_METADATA_KEY, type IRequest, type IResponse, type IRouteDefinition, type IRouteMetadata, Injectable, InsufficientStorageException, InternalServerException, type Lifetime, type LogLevel, Logger, LoopDetectedException, MODULE_METADATA_KEY, type MaybeAsync, MethodNotAllowedException, Module, NetworkAuthenticationRequiredException, NetworkConnectTimeoutException, NotAcceptableException, NotExtendedException, NotFoundException, NotImplementedException, Patch, Post, Put, ROUTE_METADATA_KEY, Request, RequestTimeoutException, ResponseException, RootInjector, Router, ServiceUnavailableException, TooManyRequestsException, type Type, UnauthorizedException, UpgradeRequiredException, VariantAlsoNegotiatesException, bootstrapApplication, getControllerMetadata, getGuardForController, getGuardForControllerAction, getInjectableMetadata, getModuleMetadata, getRouteMetadata };
|
|
330
|
+
export { AppInjector, Authorize, BadGatewayException, BadRequestException, CONTROLLER_METADATA_KEY, ConflictException, Controller, type ControllerAction, Delete, ForbiddenException, GatewayTimeoutException, Get, type HttpMethod, HttpVersionNotSupportedException, type IApp, type IBinding, type IControllerMetadata, type IGuard, type IMiddleware, type IModuleMetadata, INJECTABLE_METADATA_KEY, type IRequest, type IResponse, type IRouteDefinition, type IRouteMetadata, Injectable, InsufficientStorageException, InternalServerException, type Lifetime, type LogLevel, Logger, LoopDetectedException, MODULE_METADATA_KEY, type MaybeAsync, MethodNotAllowedException, Module, NetworkAuthenticationRequiredException, NetworkConnectTimeoutException, type NextFunction, NotAcceptableException, NotExtendedException, NotFoundException, NotImplementedException, NoxApp, Patch, PaymentRequiredException, Post, Put, ROUTE_METADATA_KEY, Request, RequestTimeoutException, ResponseException, RootInjector, Router, ServiceUnavailableException, TooManyRequestsException, type Type, UnauthorizedException, UpgradeRequiredException, UseMiddlewares, VariantAlsoNegotiatesException, bootstrapApplication, getControllerMetadata, getGuardForController, getGuardForControllerAction, getInjectableMetadata, getMiddlewaresForController, getMiddlewaresForControllerAction, getModuleMetadata, getRouteMetadata, inject };
|
package/dist/noxus.d.ts
CHANGED
|
@@ -1,8 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright 2025 NoxFly
|
|
3
|
+
* @license MIT
|
|
4
|
+
* @author NoxFly
|
|
5
|
+
*/
|
|
1
6
|
interface Type<T> extends Function {
|
|
2
7
|
new (...args: any[]): T;
|
|
3
8
|
}
|
|
4
9
|
type MaybeAsync<T> = T | Promise<T>;
|
|
5
10
|
|
|
11
|
+
|
|
6
12
|
type Lifetime = 'singleton' | 'scope' | 'transient';
|
|
7
13
|
interface IBinding {
|
|
8
14
|
lifetime: Lifetime;
|
|
@@ -31,11 +37,8 @@ declare class AppInjector {
|
|
|
31
37
|
private instantiate;
|
|
32
38
|
}
|
|
33
39
|
declare const RootInjector: AppInjector;
|
|
40
|
+
declare function inject<T>(t: Type<T>): T;
|
|
34
41
|
|
|
35
|
-
interface IApp {
|
|
36
|
-
dispose(): Promise<void>;
|
|
37
|
-
onReady(): Promise<void>;
|
|
38
|
-
}
|
|
39
42
|
|
|
40
43
|
interface IRouteMetadata {
|
|
41
44
|
method: HttpMethod;
|
|
@@ -52,18 +55,19 @@ declare const Delete: (path: string) => MethodDecorator;
|
|
|
52
55
|
declare const ROUTE_METADATA_KEY: unique symbol;
|
|
53
56
|
declare function getRouteMetadata(target: Type<unknown>): IRouteMetadata[];
|
|
54
57
|
|
|
58
|
+
|
|
55
59
|
declare class Request {
|
|
56
|
-
readonly app: IApp;
|
|
57
60
|
readonly event: Electron.MessageEvent;
|
|
58
61
|
readonly id: string;
|
|
59
62
|
readonly method: HttpMethod;
|
|
60
63
|
readonly path: string;
|
|
61
64
|
readonly body: any;
|
|
62
|
-
readonly context:
|
|
65
|
+
readonly context: AppInjector;
|
|
63
66
|
readonly params: Record<string, string>;
|
|
64
|
-
constructor(
|
|
67
|
+
constructor(event: Electron.MessageEvent, id: string, method: HttpMethod, path: string, body: any);
|
|
65
68
|
}
|
|
66
69
|
interface IRequest<T = any> {
|
|
70
|
+
senderId: number;
|
|
67
71
|
requestId: string;
|
|
68
72
|
path: string;
|
|
69
73
|
method: HttpMethod;
|
|
@@ -76,6 +80,7 @@ interface IResponse<T = any> {
|
|
|
76
80
|
error?: string;
|
|
77
81
|
}
|
|
78
82
|
|
|
83
|
+
|
|
79
84
|
interface IGuard {
|
|
80
85
|
canActivate(request: Request): MaybeAsync<boolean>;
|
|
81
86
|
}
|
|
@@ -87,32 +92,117 @@ declare function Authorize(...guardClasses: Type<IGuard>[]): MethodDecorator & C
|
|
|
87
92
|
declare function getGuardForController(controllerName: string): Type<IGuard>[];
|
|
88
93
|
declare function getGuardForControllerAction(controllerName: string, actionName: string): Type<IGuard>[];
|
|
89
94
|
|
|
95
|
+
|
|
96
|
+
type NextFunction = () => Promise<void>;
|
|
97
|
+
interface IMiddleware {
|
|
98
|
+
invoke(request: Request, response: IResponse, next: NextFunction): MaybeAsync<void>;
|
|
99
|
+
}
|
|
100
|
+
declare function UseMiddlewares(mdlw: Type<IMiddleware>[]): ClassDecorator & MethodDecorator;
|
|
101
|
+
declare function getMiddlewaresForController(controllerName: string): Type<IMiddleware>[];
|
|
102
|
+
declare function getMiddlewaresForControllerAction(controllerName: string, actionName: string): Type<IMiddleware>[];
|
|
103
|
+
|
|
104
|
+
|
|
90
105
|
interface IRouteDefinition {
|
|
91
106
|
method: string;
|
|
92
107
|
path: string;
|
|
93
108
|
controller: Type<any>;
|
|
94
109
|
handler: string;
|
|
95
110
|
guards: Type<IGuard>[];
|
|
111
|
+
middlewares: Type<IMiddleware>[];
|
|
96
112
|
}
|
|
97
113
|
type ControllerAction = (request: Request, response: IResponse) => any;
|
|
98
114
|
declare class Router {
|
|
99
115
|
private readonly routes;
|
|
116
|
+
private readonly rootMiddlewares;
|
|
117
|
+
/**
|
|
118
|
+
*
|
|
119
|
+
*/
|
|
100
120
|
registerController(controllerClass: Type<unknown>): Router;
|
|
121
|
+
/**
|
|
122
|
+
*
|
|
123
|
+
*/
|
|
124
|
+
defineRootMiddleware(middleware: Type<IMiddleware>): Router;
|
|
125
|
+
/**
|
|
126
|
+
*
|
|
127
|
+
*/
|
|
101
128
|
handle(request: Request): Promise<IResponse>;
|
|
129
|
+
/**
|
|
130
|
+
*
|
|
131
|
+
*/
|
|
102
132
|
private findRoute;
|
|
133
|
+
/**
|
|
134
|
+
*
|
|
135
|
+
*/
|
|
103
136
|
private resolveController;
|
|
104
|
-
|
|
137
|
+
/**
|
|
138
|
+
*
|
|
139
|
+
*/
|
|
140
|
+
private runRequestPipeline;
|
|
141
|
+
/**
|
|
142
|
+
*
|
|
143
|
+
*/
|
|
144
|
+
private runMiddleware;
|
|
145
|
+
/**
|
|
146
|
+
*
|
|
147
|
+
*/
|
|
148
|
+
private runGuard;
|
|
149
|
+
/**
|
|
150
|
+
*
|
|
151
|
+
*/
|
|
105
152
|
private extractParams;
|
|
106
153
|
}
|
|
107
154
|
|
|
155
|
+
|
|
156
|
+
interface IApp {
|
|
157
|
+
dispose(): Promise<void>;
|
|
158
|
+
onReady(): Promise<void>;
|
|
159
|
+
onActivated(): Promise<void>;
|
|
160
|
+
}
|
|
161
|
+
declare class NoxApp {
|
|
162
|
+
private readonly router;
|
|
163
|
+
private readonly messagePorts;
|
|
164
|
+
private app;
|
|
165
|
+
constructor(router: Router);
|
|
166
|
+
/**
|
|
167
|
+
*
|
|
168
|
+
*/
|
|
169
|
+
init(): Promise<NoxApp>;
|
|
170
|
+
/**
|
|
171
|
+
*
|
|
172
|
+
*/
|
|
173
|
+
private giveTheRendererAPort;
|
|
174
|
+
/**
|
|
175
|
+
* Electron specific message handling.
|
|
176
|
+
* Replaces HTTP calls by using Electron's IPC mechanism.
|
|
177
|
+
*/
|
|
178
|
+
private onRendererMessage;
|
|
179
|
+
/**
|
|
180
|
+
* MacOS specific behavior.
|
|
181
|
+
*/
|
|
182
|
+
private onAppActivated;
|
|
183
|
+
private shutdownChannel;
|
|
184
|
+
/**
|
|
185
|
+
*
|
|
186
|
+
*/
|
|
187
|
+
private onAllWindowsClosed;
|
|
188
|
+
configure(app: Type<IApp>): NoxApp;
|
|
189
|
+
use(middleware: Type<IMiddleware>): NoxApp;
|
|
190
|
+
/**
|
|
191
|
+
* Should be called after the bootstrapApplication function is called.
|
|
192
|
+
*/
|
|
193
|
+
start(): NoxApp;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
|
|
108
197
|
/**
|
|
109
198
|
*
|
|
110
199
|
*/
|
|
111
|
-
declare function bootstrapApplication(
|
|
200
|
+
declare function bootstrapApplication(rootModule: Type<any>): Promise<NoxApp>;
|
|
112
201
|
|
|
113
|
-
declare
|
|
114
|
-
|
|
202
|
+
declare class ResponseException extends Error {
|
|
203
|
+
readonly status: number;
|
|
115
204
|
constructor(message?: string);
|
|
205
|
+
constructor(statusCode?: number, message?: string);
|
|
116
206
|
}
|
|
117
207
|
declare class BadRequestException extends ResponseException {
|
|
118
208
|
readonly status = 400;
|
|
@@ -120,6 +210,9 @@ declare class BadRequestException extends ResponseException {
|
|
|
120
210
|
declare class UnauthorizedException extends ResponseException {
|
|
121
211
|
readonly status = 401;
|
|
122
212
|
}
|
|
213
|
+
declare class PaymentRequiredException extends ResponseException {
|
|
214
|
+
readonly status = 402;
|
|
215
|
+
}
|
|
123
216
|
declare class ForbiddenException extends ResponseException {
|
|
124
217
|
readonly status = 403;
|
|
125
218
|
}
|
|
@@ -181,6 +274,7 @@ declare class NetworkConnectTimeoutException extends ResponseException {
|
|
|
181
274
|
readonly status = 599;
|
|
182
275
|
}
|
|
183
276
|
|
|
277
|
+
|
|
184
278
|
interface IControllerMetadata {
|
|
185
279
|
path: string;
|
|
186
280
|
guards: Type<IGuard>[];
|
|
@@ -189,10 +283,12 @@ declare function Controller(path: string): ClassDecorator;
|
|
|
189
283
|
declare const CONTROLLER_METADATA_KEY: unique symbol;
|
|
190
284
|
declare function getControllerMetadata(target: Type<unknown>): IControllerMetadata | undefined;
|
|
191
285
|
|
|
286
|
+
|
|
192
287
|
declare function Injectable(lifetime?: Lifetime): ClassDecorator;
|
|
193
288
|
declare const INJECTABLE_METADATA_KEY: unique symbol;
|
|
194
289
|
declare function getInjectableMetadata(target: Type<unknown>): Lifetime | undefined;
|
|
195
290
|
|
|
291
|
+
|
|
196
292
|
declare function Module(metadata: IModuleMetadata): ClassDecorator;
|
|
197
293
|
declare const MODULE_METADATA_KEY: unique symbol;
|
|
198
294
|
interface IModuleMetadata {
|
|
@@ -231,4 +327,4 @@ declare namespace Logger {
|
|
|
231
327
|
function debug(...args: any[]): void;
|
|
232
328
|
}
|
|
233
329
|
|
|
234
|
-
export { Authorize, BadGatewayException, BadRequestException, CONTROLLER_METADATA_KEY, ConflictException, Controller, type ControllerAction, Delete, ForbiddenException, GatewayTimeoutException, Get, type HttpMethod, HttpVersionNotSupportedException, type IApp, type IBinding, type IControllerMetadata, type IGuard, type IModuleMetadata, INJECTABLE_METADATA_KEY, type IRequest, type IResponse, type IRouteDefinition, type IRouteMetadata, Injectable, InsufficientStorageException, InternalServerException, type Lifetime, type LogLevel, Logger, LoopDetectedException, MODULE_METADATA_KEY, type MaybeAsync, MethodNotAllowedException, Module, NetworkAuthenticationRequiredException, NetworkConnectTimeoutException, NotAcceptableException, NotExtendedException, NotFoundException, NotImplementedException, Patch, Post, Put, ROUTE_METADATA_KEY, Request, RequestTimeoutException, ResponseException, RootInjector, Router, ServiceUnavailableException, TooManyRequestsException, type Type, UnauthorizedException, UpgradeRequiredException, VariantAlsoNegotiatesException, bootstrapApplication, getControllerMetadata, getGuardForController, getGuardForControllerAction, getInjectableMetadata, getModuleMetadata, getRouteMetadata };
|
|
330
|
+
export { AppInjector, Authorize, BadGatewayException, BadRequestException, CONTROLLER_METADATA_KEY, ConflictException, Controller, type ControllerAction, Delete, ForbiddenException, GatewayTimeoutException, Get, type HttpMethod, HttpVersionNotSupportedException, type IApp, type IBinding, type IControllerMetadata, type IGuard, type IMiddleware, type IModuleMetadata, INJECTABLE_METADATA_KEY, type IRequest, type IResponse, type IRouteDefinition, type IRouteMetadata, Injectable, InsufficientStorageException, InternalServerException, type Lifetime, type LogLevel, Logger, LoopDetectedException, MODULE_METADATA_KEY, type MaybeAsync, MethodNotAllowedException, Module, NetworkAuthenticationRequiredException, NetworkConnectTimeoutException, type NextFunction, NotAcceptableException, NotExtendedException, NotFoundException, NotImplementedException, NoxApp, Patch, PaymentRequiredException, Post, Put, ROUTE_METADATA_KEY, Request, RequestTimeoutException, ResponseException, RootInjector, Router, ServiceUnavailableException, TooManyRequestsException, type Type, UnauthorizedException, UpgradeRequiredException, UseMiddlewares, VariantAlsoNegotiatesException, bootstrapApplication, getControllerMetadata, getGuardForController, getGuardForControllerAction, getInjectableMetadata, getMiddlewaresForController, getMiddlewaresForControllerAction, getModuleMetadata, getRouteMetadata, inject };
|