@noxfly/noxus 1.0.2 → 1.0.4

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 CHANGED
@@ -334,6 +334,51 @@ const response: User[] = await electronService.request<User[]>({
334
334
  ![Startup image](./images/screenshot-requests.png)
335
335
 
336
336
 
337
+ ### Error Handling
338
+
339
+ You have a bunch of `*Exception` classes that are at your disposal to help you have a clean code.
340
+
341
+ Replace `*` By any of the HTTP status code's name that is available in the list below.
342
+
343
+ If you throw any of these exception, the response to the renderer will contains the associated status code.
344
+
345
+ You can specify a message in the constructor.
346
+
347
+ You throw it as follow :
348
+ ```ts
349
+ throw new UnauthorizedException("Invalid credentials");
350
+ throw new BadRequestException("id is missing in the body");
351
+ throw new UnavailableException();
352
+ // ...
353
+ ```
354
+
355
+ | status code | class to throw |
356
+ | ----------- | -------------------------------------- |
357
+ | 400 | BadRequestException |
358
+ | 401 | UnauthorizedException |
359
+ | 402 | PaymentRequiredException |
360
+ | 403 | ForbiddenException |
361
+ | 404 | NotFoundException |
362
+ | 405 | MethodNotAllowedException |
363
+ | 406 | NotAcceptableException |
364
+ | 408 | RequestTimeoutException |
365
+ | 409 | ConflictException |
366
+ | 426 | UpgradeRequiredException |
367
+ | 429 | TooManyRequestsException |
368
+ | 500 | InternalServerException |
369
+ | 501 | NotImplementedException |
370
+ | 502 | BadGatewayException |
371
+ | 503 | ServiceUnavailableException |
372
+ | 504 | GatewayTimeoutException |
373
+ | 505 | HttpVersionNotSupportedException |
374
+ | 506 | VariantAlsoNegotiatesException |
375
+ | 507 | InsufficientStorageException |
376
+ | 508 | LoopDetectedException |
377
+ | 510 | NotExtendedException |
378
+ | 511 | NetworkAuthenticationRequiredException |
379
+ | 599 | NetworkConnectTimeoutException |
380
+
381
+
337
382
  ## Contributing
338
383
 
339
384
  1. Clone the repo
package/dist/noxus.d.mts CHANGED
@@ -1,36 +1,56 @@
1
+ interface Type<T> extends Function {
2
+ new (...args: any[]): T;
3
+ }
1
4
  type MaybeAsync<T> = T | Promise<T>;
2
5
 
6
+ type Lifetime = 'singleton' | 'scope' | 'transient';
7
+ interface IBinding {
8
+ lifetime: Lifetime;
9
+ implementation: Type<unknown>;
10
+ instance?: InstanceType<Type<unknown>>;
11
+ }
12
+ declare class AppInjector {
13
+ readonly name: string | null;
14
+ bindings: Map<Type<unknown>, IBinding>;
15
+ singletons: Map<Type<unknown>, unknown>;
16
+ scoped: Map<Type<unknown>, unknown>;
17
+ constructor(name?: string | null);
18
+ /**
19
+ * Utilisé généralement pour créer un scope d'injection de dépendances
20
+ * au niveau "scope" (donc durée de vie d'une requête)
21
+ */
22
+ createScope(): AppInjector;
23
+ /**
24
+ * Appelé lorsqu'on souhaite résoudre une dépendance,
25
+ * c'est-à-dire récupérer l'instance d'une classe donnée.
26
+ */
27
+ resolve<T extends Type<unknown>>(target: T): InstanceType<T>;
28
+ /**
29
+ *
30
+ */
31
+ private instantiate;
32
+ }
33
+ declare const RootInjector: AppInjector;
34
+
3
35
  interface IApp {
4
36
  dispose(): Promise<void>;
5
37
  onReady(): Promise<void>;
6
38
  }
7
- declare function Injectable(lifetime?: Lifetime): ClassDecorator;
8
- declare function Module(metadata: IModuleMetadata): ClassDecorator;
9
39
 
10
- type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
11
- interface IRouteDefinition {
12
- method: string;
40
+ interface IRouteMetadata {
41
+ method: HttpMethod;
13
42
  path: string;
14
- controller: Type<any>;
15
43
  handler: string;
16
44
  guards: Type<IGuard>[];
17
45
  }
18
- type ControllerAction = (request: Request, response: IResponse) => any;
19
- declare function Controller(path: string): ClassDecorator;
46
+ type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
20
47
  declare const Get: (path: string) => MethodDecorator;
21
48
  declare const Post: (path: string) => MethodDecorator;
22
49
  declare const Put: (path: string) => MethodDecorator;
23
50
  declare const Patch: (path: string) => MethodDecorator;
24
51
  declare const Delete: (path: string) => MethodDecorator;
25
- declare class Router {
26
- private readonly routes;
27
- registerController(controllerClass: Type<unknown>): Router;
28
- handle(request: Request): Promise<IResponse>;
29
- private findRoute;
30
- private resolveController;
31
- private verifyRequestBody;
32
- private extractParams;
33
- }
52
+ declare const ROUTE_METADATA_KEY: unique symbol;
53
+ declare function getRouteMetadata(target: Type<unknown>): IRouteMetadata[];
34
54
 
35
55
  declare class Request {
36
56
  readonly app: IApp;
@@ -67,62 +87,23 @@ declare function Authorize(...guardClasses: Type<IGuard>[]): MethodDecorator & C
67
87
  declare function getGuardForController(controllerName: string): Type<IGuard>[];
68
88
  declare function getGuardForControllerAction(controllerName: string, actionName: string): Type<IGuard>[];
69
89
 
70
- interface Type<T> extends Function {
71
- new (...args: any[]): T;
72
- }
73
- declare const MODULE_METADATA_KEY: unique symbol;
74
- declare const INJECTABLE_METADATA_KEY: unique symbol;
75
- declare const CONTROLLER_METADATA_KEY: unique symbol;
76
- declare const ROUTE_METADATA_KEY: unique symbol;
77
- interface IModuleMetadata {
78
- imports?: Type<unknown>[];
79
- providers?: Type<unknown>[];
80
- controllers?: Type<unknown>[];
81
- exports?: Type<unknown>[];
82
- }
83
- interface IRouteMetadata {
84
- method: HttpMethod;
90
+ interface IRouteDefinition {
91
+ method: string;
85
92
  path: string;
93
+ controller: Type<any>;
86
94
  handler: string;
87
95
  guards: Type<IGuard>[];
88
96
  }
89
- interface IControllerMetadata {
90
- path: string;
91
- guards: Type<IGuard>[];
92
- }
93
- declare function getControllerMetadata(target: Type<unknown>): IControllerMetadata | undefined;
94
- declare function getRouteMetadata(target: Type<unknown>): IRouteMetadata[];
95
- declare function getModuleMetadata(target: Function): IModuleMetadata | undefined;
96
- declare function getInjectableMetadata(target: Type<unknown>): Lifetime | undefined;
97
-
98
- type Lifetime = 'singleton' | 'scope' | 'transient';
99
- interface IBinding {
100
- lifetime: Lifetime;
101
- implementation: Type<unknown>;
102
- instance?: InstanceType<Type<unknown>>;
103
- }
104
- declare class AppInjector {
105
- readonly name: string | null;
106
- bindings: Map<Type<unknown>, IBinding>;
107
- singletons: Map<Type<unknown>, unknown>;
108
- scoped: Map<Type<unknown>, unknown>;
109
- constructor(name?: string | null);
110
- /**
111
- * Utilisé généralement pour créer un scope d'injection de dépendances
112
- * au niveau "scope" (donc durée de vie d'une requête)
113
- */
114
- createScope(): AppInjector;
115
- /**
116
- * Appelé lorsqu'on souhaite résoudre une dépendance,
117
- * c'est-à-dire récupérer l'instance d'une classe donnée.
118
- */
119
- resolve<T extends Type<unknown>>(target: T): InstanceType<T>;
120
- /**
121
- *
122
- */
123
- private instantiate;
97
+ type ControllerAction = (request: Request, response: IResponse) => any;
98
+ declare class Router {
99
+ private readonly routes;
100
+ registerController(controllerClass: Type<unknown>): Router;
101
+ handle(request: Request): Promise<IResponse>;
102
+ private findRoute;
103
+ private resolveController;
104
+ private verifyRequestBody;
105
+ private extractParams;
124
106
  }
125
- declare const RootInjector: AppInjector;
126
107
 
127
108
  /**
128
109
  *
@@ -131,7 +112,7 @@ declare function bootstrapApplication(root: Type<IApp>, rootModule: Type<any>):
131
112
 
132
113
  declare abstract class ResponseException extends Error {
133
114
  abstract readonly status: number;
134
- constructor(message: string);
115
+ constructor(message?: string);
135
116
  }
136
117
  declare class BadRequestException extends ResponseException {
137
118
  readonly status = 400;
@@ -200,6 +181,28 @@ declare class NetworkConnectTimeoutException extends ResponseException {
200
181
  readonly status = 599;
201
182
  }
202
183
 
184
+ interface IControllerMetadata {
185
+ path: string;
186
+ guards: Type<IGuard>[];
187
+ }
188
+ declare function Controller(path: string): ClassDecorator;
189
+ declare const CONTROLLER_METADATA_KEY: unique symbol;
190
+ declare function getControllerMetadata(target: Type<unknown>): IControllerMetadata | undefined;
191
+
192
+ declare function Injectable(lifetime?: Lifetime): ClassDecorator;
193
+ declare const INJECTABLE_METADATA_KEY: unique symbol;
194
+ declare function getInjectableMetadata(target: Type<unknown>): Lifetime | undefined;
195
+
196
+ declare function Module(metadata: IModuleMetadata): ClassDecorator;
197
+ declare const MODULE_METADATA_KEY: unique symbol;
198
+ interface IModuleMetadata {
199
+ imports?: Type<unknown>[];
200
+ exports?: Type<unknown>[];
201
+ providers?: Type<unknown>[];
202
+ controllers?: Type<unknown>[];
203
+ }
204
+ declare function getModuleMetadata(target: Function): IModuleMetadata | undefined;
205
+
203
206
  type LogLevel = 'log' | 'info' | 'warn' | 'error' | 'debug';
204
207
  declare namespace Logger {
205
208
  function setLogLevel(level: LogLevel): void;
package/dist/noxus.d.ts CHANGED
@@ -1,36 +1,56 @@
1
+ interface Type<T> extends Function {
2
+ new (...args: any[]): T;
3
+ }
1
4
  type MaybeAsync<T> = T | Promise<T>;
2
5
 
6
+ type Lifetime = 'singleton' | 'scope' | 'transient';
7
+ interface IBinding {
8
+ lifetime: Lifetime;
9
+ implementation: Type<unknown>;
10
+ instance?: InstanceType<Type<unknown>>;
11
+ }
12
+ declare class AppInjector {
13
+ readonly name: string | null;
14
+ bindings: Map<Type<unknown>, IBinding>;
15
+ singletons: Map<Type<unknown>, unknown>;
16
+ scoped: Map<Type<unknown>, unknown>;
17
+ constructor(name?: string | null);
18
+ /**
19
+ * Utilisé généralement pour créer un scope d'injection de dépendances
20
+ * au niveau "scope" (donc durée de vie d'une requête)
21
+ */
22
+ createScope(): AppInjector;
23
+ /**
24
+ * Appelé lorsqu'on souhaite résoudre une dépendance,
25
+ * c'est-à-dire récupérer l'instance d'une classe donnée.
26
+ */
27
+ resolve<T extends Type<unknown>>(target: T): InstanceType<T>;
28
+ /**
29
+ *
30
+ */
31
+ private instantiate;
32
+ }
33
+ declare const RootInjector: AppInjector;
34
+
3
35
  interface IApp {
4
36
  dispose(): Promise<void>;
5
37
  onReady(): Promise<void>;
6
38
  }
7
- declare function Injectable(lifetime?: Lifetime): ClassDecorator;
8
- declare function Module(metadata: IModuleMetadata): ClassDecorator;
9
39
 
10
- type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
11
- interface IRouteDefinition {
12
- method: string;
40
+ interface IRouteMetadata {
41
+ method: HttpMethod;
13
42
  path: string;
14
- controller: Type<any>;
15
43
  handler: string;
16
44
  guards: Type<IGuard>[];
17
45
  }
18
- type ControllerAction = (request: Request, response: IResponse) => any;
19
- declare function Controller(path: string): ClassDecorator;
46
+ type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
20
47
  declare const Get: (path: string) => MethodDecorator;
21
48
  declare const Post: (path: string) => MethodDecorator;
22
49
  declare const Put: (path: string) => MethodDecorator;
23
50
  declare const Patch: (path: string) => MethodDecorator;
24
51
  declare const Delete: (path: string) => MethodDecorator;
25
- declare class Router {
26
- private readonly routes;
27
- registerController(controllerClass: Type<unknown>): Router;
28
- handle(request: Request): Promise<IResponse>;
29
- private findRoute;
30
- private resolveController;
31
- private verifyRequestBody;
32
- private extractParams;
33
- }
52
+ declare const ROUTE_METADATA_KEY: unique symbol;
53
+ declare function getRouteMetadata(target: Type<unknown>): IRouteMetadata[];
34
54
 
35
55
  declare class Request {
36
56
  readonly app: IApp;
@@ -67,62 +87,23 @@ declare function Authorize(...guardClasses: Type<IGuard>[]): MethodDecorator & C
67
87
  declare function getGuardForController(controllerName: string): Type<IGuard>[];
68
88
  declare function getGuardForControllerAction(controllerName: string, actionName: string): Type<IGuard>[];
69
89
 
70
- interface Type<T> extends Function {
71
- new (...args: any[]): T;
72
- }
73
- declare const MODULE_METADATA_KEY: unique symbol;
74
- declare const INJECTABLE_METADATA_KEY: unique symbol;
75
- declare const CONTROLLER_METADATA_KEY: unique symbol;
76
- declare const ROUTE_METADATA_KEY: unique symbol;
77
- interface IModuleMetadata {
78
- imports?: Type<unknown>[];
79
- providers?: Type<unknown>[];
80
- controllers?: Type<unknown>[];
81
- exports?: Type<unknown>[];
82
- }
83
- interface IRouteMetadata {
84
- method: HttpMethod;
90
+ interface IRouteDefinition {
91
+ method: string;
85
92
  path: string;
93
+ controller: Type<any>;
86
94
  handler: string;
87
95
  guards: Type<IGuard>[];
88
96
  }
89
- interface IControllerMetadata {
90
- path: string;
91
- guards: Type<IGuard>[];
92
- }
93
- declare function getControllerMetadata(target: Type<unknown>): IControllerMetadata | undefined;
94
- declare function getRouteMetadata(target: Type<unknown>): IRouteMetadata[];
95
- declare function getModuleMetadata(target: Function): IModuleMetadata | undefined;
96
- declare function getInjectableMetadata(target: Type<unknown>): Lifetime | undefined;
97
-
98
- type Lifetime = 'singleton' | 'scope' | 'transient';
99
- interface IBinding {
100
- lifetime: Lifetime;
101
- implementation: Type<unknown>;
102
- instance?: InstanceType<Type<unknown>>;
103
- }
104
- declare class AppInjector {
105
- readonly name: string | null;
106
- bindings: Map<Type<unknown>, IBinding>;
107
- singletons: Map<Type<unknown>, unknown>;
108
- scoped: Map<Type<unknown>, unknown>;
109
- constructor(name?: string | null);
110
- /**
111
- * Utilisé généralement pour créer un scope d'injection de dépendances
112
- * au niveau "scope" (donc durée de vie d'une requête)
113
- */
114
- createScope(): AppInjector;
115
- /**
116
- * Appelé lorsqu'on souhaite résoudre une dépendance,
117
- * c'est-à-dire récupérer l'instance d'une classe donnée.
118
- */
119
- resolve<T extends Type<unknown>>(target: T): InstanceType<T>;
120
- /**
121
- *
122
- */
123
- private instantiate;
97
+ type ControllerAction = (request: Request, response: IResponse) => any;
98
+ declare class Router {
99
+ private readonly routes;
100
+ registerController(controllerClass: Type<unknown>): Router;
101
+ handle(request: Request): Promise<IResponse>;
102
+ private findRoute;
103
+ private resolveController;
104
+ private verifyRequestBody;
105
+ private extractParams;
124
106
  }
125
- declare const RootInjector: AppInjector;
126
107
 
127
108
  /**
128
109
  *
@@ -131,7 +112,7 @@ declare function bootstrapApplication(root: Type<IApp>, rootModule: Type<any>):
131
112
 
132
113
  declare abstract class ResponseException extends Error {
133
114
  abstract readonly status: number;
134
- constructor(message: string);
115
+ constructor(message?: string);
135
116
  }
136
117
  declare class BadRequestException extends ResponseException {
137
118
  readonly status = 400;
@@ -200,6 +181,28 @@ declare class NetworkConnectTimeoutException extends ResponseException {
200
181
  readonly status = 599;
201
182
  }
202
183
 
184
+ interface IControllerMetadata {
185
+ path: string;
186
+ guards: Type<IGuard>[];
187
+ }
188
+ declare function Controller(path: string): ClassDecorator;
189
+ declare const CONTROLLER_METADATA_KEY: unique symbol;
190
+ declare function getControllerMetadata(target: Type<unknown>): IControllerMetadata | undefined;
191
+
192
+ declare function Injectable(lifetime?: Lifetime): ClassDecorator;
193
+ declare const INJECTABLE_METADATA_KEY: unique symbol;
194
+ declare function getInjectableMetadata(target: Type<unknown>): Lifetime | undefined;
195
+
196
+ declare function Module(metadata: IModuleMetadata): ClassDecorator;
197
+ declare const MODULE_METADATA_KEY: unique symbol;
198
+ interface IModuleMetadata {
199
+ imports?: Type<unknown>[];
200
+ exports?: Type<unknown>[];
201
+ providers?: Type<unknown>[];
202
+ controllers?: Type<unknown>[];
203
+ }
204
+ declare function getModuleMetadata(target: Function): IModuleMetadata | undefined;
205
+
203
206
  type LogLevel = 'log' | 'info' | 'warn' | 'error' | 'debug';
204
207
  declare namespace Logger {
205
208
  function setLogLevel(level: LogLevel): void;