@nmtjs/core 0.13.1 → 0.14.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.
@@ -10,6 +10,10 @@ export declare const kFactoryInjectable: unique symbol;
10
10
  export type kFactoryInjectable = typeof kFactoryInjectable;
11
11
  export declare const kClassInjectable: unique symbol;
12
12
  export type kClassInjectable = typeof kClassInjectable;
13
+ export declare const kClassInjectableCreate: unique symbol;
14
+ export type kClassInjectableCreate = typeof kClassInjectableCreate;
15
+ export declare const kClassInjectableDispose: unique symbol;
16
+ export type kClassInjectableDispose = typeof kClassInjectableDispose;
13
17
  export declare const kProvider: unique symbol;
14
18
  export type kProvider = typeof kProvider;
15
19
  export declare const kHookCollection: unique symbol;
package/dist/constants.js CHANGED
@@ -4,6 +4,8 @@ export const kLazyInjectable = Symbol.for('neemata:LazyInjectableKey');
4
4
  export const kValueInjectable = Symbol.for('neemata:ValueInjectableKey');
5
5
  export const kFactoryInjectable = Symbol.for('neemata:FactoryInjectableKey');
6
6
  export const kClassInjectable = Symbol.for('neemata:ClassInjectableKey');
7
+ export const kClassInjectableCreate = Symbol.for('neemata:ClassInjectableCreateKey');
8
+ export const kClassInjectableDispose = Symbol.for('neemata:ClassInjectableDisposeKey');
7
9
  export const kProvider = Symbol.for('neemata:ProviderKey');
8
10
  export const kHookCollection = Symbol.for('neemata:HookCollectionKey');
9
11
  export const kPlugin = Symbol.for('neemata:PluginKey');
@@ -1,7 +1,7 @@
1
- import { Scope } from './enums.ts';
2
- import { type AnyInjectable, type Dependencies, type DependencyContext, type ResolveInjectableType } from './injectables.ts';
1
+ import type { AnyInjectable, Dependencies, DependencyContext, ResolveInjectableType } from './injectables.ts';
3
2
  import type { Logger } from './logger.ts';
4
3
  import type { Registry } from './registry.ts';
