@xrystal/core 3.19.1 → 3.19.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.19.1",
4
+ "version": "3.19.3",
5
5
  "description": "Project core for xrystal",
6
6
  "publishConfig": {
7
7
  "access": "public",
@@ -37,17 +37,21 @@
37
37
  "start": "bun --env-file=../infrastructer/x/environments/.global.env --env-file=../infrastructer/x/environments/.dev.env source/index.js"
38
38
  },
39
39
  "dependencies": {
40
+ "@types/lodash": "^4.17.23",
40
41
  "@types/yaml": "^1.9.7",
41
42
  "awilix": "^12.0.5",
42
43
  "chalk": "^5.6.2",
43
44
  "commander": "^13.0.0",
44
45
  "ejs": "^3.1.9",
45
46
  "handlebars": "^4.7.8",
47
+ "i": "^0.3.7",
46
48
  "i18next": "^25.6.3",
47
49
  "i18next-fs-backend": "^2.6.1",
48
50
  "i18next-http-middleware": "^3.8.2",
49
51
  "kafkajs": "^2.2.4",
52
+ "lodash": "^4.17.23",
50
53
  "moment-timezone": "^0.6.0",
54
+ "npm": "^11.7.0",
51
55
  "ora": "^9.0.0",
52
56
  "picocolors": "^1.1.1",
53
57
  "qs": "^6.14.1",
@@ -25,9 +25,10 @@ export interface IConfig {
25
25
  cwd: string;
26
26
  env: string;
27
27
  }
28
+ export interface IConfigsService extends IConfig {
29
+ }
28
30
  export default class ConfigsService implements IService {
29
31
  #private;
30
- private config;
31
32
  readonly publicFolderName: string;
32
33
  readonly kafkaLogsTopic: string;
33
34
  constructor({ systemService }: {
@@ -1,20 +1,20 @@
1
1
  import path from 'node:path';
2
+ import { merge } from 'lodash';
2
3
  import { Constants } from '../../utils';
3
4
  import { pathToFileURL } from 'node:url';
4
5
  export default class ConfigsService {
5
- config;
6
6
  publicFolderName = Constants.publicFolderName;
7
7
  kafkaLogsTopic = Constants.kafkaLogsTopic;
8
8
  #systemService;
9
+ #config;
9
10
  constructor({ systemService }) {
10
11
  this.#systemService = systemService;
11
- return new Proxy(this, {
12
+ const proxy = new Proxy(this, {
12
13
  get: (target, prop) => {
13
- if (prop in target)
14
- return target[prop];
15
- return target.config[prop];
14
+ return target[prop] || target.config?.[prop];
16
15
  }
17
16
  });
17
+ return proxy;
18
18
  }
19
19
  load = async ({}) => {
20
20
  const cwd = process.cwd();
@@ -25,7 +25,7 @@ export default class ConfigsService {
25
25
  : path.resolve(tmp.configs.rootFolderPath, extendConfigs);
26
26
  const fileUrl = pathToFileURL(fullPath).href;
27
27
  const modulePromise = import(fileUrl);
28
- this.config = {
28
+ this.#config = {
29
29
  worker: process.env.WORKER === 'true' ? true : false || false,
30
30
  nodeEnv: process.env.NODE_ENV,
31
31
  debug: process.env.SYSTEM_LOGGER_LAYER,
@@ -53,26 +53,21 @@ export default class ConfigsService {
53
53
  try {
54
54
  const moduleData = await modulePromise;
55
55
  const importedConfigs = moduleData?.default || moduleData?.configs || moduleData || {};
56
- this.config = {
57
- ...this.config,
58
- ...(typeof importedConfigs === 'object' ? importedConfigs : {})
59
- };
56
+ const merged = merge({}, this.#config, importedConfigs || {});
57
+ this.#config = Object.freeze(merged);
60
58
  }
61
59
  catch (e) {
62
60
  // error
63
61
  }
64
62
  };
65
63
  setConfig(newConfigs) {
66
- const mergedData = {
67
- ...this.config,
68
- ...newConfigs
69
- };
70
- this.config = Object.freeze(mergedData);
64
+ const mergedDObj = Object.assign(this.#config, newConfigs);
65
+ this.#config = Object.freeze(mergedDObj);
71
66
  }
72
67
  _(key) {
73
- return this.config[key];
68
+ return this.#config[key];
74
69
  }
75
70
  get all() {
76
- return this.config;
71
+ return this.#config;
77
72
  }
78
73
  }
@@ -60,7 +60,6 @@ declare abstract class Controller {
60
60
  };
61
61
  protected get req(): CustomRequest;
62
62
  protected get res(): CustomResponse;
63
- protected parsedQuerys: (url: string) => Record<string, any>;
64
63
  }
65
64
  export declare abstract class ControllerService extends Controller {
66
65
  load(props?: {
@@ -1,4 +1,3 @@
1
- import qs from 'qs';
2
1
  import { AsyncLocalStorage } from 'node:async_hooks';
3
2
  import { LoggerLayerEnum, ProtocolEnum, responseMessageHelper, ResponseSchema, x } from '../../utils/index';
4
3
  import LoggerService from '../logger';
@@ -16,22 +15,18 @@ class Controller {
16
15
  get req() {
17
16
  const store = this.currentStore;
18
17
  const identityT = (k) => k;
19
- if (!store)
18
+ if (!store?.ctx)
20
19
  return { url: '', method: '', headers: {}, params: {}, query: {}, lang: 'en', t: identityT };
21
- const { ctx, req: nativeReq } = store;
22
- const source = nativeReq || ctx?.request || ctx;
23
20
  return {
24
- url: source?.url || '',
25
- method: source?.method || 'GET',
26
- headers: source?.headers && typeof source.headers.entries === 'function'
27
- ? Object.fromEntries(source.headers.entries())
28
- : (source?.headers || {}),
29
- body: ctx?.body || nativeReq?.body || source?.body,
30
- params: ctx?.params || nativeReq?.params || source?.params || {},
31
- query: ctx?.query || nativeReq?.query || source?.query || {},
32
- accounts: ctx?.user || nativeReq?.accounts || source?.accounts,
33
- lang: ctx?.lang || nativeReq?.lang || source?.lang || 'en',
34
- t: ctx?.t || nativeReq?.t || source?.t || identityT
21
+ url: store.ctx.url,
22
+ method: store.ctx.method,
23
+ headers: store.ctx.headers,
24
+ body: store.ctx.body,
25
+ query: store.ctx.query,
26
+ params: store.ctx.params,
27
+ lang: store.ctx.lang,
28
+ t: store.ctx.t || identityT,
29
+ accounts: store.ctx.accounts || store.req?.accounts
35
30
  };
36
31
  }
37
32
  get res() {
@@ -76,15 +71,6 @@ class Controller {
76
71
  json(data) { return this.send(data); }
77
72
  };
78
73
  }
79
- parsedQuerys = (url) => {
80
- try {
81
- const queryPart = url.split('?')[1];
82
- return queryPart ? qs.parse(queryPart, { decoder: decodeURIComponent }) : {};
83
- }
84
- catch {
85
- return {};
86
- }
87
- };
88
74
  }
89
75
  export class ControllerService extends Controller {
90
76
  async load(props = {}) {
@@ -93,14 +79,19 @@ export class ControllerService extends Controller {
93
79
  }
94
80
  async schema({ checks, logic, response }) {
95
81
  try {
96
- const req = this.req;
97
- const res = this.res;
98
- const payload = { req, res };
99
- const convertedPayload = { ...payload, query: this.parsedQuerys(req.url) };
82
+ const currentReq = this.req;
83
+ const currentRes = this.res;
84
+ const payload = { req: currentReq, res: currentRes };
85
+ const convertedPayload = {
86
+ ...payload,
87
+ query: currentReq.query,
88
+ body: currentReq.body,
89
+ params: currentReq.params
90
+ };
100
91
  if (checks) {
101
92
  const checkResult = await checks({ payload, convertedPayload });
102
- if (checkResult === false || (checkResult && !Array.isArray(checkResult) && typeof checkResult === 'object')) {
103
- return res.status(checkResult?.code || 400).send(new ResponseSchema({
93
+ if (checkResult === false || (checkResult && typeof checkResult === 'object' && !Array.isArray(checkResult))) {
94
+ return currentRes.status(checkResult?.code || 400).send(new ResponseSchema({
104
95
  status: false,
105
96
  message: checkResult?.message || '',
106
97
  code: checkResult?.code || 400
@@ -109,34 +100,31 @@ export class ControllerService extends Controller {
109
100
  }
110
101
  const logicResult = await logic({ payload, convertedPayload });
111
102
  if (logicResult?.status === false) {
112
- return res.status(logicResult.code || 400).send(new ResponseSchema({
103
+ return currentRes.status(logicResult.code || 400).send(new ResponseSchema({
113
104
  status: false,
114
105
  message: logicResult.message,
115
106
  code: logicResult.code || 400
116
107
  }).getResponse);
117
108
  }
118
- if (logicResult?.response instanceof Function)
119
- return logicResult.response(logicResult.payload);
120
109
  if (logicResult instanceof Response)
121
110
  return logicResult;
122
111
  if (response) {
123
112
  const resResult = await response({ payload, convertedPayload, logicResult });
124
113
  const messageData = Array.isArray(resResult) ? resResult : resResult?.message;
125
- const successObj = {
114
+ return currentRes.status(200).send(new ResponseSchema({
126
115
  status: true,
127
116
  message: Array.isArray(messageData)
128
- ? responseMessageHelper.successFully(messageData[0], messageData[1], req.t)
117
+ ? responseMessageHelper.successFully(messageData[0], messageData[1], currentReq.t)
129
118
  : (messageData || ''),
130
119
  payload: logicResult?.payload !== undefined ? logicResult.payload : logicResult
131
- };
132
- return res.status(200).send(new ResponseSchema(successObj).getResponse);
120
+ }).getResponse);
133
121
  }
134
- return res.send(logicResult?.payload !== undefined ? logicResult.payload : logicResult);
122
+ return currentRes.send(logicResult?.payload !== undefined ? logicResult.payload : logicResult);
135
123
  }
136
124
  catch (error) {
137
125
  this.logger.winston.log({
138
126
  level: LoggerLayerEnum[LoggerLayerEnum.CRITICAL].toLowerCase(),
139
- message: `Controller Error: ${error}`,
127
+ message: `Controller Error: ${error}`
140
128
  });
141
129
  return this.res.status(500).send(new ResponseSchema({
142
130
  status: false,
@@ -1,9 +1,10 @@
1
+ import _ from 'lodash';
1
2
  import x from './classes/class.x';
2
- import locator, { Locator } from './classes/class.service-locator';
3
+ import locator from './classes/class.service-locator';
3
4
  export * from './classes/class.tmp-file-loader';
4
5
  export * from './classes/class.response';
5
6
  export * from './classes/class.services';
6
7
  export * from './classes/class.interfaces';
7
8
  export * from './types';
8
9
  export * from './enums';
9
- export { x, locator, Locator };
10
+ export { x, locator, _ };
@@ -1,9 +1,10 @@
1
+ import _ from 'lodash';
1
2
  import x from './classes/class.x';
2
- import locator, { Locator } from './classes/class.service-locator';
3
+ import locator from './classes/class.service-locator';
3
4
  export * from './classes/class.tmp-file-loader';
4
5
  export * from './classes/class.response';
5
6
  export * from './classes/class.services';
6
7
  export * from './classes/class.interfaces';
7
8
  export * from './types';
8
9
  export * from './enums';
9
- export { x, locator, Locator };
10
+ export { x, locator, _ };