@noxfly/noxus 1.0.4 → 1.0.5

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 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 application = await bootstrapApplication(Application, AppModule);
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,23 @@ 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
+ § TODO
406
+
382
407
  ## Contributing
383
408
 
384
409
  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,8 +55,8 @@ 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;
@@ -61,9 +64,10 @@ declare class Request {
61
64
  readonly body: any;
62
65
  readonly context: any;
63
66
  readonly params: Record<string, string>;
64
- constructor(app: IApp, event: Electron.MessageEvent, id: string, method: HttpMethod, path: string, body: any);
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,6 +92,7 @@ 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
+
90
96
  interface IRouteDefinition {
91
97
  method: string;
92
98
  path: string;
@@ -105,10 +111,51 @@ declare class Router {
105
111
  private extractParams;
106
112
  }
107
113
 
114
+
115
+ interface IApp {
116
+ dispose(): Promise<void>;
117
+ onReady(): Promise<void>;
118
+ onActivated(): Promise<void>;
119
+ }
120
+ declare class NoxApp {
121
+ private readonly router;
122
+ private readonly messagePorts;
123
+ private app;
124
+ constructor(router: Router);
125
+ /**
126
+ *
127
+ */
128
+ init(): Promise<NoxApp>;
129
+ /**
130
+ *
131
+ */
132
+ private giveTheRendererAPort;
133
+ /**
134
+ * Electron specific message handling.
135
+ * Replaces HTTP calls by using Electron's IPC mechanism.
136
+ */
137
+ private onRendererMessage;
138
+ /**
139
+ * MacOS specific behavior.
140
+ */
141
+ private onAppActivated;
142
+ private shutdownChannel;
143
+ /**
144
+ *
145
+ */
146
+ private onAllWindowsClosed;
147
+ configure(app: Type<IApp>): NoxApp;
148
+ /**
149
+ * Should be called after the bootstrapApplication function is called.
150
+ */
151
+ start(): NoxApp;
152
+ }
153
+
154
+
108
155
  /**
109
156
  *
110
157
  */
111
- declare function bootstrapApplication(root: Type<IApp>, rootModule: Type<any>): Promise<IApp>;
158
+ declare function bootstrapApplication(rootModule: Type<any>): Promise<NoxApp>;
112
159
 
113
160
  declare abstract class ResponseException extends Error {
114
161
  abstract readonly status: number;
@@ -120,6 +167,9 @@ declare class BadRequestException extends ResponseException {
120
167
  declare class UnauthorizedException extends ResponseException {
121
168
  readonly status = 401;
122
169
  }
170
+ declare class PaymentRequiredException extends ResponseException {
171
+ readonly status = 402;
172
+ }
123
173
  declare class ForbiddenException extends ResponseException {
124
174
  readonly status = 403;
125
175
  }
@@ -181,6 +231,7 @@ declare class NetworkConnectTimeoutException extends ResponseException {
181
231
  readonly status = 599;
182
232
  }
183
233
 
234
+
184
235
  interface IControllerMetadata {
185
236
  path: string;
186
237
  guards: Type<IGuard>[];
@@ -189,10 +240,12 @@ declare function Controller(path: string): ClassDecorator;
189
240
  declare const CONTROLLER_METADATA_KEY: unique symbol;
190
241
  declare function getControllerMetadata(target: Type<unknown>): IControllerMetadata | undefined;
191
242
 
243
+
192
244
  declare function Injectable(lifetime?: Lifetime): ClassDecorator;
193
245
  declare const INJECTABLE_METADATA_KEY: unique symbol;
194
246
  declare function getInjectableMetadata(target: Type<unknown>): Lifetime | undefined;
195
247
 
248
+
196
249
  declare function Module(metadata: IModuleMetadata): ClassDecorator;
197
250
  declare const MODULE_METADATA_KEY: unique symbol;
198
251
  interface IModuleMetadata {
@@ -231,4 +284,4 @@ declare namespace Logger {
231
284
  function debug(...args: any[]): void;
232
285
  }
233
286
 
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 };
287
+ 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, NoxApp, Patch, PaymentRequiredException, Post, Put, ROUTE_METADATA_KEY, Request, RequestTimeoutException, ResponseException, RootInjector, Router, ServiceUnavailableException, TooManyRequestsException, type Type, UnauthorizedException, UpgradeRequiredException, VariantAlsoNegotiatesException, bootstrapApplication, getControllerMetadata, getGuardForController, getGuardForControllerAction, getInjectableMetadata, 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,8 +55,8 @@ 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;
@@ -61,9 +64,10 @@ declare class Request {
61
64
  readonly body: any;
62
65
  readonly context: any;
63
66
  readonly params: Record<string, string>;
64
- constructor(app: IApp, event: Electron.MessageEvent, id: string, method: HttpMethod, path: string, body: any);
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,6 +92,7 @@ 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
+
90
96
  interface IRouteDefinition {
91
97
  method: string;
92
98
  path: string;
@@ -105,10 +111,51 @@ declare class Router {
105
111
  private extractParams;
106
112
  }
107
113
 
114
+
115
+ interface IApp {
116
+ dispose(): Promise<void>;
117
+ onReady(): Promise<void>;
118
+ onActivated(): Promise<void>;
119
+ }
120
+ declare class NoxApp {
121
+ private readonly router;
122
+ private readonly messagePorts;
123
+ private app;
124
+ constructor(router: Router);
125
+ /**
126
+ *
127
+ */
128
+ init(): Promise<NoxApp>;
129
+ /**
130
+ *
131
+ */
132
+ private giveTheRendererAPort;
133
+ /**
134
+ * Electron specific message handling.
135
+ * Replaces HTTP calls by using Electron's IPC mechanism.
136
+ */
137
+ private onRendererMessage;
138
+ /**
139
+ * MacOS specific behavior.
140
+ */
141
+ private onAppActivated;
142
+ private shutdownChannel;
143
+ /**
144
+ *
145
+ */
146
+ private onAllWindowsClosed;
147
+ configure(app: Type<IApp>): NoxApp;
148
+ /**
149
+ * Should be called after the bootstrapApplication function is called.
150
+ */
151
+ start(): NoxApp;
152
+ }
153
+
154
+
108
155
  /**
109
156
  *
110
157
  */
111
- declare function bootstrapApplication(root: Type<IApp>, rootModule: Type<any>): Promise<IApp>;
158
+ declare function bootstrapApplication(rootModule: Type<any>): Promise<NoxApp>;
112
159
 
113
160
  declare abstract class ResponseException extends Error {
114
161
  abstract readonly status: number;
@@ -120,6 +167,9 @@ declare class BadRequestException extends ResponseException {
120
167
  declare class UnauthorizedException extends ResponseException {
121
168
  readonly status = 401;
122
169
  }
170
+ declare class PaymentRequiredException extends ResponseException {
171
+ readonly status = 402;
172
+ }
123
173
  declare class ForbiddenException extends ResponseException {
124
174
  readonly status = 403;
125
175
  }
@@ -181,6 +231,7 @@ declare class NetworkConnectTimeoutException extends ResponseException {
181
231
  readonly status = 599;
182
232
  }
183
233
 
234
+
184
235
  interface IControllerMetadata {
185
236
  path: string;
186
237
  guards: Type<IGuard>[];
@@ -189,10 +240,12 @@ declare function Controller(path: string): ClassDecorator;
189
240
  declare const CONTROLLER_METADATA_KEY: unique symbol;
190
241
  declare function getControllerMetadata(target: Type<unknown>): IControllerMetadata | undefined;
191
242
 
243
+
192
244
  declare function Injectable(lifetime?: Lifetime): ClassDecorator;
193
245
  declare const INJECTABLE_METADATA_KEY: unique symbol;
194
246
  declare function getInjectableMetadata(target: Type<unknown>): Lifetime | undefined;
195
247
 
248
+
196
249
  declare function Module(metadata: IModuleMetadata): ClassDecorator;
197
250
  declare const MODULE_METADATA_KEY: unique symbol;
198
251
  interface IModuleMetadata {
@@ -231,4 +284,4 @@ declare namespace Logger {
231
284
  function debug(...args: any[]): void;
232
285
  }
233
286
 
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 };
287
+ 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, NoxApp, Patch, PaymentRequiredException, Post, Put, ROUTE_METADATA_KEY, Request, RequestTimeoutException, ResponseException, RootInjector, Router, ServiceUnavailableException, TooManyRequestsException, type Type, UnauthorizedException, UpgradeRequiredException, VariantAlsoNegotiatesException, bootstrapApplication, getControllerMetadata, getGuardForController, getGuardForControllerAction, getInjectableMetadata, getModuleMetadata, getRouteMetadata, inject };