@xrystal/core 3.14.9 → 3.15.2
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
|
@@ -5,7 +5,7 @@ export const controllerContextStorage = new AsyncLocalStorage();
|
|
|
5
5
|
export const getControllerCtx = () => controllerContextStorage.getStore();
|
|
6
6
|
class Controller {
|
|
7
7
|
loggerService;
|
|
8
|
-
supportedProtocols = [ProtocolEnum.HTTP];
|
|
8
|
+
supportedProtocols = [ProtocolEnum.HTTP, ProtocolEnum.WEBSOCKET];
|
|
9
9
|
constructor({ loggerService }) {
|
|
10
10
|
this.loggerService = loggerService;
|
|
11
11
|
}
|
|
@@ -15,15 +15,15 @@ class Controller {
|
|
|
15
15
|
get protocol() {
|
|
16
16
|
const incomingProtocol = this.currentStore?.protocol || ProtocolEnum.HTTP;
|
|
17
17
|
if (!this.supportedProtocols.includes(incomingProtocol)) {
|
|
18
|
-
throw new Error(`[DI] Protocol ${incomingProtocol}
|
|
18
|
+
throw new Error(`[DI] Protocol ${incomingProtocol} not supported. Supported: ${this.supportedProtocols.join(', ')}`);
|
|
19
19
|
}
|
|
20
20
|
return incomingProtocol;
|
|
21
21
|
}
|
|
22
22
|
get req() {
|
|
23
23
|
const store = this.currentStore;
|
|
24
|
-
const
|
|
24
|
+
const defaultT = (k) => k;
|
|
25
25
|
if (!store)
|
|
26
|
-
return {};
|
|
26
|
+
return { url: '', method: '', headers: {}, params: {}, query: {}, t: defaultT };
|
|
27
27
|
if (store.ctx) {
|
|
28
28
|
const { ctx } = store;
|
|
29
29
|
return {
|
|
@@ -34,25 +34,26 @@ class Controller {
|
|
|
34
34
|
params: ctx.params || {},
|
|
35
35
|
query: ctx.query || {},
|
|
36
36
|
accounts: ctx.user || ctx.accounts,
|
|
37
|
-
t: ctx.t
|
|
37
|
+
t: ctx.t || defaultT
|
|
38
38
|
};
|
|
39
39
|
}
|
|
40
|
+
const { req } = store;
|
|
40
41
|
return {
|
|
41
|
-
url:
|
|
42
|
-
method:
|
|
43
|
-
headers:
|
|
44
|
-
body:
|
|
45
|
-
params:
|
|
46
|
-
query:
|
|
47
|
-
accounts:
|
|
48
|
-
t:
|
|
42
|
+
url: req?.originalUrl || req?.url || '',
|
|
43
|
+
method: req?.method || 'GET',
|
|
44
|
+
headers: req?.headers || {},
|
|
45
|
+
body: req?.body,
|
|
46
|
+
params: req?.params || {},
|
|
47
|
+
query: req?.query || {},
|
|
48
|
+
accounts: req?.accounts,
|
|
49
|
+
t: req?.t || defaultT
|
|
49
50
|
};
|
|
50
51
|
}
|
|
51
52
|
get res() {
|
|
52
53
|
const store = this.currentStore;
|
|
53
|
-
const currentProtocol = this.protocol;
|
|
54
54
|
if (!store)
|
|
55
|
-
return {};
|
|
55
|
+
return { status: () => { }, send: () => { }, json: () => { }, locals: {} };
|
|
56
|
+
const currentProtocol = this.protocol;
|
|
56
57
|
if (store.ctx) {
|
|
57
58
|
return {
|
|
58
59
|
locals: {},
|
|
@@ -61,9 +62,10 @@ class Controller {
|
|
|
61
62
|
return this;
|
|
62
63
|
},
|
|
63
64
|
send(data) {
|
|
65
|
+
const plainData = data && typeof data === 'object' ? JSON.parse(JSON.stringify(data)) : data;
|
|
64
66
|
if (currentProtocol === ProtocolEnum.WEBSOCKET)
|
|
65
|
-
return
|
|
66
|
-
return new Response(JSON.stringify(
|
|
67
|
+
return plainData;
|
|
68
|
+
return new Response(JSON.stringify(plainData), {
|
|
67
69
|
status: this.locals._code || 200,
|
|
68
70
|
headers: { 'content-type': 'application/json' }
|
|
69
71
|
});
|
|
@@ -71,27 +73,30 @@ class Controller {
|
|
|
71
73
|
json(data) { return this.send(data); }
|
|
72
74
|
};
|
|
73
75
|
}
|
|
76
|
+
const { res } = store;
|
|
74
77
|
return {
|
|
75
|
-
locals:
|
|
78
|
+
locals: res?.locals || {},
|
|
76
79
|
status(code) {
|
|
77
|
-
if (
|
|
78
|
-
|
|
80
|
+
if (res?.status)
|
|
81
|
+
res.status(code);
|
|
79
82
|
return this;
|
|
80
83
|
},
|
|
81
84
|
send(data) {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
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;
|
|
85
89
|
},
|
|
86
90
|
json(data) {
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
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;
|
|
90
95
|
}
|
|
91
96
|
};
|
|
92
97
|
}
|
|
93
98
|
responseProtocolSwitch = async ({ res, resStatus = 200, context, req }) => {
|
|
94
|
-
const responseData = context({ localeLanguageConverter: req
|
|
99
|
+
const responseData = context({ localeLanguageConverter: req?.t });
|
|
95
100
|
return res.status(resStatus).send(responseData);
|
|
96
101
|
};
|
|
97
102
|
parsedQuerys = (url) => {
|
|
@@ -116,46 +121,38 @@ export class ControllerService extends Controller {
|
|
|
116
121
|
if (checks) {
|
|
117
122
|
const checkResult = await checks({ payload, convertedPayload });
|
|
118
123
|
if (checkResult?.message) {
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
res: currentRes,
|
|
122
|
-
context: () => new ResponseSchema(checkResult).getResponse
|
|
123
|
-
});
|
|
124
|
+
const errorObj = new ResponseSchema(checkResult).getResponse;
|
|
125
|
+
return await this.responseProtocolSwitch({ req: currentReq, res: currentRes, context: () => errorObj });
|
|
124
126
|
}
|
|
125
127
|
}
|
|
126
128
|
const logicResult = await logic({ payload, convertedPayload });
|
|
127
129
|
if (logicResult.response instanceof Function)
|
|
128
130
|
return logicResult.response(logicResult.payload);
|
|
129
131
|
if (logicResult.message) {
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
res: currentRes,
|
|
133
|
-
resStatus: 400,
|
|
134
|
-
context: () => new ResponseSchema(logicResult).getResponse
|
|
135
|
-
});
|
|
132
|
+
const logicErrorObj = new ResponseSchema(logicResult).getResponse;
|
|
133
|
+
return await this.responseProtocolSwitch({ req: currentReq, res: currentRes, resStatus: 400, context: () => logicErrorObj });
|
|
136
134
|
}
|
|
137
135
|
if (response) {
|
|
138
|
-
const resResult = await response({ payload, convertedPayload });
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
}).getResponse
|
|
148
|
-
});
|
|
136
|
+
const resResult = await response({ payload, convertedPayload, logicResult });
|
|
137
|
+
const successObj = new ResponseSchema({
|
|
138
|
+
status: true,
|
|
139
|
+
message: Array.isArray(resResult.message)
|
|
140
|
+
? responseMessageHelper.successFully(resResult.message[0], resResult.message[1], currentReq.t)
|
|
141
|
+
: resResult.message,
|
|
142
|
+
payload: logicResult.payload
|
|
143
|
+
}).getResponse;
|
|
144
|
+
return await this.responseProtocolSwitch({ req: currentReq, res: currentRes, resStatus: 200, context: () => successObj });
|
|
149
145
|
}
|
|
146
|
+
const finalPayload = logicResult.payload !== undefined ? logicResult.payload : logicResult;
|
|
147
|
+
return finalPayload && typeof finalPayload === 'object' ? JSON.parse(JSON.stringify(finalPayload)) : finalPayload;
|
|
150
148
|
}
|
|
151
149
|
catch (error) {
|
|
152
|
-
const
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
: { status: false, message: error.message };
|
|
150
|
+
const errorPayload = { status: false, message: error.message };
|
|
151
|
+
const storeRes = this.currentStore?.res || this.currentStore?.ctx?.set;
|
|
152
|
+
if (storeRes && typeof storeRes.status === 'function') {
|
|
153
|
+
return storeRes.status(500).send(errorPayload);
|
|
157
154
|
}
|
|
158
|
-
return
|
|
155
|
+
return errorPayload;
|
|
159
156
|
}
|
|
160
157
|
}
|
|
161
158
|
}
|