4
+ import { Scope } from './enums.ts';
5
5
  type InstanceWrapper = {
6
6
  private: any;
7
7
  public: any;
package/dist/container.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import assert from 'node:assert';
2
2
  import { tryCaptureStackTrace } from '@nmtjs/common';
3
+ import { kClassInjectableCreate, kClassInjectableDispose } from "./constants.js";
3
4
  import { Scope } from "./enums.js";
4
5
  import { CoreInjectables, compareScope, createExtendableClassInjectable, createValueInjectable, getDepedencencyInjectable, isClassInjectable, isFactoryInjectable, isInjectable, isLazyInjectable, isOptionalInjectable, isValueInjectable, } from "./injectables.js";
5
6
  export class Container {
@@ -102,11 +103,7 @@ export class Container {
102
103
  throw new Error('Invalid scope'); // TODO: more informative error
103
104
  }
104
105
  this.instances.set(injectable, [
105
- {
106
- private: instance,
107
- public: instance,
108
- context: undefined,
109
- },
106
+ { private: instance, public: instance, context: undefined },
110
107
  ]);
111
108
  }
112
109
  satisfies(injectable) {
@@ -184,9 +181,10 @@ export class Container {
184
181
  wrapper.public = injectable.pick(wrapper.private);
185
182
  }
186
183
  else if (isClassInjectable(injectable)) {
187
- wrapper.private = new injectable(context);
184
+ const instance = new injectable(context);
185
+ wrapper.private = instance;
188
186
  wrapper.public = wrapper.private;
189
- await wrapper.private.$onCreate();
187
+ await instance[kClassInjectableCreate]?.call(instance);
190
188
  }
191
189
  else {
192
190
  throw new Error('Invalid injectable type');
@@ -201,9 +199,7 @@ export class Container {
201
199
  }
202
200
  createInjectFunction() {
203
201
  const inject = (injectable, context) => {
204
- const dependencies = {
205
- ...injectable.dependencies,
206
- };
202
+ const dependencies = { ...injectable.dependencies };
207
203
  for (const key in context) {
208
204
  const dep = context[key];
209
205
  if (isInjectable(dep) || isOptionalInjectable(dep)) {
@@ -307,7 +303,7 @@ export class Container {
307
303
  await dispose(instance, context);
308
304
  }
309
305
  else if (isClassInjectable(injectable)) {
310
- await instance.$onDispose();
306
+ await instance[kClassInjectableDispose]();
311
307
  }
312
308
  }
313
309
  }
package/dist/hooks.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import type { Callback } from '@nmtjs/common';
2
- import { kHookCollection } from './constants.ts';
3
2
  import type { Hook } from './enums.ts';
3
+ import { kHookCollection } from './constants.ts';
4
4
  export interface HookType {
5
5
  [key: string]: (...args: any[]) => any;
6
6
  }
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
- // biome-ignore lint/correctness/noUnusedImports: TSC wants it
2
- // biome-ignore assist/source/organizeImports: TSC wants it
3
- import {} from 'pino';
1
+ // // biome-ignore lint/correctness/noUnusedImports: TSC wants it
2
+ // // biome-ignore assist/source/organizeImports: TSC wants it
3
+ // import {} from 'pino'
4
4
  export * from "./constants.js";
5
5
  export * from "./container.js";
6
6
  export * from "./enums.js";
@@ -1,9 +1,11 @@
1
- import { type Async, type ClassConstructor, type ClassConstructorArgs, type ClassInstance } from '@nmtjs/common';
2
- import { kClassInjectable, kFactoryInjectable, kInjectable, kLazyInjectable, kOptionalDependency, kValueInjectable } from './constants.ts';
3
- import { type Hook, Scope } from './enums.ts';
4
- import { Hooks, type HookType } from './hooks.ts';
1
+ import type { Async, ClassConstructor, ClassConstructorArgs, ClassInstance } from '@nmtjs/common';
2
+ import type { Hook } from './enums.ts';
3
+ import type { HookType } from './hooks.ts';
5
4
  import type { Logger } from './logger.ts';
6
5
  import type { Registry } from './registry.ts';
6
+ import { kClassInjectable, kClassInjectableCreate, kClassInjectableDispose, kFactoryInjectable, kInjectable, kLazyInjectable, kOptionalDependency, kValueInjectable } from './constants.ts';
7
+ import { Scope } from './enums.ts';
8
+ import { Hooks } from './hooks.ts';
7
9
  export type DependencyOptional<T extends AnyInjectable = AnyInjectable> = {
8
10
  [kOptionalDependency]: any;
9
11
  injectable: T;
@@ -53,6 +55,8 @@ export interface BaseClassInjectable<T, D extends Dependencies = {}, S extends S
53
55
  export interface ClassInjectable<T, D extends Dependencies = {}, S extends Scope = Scope.Global, A extends any[] = []> extends Dependant<D> {
54
56
  new ($context: DependencyContext<D>, ...args: A): T & {
55
57
  $context: DependencyContext<D>;
58
+ [kClassInjectableCreate]?: () => Promise<void>;
59
+ [kClassInjectableDispose]?: () => Promise<void>;
56
60
  };
57
61
  scope: S;
58
62
  [kInjectable]: any;
@@ -81,8 +85,8 @@ export declare function createFactoryInjectable<T, D extends Dependencies = {},
81
85
  export declare const createClassInjectable: <D extends Dependencies = {}, S extends Scope = Scope.Global>(dependencies?: D, scope?: S, stackTraceDepth?: number) => ClassInjectable<ClassInstance<{
82
86
  new ($context: DependencyContext<D>): {
83
87
  $context: DependencyContext<D>;
84
- $onCreate(): Promise<void>;
85
- $onDispose(): Promise<void>;
88
+ [kClassInjectableCreate](): Promise<void>;
89
+ [kClassInjectableDispose](): Promise<void>;
86
90
  };
87
91
  dependencies: D;
88
92
  scope: S;
@@ -1,5 +1,5 @@
1
- import { tryCaptureStackTrace, } from '@nmtjs/common';
2
- import { kClassInjectable, kFactoryInjectable, kHookCollection, kInjectable, kLazyInjectable, kOptionalDependency, kValueInjectable, } from "./constants.js";
1
+ import { tryCaptureStackTrace } from '@nmtjs/common';
2
+ import { kClassInjectable, kClassInjectableCreate, kClassInjectableDispose, kFactoryInjectable, kHookCollection, kInjectable, kLazyInjectable, kOptionalDependency, kValueInjectable, } from "./constants.js";
3
3
  import { Scope } from "./enums.js";
4
4
  import { Hooks } from "./hooks.js";
5
5
  const ScopeStrictness = {
@@ -94,8 +94,8 @@ export const createClassInjectable = (dependencies = {}, scope, stackTraceDepth
94
94
  constructor($context) {
95
95
  this.$context = $context;
96
96
  }
97
- async $onCreate() { }
98
- async $onDispose() { }
97
+ async [kClassInjectableCreate]() { }
98
+ async [kClassInjectableDispose]() { }
99
99
  };
100
100
  InjectableClass.scope = resolveInjectableScope(typeof scope === 'undefined', InjectableClass);
101
101
  return InjectableClass;
@@ -163,10 +163,7 @@ export function substitute(injectable, substitution, stackTraceDepth = 0) {
163
163
  }
164
164
  else if (isFactoryInjectable(injectable)) {
165
165
  // @ts-expect-error
166
- return createFactoryInjectable({
167
- ...injectable,
168
- dependencies,
169
- }, injectable.label, depth);
166
+ return createFactoryInjectable({ ...injectable, dependencies }, injectable.label, depth);
170
167
  }
171
168
  throw new Error('Invalid injectable type');
172
169
  }
@@ -221,10 +218,4 @@ function resolveInjectableScope(isDefaultScope, injectable) {
221
218
  throw new Error(`Invalid scope ${injectable.scope} for an injectable: dependencies have stricter scope - ${actualScope}`);
222
219
  return actualScope;
223
220
  }
224
- export const CoreInjectables = {
225
- logger,
226
- registry,
227
- inject,
228
- dispose,
229
- hook,
230
- };
221
+ export const CoreInjectables = { logger, registry, inject, dispose, hook };
package/dist/logger.d.ts CHANGED
@@ -1,4 +1,5 @@
1
- import { type DestinationStream, type Level, type LoggerOptions, type Logger as PinoLogger, pino, type StreamEntry } from 'pino';
1
+ import type { DestinationStream, Level, LoggerOptions, Logger as PinoLogger, StreamEntry } from 'pino';
2
+ import { pino } from 'pino';
2
3
  export type { StreamEntry } from 'pino';
3
4
  export type Logger = PinoLogger;
4
5
  export type LoggingOptions = {
package/dist/logger.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { threadId } from 'node:worker_threads';
2
- import { pino, stdTimeFunctions, } from 'pino';
2
+ import { pino, stdTimeFunctions } from 'pino';
3
3
  import { build as pretty } from 'pino-pretty';
4
4
  // TODO: use node:util inspect
5
5
  const bg = (value, color) => `\x1b[${color}m${value}\x1b[0m`;
@@ -40,11 +40,7 @@ export const createLogger = (options = {}, $group) => {
40
40
  ? pino.levels.values[destination.level]
41
41
  : Number.POSITIVE_INFINITY), Number.POSITIVE_INFINITY);
42
42
  const level = pino.levels.labels[lowestLevelValue];
43
- return pino({
44
- timestamp: stdTimeFunctions.isoTime,
45
- ...pinoOptions,
46
- level,
47
- }, pino.multistream(destinations)).child({ $group, $threadId: threadId });
43
+ return pino({ timestamp: stdTimeFunctions.isoTime, ...pinoOptions, level }, pino.multistream(destinations)).child({ $group, $threadId: threadId });
48
44
  };
49
45
  export const createConsolePrettyDestination = (level, sync = true) => ({
50
46
  level,
package/dist/plugin.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import type { Async } from '@nmtjs/common';
2
- import { kPlugin } from './constants.ts';
3
2
  import type { PluginContext } from './types.ts';
3
+ import { kPlugin } from './constants.ts';
4
4
  export interface BasePlugin<Type = any, Options = unknown, Context extends PluginContext = PluginContext> {
5
5
  name: string;
6
6
  init: (context: Context, options: Options) => Async<Type>;
@@ -1,7 +1,9 @@
1
- import { type Hook, Scope } from './enums.ts';
2
- import { Hooks, type HookType } from './hooks.ts';
3
- import { type AnyInjectable, type Dependant } from './injectables.ts';
1
+ import type { Hook } from './enums.ts';
2
+ import type { HookType } from './hooks.ts';
3
+ import type { AnyInjectable, Dependant } from './injectables.ts';
4
4
  import type { Logger } from './logger.ts';
5
+ import { Scope } from './enums.ts';
6
+ import { Hooks } from './hooks.ts';
5
7
  export declare class Registry {
6
8
  protected readonly application: {
7
9
  logger: Logger;
package/dist/registry.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { Scope } from "./enums.js";
2
2
  import { Hooks } from "./hooks.js";
3
- import { getInjectableScope, } from "./injectables.js";
3
+ import { getInjectableScope } from "./injectables.js";
4
4
  export class Registry {
5
5
  application;
6
6
  hooks = new Hooks();
package/package.json CHANGED
@@ -6,27 +6,32 @@
6
6
  "types": "./dist/index.d.ts",
7
7
  "import": "./dist/index.js",
8
8
  "module-sync": "./dist/index.js"
9
+ },
10
+ "./constants": {
11
+ "types": "./dist/constants.d.ts",
12
+ "import": "./dist/constants.js",
13
+ "module-sync": "./dist/constants.js"
9
14
  }
10
15
  },
11
16
  "peerDependencies": {
12
17
  "pino": "^9.6.0",
13
18
  "pino-pretty": "^13.0.0",
14
- "@nmtjs/common": "0.13.1",
15
- "@nmtjs/type": "0.13.1"
19
+ "@nmtjs/common": "0.14.1",
20
+ "@nmtjs/type": "0.14.1"
16
21
  },
17
22
  "devDependencies": {
18
23
  "@types/node": "^20",
19
24
  "pino": "^9.6.0",
20
25
  "pino-pretty": "^13.0.0",
21
- "@nmtjs/common": "0.13.1",
22
- "@nmtjs/type": "0.13.1"
26
+ "@nmtjs/type": "0.14.1",
27
+ "@nmtjs/common": "0.14.1"
23
28
  },
24
29
  "files": [
25
30
  "dist",
26
31
  "LICENSE.md",
27
32
  "README.md"
28
33
  ],
29
- "version": "0.13.1",
34
+ "version": "0.14.1",
30
35
  "scripts": {
31
36
  "build": "tsc",
32
37
  "type-check": "tsc --noEmit"