@navios/core 0.4.0 → 0.5.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/README.md +95 -2
- package/docs/README.md +310 -3
- package/docs/adapters.md +308 -0
- package/docs/application-setup.md +524 -0
- package/docs/attributes.md +689 -0
- package/docs/controllers.md +373 -0
- package/docs/endpoints.md +444 -0
- package/docs/exceptions.md +316 -0
- package/docs/guards.md +550 -0
- package/docs/modules.md +377 -0
- package/docs/quick-start.md +295 -0
- package/docs/services.md +427 -0
- package/docs/testing.md +704 -0
- package/lib/_tsup-dts-rollup.d.mts +310 -239
- package/lib/_tsup-dts-rollup.d.ts +310 -239
- package/lib/index.d.mts +51 -28
- package/lib/index.d.ts +51 -28
- package/lib/index.js +633 -1072
- package/lib/index.js.map +1 -1
- package/lib/index.mjs +631 -1064
- package/lib/index.mjs.map +1 -1
- package/package.json +5 -9
- package/project.json +9 -1
- package/src/__tests__/config.service.spec.mts +11 -9
- package/src/__tests__/controller.spec.mts +0 -1
- package/src/config/config.service.mts +2 -2
- package/src/decorators/controller.decorator.mts +1 -1
- package/src/decorators/endpoint.decorator.mts +2 -2
- package/src/decorators/header.decorator.mts +1 -1
- package/src/decorators/multipart.decorator.mts +1 -2
- package/src/decorators/stream.decorator.mts +2 -3
- package/src/factories/endpoint-adapter.factory.mts +21 -0
- package/src/factories/http-adapter.factory.mts +20 -0
- package/src/factories/index.mts +6 -0
- package/src/factories/multipart-adapter.factory.mts +21 -0
- package/src/factories/reply.factory.mts +21 -0
- package/src/factories/request.factory.mts +21 -0
- package/src/factories/stream-adapter.factory.mts +20 -0
- package/src/index.mts +1 -1
- package/src/interfaces/abstract-execution-context.inteface.mts +13 -0
- package/src/interfaces/abstract-http-adapter.interface.mts +20 -0
- package/src/interfaces/abstract-http-cors-options.interface.mts +59 -0
- package/src/interfaces/abstract-http-handler-adapter.interface.mts +13 -0
- package/src/interfaces/abstract-http-listen-options.interface.mts +4 -0
- package/src/interfaces/can-activate.mts +4 -2
- package/src/interfaces/http-header.mts +18 -0
- package/src/interfaces/index.mts +6 -0
- package/src/logger/console-logger.service.mts +28 -44
- package/src/logger/index.mts +1 -2
- package/src/logger/logger.service.mts +9 -128
- package/src/logger/logger.tokens.mts +21 -0
- package/src/metadata/handler.metadata.mts +7 -5
- package/src/navios.application.mts +65 -172
- package/src/navios.environment.mts +30 -0
- package/src/navios.factory.mts +53 -12
- package/src/services/guard-runner.service.mts +19 -9
- package/src/services/index.mts +0 -2
- package/src/services/module-loader.service.mts +4 -3
- package/src/tokens/endpoint-adapter.token.mts +8 -0
- package/src/tokens/execution-context.token.mts +2 -2
- package/src/tokens/http-adapter.token.mts +8 -0
- package/src/tokens/index.mts +4 -1
- package/src/tokens/multipart-adapter.token.mts +8 -0
- package/src/tokens/reply.token.mts +1 -5
- package/src/tokens/request.token.mts +1 -7
- package/src/tokens/stream-adapter.token.mts +8 -0
- package/docs/recipes/prisma.md +0 -60
- package/e2e/endpoints/get.spec.mts +0 -97
- package/e2e/endpoints/post.spec.mts +0 -113
- package/examples/simple-test/api/index.mts +0 -64
- package/examples/simple-test/config/config.service.mts +0 -14
- package/examples/simple-test/config/configuration.mts +0 -7
- package/examples/simple-test/index.mts +0 -16
- package/examples/simple-test/src/acl/acl-modern.guard.mts +0 -15
- package/examples/simple-test/src/acl/acl.guard.mts +0 -14
- package/examples/simple-test/src/acl/app.guard.mts +0 -27
- package/examples/simple-test/src/acl/one-more.guard.mts +0 -15
- package/examples/simple-test/src/acl/public.attribute.mts +0 -21
- package/examples/simple-test/src/app.module.mts +0 -9
- package/examples/simple-test/src/user/user.controller.mts +0 -72
- package/examples/simple-test/src/user/user.module.mts +0 -14
- package/examples/simple-test/src/user/user.service.mts +0 -14
- package/src/adapters/endpoint-adapter.service.mts +0 -72
- package/src/adapters/handler-adapter.interface.mts +0 -21
- package/src/adapters/index.mts +0 -4
- package/src/adapters/multipart-adapter.service.mts +0 -135
- package/src/adapters/stream-adapter.service.mts +0 -91
- package/src/logger/logger.factory.mts +0 -36
- package/src/logger/pino-wrapper.mts +0 -64
- package/src/services/controller-adapter.service.mts +0 -124
- package/src/services/execution-context.mts +0 -54
- package/src/tokens/application.token.mts +0 -9
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { AnyInjectableType, InjectionToken } from '@navios/di'
|
|
2
|
+
|
|
3
|
+
import { Injectable } from '@navios/di'
|
|
4
|
+
|
|
5
|
+
export interface NaviosEnvironmentOptions {
|
|
6
|
+
// Future options can be added here
|
|
7
|
+
httpTokens?: Map<InjectionToken<any, undefined>, AnyInjectableType>
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
@Injectable()
|
|
11
|
+
export class NaviosEnvironment {
|
|
12
|
+
private httpTokens = new Map<
|
|
13
|
+
InjectionToken<any, undefined>,
|
|
14
|
+
AnyInjectableType
|
|
15
|
+
>()
|
|
16
|
+
|
|
17
|
+
setupHttpEnvironment(
|
|
18
|
+
tokens: Map<InjectionToken<any, undefined>, AnyInjectableType>,
|
|
19
|
+
) {
|
|
20
|
+
this.httpTokens = tokens
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
getHttpToken(token: InjectionToken<any, undefined>) {
|
|
24
|
+
return this.httpTokens.get(token)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
hasHttpSetup() {
|
|
28
|
+
return this.httpTokens.size > 0
|
|
29
|
+
}
|
|
30
|
+
}
|
package/src/navios.factory.mts
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type {
|
|
2
|
+
AnyInjectableType,
|
|
3
|
+
ClassTypeWithInstance,
|
|
4
|
+
InjectionToken,
|
|
5
|
+
} from '@navios/di'
|
|
2
6
|
|
|
3
|
-
import {
|
|
7
|
+
import { Container, InjectableScope, InjectableType } from '@navios/di'
|
|
4
8
|
|
|
5
9
|
import type { NaviosModule } from './interfaces/index.mjs'
|
|
6
10
|
import type {
|
|
@@ -8,29 +12,66 @@ import type {
|
|
|
8
12
|
NaviosApplicationOptions,
|
|
9
13
|
} from './navios.application.mjs'
|
|
10
14
|
|
|
11
|
-
import { isNil,
|
|
15
|
+
import { ConsoleLogger, isNil, LoggerOutput } from './logger/index.mjs'
|
|
12
16
|
import { NaviosApplication } from './navios.application.mjs'
|
|
17
|
+
import { NaviosEnvironment } from './navios.environment.mjs'
|
|
13
18
|
|
|
14
19
|
export class NaviosFactory {
|
|
15
20
|
static async create(
|
|
16
21
|
appModule: ClassTypeWithInstance<NaviosModule>,
|
|
17
|
-
options: NaviosApplicationOptions = {
|
|
22
|
+
options: NaviosApplicationOptions = {
|
|
23
|
+
adapter: [],
|
|
24
|
+
},
|
|
18
25
|
) {
|
|
19
|
-
const
|
|
20
|
-
this.registerLoggerConfiguration(options)
|
|
21
|
-
|
|
26
|
+
const container = new Container()
|
|
27
|
+
await this.registerLoggerConfiguration(container, options)
|
|
28
|
+
const adapters = Array.isArray(options.adapter)
|
|
29
|
+
? options.adapter
|
|
30
|
+
: [options.adapter]
|
|
31
|
+
for (const adapter of adapters) {
|
|
32
|
+
await this.registerEnvironment(container, adapter)
|
|
33
|
+
}
|
|
34
|
+
const app = await container.get(NaviosApplication)
|
|
35
|
+
await app.setup(appModule, options)
|
|
22
36
|
return app
|
|
23
37
|
}
|
|
24
38
|
|
|
25
|
-
private static
|
|
26
|
-
|
|
39
|
+
private static async registerEnvironment(
|
|
40
|
+
container: Container,
|
|
41
|
+
environment: {
|
|
42
|
+
httpTokens?: Map<InjectionToken<any, undefined>, AnyInjectableType>
|
|
43
|
+
} = {},
|
|
27
44
|
) {
|
|
28
|
-
|
|
29
|
-
|
|
45
|
+
const naviosEnvironment = await container.get(NaviosEnvironment)
|
|
46
|
+
const { httpTokens } = environment
|
|
47
|
+
if (httpTokens) {
|
|
48
|
+
naviosEnvironment.setupHttpEnvironment(httpTokens)
|
|
30
49
|
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
private static async registerLoggerConfiguration(
|
|
53
|
+
container: Container,
|
|
54
|
+
options: NaviosApplicationContextOptions,
|
|
55
|
+
) {
|
|
31
56
|
const { logger } = options
|
|
57
|
+
if (Array.isArray(logger) || isNil(logger)) {
|
|
58
|
+
const loggerInstance = (await container.get(
|
|
59
|
+
LoggerOutput,
|
|
60
|
+
)) as ConsoleLogger
|
|
61
|
+
loggerInstance?.setup({
|
|
62
|
+
logLevels: logger,
|
|
63
|
+
})
|
|
64
|
+
}
|
|
32
65
|
if ((logger as boolean) !== true && !isNil(logger)) {
|
|
33
|
-
|
|
66
|
+
container
|
|
67
|
+
.getServiceLocator()
|
|
68
|
+
.getManager()
|
|
69
|
+
.storeCreatedHolder(
|
|
70
|
+
LoggerOutput.toString(),
|
|
71
|
+
logger,
|
|
72
|
+
InjectableType.Class,
|
|
73
|
+
InjectableScope.Singleton,
|
|
74
|
+
)
|
|
34
75
|
}
|
|
35
76
|
}
|
|
36
77
|
}
|
|
@@ -1,24 +1,32 @@
|
|
|
1
1
|
import type { ClassTypeWithInstance } from '@navios/di'
|
|
2
2
|
|
|
3
|
-
import { inject, Injectable, InjectionToken } from '@navios/di'
|
|
3
|
+
import { Container, inject, Injectable, InjectionToken } from '@navios/di'
|
|
4
4
|
|
|
5
|
-
import type {
|
|
6
|
-
|
|
5
|
+
import type {
|
|
6
|
+
AbstractExecutionContext,
|
|
7
|
+
CanActivate,
|
|
8
|
+
} from '../interfaces/index.mjs'
|
|
9
|
+
import type {
|
|
10
|
+
ControllerMetadata,
|
|
11
|
+
HandlerMetadata,
|
|
12
|
+
ModuleMetadata,
|
|
13
|
+
} from '../metadata/index.mjs'
|
|
7
14
|
|
|
8
15
|
import { HttpException } from '../exceptions/index.mjs'
|
|
9
16
|
|
|
10
17
|
@Injectable()
|
|
11
18
|
export class GuardRunnerService {
|
|
19
|
+
protected container = inject(Container)
|
|
12
20
|
async runGuards(
|
|
13
21
|
allGuards: Set<
|
|
14
22
|
| ClassTypeWithInstance<CanActivate>
|
|
15
23
|
| InjectionToken<CanActivate, undefined>
|
|
16
24
|
>,
|
|
17
|
-
executionContext:
|
|
25
|
+
executionContext: AbstractExecutionContext,
|
|
18
26
|
) {
|
|
19
27
|
let canActivate = true
|
|
20
28
|
for (const guard of Array.from(allGuards).reverse()) {
|
|
21
|
-
const guardInstance = await
|
|
29
|
+
const guardInstance = await this.container.get(
|
|
22
30
|
guard as InjectionToken<CanActivate, undefined>,
|
|
23
31
|
)
|
|
24
32
|
if (!guardInstance.canActivate) {
|
|
@@ -60,7 +68,9 @@ export class GuardRunnerService {
|
|
|
60
68
|
}
|
|
61
69
|
|
|
62
70
|
makeContext(
|
|
63
|
-
|
|
71
|
+
moduleMetadata: ModuleMetadata,
|
|
72
|
+
controllerMetadata: ControllerMetadata,
|
|
73
|
+
endpoint: HandlerMetadata,
|
|
64
74
|
): Set<
|
|
65
75
|
ClassTypeWithInstance<CanActivate> | InjectionToken<CanActivate, undefined>
|
|
66
76
|
> {
|
|
@@ -68,9 +78,9 @@ export class GuardRunnerService {
|
|
|
68
78
|
| ClassTypeWithInstance<CanActivate>
|
|
69
79
|
| InjectionToken<CanActivate, undefined>
|
|
70
80
|
>()
|
|
71
|
-
const endpointGuards =
|
|
72
|
-
const controllerGuards =
|
|
73
|
-
const moduleGuards =
|
|
81
|
+
const endpointGuards = endpoint.guards
|
|
82
|
+
const controllerGuards = controllerMetadata.guards
|
|
83
|
+
const moduleGuards = moduleMetadata.guards
|
|
74
84
|
if (endpointGuards.size > 0) {
|
|
75
85
|
for (const guard of endpointGuards) {
|
|
76
86
|
guards.add(guard)
|
package/src/services/index.mts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { ClassTypeWithInstance } from '@navios/di'
|
|
2
2
|
|
|
3
|
-
import { inject, Injectable
|
|
3
|
+
import { Container, inject, Injectable } from '@navios/di'
|
|
4
4
|
|
|
5
5
|
import type { NaviosModule } from '../interfaces/index.mjs'
|
|
6
6
|
import type { ModuleMetadata } from '../metadata/index.mjs'
|
|
@@ -10,9 +10,10 @@ import { extractModuleMetadata } from '../metadata/index.mjs'
|
|
|
10
10
|
|
|
11
11
|
@Injectable()
|
|
12
12
|
export class ModuleLoaderService {
|
|
13
|
-
private logger =
|
|
13
|
+
private logger = inject(Logger, {
|
|
14
14
|
context: ModuleLoaderService.name,
|
|
15
15
|
})
|
|
16
|
+
protected container = inject(Container)
|
|
16
17
|
private modulesMetadata: Map<string, ModuleMetadata> = new Map()
|
|
17
18
|
private loadedModules: Map<string, any> = new Map()
|
|
18
19
|
private initialized = false
|
|
@@ -43,7 +44,7 @@ export class ModuleLoaderService {
|
|
|
43
44
|
this.traverseModules(importedModule, metadata),
|
|
44
45
|
)
|
|
45
46
|
await Promise.all(loadingPromises)
|
|
46
|
-
const instance = await
|
|
47
|
+
const instance = await this.container.get(module)
|
|
47
48
|
if (instance.onModuleInit) {
|
|
48
49
|
await instance.onModuleInit()
|
|
49
50
|
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { InjectionToken } from '@navios/di'
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import type { AbstractExecutionContext } from '../interfaces/index.mjs'
|
|
4
4
|
|
|
5
5
|
export const ExecutionContextInjectionToken = 'ExecutionContextInjectionToken'
|
|
6
6
|
|
|
7
|
-
export const
|
|
7
|
+
export const ExecutionContext = InjectionToken.create<AbstractExecutionContext>(
|
|
8
8
|
ExecutionContextInjectionToken,
|
|
9
9
|
)
|
package/src/tokens/index.mts
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
export * from './
|
|
1
|
+
export * from './endpoint-adapter.token.mjs'
|
|
2
2
|
export * from './execution-context.token.mjs'
|
|
3
|
+
export * from './http-adapter.token.mjs'
|
|
4
|
+
export * from './multipart-adapter.token.mjs'
|
|
3
5
|
export * from './reply.token.mjs'
|
|
4
6
|
export * from './request.token.mjs'
|
|
7
|
+
export * from './stream-adapter.token.mjs'
|
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
import type { FastifyReply } from 'fastify'
|
|
2
|
-
|
|
3
1
|
import { InjectionToken } from '@navios/di'
|
|
4
2
|
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
export const Reply = InjectionToken.create<FastifyReply>(ReplyInjectionToken)
|
|
3
|
+
export const Reply = InjectionToken.create<any>('ReplyToken')
|
|
@@ -1,9 +1,3 @@
|
|
|
1
|
-
import type { FastifyRequest } from 'fastify'
|
|
2
|
-
|
|
3
1
|
import { InjectionToken } from '@navios/di'
|
|
4
2
|
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
export const Request = InjectionToken.create<FastifyRequest>(
|
|
8
|
-
RequestInjectionToken,
|
|
9
|
-
)
|
|
3
|
+
export const Request = InjectionToken.create<any>('RequestToken')
|
package/docs/recipes/prisma.md
DELETED
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
# Prisma ORM
|
|
2
|
-
|
|
3
|
-
Prisma ORM is a modern database toolkit that simplifies database access and management. It provides a type-safe query builder, migrations, and an intuitive API for working with databases.
|
|
4
|
-
|
|
5
|
-
## Installation
|
|
6
|
-
|
|
7
|
-
Follow the steps from the [Prisma documentation](https://www.prisma.io/docs/getting-started/quickstart-typescript) to install Prisma in your project.
|
|
8
|
-
|
|
9
|
-
## Integration
|
|
10
|
-
|
|
11
|
-
The simplest way to integrate Prisma with your project is to create a factory:
|
|
12
|
-
|
|
13
|
-
```typescript
|
|
14
|
-
import { Injectable, InjectableType, InjectionToken } from '@navios/core'
|
|
15
|
-
|
|
16
|
-
import { PrismaClient } from '@prisma/client'
|
|
17
|
-
|
|
18
|
-
export const PrismaService = InjectionToken.create(PrismaClient)
|
|
19
|
-
|
|
20
|
-
@Injectable({
|
|
21
|
-
token: PrismaService,
|
|
22
|
-
type: InjectableType.Factory,
|
|
23
|
-
})
|
|
24
|
-
export class PrismaServiceFactory {
|
|
25
|
-
async create() {
|
|
26
|
-
const client = new PrismaClient()
|
|
27
|
-
await client.$connect()
|
|
28
|
-
return client
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
```
|
|
32
|
-
|
|
33
|
-
## Usage
|
|
34
|
-
|
|
35
|
-
You can use the Prisma Service in your application by injecting it into your classes. Here's an example of how to use it in a service:
|
|
36
|
-
|
|
37
|
-
```typescript
|
|
38
|
-
import { Injectable, syncInject } from '@navios/core'
|
|
39
|
-
|
|
40
|
-
import { PrismaService } from './prisma.service.mjs'
|
|
41
|
-
|
|
42
|
-
@Injectable()
|
|
43
|
-
export class UserService {
|
|
44
|
-
private readonly prisma = syncInject(PrismaService)
|
|
45
|
-
|
|
46
|
-
async getUser(id: string) {
|
|
47
|
-
return this.prisma.user.findUnique({
|
|
48
|
-
where: { id },
|
|
49
|
-
})
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
async createUser(data: { name: string; email: string }) {
|
|
53
|
-
return this.prisma.user.create({
|
|
54
|
-
data,
|
|
55
|
-
})
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
```
|
|
59
|
-
|
|
60
|
-
That's it! You can now use Prisma in your application to interact with your database. You can find more information about Prisma and its features in the [Prisma documentation](https://www.prisma.io/docs/).
|
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
import { builder } from '@navios/builder'
|
|
2
|
-
|
|
3
|
-
import supertest from 'supertest'
|
|
4
|
-
import { afterAll, beforeAll, describe, expect, it } from 'vitest'
|
|
5
|
-
import { z } from 'zod/v4'
|
|
6
|
-
|
|
7
|
-
import type { EndpointParams } from '../../src/index.mjs'
|
|
8
|
-
|
|
9
|
-
import {
|
|
10
|
-
Controller,
|
|
11
|
-
Endpoint,
|
|
12
|
-
Module,
|
|
13
|
-
NaviosApplication,
|
|
14
|
-
NaviosFactory,
|
|
15
|
-
} from '../../src/index.mjs'
|
|
16
|
-
|
|
17
|
-
describe('GET variants', () => {
|
|
18
|
-
let server: NaviosApplication
|
|
19
|
-
|
|
20
|
-
const simple = builder().declareEndpoint({
|
|
21
|
-
url: '/simple',
|
|
22
|
-
method: 'GET',
|
|
23
|
-
responseSchema: z.object({
|
|
24
|
-
message: z.string(),
|
|
25
|
-
}),
|
|
26
|
-
})
|
|
27
|
-
const withUrlParams = builder().declareEndpoint({
|
|
28
|
-
url: '/with-url-params/$id',
|
|
29
|
-
method: 'GET',
|
|
30
|
-
responseSchema: z.object({
|
|
31
|
-
id: z.string(),
|
|
32
|
-
}),
|
|
33
|
-
})
|
|
34
|
-
const withQueryParams = builder().declareEndpoint({
|
|
35
|
-
url: '/with-query-params',
|
|
36
|
-
method: 'GET',
|
|
37
|
-
responseSchema: z.object({
|
|
38
|
-
name: z.string(),
|
|
39
|
-
}),
|
|
40
|
-
querySchema: z.object({
|
|
41
|
-
name: z.string(),
|
|
42
|
-
}),
|
|
43
|
-
})
|
|
44
|
-
|
|
45
|
-
@Controller()
|
|
46
|
-
class SomethingController {
|
|
47
|
-
@Endpoint(simple)
|
|
48
|
-
async getSomething(req: EndpointParams<typeof simple>) {
|
|
49
|
-
return { message: 'Hello, world!' }
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
@Endpoint(withUrlParams)
|
|
53
|
-
async getWithUrlParams(req: EndpointParams<typeof withUrlParams>) {
|
|
54
|
-
return { id: req.urlParams.id as string }
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
@Endpoint(withQueryParams)
|
|
58
|
-
async getWithQueryParams(req: EndpointParams<typeof withQueryParams>) {
|
|
59
|
-
return { name: req.params.name as string }
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
@Module({
|
|
64
|
-
controllers: [SomethingController],
|
|
65
|
-
})
|
|
66
|
-
class SomethingModule {}
|
|
67
|
-
|
|
68
|
-
beforeAll(async () => {
|
|
69
|
-
server = await NaviosFactory.create(SomethingModule)
|
|
70
|
-
await server.init()
|
|
71
|
-
})
|
|
72
|
-
|
|
73
|
-
afterAll(async () => {
|
|
74
|
-
await server.close()
|
|
75
|
-
})
|
|
76
|
-
|
|
77
|
-
it('should return 200', async () => {
|
|
78
|
-
const response = await supertest(server.getServer().server).get('/simple')
|
|
79
|
-
expect(response.status).toBe(200)
|
|
80
|
-
expect(response.body.message).toBe('Hello, world!')
|
|
81
|
-
})
|
|
82
|
-
|
|
83
|
-
it('should return 200 with url params', async () => {
|
|
84
|
-
const response = await supertest(server.getServer().server).get(
|
|
85
|
-
'/with-url-params/123',
|
|
86
|
-
)
|
|
87
|
-
expect(response.status).toBe(200)
|
|
88
|
-
expect(response.body.id).toBe('123')
|
|
89
|
-
})
|
|
90
|
-
|
|
91
|
-
it('should return 200 with query params', async () => {
|
|
92
|
-
const response = await supertest(server.getServer().server).get(
|
|
93
|
-
'/with-query-params?name=John',
|
|
94
|
-
)
|
|
95
|
-
expect(response.status).toBe(200)
|
|
96
|
-
})
|
|
97
|
-
})
|
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
import { builder } from '@navios/builder'
|
|
2
|
-
|
|
3
|
-
import supertest from 'supertest'
|
|
4
|
-
import { afterAll, beforeAll, describe, expect, it } from 'vitest'
|
|
5
|
-
import { z } from 'zod/v4'
|
|
6
|
-
|
|
7
|
-
import type { EndpointParams } from '../../src/index.mjs'
|
|
8
|
-
|
|
9
|
-
import {
|
|
10
|
-
Controller,
|
|
11
|
-
Endpoint,
|
|
12
|
-
Module,
|
|
13
|
-
NaviosApplication,
|
|
14
|
-
NaviosFactory,
|
|
15
|
-
} from '../../src/index.mjs'
|
|
16
|
-
|
|
17
|
-
describe('POST variants', () => {
|
|
18
|
-
let server: NaviosApplication
|
|
19
|
-
|
|
20
|
-
const simple = builder().declareEndpoint({
|
|
21
|
-
url: '/simple',
|
|
22
|
-
method: 'POST',
|
|
23
|
-
responseSchema: z.object({
|
|
24
|
-
message: z.string(),
|
|
25
|
-
}),
|
|
26
|
-
})
|
|
27
|
-
const withUrlParams = builder().declareEndpoint({
|
|
28
|
-
url: '/with-url-params/$id',
|
|
29
|
-
method: 'POST',
|
|
30
|
-
responseSchema: z.object({
|
|
31
|
-
id: z.string(),
|
|
32
|
-
}),
|
|
33
|
-
})
|
|
34
|
-
const withRequestAndQueryParams = builder().declareEndpoint({
|
|
35
|
-
url: '/with-request-and-query-params',
|
|
36
|
-
method: 'POST',
|
|
37
|
-
requestSchema: z.object({
|
|
38
|
-
foo: z.string(),
|
|
39
|
-
}),
|
|
40
|
-
responseSchema: z.object({
|
|
41
|
-
foo: z.string(),
|
|
42
|
-
bar: z.string(),
|
|
43
|
-
}),
|
|
44
|
-
querySchema: z.object({
|
|
45
|
-
bar: z.string(),
|
|
46
|
-
}),
|
|
47
|
-
})
|
|
48
|
-
|
|
49
|
-
@Controller()
|
|
50
|
-
class SomethingController {
|
|
51
|
-
@Endpoint(simple)
|
|
52
|
-
async getSomething(req: EndpointParams<typeof simple>) {
|
|
53
|
-
return { message: 'Hello, world!' }
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
@Endpoint(withUrlParams)
|
|
57
|
-
async getWithUrlParams(req: EndpointParams<typeof withUrlParams>) {
|
|
58
|
-
return { id: req.urlParams.id as string }
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
@Endpoint(withRequestAndQueryParams)
|
|
62
|
-
async getWithQueryParams(
|
|
63
|
-
req: EndpointParams<typeof withRequestAndQueryParams>,
|
|
64
|
-
) {
|
|
65
|
-
return {
|
|
66
|
-
foo: req.data.foo as string,
|
|
67
|
-
bar: req.params.bar as string,
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
@Module({
|
|
73
|
-
controllers: [SomethingController],
|
|
74
|
-
})
|
|
75
|
-
class SomethingModule {}
|
|
76
|
-
|
|
77
|
-
beforeAll(async () => {
|
|
78
|
-
server = await NaviosFactory.create(SomethingModule)
|
|
79
|
-
await server.init()
|
|
80
|
-
})
|
|
81
|
-
|
|
82
|
-
afterAll(async () => {
|
|
83
|
-
await server.close()
|
|
84
|
-
})
|
|
85
|
-
|
|
86
|
-
it('should return 200', async () => {
|
|
87
|
-
const response = await supertest(server.getServer().server).post('/simple')
|
|
88
|
-
expect(response.status).toBe(200)
|
|
89
|
-
expect(response.body.message).toBe('Hello, world!')
|
|
90
|
-
})
|
|
91
|
-
|
|
92
|
-
it('should return 200 with url params', async () => {
|
|
93
|
-
const response = await supertest(server.getServer().server).post(
|
|
94
|
-
'/with-url-params/123',
|
|
95
|
-
)
|
|
96
|
-
expect(response.status).toBe(200)
|
|
97
|
-
expect(response.body.id).toBe('123')
|
|
98
|
-
})
|
|
99
|
-
|
|
100
|
-
it('should return 200 with query params', async () => {
|
|
101
|
-
const response = await supertest(server.getServer().server)
|
|
102
|
-
.post('/with-request-and-query-params')
|
|
103
|
-
.send({
|
|
104
|
-
foo: 'John',
|
|
105
|
-
})
|
|
106
|
-
.query({
|
|
107
|
-
bar: 'Doe',
|
|
108
|
-
})
|
|
109
|
-
expect(response.status).toBe(200)
|
|
110
|
-
expect(response.body.foo).toBe('John')
|
|
111
|
-
expect(response.body.bar).toBe('Doe')
|
|
112
|
-
})
|
|
113
|
-
})
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
import { builder } from '@navios/builder'
|
|
2
|
-
|
|
3
|
-
import { z } from 'zod/v4'
|
|
4
|
-
|
|
5
|
-
export const authApi = builder({
|
|
6
|
-
useDiscriminatorResponse: true,
|
|
7
|
-
})
|
|
8
|
-
|
|
9
|
-
export const userEndpoint = authApi.declareEndpoint({
|
|
10
|
-
method: 'GET',
|
|
11
|
-
url: '/user',
|
|
12
|
-
responseSchema: z.object({
|
|
13
|
-
id: z.string(),
|
|
14
|
-
name: z.string(),
|
|
15
|
-
email: z.string(),
|
|
16
|
-
}),
|
|
17
|
-
})
|
|
18
|
-
|
|
19
|
-
export const patchUserEndpoint = authApi.declareEndpoint({
|
|
20
|
-
method: 'PATCH',
|
|
21
|
-
url: '/user',
|
|
22
|
-
requestSchema: z.object({
|
|
23
|
-
name: z.string(),
|
|
24
|
-
email: z.string(),
|
|
25
|
-
}),
|
|
26
|
-
responseSchema: z.object({
|
|
27
|
-
id: z.string(),
|
|
28
|
-
name: z.string(),
|
|
29
|
-
email: z.string(),
|
|
30
|
-
}),
|
|
31
|
-
})
|
|
32
|
-
|
|
33
|
-
export const discriminatorEndpoint = authApi.declareEndpoint({
|
|
34
|
-
method: 'GET',
|
|
35
|
-
url: '/discriminator',
|
|
36
|
-
responseSchema: z.discriminatedUnion('success', [
|
|
37
|
-
z.object({
|
|
38
|
-
success: z.literal(true),
|
|
39
|
-
data: z.object({
|
|
40
|
-
id: z.string(),
|
|
41
|
-
name: z.string(),
|
|
42
|
-
|
|
43
|
-
email: z.string(),
|
|
44
|
-
}),
|
|
45
|
-
}),
|
|
46
|
-
z.object({
|
|
47
|
-
success: z.literal(false),
|
|
48
|
-
error: z.object({
|
|
49
|
-
code: z.string(),
|
|
50
|
-
message: z.string(),
|
|
51
|
-
}),
|
|
52
|
-
}),
|
|
53
|
-
]),
|
|
54
|
-
})
|
|
55
|
-
|
|
56
|
-
export const multipartEndpoint = authApi.declareMultipart({
|
|
57
|
-
method: 'POST',
|
|
58
|
-
url: '/multipart',
|
|
59
|
-
requestSchema: z.object({
|
|
60
|
-
files: z.array(z.instanceof(File)),
|
|
61
|
-
something: z.string(),
|
|
62
|
-
}),
|
|
63
|
-
responseSchema: z.object({}),
|
|
64
|
-
})
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import type { Path, PathValue } from '../../../src/index.mjs'
|
|
2
|
-
|
|
3
|
-
import { provideConfig } from '../../../src/index.mjs'
|
|
4
|
-
import { configureConfig } from './configuration.mjs'
|
|
5
|
-
|
|
6
|
-
export type ConfigType = ReturnType<typeof configureConfig>
|
|
7
|
-
export type ConfigTypePaths = Path<ConfigType>
|
|
8
|
-
export type ConfigMap = {
|
|
9
|
-
[K in ConfigTypePaths]: PathValue<ConfigType, K>
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export const ConfigService = provideConfig<ConfigMap>({
|
|
13
|
-
load: configureConfig,
|
|
14
|
-
})
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { inject, NaviosFactory } from '../../src/index.mjs'
|
|
2
|
-
import { ConfigService } from './config/config.service.mjs'
|
|
3
|
-
import { AppModule } from './src/app.module.mjs'
|
|
4
|
-
|
|
5
|
-
export async function boot() {
|
|
6
|
-
const app = await NaviosFactory.create(AppModule)
|
|
7
|
-
app.enableCors({
|
|
8
|
-
methods: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS'],
|
|
9
|
-
})
|
|
10
|
-
app.enableMultipart({})
|
|
11
|
-
await app.init()
|
|
12
|
-
const configService = await inject(ConfigService)
|
|
13
|
-
const port = configService.getOrThrow('port')
|
|
14
|
-
await app.listen({ port: port, host: '0.0.0.0' })
|
|
15
|
-
}
|
|
16
|
-
await boot()
|