@navios/core 0.3.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (96) hide show
  1. package/README.md +96 -3
  2. package/docs/README.md +310 -3
  3. package/docs/adapters.md +308 -0
  4. package/docs/application-setup.md +524 -0
  5. package/docs/attributes.md +689 -0
  6. package/docs/controllers.md +373 -0
  7. package/docs/endpoints.md +444 -0
  8. package/docs/exceptions.md +316 -0
  9. package/docs/guards.md +550 -0
  10. package/docs/modules.md +251 -0
  11. package/docs/quick-start.md +295 -0
  12. package/docs/services.md +428 -0
  13. package/docs/testing.md +704 -0
  14. package/lib/_tsup-dts-rollup.d.mts +313 -280
  15. package/lib/_tsup-dts-rollup.d.ts +313 -280
  16. package/lib/index.d.mts +47 -26
  17. package/lib/index.d.ts +47 -26
  18. package/lib/index.js +633 -1068
  19. package/lib/index.js.map +1 -1
  20. package/lib/index.mjs +632 -1061
  21. package/lib/index.mjs.map +1 -1
  22. package/package.json +11 -12
  23. package/project.json +17 -4
  24. package/src/__tests__/config.service.spec.mts +11 -9
  25. package/src/__tests__/controller.spec.mts +1 -2
  26. package/src/attribute.factory.mts +1 -1
  27. package/src/config/config.provider.mts +2 -2
  28. package/src/config/config.service.mts +4 -4
  29. package/src/decorators/controller.decorator.mts +1 -1
  30. package/src/decorators/endpoint.decorator.mts +9 -10
  31. package/src/decorators/header.decorator.mts +1 -1
  32. package/src/decorators/multipart.decorator.mts +5 -5
  33. package/src/decorators/stream.decorator.mts +5 -6
  34. package/src/factories/endpoint-adapter.factory.mts +21 -0
  35. package/src/factories/http-adapter.factory.mts +20 -0
  36. package/src/factories/index.mts +6 -0
  37. package/src/factories/multipart-adapter.factory.mts +21 -0
  38. package/src/factories/reply.factory.mts +21 -0
  39. package/src/factories/request.factory.mts +21 -0
  40. package/src/factories/stream-adapter.factory.mts +20 -0
  41. package/src/index.mts +1 -1
  42. package/src/interfaces/abstract-execution-context.inteface.mts +13 -0
  43. package/src/interfaces/abstract-http-adapter.interface.mts +20 -0
  44. package/src/interfaces/abstract-http-cors-options.interface.mts +59 -0
  45. package/src/interfaces/abstract-http-handler-adapter.interface.mts +13 -0
  46. package/src/interfaces/abstract-http-listen-options.interface.mts +4 -0
  47. package/src/interfaces/can-activate.mts +4 -2
  48. package/src/interfaces/http-header.mts +18 -0
  49. package/src/interfaces/index.mts +6 -0
  50. package/src/logger/console-logger.service.mts +28 -44
  51. package/src/logger/index.mts +1 -2
  52. package/src/logger/logger.service.mts +9 -128
  53. package/src/logger/logger.tokens.mts +21 -0
  54. package/src/metadata/handler.metadata.mts +7 -5
  55. package/src/navios.application.mts +65 -172
  56. package/src/navios.environment.mts +30 -0
  57. package/src/navios.factory.mts +53 -12
  58. package/src/services/guard-runner.service.mts +19 -9
  59. package/src/services/index.mts +0 -2
  60. package/src/services/module-loader.service.mts +4 -3
  61. package/src/tokens/endpoint-adapter.token.mts +8 -0
  62. package/src/tokens/execution-context.token.mts +2 -2
  63. package/src/tokens/http-adapter.token.mts +8 -0
  64. package/src/tokens/index.mts +4 -1
  65. package/src/tokens/multipart-adapter.token.mts +8 -0
  66. package/src/tokens/reply.token.mts +1 -5
  67. package/src/tokens/request.token.mts +1 -7
  68. package/src/tokens/stream-adapter.token.mts +8 -0
  69. package/tsconfig.json +6 -1
  70. package/tsconfig.lib.json +8 -0
  71. package/tsconfig.spec.json +12 -0
  72. package/tsup.config.mts +1 -0
  73. package/docs/recipes/prisma.md +0 -60
  74. package/examples/simple-test/api/index.mts +0 -64
  75. package/examples/simple-test/config/config.service.mts +0 -14
  76. package/examples/simple-test/config/configuration.mts +0 -7
  77. package/examples/simple-test/index.mts +0 -16
  78. package/examples/simple-test/src/acl/acl-modern.guard.mts +0 -15
  79. package/examples/simple-test/src/acl/acl.guard.mts +0 -14
  80. package/examples/simple-test/src/acl/app.guard.mts +0 -27
  81. package/examples/simple-test/src/acl/one-more.guard.mts +0 -15
  82. package/examples/simple-test/src/acl/public.attribute.mts +0 -21
  83. package/examples/simple-test/src/app.module.mts +0 -9
  84. package/examples/simple-test/src/user/user.controller.mts +0 -72
  85. package/examples/simple-test/src/user/user.module.mts +0 -14
  86. package/examples/simple-test/src/user/user.service.mts +0 -14
  87. package/src/adapters/endpoint-adapter.service.mts +0 -72
  88. package/src/adapters/handler-adapter.interface.mts +0 -21
  89. package/src/adapters/index.mts +0 -4
  90. package/src/adapters/multipart-adapter.service.mts +0 -131
  91. package/src/adapters/stream-adapter.service.mts +0 -91
  92. package/src/logger/logger.factory.mts +0 -36
  93. package/src/logger/pino-wrapper.mts +0 -64
  94. package/src/services/controller-adapter.service.mts +0 -124
  95. package/src/services/execution-context.mts +0 -54
  96. 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
