@xrystal/core 3.21.1 → 3.21.4

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.1",
4
+ "version": "3.21.4",
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,3 +1,4 @@
1
+ import { ProtocolEnum } from "../../index.js";
1
2
  export declare const getCore: () => {
2
3
  _: any;
3
4
  };
@@ -8,6 +9,7 @@ export declare const getTmpConfig: ({ root, tmpFileName, ext }: {
8
9
  }) => any;
9
10
  export declare function resolveObjWithHandlebars(obj: any, context: any): any;
10
11
  export declare function findFileRecursively(startDir: string, baseName: string, extName?: string, excludedDirs?: string[]): string | null;
12
+ export declare const protocol: (callback: () => Promise<any>, protocol?: ProtocolEnum) => Promise<any>;
11
13
  export declare const argvsConverter: (argvs: string) => {
12
14
  [key: string]: string;
13
15
  };
@@ -1,7 +1,8 @@
1
1
  import path from "path";
2
2
  import fs from 'fs';
3
3
  import Handlebars from 'handlebars';
4
- import { Constants, TmpFileLoader } from "../../index.js";
4
+ import { Constants, ProtocolEnum, TmpFileLoader } from "../../index.js";
5
+ import { controllerContextStorage } from "../../../loader/controller";
5
6
  export const getCore = () => {
6
7
  const ownerTmpFilePath = findFileRecursively(".", Constants.defaultTmpFileName, Constants.defaultTmpFileExt);
7
8
  if (!ownerTmpFilePath) {
@@ -75,6 +76,13 @@ export function findFileRecursively(startDir, baseName, extName = '', excludedDi
75
76
  return null;
76
77
  }
77
78
  }
79
+ export const protocol = async (callback, protocol = ProtocolEnum.HTTP) => {
80
+ const store = controllerContextStorage.getStore();
81
+ if (store) {
82
+ store.protocol = protocol;
83
+ }
84
+ return await callback();
85
+ };
78
86
  // => ---
79
87
  export const argvsConverter = (argvs) => {
80
88
  const reelArgvs = argvs?.slice(2);
@@ -1,7 +1,7 @@
1
1
  export declare abstract class ILifeCycle<T = any> {
2
- load(props?: T | {}): Promise<void>;
3
- ready?(_context?: any): Promise<void> | void;
4
- 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;
5
5
  }
6
6
  export declare abstract class IService<T = any> extends ILifeCycle<T> {
7
7
  }
@@ -1,9 +1,9 @@
1
1
  export class ILifeCycle {
2
- async load(props = {}) {
2
+ async onInit(props = {}) {
3
3
  }
4
- ready(_context) {
4
+ onAfterInit(_context) {
5
5
  }
6
- dispose(_context) {
6
+ onDispose(_context) {
7
7
  }
8
8
  }
9
9
  export class IService extends ILifeCycle {
@@ -106,14 +106,14 @@ class DIM extends X {
106
106
  continue;
107
107
  try {
108
108
  const instance = this.container.cradle[name];
109
- if (instance && typeof instance.load === 'function') {
110
- await instance.load(item.props || {});
109
+ if (instance && typeof instance.onInit === 'function') {
110
+ await instance.onInit(item.props || {});
111
111
  }
112
112
  this.initializedNames.add(name);
113
113
  }
114
114
  catch (err) {
115
115
  if (verbose)
116
- console.error(`[DI] Priority Init Failed (${name}):`, err.message);
116
+ console.error(`[DI] Priority onInit Failed (${name}):`, err.message);
117
117
  }
118
118
  }
119
119
  const allKeys = Object.keys(this.container.registrations);
@@ -125,26 +125,26 @@ class DIM extends X {
125
125
  continue;
126
126
  try {
127
127
  const instance = this.container.cradle[key];
128
- if (instance && typeof instance.load === 'function') {
129
- await instance.load({});
128
+ if (instance && typeof instance.onInit === 'function') {
129
+ await instance.onInit({});
130
130
  }
131
131
  this.initializedNames.add(key);
132
132
  }
133
133
  catch (err) {
134
134
  if (verbose)
135
- console.error(`[DI] Auto Init Failed (${key}):`, err.message);
135
+ console.error(`[DI] Auto onInit Failed (${key}):`, err.message);
136
136
  }
137
137
  }
138
138
  for (const name of this.initializedNames) {
139
139
  try {
140
140
  const instance = this.container.cradle[name];
141
- if (instance && typeof instance.ready === 'function') {
142
- await instance.afterInit();
141
+ if (instance && typeof instance.onAfterInit === 'function') {
142
+ await instance.onAfterInit();
143
143
  }
144
144
  }
145
145
  catch (err) {
146
146
  if (verbose)
147
- console.error(`[DI] afterInit Hook Failed (${name}):`, err.message);
147
+ console.error(`[DI] onAfterInit Failed (${name}):`, err.message);
148
148
  }
149
149
  }
150
150
  }
@@ -158,6 +158,17 @@ class DIM extends X {
158
158
  return this.container.resolve(resolveName);
159
159
  }
160
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
+ }
161
172
  await this.container.dispose();
162
173
  }
163
174
  get cradle() { return this.container.cradle; }