@navios/core 0.6.0 → 0.7.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/CHANGELOG.md +115 -0
- package/README.md +18 -1
- package/docs/README.md +1 -0
- package/docs/legacy-compat.md +320 -0
- package/docs/testing.md +140 -17
- package/lib/index-DW9EPAE6.d.mts +2156 -0
- package/lib/index-DW9EPAE6.d.mts.map +1 -0
- package/lib/index-pHp-dIGt.d.cts +2156 -0
- package/lib/index-pHp-dIGt.d.cts.map +1 -0
- package/lib/index.cjs +157 -0
- package/lib/index.d.cts +3 -0
- package/lib/index.d.mts +3 -190
- package/lib/index.mjs +4 -1459
- package/lib/legacy-compat/index.cjs +315 -0
- package/lib/legacy-compat/index.cjs.map +1 -0
- package/lib/legacy-compat/index.d.cts +219 -0
- package/lib/legacy-compat/index.d.cts.map +1 -0
- package/lib/legacy-compat/index.d.mts +219 -0
- package/lib/legacy-compat/index.d.mts.map +1 -0
- package/lib/legacy-compat/index.mjs +308 -0
- package/lib/legacy-compat/index.mjs.map +1 -0
- package/lib/src-DyvCDuKO.mjs +5443 -0
- package/lib/src-DyvCDuKO.mjs.map +1 -0
- package/lib/src-QnxR5b7c.cjs +5800 -0
- package/lib/src-QnxR5b7c.cjs.map +1 -0
- package/lib/testing/index.cjs +106 -0
- package/lib/testing/index.cjs.map +1 -0
- package/lib/testing/index.d.cts +156 -0
- package/lib/testing/index.d.cts.map +1 -0
- package/lib/testing/index.d.mts +156 -0
- package/lib/testing/index.d.mts.map +1 -0
- package/lib/testing/index.mjs +100 -0
- package/lib/testing/index.mjs.map +1 -0
- package/lib/use-guards.decorator-B6q_N0sf.cjs +622 -0
- package/lib/use-guards.decorator-B6q_N0sf.cjs.map +1 -0
- package/lib/use-guards.decorator-kZ3lNK8v.mjs +454 -0
- package/lib/use-guards.decorator-kZ3lNK8v.mjs.map +1 -0
- package/package.json +28 -8
- package/project.json +2 -2
- package/src/attribute.factory.mts +154 -0
- package/src/config/config-service.interface.mts +31 -0
- package/src/config/config.provider.mts +36 -0
- package/src/config/config.service.mts +94 -4
- package/src/decorators/controller.decorator.mts +28 -0
- package/src/decorators/endpoint.decorator.mts +76 -0
- package/src/decorators/header.decorator.mts +19 -0
- package/src/decorators/http-code.decorator.mts +20 -0
- package/src/decorators/module.decorator.mts +34 -0
- package/src/decorators/multipart.decorator.mts +41 -0
- package/src/decorators/stream.decorator.mts +33 -0
- package/src/decorators/use-guards.decorator.mts +29 -0
- package/src/exceptions/bad-request.exception.mts +21 -0
- package/src/exceptions/conflict.exception.mts +24 -0
- package/src/exceptions/forbidden.exception.mts +23 -0
- package/src/exceptions/http.exception.mts +26 -0
- package/src/exceptions/internal-server-error.exception.mts +26 -0
- package/src/exceptions/not-found.exception.mts +23 -0
- package/src/exceptions/unauthorized.exception.mts +23 -0
- package/src/index.mts +1 -0
- package/src/interfaces/abstract-execution-context.inteface.mts +35 -0
- package/src/interfaces/abstract-http-adapter.interface.mts +52 -0
- package/src/interfaces/abstract-http-handler-adapter.interface.mts +2 -2
- package/src/interfaces/can-activate.mts +31 -0
- package/src/interfaces/index.mts +1 -0
- package/src/interfaces/navios-module.mts +25 -0
- package/src/interfaces/plugin.interface.mts +105 -0
- package/src/legacy-compat/__type-tests__/legacy-decorators.spec-d.mts +420 -0
- package/src/legacy-compat/__type-tests__/tsconfig.json +15 -0
- package/src/legacy-compat/context-compat.mts +93 -0
- package/src/legacy-compat/decorators/controller.decorator.mts +31 -0
- package/src/legacy-compat/decorators/endpoint.decorator.mts +99 -0
- package/src/legacy-compat/decorators/header.decorator.mts +42 -0
- package/src/legacy-compat/decorators/http-code.decorator.mts +38 -0
- package/src/legacy-compat/decorators/index.mts +9 -0
- package/src/legacy-compat/decorators/module.decorator.mts +37 -0
- package/src/legacy-compat/decorators/multipart.decorator.mts +93 -0
- package/src/legacy-compat/decorators/stream.decorator.mts +76 -0
- package/src/legacy-compat/decorators/use-guards.decorator.mts +80 -0
- package/src/legacy-compat/index.mts +40 -0
- package/src/logger/console-logger.service.mts +15 -2
- package/src/logger/log-levels.mts +9 -0
- package/src/logger/logger.service.mts +21 -0
- package/src/logger/logger.tokens.mts +23 -0
- package/src/navios.application.mts +228 -4
- package/src/navios.factory.mts +60 -1
- package/src/services/guard-runner.service.mts +12 -11
- package/src/services/module-loader.service.mts +118 -12
- package/src/stores/index.mts +1 -0
- package/src/stores/request-id.store.mts +43 -0
- package/src/testing/index.mts +2 -0
- package/src/testing/testing-module.mts +231 -0
- package/tsconfig.lib.json +1 -1
- package/tsconfig.spec.json +3 -0
- package/tsdown.config.mts +35 -0
- package/vitest.config.mts +6 -0
- package/lib/_tsup-dts-rollup.d.mts +0 -1365
- package/lib/_tsup-dts-rollup.d.ts +0 -1365
- package/lib/index.d.ts +0 -190
- package/lib/index.js +0 -1540
- package/lib/index.js.map +0 -1
- package/lib/index.mjs.map +0 -1
- package/tsup.config.mts +0 -13
|
@@ -4,9 +4,37 @@ import { Injectable, InjectableScope, InjectionToken } from '@navios/di'
|
|
|
4
4
|
|
|
5
5
|
import { getControllerMetadata } from '../metadata/index.mjs'
|
|
6
6
|
|
|
7
|
+
/**
|
|
8
|
+
* Options for configuring a Navios controller.
|
|
9
|
+
*/
|
|
7
10
|
export interface ControllerOptions {
|
|
11
|
+
/**
|
|
12
|
+
* Guards to apply to all endpoints in this controller.
|
|
13
|
+
* Guards are executed in reverse order (last guard first).
|
|
14
|
+
*/
|
|
8
15
|
guards?: ClassType[] | Set<ClassType>
|
|
9
16
|
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Decorator that marks a class as a Navios controller.
|
|
20
|
+
*
|
|
21
|
+
* Controllers handle HTTP requests and define endpoints.
|
|
22
|
+
* They are request-scoped by default, meaning a new instance is created for each request.
|
|
23
|
+
*
|
|
24
|
+
* @param options - Controller configuration options
|
|
25
|
+
* @returns A class decorator
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```typescript
|
|
29
|
+
* @Controller({ guards: [AuthGuard] })
|
|
30
|
+
* export class UserController {
|
|
31
|
+
* @Endpoint(getUserEndpoint)
|
|
32
|
+
* async getUser(request: EndpointParams<typeof getUserEndpoint>) {
|
|
33
|
+
* // Handle request
|
|
34
|
+
* }
|
|
35
|
+
* }
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
10
38
|
export function Controller({ guards }: ControllerOptions = {}) {
|
|
11
39
|
return function (target: ClassType, context: ClassDecoratorContext) {
|
|
12
40
|
if (context.kind !== 'class') {
|
|
@@ -11,6 +11,30 @@ import { ZodDiscriminatedUnion } from 'zod/v4'
|
|
|
11
11
|
import { getEndpointMetadata } from '../metadata/index.mjs'
|
|
12
12
|
import { EndpointAdapterToken } from '../tokens/index.mjs'
|
|
13
13
|
|
|
14
|
+
/**
|
|
15
|
+
* Extracts the typed parameters for an endpoint handler function.
|
|
16
|
+
*
|
|
17
|
+
* This utility type extracts URL parameters, query parameters, and request body
|
|
18
|
+
* from an endpoint declaration and flattens them into a single object.
|
|
19
|
+
*
|
|
20
|
+
* @typeParam EndpointDeclaration - The endpoint declaration from @navios/builder
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```typescript
|
|
24
|
+
* const getUserEndpoint = api.declareEndpoint({
|
|
25
|
+
* method: 'get',
|
|
26
|
+
* url: '/users/$userId',
|
|
27
|
+
* querySchema: z.object({ include: z.string().optional() }),
|
|
28
|
+
* responseSchema: z.object({ id: z.string(), name: z.string() }),
|
|
29
|
+
* })
|
|
30
|
+
*
|
|
31
|
+
* @Endpoint(getUserEndpoint)
|
|
32
|
+
* async getUser(request: EndpointParams<typeof getUserEndpoint>) {
|
|
33
|
+
* // request.urlParams.userId is typed as string
|
|
34
|
+
* // request.query.include is typed as string | undefined
|
|
35
|
+
* }
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
14
38
|
export type EndpointParams<
|
|
15
39
|
EndpointDeclaration extends {
|
|
16
40
|
config: BaseEndpointConfig<any, any, any, any, any>
|
|
@@ -39,6 +63,28 @@ export type EndpointParams<
|
|
|
39
63
|
>
|
|
40
64
|
: Util_FlatObject<EndpointFunctionArgs<Url, undefined, undefined, true>>
|
|
41
65
|
|
|
66
|
+
/**
|
|
67
|
+
* Extracts the typed return value for an endpoint handler function.
|
|
68
|
+
*
|
|
69
|
+
* This utility type extracts the response schema from an endpoint declaration
|
|
70
|
+
* and returns the appropriate Promise type.
|
|
71
|
+
*
|
|
72
|
+
* @typeParam EndpointDeclaration - The endpoint declaration from @navios/builder
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* ```typescript
|
|
76
|
+
* const getUserEndpoint = api.declareEndpoint({
|
|
77
|
+
* method: 'get',
|
|
78
|
+
* url: '/users/$userId',
|
|
79
|
+
* responseSchema: z.object({ id: z.string(), name: z.string() }),
|
|
80
|
+
* })
|
|
81
|
+
*
|
|
82
|
+
* @Endpoint(getUserEndpoint)
|
|
83
|
+
* async getUser(request: EndpointParams<typeof getUserEndpoint>): EndpointResult<typeof getUserEndpoint> {
|
|
84
|
+
* return { id: '1', name: 'John' } // Type-checked against responseSchema
|
|
85
|
+
* }
|
|
86
|
+
* ```
|
|
87
|
+
*/
|
|
42
88
|
export type EndpointResult<
|
|
43
89
|
EndpointDeclaration extends {
|
|
44
90
|
config: BaseEndpointConfig<any, any, any, any, any>
|
|
@@ -50,6 +96,36 @@ export type EndpointResult<
|
|
|
50
96
|
? Promise<z.input<Options[number]>>
|
|
51
97
|
: Promise<z.input<EndpointDeclaration['config']['responseSchema']>>
|
|
52
98
|
|
|
99
|
+
/**
|
|
100
|
+
* Decorator that marks a method as an HTTP endpoint.
|
|
101
|
+
*
|
|
102
|
+
* The endpoint must be defined using @navios/builder's `declareEndpoint` method.
|
|
103
|
+
* This ensures type safety and consistency between client and server.
|
|
104
|
+
*
|
|
105
|
+
* @param endpoint - The endpoint declaration from @navios/builder
|
|
106
|
+
* @returns A method decorator
|
|
107
|
+
*
|
|
108
|
+
* @example
|
|
109
|
+
* ```typescript
|
|
110
|
+
* import { builder } from '@navios/builder'
|
|
111
|
+
*
|
|
112
|
+
* const api = builder()
|
|
113
|
+
* const getUserEndpoint = api.declareEndpoint({
|
|
114
|
+
* method: 'get',
|
|
115
|
+
* url: '/users/$userId',
|
|
116
|
+
* responseSchema: z.object({ id: z.string(), name: z.string() }),
|
|
117
|
+
* })
|
|
118
|
+
*
|
|
119
|
+
* @Controller()
|
|
120
|
+
* export class UserController {
|
|
121
|
+
* @Endpoint(getUserEndpoint)
|
|
122
|
+
* async getUser(request: EndpointParams<typeof getUserEndpoint>) {
|
|
123
|
+
* const { userId } = request.urlParams
|
|
124
|
+
* return { id: userId, name: 'John' }
|
|
125
|
+
* }
|
|
126
|
+
* }
|
|
127
|
+
* ```
|
|
128
|
+
*/
|
|
53
129
|
export function Endpoint<
|
|
54
130
|
Method extends HttpMethod = HttpMethod,
|
|
55
131
|
Url extends string = string,
|
|
@@ -2,6 +2,25 @@ import type { HttpHeader } from '../interfaces/index.mjs'
|
|
|
2
2
|
|
|
3
3
|
import { getEndpointMetadata } from '../metadata/index.mjs'
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* Decorator that sets a custom HTTP response header for an endpoint.
|
|
7
|
+
*
|
|
8
|
+
* @param name - The header name (e.g., 'Content-Type', 'Cache-Control')
|
|
9
|
+
* @param value - The header value (string, number, or array of strings)
|
|
10
|
+
* @returns A method decorator
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* @Controller()
|
|
15
|
+
* export class UserController {
|
|
16
|
+
* @Endpoint(getUserEndpoint)
|
|
17
|
+
* @Header('Cache-Control', 'max-age=3600')
|
|
18
|
+
* async getUser() {
|
|
19
|
+
* return { id: '1', name: 'John' }
|
|
20
|
+
* }
|
|
21
|
+
* }
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
5
24
|
export function Header(name: HttpHeader, value: string | number | string[]) {
|
|
6
25
|
return <T extends Function>(
|
|
7
26
|
target: T,
|
|
@@ -1,5 +1,25 @@
|
|
|
1
1
|
import { getEndpointMetadata } from '../metadata/index.mjs'
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* Decorator that sets a custom HTTP status code for successful responses.
|
|
5
|
+
*
|
|
6
|
+
* By default, endpoints return 200 OK. Use this decorator to return a different status code.
|
|
7
|
+
*
|
|
8
|
+
* @param code - The HTTP status code to return (e.g., 201, 204, 202)
|
|
9
|
+
* @returns A method decorator
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* @Controller()
|
|
14
|
+
* export class UserController {
|
|
15
|
+
* @Endpoint(createUserEndpoint)
|
|
16
|
+
* @HttpCode(201)
|
|
17
|
+
* async createUser() {
|
|
18
|
+
* return { id: '1', name: 'John' }
|
|
19
|
+
* }
|
|
20
|
+
* }
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
3
23
|
export function HttpCode(code: number) {
|
|
4
24
|
return <T extends Function>(
|
|
5
25
|
target: T,
|
|
@@ -4,12 +4,46 @@ import { Injectable, InjectableScope, InjectionToken } from '@navios/di'
|
|
|
4
4
|
|
|
5
5
|
import { getModuleMetadata } from '../metadata/index.mjs'
|
|
6
6
|
|
|
7
|
+
/**
|
|
8
|
+
* Options for configuring a Navios module.
|
|
9
|
+
*/
|
|
7
10
|
export interface ModuleOptions {
|
|
11
|
+
/**
|
|
12
|
+
* Controllers to register in this module.
|
|
13
|
+
* Controllers handle HTTP requests and define endpoints.
|
|
14
|
+
*/
|
|
8
15
|
controllers?: ClassType[] | Set<ClassType>
|
|
16
|
+
/**
|
|
17
|
+
* Other modules to import into this module.
|
|
18
|
+
* Imported modules' controllers and services become available.
|
|
19
|
+
*/
|
|
9
20
|
imports?: ClassType[] | Set<ClassType>
|
|
21
|
+
/**
|
|
22
|
+
* Guards to apply to all controllers in this module.
|
|
23
|
+
* Guards are executed in reverse order (last guard first).
|
|
24
|
+
*/
|
|
10
25
|
guards?: ClassType[] | Set<ClassType>
|
|
11
26
|
}
|
|
12
27
|
|
|
28
|
+
/**
|
|
29
|
+
* Decorator that marks a class as a Navios module.
|
|
30
|
+
*
|
|
31
|
+
* Modules are the basic building blocks of a Navios application.
|
|
32
|
+
* They organize controllers, services, and other modules into logical units.
|
|
33
|
+
*
|
|
34
|
+
* @param options - Module configuration options
|
|
35
|
+
* @returns A class decorator
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```typescript
|
|
39
|
+
* @Module({
|
|
40
|
+
* controllers: [UserController, AuthController],
|
|
41
|
+
* imports: [DatabaseModule],
|
|
42
|
+
* guards: [AuthGuard],
|
|
43
|
+
* })
|
|
44
|
+
* export class AppModule {}
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
13
47
|
export function Module(
|
|
14
48
|
{ controllers = [], imports = [], guards = [] }: ModuleOptions = {
|
|
15
49
|
controllers: [],
|
|
@@ -11,6 +11,13 @@ import { ZodDiscriminatedUnion } from 'zod/v4'
|
|
|
11
11
|
import { getEndpointMetadata } from '../metadata/index.mjs'
|
|
12
12
|
import { MultipartAdapterToken } from '../tokens/index.mjs'
|
|
13
13
|
|
|
14
|
+
/**
|
|
15
|
+
* Extracts the typed parameters for a multipart endpoint handler function.
|
|
16
|
+
*
|
|
17
|
+
* Similar to `EndpointParams`, but specifically for multipart/form-data endpoints.
|
|
18
|
+
*
|
|
19
|
+
* @typeParam EndpointDeclaration - The endpoint declaration from @navios/builder
|
|
20
|
+
*/
|
|
14
21
|
export type MultipartParams<
|
|
15
22
|
EndpointDeclaration extends {
|
|
16
23
|
config: BaseEndpointConfig<any, any, any, any, any>
|
|
@@ -39,6 +46,11 @@ export type MultipartParams<
|
|
|
39
46
|
>
|
|
40
47
|
: Util_FlatObject<EndpointFunctionArgs<Url, undefined, undefined, true>>
|
|
41
48
|
|
|
49
|
+
/**
|
|
50
|
+
* Extracts the typed return value for a multipart endpoint handler function.
|
|
51
|
+
*
|
|
52
|
+
* @typeParam EndpointDeclaration - The endpoint declaration from @navios/builder
|
|
53
|
+
*/
|
|
42
54
|
export type MultipartResult<
|
|
43
55
|
EndpointDeclaration extends {
|
|
44
56
|
config: BaseEndpointConfig<any, any, any, any, any>
|
|
@@ -50,6 +62,35 @@ export type MultipartResult<
|
|
|
50
62
|
? Promise<z.input<Options[number]>>
|
|
51
63
|
: Promise<z.input<EndpointDeclaration['config']['responseSchema']>>
|
|
52
64
|
|
|
65
|
+
/**
|
|
66
|
+
* Decorator that marks a method as a multipart/form-data endpoint.
|
|
67
|
+
*
|
|
68
|
+
* Use this decorator for endpoints that handle file uploads or form data.
|
|
69
|
+
* The endpoint must be defined using @navios/builder's `declareMultipart` method.
|
|
70
|
+
*
|
|
71
|
+
* @param endpoint - The multipart endpoint declaration from @navios/builder
|
|
72
|
+
* @returns A method decorator
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* ```typescript
|
|
76
|
+
* const uploadFileEndpoint = api.declareMultipart({
|
|
77
|
+
* method: 'post',
|
|
78
|
+
* url: '/upload',
|
|
79
|
+
* requestSchema: z.object({ file: z.instanceof(File) }),
|
|
80
|
+
* responseSchema: z.object({ url: z.string() }),
|
|
81
|
+
* })
|
|
82
|
+
*
|
|
83
|
+
* @Controller()
|
|
84
|
+
* export class FileController {
|
|
85
|
+
* @Multipart(uploadFileEndpoint)
|
|
86
|
+
* async uploadFile(request: MultipartParams<typeof uploadFileEndpoint>) {
|
|
87
|
+
* const { file } = request.data
|
|
88
|
+
* // Handle file upload
|
|
89
|
+
* return { url: 'https://example.com/file.jpg' }
|
|
90
|
+
* }
|
|
91
|
+
* }
|
|
92
|
+
* ```
|
|
93
|
+
*/
|
|
53
94
|
export function Multipart<
|
|
54
95
|
Method extends HttpMethod = HttpMethod,
|
|
55
96
|
Url extends string = string,
|
|
@@ -9,6 +9,13 @@ import type { ZodObject, ZodType } from 'zod/v4'
|
|
|
9
9
|
import { getEndpointMetadata } from '../metadata/index.mjs'
|
|
10
10
|
import { StreamAdapterToken } from '../tokens/index.mjs'
|
|
11
11
|
|
|
12
|
+
/**
|
|
13
|
+
* Extracts the typed parameters for a stream endpoint handler function.
|
|
14
|
+
*
|
|
15
|
+
* Similar to `EndpointParams`, but specifically for streaming endpoints.
|
|
16
|
+
*
|
|
17
|
+
* @typeParam EndpointDeclaration - The stream endpoint declaration from @navios/builder
|
|
18
|
+
*/
|
|
12
19
|
export type StreamParams<
|
|
13
20
|
EndpointDeclaration extends {
|
|
14
21
|
config: BaseStreamConfig<any, any, any, any>
|
|
@@ -37,6 +44,32 @@ export type StreamParams<
|
|
|
37
44
|
>
|
|
38
45
|
: Util_FlatObject<EndpointFunctionArgs<Url, undefined, undefined, true>>
|
|
39
46
|
|
|
47
|
+
/**
|
|
48
|
+
* Decorator that marks a method as a streaming endpoint.
|
|
49
|
+
*
|
|
50
|
+
* Use this decorator for endpoints that stream data (e.g., file downloads, SSE).
|
|
51
|
+
* The endpoint must be defined using @navios/builder's `declareStream` method.
|
|
52
|
+
*
|
|
53
|
+
* @param endpoint - The stream endpoint declaration from @navios/builder
|
|
54
|
+
* @returns A method decorator
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* ```typescript
|
|
58
|
+
* const downloadFileEndpoint = api.declareStream({
|
|
59
|
+
* method: 'get',
|
|
60
|
+
* url: '/files/$fileId',
|
|
61
|
+
* })
|
|
62
|
+
*
|
|
63
|
+
* @Controller()
|
|
64
|
+
* export class FileController {
|
|
65
|
+
* @Stream(downloadFileEndpoint)
|
|
66
|
+
* async downloadFile(request: StreamParams<typeof downloadFileEndpoint>, reply: any) {
|
|
67
|
+
* const { fileId } = request.urlParams
|
|
68
|
+
* // Stream file data to reply
|
|
69
|
+
* }
|
|
70
|
+
* }
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
40
73
|
export function Stream<
|
|
41
74
|
Method extends HttpMethod = HttpMethod,
|
|
42
75
|
Url extends string = string,
|
|
@@ -11,6 +11,35 @@ import {
|
|
|
11
11
|
getEndpointMetadata,
|
|
12
12
|
} from '../metadata/index.mjs'
|
|
13
13
|
|
|
14
|
+
/**
|
|
15
|
+
* Decorator that applies guards to a controller or endpoint.
|
|
16
|
+
*
|
|
17
|
+
* Guards are used for authentication, authorization, and request validation.
|
|
18
|
+
* They implement the `CanActivate` interface and are executed before the endpoint handler.
|
|
19
|
+
* Guards can be applied at the module, controller, or endpoint level.
|
|
20
|
+
*
|
|
21
|
+
* @param guards - Guard classes or injection tokens to apply
|
|
22
|
+
* @returns A class or method decorator
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```typescript
|
|
26
|
+
* // Apply to a controller
|
|
27
|
+
* @Controller()
|
|
28
|
+
* @UseGuards(AuthGuard, RoleGuard)
|
|
29
|
+
* export class UserController {
|
|
30
|
+
* @Endpoint(getUserEndpoint)
|
|
31
|
+
* async getUser() { }
|
|
32
|
+
* }
|
|
33
|
+
*
|
|
34
|
+
* // Apply to a specific endpoint
|
|
35
|
+
* @Controller()
|
|
36
|
+
* export class UserController {
|
|
37
|
+
* @Endpoint(getUserEndpoint)
|
|
38
|
+
* @UseGuards(AuthGuard)
|
|
39
|
+
* async getUser() { }
|
|
40
|
+
* }
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
14
43
|
export function UseGuards(
|
|
15
44
|
...guards: (
|
|
16
45
|
| ClassTypeWithInstance<CanActivate>
|
|
@@ -1,6 +1,27 @@
|
|
|
1
1
|
import { HttpException } from './http.exception.mjs'
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* Exception that represents a 400 Bad Request HTTP error.
|
|
5
|
+
*
|
|
6
|
+
* Use this exception when the client's request is malformed or invalid.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* @Endpoint(createUserEndpoint)
|
|
11
|
+
* async createUser(request: EndpointParams<typeof createUserEndpoint>) {
|
|
12
|
+
* if (!request.data.email) {
|
|
13
|
+
* throw new BadRequestException('Email is required')
|
|
14
|
+
* }
|
|
15
|
+
* // ...
|
|
16
|
+
* }
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
3
19
|
export class BadRequestException extends HttpException {
|
|
20
|
+
/**
|
|
21
|
+
* Creates a new BadRequestException.
|
|
22
|
+
*
|
|
23
|
+
* @param message - Error message or response object
|
|
24
|
+
*/
|
|
4
25
|
constructor(message: string | object) {
|
|
5
26
|
super(400, message)
|
|
6
27
|
}
|
|
@@ -1,6 +1,30 @@
|
|
|
1
1
|
import { HttpException } from './http.exception.mjs'
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* Exception that represents a 409 Conflict HTTP error.
|
|
5
|
+
*
|
|
6
|
+
* Use this exception when the request conflicts with the current state of the resource
|
|
7
|
+
* (e.g., trying to create a resource that already exists).
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* @Endpoint(createUserEndpoint)
|
|
12
|
+
* async createUser(request: EndpointParams<typeof createUserEndpoint>) {
|
|
13
|
+
* const existing = await this.userService.findByEmail(request.data.email)
|
|
14
|
+
* if (existing) {
|
|
15
|
+
* throw new ConflictException('User with this email already exists')
|
|
16
|
+
* }
|
|
17
|
+
* // ...
|
|
18
|
+
* }
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
3
21
|
export class ConflictException extends HttpException {
|
|
22
|
+
/**
|
|
23
|
+
* Creates a new ConflictException.
|
|
24
|
+
*
|
|
25
|
+
* @param message - Error message or response object
|
|
26
|
+
* @param error - Optional underlying error for logging
|
|
27
|
+
*/
|
|
4
28
|
constructor(message: string | object, error?: Error) {
|
|
5
29
|
super(409, message, error)
|
|
6
30
|
}
|
|
@@ -1,6 +1,29 @@
|
|
|
1
1
|
import { HttpException } from './http.exception.mjs'
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* Exception that represents a 403 Forbidden HTTP error.
|
|
5
|
+
*
|
|
6
|
+
* Use this exception when the client is authenticated but does not have
|
|
7
|
+
* permission to access the requested resource.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* @Endpoint(deleteUserEndpoint)
|
|
12
|
+
* @UseGuards(AuthGuard, RoleGuard)
|
|
13
|
+
* async deleteUser(request: EndpointParams<typeof deleteUserEndpoint>) {
|
|
14
|
+
* if (!this.userService.hasPermission(request.user, 'delete')) {
|
|
15
|
+
* throw new ForbiddenException('Insufficient permissions')
|
|
16
|
+
* }
|
|
17
|
+
* // ...
|
|
18
|
+
* }
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
3
21
|
export class ForbiddenException extends HttpException {
|
|
22
|
+
/**
|
|
23
|
+
* Creates a new ForbiddenException.
|
|
24
|
+
*
|
|
25
|
+
* @param message - Error message
|
|
26
|
+
*/
|
|
4
27
|
constructor(message: string) {
|
|
5
28
|
super(403, message)
|
|
6
29
|
}
|
|
@@ -1,4 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Base exception class for all HTTP exceptions in Navios.
|
|
3
|
+
*
|
|
4
|
+
* All HTTP exception classes extend this base class. When thrown from an endpoint handler,
|
|
5
|
+
* Navios will automatically convert it to an appropriate HTTP response with the specified
|
|
6
|
+
* status code and response body.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* @Endpoint(getUserEndpoint)
|
|
11
|
+
* async getUser(request: EndpointParams<typeof getUserEndpoint>) {
|
|
12
|
+
* const user = await this.userService.findById(request.urlParams.userId)
|
|
13
|
+
* if (!user) {
|
|
14
|
+
* throw new HttpException(404, 'User not found')
|
|
15
|
+
* }
|
|
16
|
+
* return user
|
|
17
|
+
* }
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
1
20
|
export class HttpException {
|
|
21
|
+
/**
|
|
22
|
+
* Creates a new HttpException instance.
|
|
23
|
+
*
|
|
24
|
+
* @param statusCode - HTTP status code (e.g., 400, 404, 500)
|
|
25
|
+
* @param response - Response body (string or object)
|
|
26
|
+
* @param error - Optional underlying error for logging/debugging
|
|
27
|
+
*/
|
|
2
28
|
constructor(
|
|
3
29
|
public readonly statusCode: number,
|
|
4
30
|
public readonly response: string | object,
|
|
@@ -1,6 +1,32 @@
|
|
|
1
1
|
import { HttpException } from './http.exception.mjs'
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* Exception that represents a 500 Internal Server Error HTTP error.
|
|
5
|
+
*
|
|
6
|
+
* Use this exception when an unexpected error occurs on the server.
|
|
7
|
+
* Generally, you should let unhandled errors bubble up rather than catching
|
|
8
|
+
* and rethrowing as InternalServerErrorException, as Navios will handle them appropriately.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* @Endpoint(processPaymentEndpoint)
|
|
13
|
+
* async processPayment(request: EndpointParams<typeof processPaymentEndpoint>) {
|
|
14
|
+
* try {
|
|
15
|
+
* return await this.paymentService.process(request.data)
|
|
16
|
+
* } catch (error) {
|
|
17
|
+
* this.logger.error('Payment processing failed', error)
|
|
18
|
+
* throw new InternalServerErrorException('Payment processing failed', error)
|
|
19
|
+
* }
|
|
20
|
+
* }
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
3
23
|
export class InternalServerErrorException extends HttpException {
|
|
24
|
+
/**
|
|
25
|
+
* Creates a new InternalServerErrorException.
|
|
26
|
+
*
|
|
27
|
+
* @param message - Error message or response object
|
|
28
|
+
* @param error - Optional underlying error for logging
|
|
29
|
+
*/
|
|
4
30
|
constructor(message: string | object, error?: Error) {
|
|
5
31
|
super(500, message, error)
|
|
6
32
|
}
|
|
@@ -1,6 +1,29 @@
|
|
|
1
1
|
import { HttpException } from './http.exception.mjs'
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* Exception that represents a 404 Not Found HTTP error.
|
|
5
|
+
*
|
|
6
|
+
* Use this exception when the requested resource does not exist.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* @Endpoint(getUserEndpoint)
|
|
11
|
+
* async getUser(request: EndpointParams<typeof getUserEndpoint>) {
|
|
12
|
+
* const user = await this.userService.findById(request.urlParams.userId)
|
|
13
|
+
* if (!user) {
|
|
14
|
+
* throw new NotFoundException('User not found')
|
|
15
|
+
* }
|
|
16
|
+
* return user
|
|
17
|
+
* }
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
3
20
|
export class NotFoundException extends HttpException {
|
|
21
|
+
/**
|
|
22
|
+
* Creates a new NotFoundException.
|
|
23
|
+
*
|
|
24
|
+
* @param response - Error message or response object
|
|
25
|
+
* @param error - Optional underlying error for logging
|
|
26
|
+
*/
|
|
4
27
|
constructor(
|
|
5
28
|
public readonly response: string | object,
|
|
6
29
|
public readonly error?: Error,
|
|
@@ -1,6 +1,29 @@
|
|
|
1
1
|
import { HttpException } from './http.exception.mjs'
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* Exception that represents a 401 Unauthorized HTTP error.
|
|
5
|
+
*
|
|
6
|
+
* Use this exception when the client is not authenticated or authentication failed.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* @Endpoint(getUserEndpoint)
|
|
11
|
+
* @UseGuards(AuthGuard)
|
|
12
|
+
* async getUser(request: EndpointParams<typeof getUserEndpoint>) {
|
|
13
|
+
* if (!request.headers.authorization) {
|
|
14
|
+
* throw new UnauthorizedException('Authentication required')
|
|
15
|
+
* }
|
|
16
|
+
* // ...
|
|
17
|
+
* }
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
3
20
|
export class UnauthorizedException extends HttpException {
|
|
21
|
+
/**
|
|
22
|
+
* Creates a new UnauthorizedException.
|
|
23
|
+
*
|
|
24
|
+
* @param message - Error message or response object
|
|
25
|
+
* @param error - Optional underlying error for logging
|
|
26
|
+
*/
|
|
4
27
|
constructor(message: string | object, error?: Error) {
|
|
5
28
|
super(401, message, error)
|
|
6
29
|
}
|
package/src/index.mts
CHANGED
|
@@ -6,6 +6,7 @@ export * from './interfaces/index.mjs'
|
|
|
6
6
|
export * from './logger/index.mjs'
|
|
7
7
|
export * from './metadata/index.mjs'
|
|
8
8
|
export * from './services/index.mjs'
|
|
9
|
+
export * from './stores/index.mjs'
|
|
9
10
|
export * from './tokens/index.mjs'
|
|
10
11
|
export * from './attribute.factory.mjs'
|
|
11
12
|
export * from './factories/index.mjs'
|
|
@@ -4,10 +4,45 @@ import type {
|
|
|
4
4
|
ModuleMetadata,
|
|
5
5
|
} from '../metadata/index.mjs'
|
|
6
6
|
|
|
7
|
+
/**
|
|
8
|
+
* Interface providing access to the execution context during request handling.
|
|
9
|
+
*
|
|
10
|
+
* The execution context provides access to metadata and request/reply objects
|
|
11
|
+
* for the current request. It is available in guards and can be injected into
|
|
12
|
+
* services if needed.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* @Injectable()
|
|
17
|
+
* export class AuthGuard implements CanActivate {
|
|
18
|
+
* async canActivate(context: AbstractExecutionContext): Promise<boolean> {
|
|
19
|
+
* const request = context.getRequest()
|
|
20
|
+
* const handler = context.getHandler()
|
|
21
|
+
* // Access request and handler metadata
|
|
22
|
+
* return true
|
|
23
|
+
* }
|
|
24
|
+
* }
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
7
27
|
export interface AbstractExecutionContext {
|
|
28
|
+
/**
|
|
29
|
+
* Gets the metadata for the module containing the current endpoint.
|
|
30
|
+
*/
|
|
8
31
|
getModule(): ModuleMetadata
|
|
32
|
+
/**
|
|
33
|
+
* Gets the metadata for the controller containing the current endpoint.
|
|
34
|
+
*/
|
|
9
35
|
getController(): ControllerMetadata
|
|
36
|
+
/**
|
|
37
|
+
* Gets the metadata for the current endpoint handler.
|
|
38
|
+
*/
|
|
10
39
|
getHandler(): HandlerMetadata
|
|
40
|
+
/**
|
|
41
|
+
* Gets the HTTP request object (adapter-specific).
|
|
42
|
+
*/
|
|
11
43
|
getRequest(): any
|
|
44
|
+
/**
|
|
45
|
+
* Gets the HTTP reply object (adapter-specific).
|
|
46
|
+
*/
|
|
12
47
|
getReply(): any
|
|
13
48
|
}
|