+ }
@@ -1,6 +1,10 @@
1
- import type { ClassTypeWithInstance } from '@navios/di'
1
+ import type {
2
+ AnyInjectableType,
3
+ ClassTypeWithInstance,
4
+ InjectionToken,
5
+ } from '@navios/di'
2
6
 
3
- import { inject } from '@navios/di'
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, LoggerInstance } from './logger/index.mjs'
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 app = await inject(NaviosApplication)
20
- this.registerLoggerConfiguration(options)
21
- app.setup(appModule, options)
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 registerLoggerConfiguration(
26
- options: NaviosApplicationContextOptions,
39
+ private static async registerEnvironment(
40
+ container: Container,
41
+ environment: {
42
+ httpTokens?: Map<InjectionToken<any, undefined>, AnyInjectableType>
43
+ } = {},
27
44
  ) {
28
- if (!options) {
29
- return
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
- LoggerInstance.overrideLogger(logger)
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 { CanActivate } from '../interfaces/index.mjs'
6
- import type { ExecutionContext } from './execution-context.mjs'
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: 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 inject(
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
- executionContext: ExecutionContext,
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 = executionContext.getHandler().guards
72
- const controllerGuards = executionContext.getController().guards
73
- const moduleGuards = executionContext.getModule().guards
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)
@@ -1,4 +1,2 @@
1
- export * from './controller-adapter.service.mjs'
2
- export * from './execution-context.mjs'
3
1
  export * from './guard-runner.service.mjs'
4
2
  export * from './module-loader.service.mjs'
@@ -1,6 +1,6 @@
1
1
  import type { ClassTypeWithInstance } from '@navios/di'
2
2
 
3
- import { inject, Injectable, syncInject } from '@navios/di'
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 = syncInject(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 inject(module)
47
+ const instance = await this.container.get(module)
47
48
  if (instance.onModuleInit) {
48
49
  await instance.onModuleInit()
49
50
  }
@@ -0,0 +1,8 @@
1
+ import { InjectionToken } from '@navios/di'
2
+
3
+ import type { AbstractHttpHandlerAdapterInterface } from '../interfaces/index.mjs'
4
+
5
+ export const EndpointAdapterToken =
6
+ InjectionToken.create<AbstractHttpHandlerAdapterInterface>(
7
+ 'EndpointAdapterToken',
8
+ )
@@ -1,9 +1,9 @@
1
1
  import { InjectionToken } from '@navios/di'
2
2
 
3
- import { ExecutionContext } from '../services/index.mjs'
3
+ import type { AbstractExecutionContext } from '../interfaces/index.mjs'
4
4
 
5
5
  export const ExecutionContextInjectionToken = 'ExecutionContextInjectionToken'
6
6
 
7
- export const ExecutionContextToken = InjectionToken.create<ExecutionContext>(
7
+ export const ExecutionContext = InjectionToken.create<AbstractExecutionContext>(
8
8
  ExecutionContextInjectionToken,
9
9
  )
@@ -0,0 +1,8 @@
1
+ import { InjectionToken } from '@navios/di'
2
+
3
+ import type { AbstractHttpAdapterInterface } from '../interfaces/index.mjs'
4
+
5
+ export const HttpAdapterToken =
6
+ InjectionToken.create<AbstractHttpAdapterInterface<any, any, any, any>>(
7
+ 'HttpAdapterToken',
8
+ )
@@ -1,4 +1,7 @@
1
- export * from './application.token.mjs'
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'
@@ -0,0 +1,8 @@
1
+ import { InjectionToken } from '@navios/di'
2
+
3
+ import type { AbstractHttpHandlerAdapterInterface } from '../interfaces/index.mjs'
4
+
5
+ export const MultipartAdapterToken =
6
+ InjectionToken.create<AbstractHttpHandlerAdapterInterface>(
7
+ 'MultipartAdapterToken',
8
+ )
@@ -1,7 +1,3 @@
1
- import type { FastifyReply } from 'fastify'
2
-
3
1
  import { InjectionToken } from '@navios/di'
4
2
 
5
- const ReplyInjectionToken = 'ReplyInjectionToken'
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 RequestInjectionToken = 'RequestInjectionToken'
6
-
7
- export const Request = InjectionToken.create<FastifyRequest>(
8
- RequestInjectionToken,
9
- )
3
+ export const Request = InjectionToken.create<any>('RequestToken')
@@ -0,0 +1,8 @@
1
+ import { InjectionToken } from '@navios/di'
2
+
3
+ import type { AbstractHttpHandlerAdapterInterface } from '../interfaces/index.mjs'
4
+
5
+ export const StreamAdapterToken =
6
+ InjectionToken.create<AbstractHttpHandlerAdapterInterface>(
7
+ 'StreamAdapterToken',
8
+ )
package/tsconfig.json CHANGED
@@ -4,8 +4,13 @@
4
4
  "module": "Node18",
5
5
  "outDir": "dist"
6
6
  },
7
- "include": ["src/**/*"],
8
7
  "references": [
8
+ {
9
+ "path": "./tsconfig.lib.json"
10
+ },
11
+ {
12
+ "path": "./tsconfig.spec.json"
13
+ },
9
14
  {
10
15
  "path": "../builder"
11
16
  },
@@ -0,0 +1,8 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "compilerOptions": {
4
+ "outDir": "./dist"
5
+ },
6
+ "include": ["src/**/*"],
7
+ "exclude": ["src/**/*.spec.mts"]
8
+ }
@@ -0,0 +1,12 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "compilerOptions": {
4
+ "outDir": "./dist"
5
+ },
6
+ "include": ["src/**/*.spec.mts", "e2e/**/*.mts", "vitest.config.mts"],
7
+ "references": [
8
+ {
9
+ "path": "./tsconfig.lib.json"
10
+ }
11
+ ]
12
+ }
package/tsup.config.mts CHANGED
@@ -5,6 +5,7 @@ export default defineConfig({
5
5
  outDir: 'lib',
6
6
  format: ['esm', 'cjs'],
7
7
  clean: true,
8
+ tsconfig: 'tsconfig.lib.json',
8
9
  treeshake: 'smallest',
9
10
  sourcemap: true,
10
11
  platform: 'node',
@@ -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,64 +0,0 @@
1
- import { builder } from '@navios/builder'
2
-
3
- import { z } from 'zod'
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,7 +0,0 @@
1
- import { envInt } from '../../../src/index.mjs'
2
-
3
- export function configureConfig() {
4
- return {
5
- port: envInt('PORT', 4250),
6
- }
7
- }
@@ -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()
@@ -1,15 +0,0 @@
1
- import type { CanActivate, ExecutionContext } from '../../../../src/index.mjs'
2
-
3
- import { Injectable, Logger, syncInject } from '../../../../src/index.mjs'
4
-
5
- @Injectable()
6
- export class AclModernGuard implements CanActivate {
7
- logger = syncInject(Logger, {
8
- context: AclModernGuard.name,
9
- })
10
-
11
- canActivate(executionContext: ExecutionContext): Promise<boolean> | boolean {
12
- this.logger.log('ACL Modern Guard activated')
13
- return true
14
- }
15
- }
@@ -1,14 +0,0 @@
1
- import type { CanActivate, ExecutionContext } from '../../../../src/index.mjs'
2
-
3
- import { Injectable, Logger, syncInject } from '../../../../src/index.mjs'
4
-
5
- @Injectable()
6
- export class AclGuard implements CanActivate {
7
- logger = syncInject(Logger, {
8
- context: AclGuard.name,
9
- })
10
- canActivate(executionContext: ExecutionContext): Promise<boolean> | boolean {
11
- this.logger.log('ACL Guard activated')
12
- return true
13
- }
14
- }
@@ -1,27 +0,0 @@
1
- import type { CanActivate, ExecutionContext } from '../../../../src/index.mjs'
2
-
3
- import {
4
- AttributeFactory,
5
- Injectable,
6
- Logger,
7
- syncInject,
8
- } from '../../../../src/index.mjs'
9
- import { Public } from './public.attribute.mjs'
10
-
11
- @Injectable()
12
- export class AppGuard implements CanActivate {
13
- logger = syncInject(Logger, {
14
- context: AppGuard.name,
15
- })
16
-
17
- canActivate(executionContext: ExecutionContext): Promise<boolean> | boolean {
18
- const isPublic = AttributeFactory.getLast(Public, [
19
- executionContext.getModule(),
20
- executionContext.getController(),
21
- executionContext.getHandler(),
22
- ])
23
- this.logger.log('App Guard activated')
24
- this.logger.log('isPublic', isPublic)
25
- return true
26
- }
27
- }
@@ -1,15 +0,0 @@
1
- import type { CanActivate, ExecutionContext } from '../../../../src/index.mjs'
2
-
3
- import { Injectable, Logger, syncInject } from '../../../../src/index.mjs'
4
-
5
- @Injectable()
6
- export class OneMoreGuard implements CanActivate {
7
- logger = syncInject(Logger, {
8
- context: OneMoreGuard.name,
9
- })
10
-
11
- canActivate(executionContext: ExecutionContext): Promise<boolean> | boolean {
12
- this.logger.log('One More Guard activated')
13
- return true
14
- }
15
- }
@@ -1,21 +0,0 @@
1
- import { z } from 'zod'
2
-
3
- import { AttributeFactory } from '../../../../src/index.mjs'
4
-
5
- export const PublicSymbol = Symbol.for('Public')
6
-
7
- export const Public = AttributeFactory.createAttribute(PublicSymbol)
8
- export const RolesSymbol = Symbol.for('Roles')
9
-
10
- export const RolesSchema = z.object({
11
- roles: z.array(
12
- z.union([
13
- z.literal('VIEWER'),
14
- z.literal('USER'),
15
- z.literal('ADMIN'),
16
- z.literal('OWNER'),
17
- ]),
18
- ),
19
- })
20
-
21
- export const Roles = AttributeFactory.createAttribute(RolesSymbol, RolesSchema)
@@ -1,9 +0,0 @@
1
- import { Module } from '../../../src/index.mjs'
2
- import { AppGuard } from './acl/app.guard.mjs'
3
- import { UserModule } from './user/user.module.mjs'
4
-
5
- @Module({
6
- imports: [UserModule],
7
- guards: [AppGuard],
8
- })
9
- export class AppModule {}