@navios/core 0.2.1 → 0.3.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 (41) hide show
  1. package/README.md +7 -3
  2. package/docs/README.md +6 -0
  3. package/docs/recipes/prisma.md +60 -0
  4. package/examples/simple-test/api/index.mts +64 -0
  5. package/examples/simple-test/config/config.service.mts +14 -0
  6. package/examples/simple-test/config/configuration.mts +7 -0
  7. package/examples/simple-test/index.mts +16 -0
  8. package/examples/simple-test/src/acl/acl-modern.guard.mts +15 -0
  9. package/examples/simple-test/src/acl/acl.guard.mts +14 -0
  10. package/examples/simple-test/src/acl/app.guard.mts +27 -0
  11. package/examples/simple-test/src/acl/one-more.guard.mts +15 -0
  12. package/examples/simple-test/src/acl/public.attribute.mts +21 -0
  13. package/examples/simple-test/src/app.module.mts +9 -0
  14. package/examples/simple-test/src/user/user.controller.mts +72 -0
  15. package/examples/simple-test/src/user/user.module.mts +14 -0
  16. package/examples/simple-test/src/user/user.service.mts +14 -0
  17. package/{dist → lib}/_tsup-dts-rollup.d.mts +76 -55
  18. package/{dist → lib}/_tsup-dts-rollup.d.ts +76 -55
  19. package/{dist → lib}/index.d.mts +13 -4
  20. package/{dist → lib}/index.d.ts +13 -4
  21. package/{dist → lib}/index.js +270 -490
  22. package/lib/index.js.map +1 -0
  23. package/{dist → lib}/index.mjs +140 -353
  24. package/lib/index.mjs.map +1 -0
  25. package/package.json +12 -13
  26. package/project.json +53 -0
  27. package/src/__tests__/config.service.spec.mts +41 -0
  28. package/src/__tests__/controller.spec.mts +1 -1
  29. package/src/adapters/endpoint-adapter.service.mts +1 -1
  30. package/src/adapters/multipart-adapter.service.mts +1 -1
  31. package/src/adapters/stream-adapter.service.mts +1 -1
  32. package/src/config/config-service.interface.mts +1 -1
  33. package/src/config/config.provider.mts +22 -50
  34. package/src/config/config.service.mts +26 -9
  35. package/src/decorators/endpoint.decorator.mts +1 -6
  36. package/src/decorators/multipart.decorator.mts +1 -1
  37. package/src/decorators/stream.decorator.mts +1 -1
  38. package/src/metadata/handler.metadata.mts +1 -1
  39. package/tsconfig.json +16 -0
  40. package/tsup.config.mts +12 -0
  41. package/vitest.config.mts +9 -0
@@ -1,61 +1,33 @@
1
- import {
2
- BoundInjectionToken,
3
- Injectable,
4
- InjectableType,
5
- InjectionToken,
6
- syncInject,
7
- } from '@navios/di'
1
+ import { env } from 'node:process'
8
2
 
9
- import { z } from 'zod'
3
+ import { FactoryInjectionToken, InjectionToken } from '@navios/di'
10
4
 
11
- import type { ConfigService } from './config-service.interface.mjs'
5
+ import { z } from 'zod'
12
6
 
13
- import { Logger } from '../logger/index.mjs'
14
- import { ConfigServiceInstance } from './config.service.mjs'
7
+ import type { ConfigServiceOptions } from './config.service.mjs'
15
8
 
16
- export const ConfigProviderOptions = z.object({
17
- load: z.function(),
18
- })
19
-
20
- export const ConfigProvider = InjectionToken.create<
9
+ import {
21
10
  ConfigService,
22
- typeof ConfigProviderOptions
23
- >(ConfigServiceInstance, ConfigProviderOptions)
11
+ ConfigServiceOptionsSchema,
12
+ ConfigServiceToken,
13
+ } from './config.service.mjs'
24
14
 
