@xrystal/core 3.21.0 → 3.21.3

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "author": "Yusuf Yasir KAYGUSUZ",
3
3
  "name": "@xrystal/core",
4
- "version": "3.21.0",
4
+ "version": "3.21.3",
5
5
  "description": "Project core for xrystal",
6
6
  "publishConfig": {
7
7
  "access": "public",
@@ -6,6 +6,6 @@ export default class ClientsService implements IService<any> {
6
6
  constructor({ configsService }: {
7
7
  configsService: ConfigsService;
8
8
  });
9
- load(): Promise<void>;
9
+ onInit(): Promise<void>;
10
10
  get instances(): Record<string, any>;
11
11
  }
@@ -5,7 +5,7 @@ export default class ClientsService {
5
5
  constructor({ configsService }) {
6
6
  this.#configsService = configsService;
7
7
  }
8
- async load() {
8
+ async onInit() {
9
9
  const baseClient = new BaseApiClient({
10
10
  clientName: CoreServiceEnum.BASE_API_CLIENT,
11
11
  baseURL: this.#configsService.all.baseApiUri || ''
@@ -37,7 +37,7 @@ export default class ConfigsService implements IService<any> {
37
37
  constructor({ systemService }: {
38
38
  systemService: SystemService;
39
39
  });
40
- load: ({}: {}) => Promise<void>;
40
+ onInit: ({}: {}) => Promise<void>;
41
41
  setConfig(newConfigs: any): void;
42
42
  _<K extends keyof IConfig>(key: K): IConfig[K];
43
43
  get all(): IConfig;
@@ -16,7 +16,7 @@ export default class ConfigsService {
16
16
  });
17
17
  return proxy;
18
18
  }
19
- load = async ({}) => {
19
+ onInit = async ({}) => {
20
20
  const cwd = process.cwd();
21
21
  const tmp = this.#systemService.tmp;
22
22
  const extendConfigs = tmp.configs.loaders.configs.loadPath;
@@ -51,7 +51,7 @@ export declare abstract class ControllerService extends Controller implements IS
51
51
  systemService: SystemService;
52
52
  loggerService: LoggerService;
53
53
  });
54
- load(): Promise<void>;
54
+ onInit(): Promise<void>;
55
55
  schema({ checks, logic, response }: {
56
56
  checks?: (args: any) => Promise<any>;
57
57
  logic?: (args: any) => Promise<any>;
@@ -77,7 +77,7 @@ export class ControllerService extends Controller {
77
77
  this.systemService = systemService;
78
78
  this.logger = loggerService;
79
79
  }
80
- async load() {
80
+ async onInit() {
81
81
  const protocols = this.systemService?.tmp?.configs?.loaders?.controller?.protocols;
82
82
  this.supportedProtocols = Array.isArray(protocols) ? protocols : [protocols || ProtocolEnum.HTTP];
83
83
  }
@@ -5,7 +5,7 @@ export default class EventsService implements IService<any> {
5
5
  constructor({ loggerService }: {
6
6
  loggerService: LoggerService;
7
7
  });
8
- load: ({}: {
8
+ onInit: ({}: {
9
9
  logger: LoggerService;
10
10
  }) => any;
11
11
  private _globalLoader;
@@ -4,7 +4,7 @@ export default class EventsService {
4
4
  constructor({ loggerService }) {
5
5
  this.#loggerService = loggerService;
6
6
  }
7
- load = ({}) => {
7
+ onInit = ({}) => {
8
8
  this._globalLoader();
9
9
  this._DILoader();
10
10
  };
@@ -8,7 +8,7 @@ export default class LocalizationsService implements IService<any> {
8
8
  systemService: SystemService;
9
9
  configsService: ConfigsService;
10
10
  });
11
- load: ({}: {}) => any;
11
+ onInit: ({}: {}) => any;
12
12
  i18next: ({ loadPath, fallbackLang, preloadLang, }: {
13
13
  loadPath?: string;
14
14
  fallbackLang?: string;
@@ -9,7 +9,7 @@ export default class LocalizationsService {
9
9
  this.#systemService = systemService;
10
10
  this.#configsService = configsService;
11
11
  }
12
- load = ({}) => {
12
+ onInit = ({}) => {
13
13
  const localization = this.#systemService.tmp.configs.loaders.localization;
14
14
  this.i18next({
15
15
  loadPath: localization.loadPath,
@@ -26,7 +26,7 @@ export default class LoggerService implements IService<any> {
26
26
  systemService: SystemService;
27
27
  configsService: ConfigsService;
28
28
  });
29
- load: () => Promise<void>;
29
+ onInit: () => Promise<void>;
30
30
  winstonLoader: ({ loadPath, loggerLevel }: {
31
31
  loadPath: string;
32
32
  loggerLevel: string;
@@ -44,7 +44,7 @@ export default class LoggerService {
44
44
  this.#systemService = systemService;
45
45
  this.#configsService = configsService;
46
46
  }
47
- load = async () => {
47
+ onInit = async () => {
48
48
  this.serviceName = this.#systemService?.tmp?.configs?.service;
49
49
  this.kafkaLogsTopic = this.#configsService?.all?.kafkaLogsTopic;
50
50
  winston.addColors(customColors);
@@ -3,7 +3,7 @@ export default class SystemService implements IService<any> {
3
3
  protected _core: Record<string, any>;
4
4
  protected _tmp: Record<string, any>;
5
5
  cwd: string;
6
- load: ({ core }: {
6
+ onInit: ({ core }: {
7
7
  core: any;
8
8
  }) => Promise<void>;
9
9
  private initializeKafkaInfrastructure;
@@ -4,7 +4,7 @@ export default class SystemService {
4
4
  _core = {};
5
5
  _tmp = {};
6
6
  cwd = process.cwd();
7
- load = async ({ core }) => {
7
+ onInit = async ({ core }) => {
8
8
  this._core = core;
9
9
  this._tmp = this._core._;
10
10
  await this._systemLoader({});
@@ -1,6 +1,7 @@
1
1
  export declare abstract class ILifeCycle<T = any> {
2
- load(props?: T | {}): Promise<void>;
3
- dispose?(_context?: any): Promise<void> | void;
2
+ onInit(props?: T | {}): Promise<void>;
3
+ onAfterInit?(_context?: any): Promise<void> | void;
4
+ onDispose?(_context?: any): Promise<void> | void;
4
5
  }
5
6
  export declare abstract class IService<T = any> extends ILifeCycle<T> {
6
7
  }
@@ -1,7 +1,9 @@
1
1
  export class ILifeCycle {
2
- async load(props = {}) {
2
+ async onInit(props = {}) {
3
3
  }
4
- dispose(_context) {
4
+ onAfterInit(_context) {
5
+ }
6
+ onDispose(_context) {
5
7
  }
6
8
  }
7
9
  export class IService extends ILifeCycle {
@@ -1,10 +1,11 @@
1
1
  import { AwilixContainer, LifetimeType } from 'awilix';
2
+ export declare const xSymbol: unique symbol;
2
3
  type Constructor<T> = new (...args: any[]) => T;
3
4
  type AbstractConstructor<T> = abstract new (...args: any[]) => T;
4
- declare abstract class XHelper {
5
+ declare abstract class X {
5
6
  protected checkRegistration(container: AwilixContainer, name: string): boolean;
6
7
  }
7
- declare class X extends XHelper {
8
+ declare class DIM extends X {
8
9
  private container;
9
10
  private initializedNames;
10
11
  private loadedPaths;
@@ -30,5 +31,5 @@ declare class X extends XHelper {
30
31
  shutdown(): Promise<void>;
31
32
  get cradle(): any;
32
33
  }
33
- declare const _default: X;
34
+ declare const _default: DIM;
34
35
  export default _default;
@@ -1,12 +1,13 @@
1
1
  import { createContainer, asClass, asValue, InjectionMode, listModules, Lifetime } from 'awilix';
2
2
  import path from 'node:path';
3
3
  import { pathToFileURL } from 'node:url';
4
- class XHelper {
4
+ export const xSymbol = Symbol.for('@xrystal/core/di-container');
5
+ class X {
5
6
  checkRegistration(container, name) {
6
7
  return !!container.registrations[name];
7
8
  }
8
9
  }
9
- class X extends XHelper {
10
+ class DIM extends X {
10
11
  container;
11
12
  initializedNames = new Set();
12
13
  loadedPaths = new Set();
@@ -96,7 +97,6 @@ class X extends XHelper {
96
97
  this.isInitializing = true;
97
98
  try {
98
99
  const inputList = input ? (Array.isArray(input) ? input : [input]) : [];
99
- // 1. ADIM: Senin verdiğin sıraya göre TEK TEK yükle (Senin için en önemli yer)
100
100
  for (const item of inputList) {
101
101
  const name = this.getName(item.service);
102
102
  if (!name || this.initializedNames.has(name))
@@ -105,20 +105,17 @@ class X extends XHelper {
105
105
  if (!reg || reg.lifetime !== Lifetime.SINGLETON)
106
106
  continue;
107
107
  try {
108
- // Awilix instance'ı oluşturur (constructor çalışır)
109
108
  const instance = this.container.cradle[name];
110
- if (instance && typeof instance.load === 'function') {
111
- // Burada await ediyoruz, yani bu servis bitmeden döngü ilerlemez
112
- await instance.load(item.props || {});
109
+ if (instance && typeof instance.onInit === 'function') {
110
+ await instance.onInit(item.props || {});
113
111
  }
114
112
  this.initializedNames.add(name);
115
113
  }
116
114
  catch (err) {
117
115
  if (verbose)
118
- console.error(`[DI] Priority Init Failed (${name}):`, err.message);
116
+ console.error(`[DI] Priority onInit Failed (${name}):`, err.message);
119
117
  }
120
118
  }
121
- // 2. ADIM: Listede olmayan ama register edilmiş diğer geri kalan singleton'ları yükle
122
119
  const allKeys = Object.keys(this.container.registrations);
123
120
  for (const key of allKeys) {
124
121
  if (this.initializedNames.has(key))
@@ -128,14 +125,26 @@ class X extends XHelper {
128
125
  continue;
129
126
  try {
130
127
  const instance = this.container.cradle[key];
131
- if (instance && typeof instance.load === 'function') {
132
- await instance.load({});
128
+ if (instance && typeof instance.onInit === 'function') {
129
+ await instance.onInit({});
133
130
  }
134
131
  this.initializedNames.add(key);
135
132
  }
136
133
  catch (err) {
137
134
  if (verbose)
138
- console.error(`[DI] Auto Init Failed (${key}):`, err.message);
135
+ console.error(`[DI] Auto onInit Failed (${key}):`, err.message);
136
+ }
137
+ }
138
+ for (const name of this.initializedNames) {
139
+ try {
140
+ const instance = this.container.cradle[name];
141
+ if (instance && typeof instance.onAfterInit === 'function') {
142
+ await instance.onAfterInit();
143
+ }
144
+ }
145
+ catch (err) {
146
+ if (verbose)
147
+ console.error(`[DI] onAfterInit Failed (${name}):`, err.message);
139
148
  }
140
149
  }
141
150
  }
@@ -149,13 +158,22 @@ class X extends XHelper {
149
158
  return this.container.resolve(resolveName);
150
159
  }
151
160
  async shutdown() {
161
+ for (const name of this.initializedNames) {
162
+ try {
163
+ const instance = this.container.cradle[name];
164
+ if (instance && typeof instance.onDispose === 'function') {
165
+ await instance.onDispose();
166
+ }
167
+ }
168
+ catch (err) {
169
+ console.error(`[DI] onDispose Failed (${name})`);
170
+ }
171
+ }
152
172
  await this.container.dispose();
153
173
  }
154
174
  get cradle() { return this.container.cradle; }
155
175
  }
156
- const X_SYMBOL = Symbol.for('X');
157
- const globalObj = global;
158
- if (!globalObj[X_SYMBOL]) {
159
- globalObj[X_SYMBOL] = new X();
176
+ if (!global[xSymbol]) {
177
+ global[xSymbol] = new DIM();
160
178
  }
161
- export default globalObj[X_SYMBOL];
179
+ export default global[xSymbol];