@xrystal/core 3.15.2 → 3.15.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
|
@@ -41,7 +41,7 @@ declare abstract class Controller {
|
|
|
41
41
|
protected get protocol(): ProtocolEnum;
|
|
42
42
|
protected get req(): CustomRequest;
|
|
43
43
|
protected get res(): CustomResponse;
|
|
44
|
-
protected responseProtocolSwitch: ({ res, resStatus, context
|
|
44
|
+
protected responseProtocolSwitch: ({ res, resStatus, context }: any) => Promise<any>;
|
|
45
45
|
protected parsedQuerys: (url: string) => Record<string, any>;
|
|
46
46
|
}
|
|
47
47
|
export declare abstract class ControllerService extends Controller {
|
|
@@ -9,9 +9,7 @@ class Controller {
|
|
|
9
9
|
constructor({ loggerService }) {
|
|
10
10
|
this.loggerService = loggerService;
|
|
11
11
|
}
|
|
12
|
-
get currentStore() {
|
|
13
|
-
return getControllerCtx();
|
|
14
|
-
}
|
|
12
|
+
get currentStore() { return getControllerCtx(); }
|
|
15
13
|
get protocol() {
|
|
16
14
|
const incomingProtocol = this.currentStore?.protocol || ProtocolEnum.HTTP;
|
|
17
15
|
if (!this.supportedProtocols.includes(incomingProtocol)) {
|
|
@@ -21,23 +19,23 @@ class Controller {
|
|
|
21
19
|
}
|
|
22
20
|
get req() {
|
|
23
21
|
const store = this.currentStore;
|
|
24
|
-
const
|
|
22
|
+
const identityT = (k) => k;
|
|
25
23
|
if (!store)
|
|
26
|
-
return { url: '', method: '', headers: {}, params: {}, query: {}, t:
|
|
27
|
-
|
|
28
|
-
|
|
24
|
+
return { url: '', method: '', headers: {}, params: {}, query: {}, t: identityT };
|
|
25
|
+
const { ctx, req } = store;
|
|
26
|
+
if (ctx) {
|
|
27
|
+
const request = ctx.request || (ctx.data?.request);
|
|
29
28
|
return {
|
|
30
|
-
url:
|
|
31
|
-
method:
|
|
29
|
+
url: request?.url || '',
|
|
30
|
+
method: request?.method || '',
|
|
32
31
|
headers: ctx.headers || {},
|
|
33
32
|
body: ctx.body,
|
|
34
33
|
params: ctx.params || {},
|
|
35
34
|
query: ctx.query || {},
|
|
36
|
-
accounts: ctx.user || ctx.accounts,
|
|
37
|
-
t: ctx.t ||
|
|
35
|
+
accounts: ctx.user || ctx.data?.user || ctx.accounts,
|
|
36
|
+
t: ctx.t || identityT
|
|
38
37
|
};
|
|
39
38
|
}
|
|
40
|
-
const { req } = store;
|
|
41
39
|
return {
|
|
42
40
|
url: req?.originalUrl || req?.url || '',
|
|
43
41
|
method: req?.method || 'GET',
|
|
@@ -46,21 +44,18 @@ class Controller {
|
|
|
46
44
|
params: req?.params || {},
|
|
47
45
|
query: req?.query || {},
|
|
48
46
|
accounts: req?.accounts,
|
|
49
|
-
t: req?.t ||
|
|
47
|
+
t: req?.t || identityT
|
|
50
48
|
};
|
|
51
49
|
}
|
|
52
50
|
get res() {
|
|
53
51
|
const store = this.currentStore;
|
|
54
52
|
if (!store)
|
|
55
53
|
return { status: () => { }, send: () => { }, json: () => { }, locals: {} };
|
|
56
|
-
const currentProtocol = this.
|
|
54
|
+
const currentProtocol = this.controllerType || ProtocolEnum.HTTP;
|
|
57
55
|
if (store.ctx) {
|
|
58
56
|
return {
|
|
59
57
|
locals: {},
|
|
60
|
-
status(code) {
|
|
61
|
-
this.locals._code = code;
|
|
62
|
-
return this;
|
|
63
|
-
},
|
|
58
|
+
status(code) { this.locals._code = code; return this; },
|
|
64
59
|
send(data) {
|
|
65
60
|
const plainData = data && typeof data === 'object' ? JSON.parse(JSON.stringify(data)) : data;
|
|
66
61
|
if (currentProtocol === ProtocolEnum.WEBSOCKET)
|
|
@@ -73,30 +68,10 @@ class Controller {
|
|
|
73
68
|
json(data) { return this.send(data); }
|
|
74
69
|
};
|
|
75
70
|
}
|
|
76
|
-
|
|
77
|
-
return {
|
|
78
|
-
locals: res?.locals || {},
|
|
79
|
-
status(code) {
|
|
80
|
-
if (res?.status)
|
|
81
|
-
res.status(code);
|
|
82
|
-
return this;
|
|
83
|
-
},
|
|
84
|
-
send(data) {
|
|
85
|
-
const plainData = data && typeof data === 'object' ? JSON.parse(JSON.stringify(data)) : data;
|
|
86
|
-
if (res?.send)
|
|
87
|
-
return res.send(plainData);
|
|
88
|
-
return plainData;
|
|
89
|
-
},
|
|
90
|
-
json(data) {
|
|
91
|
-
const plainData = data && typeof data === 'object' ? JSON.parse(JSON.stringify(data)) : data;
|
|
92
|
-
if (res?.json)
|
|
93
|
-
return res.json(plainData);
|
|
94
|
-
return plainData;
|
|
95
|
-
}
|
|
96
|
-
};
|
|
71
|
+
return store.res;
|
|
97
72
|
}
|
|
98
|
-
responseProtocolSwitch = async ({ res, resStatus = 200, context
|
|
99
|
-
const responseData = context({ localeLanguageConverter: req?.t });
|
|
73
|
+
responseProtocolSwitch = async ({ res, resStatus = 200, context }) => {
|
|
74
|
+
const responseData = context({ localeLanguageConverter: this.req?.t });
|
|
100
75
|
return res.status(resStatus).send(responseData);
|
|
101
76
|
};
|
|
102
77
|
parsedQuerys = (url) => {
|
|
@@ -122,7 +97,7 @@ export class ControllerService extends Controller {
|
|
|
122
97
|
const checkResult = await checks({ payload, convertedPayload });
|
|
123
98
|
if (checkResult?.message) {
|
|
124
99
|
const errorObj = new ResponseSchema(checkResult).getResponse;
|
|
125
|
-
return await this.responseProtocolSwitch({
|
|
100
|
+
return await this.responseProtocolSwitch({ res: currentRes, context: () => errorObj });
|
|
126
101
|
}
|
|
127
102
|
}
|
|
128
103
|
const logicResult = await logic({ payload, convertedPayload });
|
|
@@ -130,7 +105,7 @@ export class ControllerService extends Controller {
|
|
|
130
105
|
return logicResult.response(logicResult.payload);
|
|
131
106
|
if (logicResult.message) {
|
|
132
107
|
const logicErrorObj = new ResponseSchema(logicResult).getResponse;
|
|
133
|
-
return await this.responseProtocolSwitch({
|
|
108
|
+
return await this.responseProtocolSwitch({ res: currentRes, resStatus: 400, context: () => logicErrorObj });
|
|
134
109
|
}
|
|
135
110
|
if (response) {
|
|
136
111
|
const resResult = await response({ payload, convertedPayload, logicResult });
|
|
@@ -141,7 +116,7 @@ export class ControllerService extends Controller {
|
|
|
141
116
|
: resResult.message,
|
|
142
117
|
payload: logicResult.payload
|
|
143
118
|
}).getResponse;
|
|
144
|
-
return await this.responseProtocolSwitch({
|
|
119
|
+
return await this.responseProtocolSwitch({ res: currentRes, resStatus: 200, context: () => successObj });
|
|
145
120
|
}
|
|
146
121
|
const finalPayload = logicResult.payload !== undefined ? logicResult.payload : logicResult;
|
|
147
122
|
return finalPayload && typeof finalPayload === 'object' ? JSON.parse(JSON.stringify(finalPayload)) : finalPayload;
|