@xrystal/core 3.14.9 → 3.15.1
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,13 +15,12 @@ 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 currentProtocol = this.protocol;
|
|
25
24
|
if (!store)
|
|
26
25
|
return {};
|
|
27
26
|
if (store.ctx) {
|
|
@@ -50,9 +49,9 @@ class Controller {
|
|
|
50
49
|
}
|
|
51
50
|
get res() {
|
|
52
51
|
const store = this.currentStore;
|
|
53
|
-
const currentProtocol = this.protocol;
|
|
54
52
|
if (!store)
|
|
55
53
|
return {};
|
|
54
|
+
const currentProtocol = this.protocol;
|
|
56
55
|
if (store.ctx) {
|
|
57
56
|
return {
|
|
58
57
|
locals: {},
|
|
@@ -61,9 +60,10 @@ class Controller {
|
|
|
61
60
|
return this;
|
|
62
61
|
},
|
|
63
62
|
send(data) {
|
|
63
|
+
const plainData = JSON.parse(JSON.stringify(data));
|
|
64
64
|
if (currentProtocol === ProtocolEnum.WEBSOCKET)
|
|
65
|
-
return
|
|
66
|
-
return new Response(JSON.stringify(
|
|
65
|
+
return plainData;
|
|
66
|
+
return new Response(JSON.stringify(plainData), {
|
|
67
67
|
status: this.locals._code || 200,
|
|
68
68
|
headers: { 'content-type': 'application/json' }
|
|
69
69
|
});
|
|
@@ -116,46 +116,37 @@ export class ControllerService extends Controller {
|
|
|
116
116
|
if (checks) {
|
|
117
117
|
const checkResult = await checks({ payload, convertedPayload });
|
|
118
118
|
if (checkResult?.message) {
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
res: currentRes,
|
|
122
|
-
context: () => new ResponseSchema(checkResult).getResponse
|
|
123
|
-
});
|
|
119
|
+
const errorObj = new ResponseSchema(checkResult).getResponse;
|
|
120
|
+
return await this.responseProtocolSwitch({ req: currentReq, res: currentRes, context: () => errorObj });
|
|
124
121
|
}
|
|
125
122
|
}
|
|
126
123
|
const logicResult = await logic({ payload, convertedPayload });
|
|
127
124
|
if (logicResult.response instanceof Function)
|
|
128
125
|
return logicResult.response(logicResult.payload);
|
|
129
126
|
if (logicResult.message) {
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
res: currentRes,
|
|
133
|
-
resStatus: 400,
|
|
134
|
-
context: () => new ResponseSchema(logicResult).getResponse
|
|
135
|
-
});
|
|
127
|
+
const logicErrorObj = new ResponseSchema(logicResult).getResponse;
|
|
128
|
+
return await this.responseProtocolSwitch({ req: currentReq, res: currentRes, resStatus: 400, context: () => logicErrorObj });
|
|
136
129
|
}
|
|
137
130
|
if (response) {
|
|
138
|
-
const resResult = await response({ payload, convertedPayload });
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
}).getResponse
|
|
148
|
-
});
|
|
131
|
+
const resResult = await response({ payload, convertedPayload, logicResult });
|
|
132
|
+
const successObj = new ResponseSchema({
|
|
133
|
+
status: true,
|
|
134
|
+
message: Array.isArray(resResult.message)
|
|
135
|
+
? responseMessageHelper.successFully(resResult.message[0], resResult.message[1], currentReq.t)
|
|
136
|
+
: resResult.message,
|
|
137
|
+
payload: logicResult.payload
|
|
138
|
+
}).getResponse;
|
|
139
|
+
return await this.responseProtocolSwitch({ req: currentReq, res: currentRes, resStatus: 200, context: () => successObj });
|
|
149
140
|
}
|
|
141
|
+
return JSON.parse(JSON.stringify(logicResult.payload || logicResult));
|
|
150
142
|
}
|
|
151
143
|
catch (error) {
|
|
152
|
-
const
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
: { status: false, message: error.message };
|
|
144
|
+
const errorPayload = { status: false, message: error.message };
|
|
145
|
+
const storeRes = this.currentStore?.res || this.currentStore?.ctx?.set;
|
|
146
|
+
if (storeRes && typeof storeRes.status === 'function') {
|
|
147
|
+
return storeRes.status(500).send(errorPayload);
|
|
157
148
|
}
|
|
158
|
-
return
|
|
149
|
+
return errorPayload;
|
|
159
150
|
}
|
|
160
151
|
}
|
|
161
152
|
}
|