25
- @Injectable({
26
- token: ConfigProvider,
27
- type: InjectableType.Factory,
15
+ export const ConfigProviderOptions = z.object({
16
+ load: z.function().returns(ConfigServiceOptionsSchema),
28
17
  })
29
- export class ConfigProviderFactory {
30
- logger = syncInject(Logger, {
31
- context: 'ConfigService',
32
- })
33
-
34
- async create(
35
- ctx: any,
36
- args: z.infer<typeof ConfigProviderOptions>,
37
- ): Promise<ConfigService> {
38
- const { load } = args
39
- const logger = this.logger
40
- try {
41
- const config = await load()
42
-
43
- return new ConfigServiceInstance(
44
- config,
45
- logger,
46
- ) as unknown as ConfigService
47
- } catch (error) {
48
- logger.error('Error loading config', error)
49
- throw error
50
- }
51
- }
52
- }
53
18
 
54
- export function provideConfig<ConfigMap extends Record<string, unknown>>(
19
+ export function provideConfig<ConfigMap extends ConfigServiceOptions>(
55
20
  options: z.input<typeof ConfigProviderOptions>,
56
- ): BoundInjectionToken<
57
- ConfigServiceInstance<ConfigMap>,
58
- typeof ConfigProviderOptions
21
+ ): FactoryInjectionToken<
22
+ ConfigService<ConfigMap>,
23
+ typeof ConfigServiceOptionsSchema
59
24
  > {
60
- return InjectionToken.bound(ConfigProvider, options)
25
+ return InjectionToken.factory(ConfigServiceToken, async () => options.load())
61
26
  }
27
+
28
+ export const EnvConfigProvider = InjectionToken.bound<
29
+ ConfigService<Record<string, string>>,
30
+ typeof ConfigServiceOptionsSchema
31
+ >(ConfigServiceToken, {
32
+ ...env,
33
+ })
@@ -1,16 +1,33 @@
1
- import { NaviosException } from '@navios/common'
1
+ import { NaviosException } from '@navios/builder'
2
+ import { Injectable, InjectionToken, syncInject } from '@navios/di'
2
3
 
3
- import type { LoggerService } from '../logger/index.mjs'
4
- import type { ConfigService } from './config-service.interface.mjs'
4
+ import { z } from 'zod'
5
+
6
+ import type { ConfigServiceInterface as IConfigService } from './config-service.interface.mjs'
5
7
  import type { Path, PathValue } from './types.mjs'
6
8
 
7
- export class ConfigServiceInstance<Config = Record<string, unknown>>
8
- implements ConfigService<Config>
9
+ import { Logger } from '../logger/index.mjs'
10
+
11
+ export const ConfigServiceOptionsSchema = z.record(z.unknown())
12
+ export type ConfigServiceOptions = z.infer<typeof ConfigServiceOptionsSchema>
13
+
14
+ export const ConfigServiceToken = InjectionToken.create<
15
+ IConfigService,
16
+ typeof ConfigServiceOptionsSchema
17
+ >(Symbol.for('ConfigService'), ConfigServiceOptionsSchema)
18
+
19
+ @Injectable({
20
+ token: ConfigServiceToken,
21
+ })
22
+ export class ConfigService<
23
+ Config extends ConfigServiceOptions = Record<string, unknown>,
24
+ > implements IConfigService<Config>
9
25
  {
10
- constructor(
11
- private config: Config = {} as Config,
12
- private logger: LoggerService,
13
- ) {}
26
+ private readonly logger = syncInject(Logger, {
27
+ context: ConfigService.name,
28
+ })
29
+
30
+ constructor(private config: Config = {} as Config) {}
14
31
 
15
32
  getConfig(): Config {
16
33
  return this.config
@@ -3,7 +3,7 @@ import type {
3
3
  EndpointFunctionArgs,
4
4
  HttpMethod,
5
5
  Util_FlatObject,
6
- } from '@navios/common'
6
+ } from '@navios/builder'
7
7
  import type { AnyZodObject, z, ZodType } from 'zod'
8
8
 
9
9
  import { ZodDiscriminatedUnion } from 'zod'
@@ -78,11 +78,6 @@ export function Endpoint<
78
78
  ) => Promise<z.input<ResponseSchema>>,
79
79
  context: ClassMethodDecoratorContext,
80
80
  ) => {
81
- if (typeof target !== 'function') {
82
- throw new Error(
83
- '[Navios] Endpoint decorator can only be used on functions.',
84
- )
85
- }
86
81
  if (context.kind !== 'method') {
87
82
  throw new Error(
88
83
  '[Navios] Endpoint decorator can only be used on methods.',
@@ -3,7 +3,7 @@ import type {
3
3
  EndpointFunctionArgs,
4
4
  HttpMethod,
5
5
  Util_FlatObject,
6
- } from '@navios/common'
6
+ } from '@navios/builder'
7
7
  import type { AnyZodObject, z, ZodType } from 'zod'
8
8
 
9
9
  import { ZodDiscriminatedUnion } from 'zod'
@@ -3,7 +3,7 @@ import type {
3
3
  EndpointFunctionArgs,
4
4
  HttpMethod,
5
5
  Util_FlatObject,
6
- } from '@navios/common'
6
+ } from '@navios/builder'
7
7
  import type { FastifyReply } from 'fastify'
8
8
  import type { AnyZodObject, ZodType } from 'zod'
9
9
 
@@ -1,4 +1,4 @@
1
- import type { HttpMethod } from '@navios/common'
1
+ import type { HttpMethod } from '@navios/builder'
2
2
  import type { ClassTypeWithInstance, InjectionToken } from '@navios/di'
3
3
  import type { HttpHeader } from 'fastify/types/utils.js'
4
4
 
package/tsconfig.json ADDED
@@ -0,0 +1,16 @@
1
+ {
2
+ "extends": "../../tsconfig.json",
3
+ "compilerOptions": {
4
+ "module": "Node18",
5
+ "outDir": "dist"
6
+ },
7
+ "include": ["src/**/*"],
8
+ "references": [
9
+ {
10
+ "path": "../builder"
11
+ },
12
+ {
13
+ "path": "../di"
14
+ }
15
+ ]
16
+ }
@@ -0,0 +1,12 @@
1
+ import { defineConfig } from 'tsup'
2
+
3
+ export default defineConfig({
4
+ entry: ['src/index.mts'],
5
+ outDir: 'lib',
6
+ format: ['esm', 'cjs'],
7
+ clean: true,
8
+ treeshake: 'smallest',
9
+ sourcemap: true,
10
+ platform: 'node',
11
+ experimentalDts: true,
12
+ })
@@ -0,0 +1,9 @@
1
+ import { defineProject } from 'vitest/config'
2
+
3
+ export default defineProject({
4
+ test: {
5
+ typecheck: {
6
+ enabled: true,
7
+ },
8
+ },
9
+ })