@xrystal/core 3.19.2 → 3.19.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.19.2",
4
+ "version": "3.19.4",
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",
@@ -1,4 +1,5 @@
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 {
@@ -52,21 +53,16 @@ export default class ConfigsService {
52
53
  try {
53
54
  const moduleData = await modulePromise;
54
55
  const importedConfigs = moduleData?.default || moduleData?.configs || moduleData || {};
55
- this.#config = {
56
- ...this.#config,
57
- ...(typeof importedConfigs === 'object' ? importedConfigs : {})
58
- };
56
+ const merged = merge({}, this.#config, importedConfigs || {});
57
+ this.#config = Object.freeze(merged);
59
58
  }
60
59
  catch (e) {
61
60
  // error
62
61
  }
63
62
  };
64
63
  setConfig(newConfigs) {
65
- const mergedData = {
66
- ...this.#config,
67
- ...newConfigs
68
- };
69
- this.#config = Object.freeze(mergedData);
64
+ const mergedDObj = Object.assign(this.#config, newConfigs);
65
+ this.#config = Object.freeze(mergedDObj);
70
66
  }
71
67
  _(key) {
72
68
  return this.#config[key];
@@ -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?: {
@@ -68,7 +67,7 @@ export declare abstract class ControllerService extends Controller {
68
67
  }): Promise<void>;
69
68
  schema({ checks, logic, response }: {
70
69
  checks?: (args: any) => Promise<any>;
71
- logic: (args: any) => Promise<any>;
70
+ logic?: (args: any) => Promise<any>;
72
71
  response?: (args: any) => Promise<any>;
73
72
  }): Promise<any>;
74
73
  }
@@ -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,50 +79,53 @@ 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 = { ...payload, query: currentReq.query, body: currentReq.body, params: currentReq.params };
100
86
  if (checks) {
101
87
  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({
88
+ if (checkResult === false || (checkResult && typeof checkResult === 'object' && !Array.isArray(checkResult))) {
89
+ return currentRes.status(checkResult?.code || 400).send(new ResponseSchema({
104
90
  status: false,
105
91
  message: checkResult?.message || '',
106
92
  code: checkResult?.code || 400
107
93
  }).getResponse);
108
94
  }
109
95
  }
110
- const logicResult = await logic({ payload, convertedPayload });
96
+ const logicResult = logic ? await logic({ payload, convertedPayload }) : {};
111
97
  if (logicResult?.status === false) {
112
- return res.status(logicResult.code || 400).send(new ResponseSchema({
98
+ return currentRes.status(logicResult.code || 400).send(new ResponseSchema({
113
99
  status: false,
114
100
  message: logicResult.message,
115
101
  code: logicResult.code || 400
116
102
  }).getResponse);
117
103
  }
118
- if (logicResult?.response instanceof Function)
119
- return logicResult.response(logicResult.payload);
120
104
  if (logicResult instanceof Response)
121
105
  return logicResult;
122
- if (response) {
123
- const resResult = await response({ payload, convertedPayload, logicResult });
124
- const messageData = Array.isArray(resResult) ? resResult : resResult?.message;
125
- const successObj = {
126
- status: true,
127
- message: Array.isArray(messageData)
128
- ? responseMessageHelper.successFully(messageData[0], messageData[1], req.t)
129
- : (messageData || ''),
130
- payload: logicResult?.payload !== undefined ? logicResult.payload : logicResult
131
- };
132
- return res.status(200).send(new ResponseSchema(successObj).getResponse);
106
+ const resResult = response ? await response({ payload, convertedPayload, logicResult }) : logicResult;
107
+ let finalMessage = '';
108
+ if (Array.isArray(resResult)) {
109
+ finalMessage = responseMessageHelper.successFully(resResult[0], resResult[1], currentReq.t);
133
110
  }
134
- return res.send(logicResult?.payload !== undefined ? logicResult.payload : logicResult);
111
+ else if (typeof resResult === 'string') {
112
+ finalMessage = resResult;
113
+ }
114
+ else if (resResult?.message) {
115
+ finalMessage = Array.isArray(resResult.message)
116
+ ? responseMessageHelper.successFully(resResult.message[0], resResult.message[1], currentReq.t)
117
+ : resResult.message;
118
+ }
119
+ return currentRes.status(200).send(new ResponseSchema({
120
+ status: true,
121
+ message: finalMessage,
122
+ payload: logicResult?.payload !== undefined ? logicResult.payload : logicResult
123
+ }).getResponse);
135
124
  }
136
125
  catch (error) {
137
126
  this.logger.winston.log({
138
127
  level: LoggerLayerEnum[LoggerLayerEnum.CRITICAL].toLowerCase(),
139
- message: `Controller Error: ${error}`,
128
+ message: `Controller Error: ${error}`
140
129
  });
141
130
  return this.res.status(500).send(new ResponseSchema({
142
131
  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, _ };