@xrystal/core 3.16.7 → 3.16.9
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,6 +1,6 @@
|
|
|
1
1
|
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
2
|
-
import LoggerService from '../logger';
|
|
3
2
|
import { ProtocolEnum } from '../../utils/index';
|
|
3
|
+
import LoggerService from '../logger';
|
|
4
4
|
export declare const controllerContextStorage: AsyncLocalStorage<{
|
|
5
5
|
protocol?: ProtocolEnum;
|
|
6
6
|
ctx?: any;
|
|
@@ -33,7 +33,8 @@ export interface CustomRequest {
|
|
|
33
33
|
body?: any;
|
|
34
34
|
params: Record<string, any>;
|
|
35
35
|
query: Record<string, any>;
|
|
36
|
-
|
|
36
|
+
lang: string;
|
|
37
|
+
t: (k: string, args?: any) => string;
|
|
37
38
|
}
|
|
38
39
|
export interface CustomResponse {
|
|
39
40
|
status: (code: number) => CustomResponse;
|
|
@@ -42,9 +43,9 @@ export interface CustomResponse {
|
|
|
42
43
|
locals: Record<string, any>;
|
|
43
44
|
}
|
|
44
45
|
declare abstract class Controller {
|
|
45
|
-
protected
|
|
46
|
+
protected logger: LoggerService;
|
|
46
47
|
protected supportedProtocols: ProtocolEnum[];
|
|
47
|
-
constructor(
|
|
48
|
+
constructor();
|
|
48
49
|
protected get protocol(): ProtocolEnum;
|
|
49
50
|
protected get currentStore(): {
|
|
50
51
|
protocol?: ProtocolEnum;
|
|
@@ -63,7 +64,6 @@ declare abstract class Controller {
|
|
|
63
64
|
protected parsedQuerys: (url: string) => Record<string, any>;
|
|
64
65
|
}
|
|
65
66
|
export declare abstract class ControllerService extends Controller {
|
|
66
|
-
constructor({ loggerService }: any);
|
|
67
67
|
load(props?: {
|
|
68
68
|
type?: ProtocolEnum | ProtocolEnum[];
|
|
69
69
|
}): Promise<void>;
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import qs from 'qs';
|
|
2
2
|
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
3
|
-
import { ProtocolEnum, responseMessageHelper, ResponseSchema } from '../../utils/index';
|
|
3
|
+
import { LoggerLayerEnum, ProtocolEnum, responseMessageHelper, ResponseSchema, x } from '../../utils/index';
|
|
4
|
+
import LoggerService from '../logger';
|
|
4
5
|
export const controllerContextStorage = new AsyncLocalStorage();
|
|
5
6
|
export const getControllerCtx = () => controllerContextStorage.getStore();
|
|
6
7
|
class Controller {
|
|
7
|
-
|
|
8
|
+
logger = x.get(LoggerService);
|
|
8
9
|
supportedProtocols = [ProtocolEnum.HTTP, ProtocolEnum.WEBSOCKET];
|
|
9
|
-
constructor(
|
|
10
|
-
this.loggerService = loggerService;
|
|
10
|
+
constructor() {
|
|
11
11
|
}
|
|
12
12
|
get protocol() {
|
|
13
13
|
return this.currentStore?.protocol || ProtocolEnum.HTTP;
|
|
@@ -19,7 +19,7 @@ class Controller {
|
|
|
19
19
|
const store = this.currentStore;
|
|
20
20
|
const identityT = (k) => k;
|
|
21
21
|
if (!store)
|
|
22
|
-
return { url: '', method: '', headers: {}, params: {}, query: {}, t: identityT };
|
|
22
|
+
return { url: '', method: '', headers: {}, params: {}, query: {}, lang: 'en', t: identityT };
|
|
23
23
|
const { ctx, req: nativeReq } = store;
|
|
24
24
|
const source = nativeReq || ctx?.request || ctx;
|
|
25
25
|
return {
|
|
@@ -32,6 +32,7 @@ class Controller {
|
|
|
32
32
|
params: ctx?.params || nativeReq?.params || source?.params || {},
|
|
33
33
|
query: ctx?.query || nativeReq?.query || source?.query || {},
|
|
34
34
|
accounts: ctx?.user || nativeReq?.accounts || source?.accounts,
|
|
35
|
+
lang: ctx?.lang || nativeReq?.lang || 'en',
|
|
35
36
|
t: ctx?.t || nativeReq?.t || identityT
|
|
36
37
|
};
|
|
37
38
|
}
|
|
@@ -88,9 +89,6 @@ class Controller {
|
|
|
88
89
|
};
|
|
89
90
|
}
|
|
90
91
|
export class ControllerService extends Controller {
|
|
91
|
-
constructor({ loggerService }) {
|
|
92
|
-
super({ loggerService });
|
|
93
|
-
}
|
|
94
92
|
async load(props = {}) {
|
|
95
93
|
if (props.type)
|
|
96
94
|
this.supportedProtocols = Array.isArray(props.type) ? props.type : [props.type];
|
|
@@ -117,7 +115,7 @@ export class ControllerService extends Controller {
|
|
|
117
115
|
const successObj = {
|
|
118
116
|
status: true,
|
|
119
117
|
message: Array.isArray(resResult.message)
|
|
120
|
-
? responseMessageHelper.successFully(resResult.message[0], resResult.message[1], req.
|
|
118
|
+
? responseMessageHelper.successFully(resResult.message[0], resResult.message[1], req.lang)
|
|
121
119
|
: resResult.message,
|
|
122
120
|
payload: logicResult?.payload !== undefined ? logicResult.payload : logicResult
|
|
123
121
|
};
|
|
@@ -126,7 +124,10 @@ export class ControllerService extends Controller {
|
|
|
126
124
|
return res.send(logicResult?.payload !== undefined ? logicResult.payload : logicResult);
|
|
127
125
|
}
|
|
128
126
|
catch (error) {
|
|
129
|
-
this.
|
|
127
|
+
this.logger.winston.log({
|
|
128
|
+
level: LoggerLayerEnum[LoggerLayerEnum.CRITICAL].toLowerCase(),
|
|
129
|
+
message: `Controller Error: ${error}`,
|
|
130
|
+
});
|
|
130
131
|
return this.res.status(500).send({
|
|
131
132
|
status: false,
|
|
132
133
|
message: error.message
|