@xrystal/core 3.15.3 → 3.15.7
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
|
@@ -13,7 +13,7 @@ class Controller {
|
|
|
13
13
|
get protocol() {
|
|
14
14
|
const incomingProtocol = this.currentStore?.protocol || ProtocolEnum.HTTP;
|
|
15
15
|
if (!this.supportedProtocols.includes(incomingProtocol)) {
|
|
16
|
-
throw new Error(`[DI] Protocol ${incomingProtocol} not supported
|
|
16
|
+
throw new Error(`[DI] Protocol ${incomingProtocol} not supported.`);
|
|
17
17
|
}
|
|
18
18
|
return incomingProtocol;
|
|
19
19
|
}
|
|
@@ -22,9 +22,9 @@ class Controller {
|
|
|
22
22
|
const identityT = (k) => k;
|
|
23
23
|
if (!store)
|
|
24
24
|
return { url: '', method: '', headers: {}, params: {}, query: {}, t: identityT };
|
|
25
|
-
const
|
|
25
|
+
const ctx = store.ctx;
|
|
26
26
|
if (ctx) {
|
|
27
|
-
const request = ctx.request ||
|
|
27
|
+
const request = ctx.request || ctx.data?.request;
|
|
28
28
|
return {
|
|
29
29
|
url: request?.url || '',
|
|
30
30
|
method: request?.method || '',
|
|
@@ -33,9 +33,10 @@ class Controller {
|
|
|
33
33
|
params: ctx.params || {},
|
|
34
34
|
query: ctx.query || {},
|
|
35
35
|
accounts: ctx.user || ctx.data?.user || ctx.accounts,
|
|
36
|
-
t: ctx.t || identityT
|
|
36
|
+
t: ctx.t || ctx.data?.t || identityT
|
|
37
37
|
};
|
|
38
38
|
}
|
|
39
|
+
const req = store.req;
|
|
39
40
|
return {
|
|
40
41
|
url: req?.originalUrl || req?.url || '',
|
|
41
42
|
method: req?.method || 'GET',
|
|
@@ -51,7 +52,7 @@ class Controller {
|
|
|
51
52
|
const store = this.currentStore;
|
|
52
53
|
if (!store)
|
|
53
54
|
return { status: () => { }, send: () => { }, json: () => { }, locals: {} };
|
|
54
|
-
const currentProtocol = this.
|
|
55
|
+
const currentProtocol = this.protocol;
|
|
55
56
|
if (store.ctx) {
|
|
56
57
|
return {
|
|
57
58
|
locals: {},
|
|
@@ -68,7 +69,16 @@ class Controller {
|
|
|
68
69
|
json(data) { return this.send(data); }
|
|
69
70
|
};
|
|
70
71
|
}
|
|
71
|
-
return
|
|
72
|
+
return {
|
|
73
|
+
locals: store.res?.locals || {},
|
|
74
|
+
status(code) { if (store.res?.status)
|
|
75
|
+
store.res.status(code); return this; },
|
|
76
|
+
send(data) {
|
|
77
|
+
const plainData = data && typeof data === 'object' ? JSON.parse(JSON.stringify(data)) : data;
|
|
78
|
+
return store.res?.send ? store.res.send(plainData) : plainData;
|
|
79
|
+
},
|
|
80
|
+
json(data) { return this.send(data); }
|
|
81
|
+
};
|
|
72
82
|
}
|
|
73
83
|
responseProtocolSwitch = async ({ res, resStatus = 200, context }) => {
|
|
74
84
|
const responseData = context({ localeLanguageConverter: this.req?.t });
|
|
@@ -81,53 +91,42 @@ class Controller {
|
|
|
81
91
|
}
|
|
82
92
|
export class ControllerService extends Controller {
|
|
83
93
|
async load(props = {}) {
|
|
84
|
-
if (props.type)
|
|
94
|
+
if (props.type)
|
|
85
95
|
this.supportedProtocols = Array.isArray(props.type) ? props.type : [props.type];
|
|
86
|
-
}
|
|
87
96
|
}
|
|
88
97
|
async schema({ checks, logic, response }) {
|
|
89
98
|
try {
|
|
90
99
|
const currentReq = this.req;
|
|
91
100
|
const currentRes = this.res;
|
|
92
|
-
if (!currentReq || !currentRes)
|
|
93
|
-
return;
|
|
94
101
|
const payload = { req: currentReq, res: currentRes };
|
|
95
102
|
const convertedPayload = { ...payload, parsedQuerys: this.parsedQuerys(currentReq.url) };
|
|
96
103
|
if (checks) {
|
|
97
104
|
const checkResult = await checks({ payload, convertedPayload });
|
|
98
105
|
if (checkResult?.message) {
|
|
99
|
-
|
|
100
|
-
return await this.responseProtocolSwitch({ res: currentRes, context: () => errorObj });
|
|
106
|
+
return await this.responseProtocolSwitch({ res: currentRes, context: () => new ResponseSchema(checkResult).getResponse });
|
|
101
107
|
}
|
|
102
108
|
}
|
|
103
109
|
const logicResult = await logic({ payload, convertedPayload });
|
|
104
110
|
if (logicResult.response instanceof Function)
|
|
105
111
|
return logicResult.response(logicResult.payload);
|
|
106
112
|
if (logicResult.message) {
|
|
107
|
-
|
|
108
|
-
return await this.responseProtocolSwitch({ res: currentRes, resStatus: 400, context: () => logicErrorObj });
|
|
113
|
+
return await this.responseProtocolSwitch({ res: currentRes, resStatus: 400, context: () => new ResponseSchema(logicResult).getResponse });
|
|
109
114
|
}
|
|
110
115
|
if (response) {
|
|
111
116
|
const resResult = await response({ payload, convertedPayload, logicResult });
|
|
112
|
-
const successObj =
|
|
117
|
+
const successObj = {
|
|
113
118
|
status: true,
|
|
114
119
|
message: Array.isArray(resResult.message)
|
|
115
120
|
? responseMessageHelper.successFully(resResult.message[0], resResult.message[1], currentReq.t)
|
|
116
121
|
: resResult.message,
|
|
117
122
|
payload: logicResult.payload
|
|
118
|
-
}
|
|
119
|
-
return await this.responseProtocolSwitch({ res: currentRes, resStatus: 200, context: () => successObj });
|
|
123
|
+
};
|
|
124
|
+
return await this.responseProtocolSwitch({ res: currentRes, resStatus: 200, context: () => new ResponseSchema(successObj).getResponse });
|
|
120
125
|
}
|
|
121
|
-
|
|
122
|
-
return finalPayload && typeof finalPayload === 'object' ? JSON.parse(JSON.stringify(finalPayload)) : finalPayload;
|
|
126
|
+
return JSON.parse(JSON.stringify(logicResult.payload !== undefined ? logicResult.payload : logicResult));
|
|
123
127
|
}
|
|
124
128
|
catch (error) {
|
|
125
|
-
|
|
126
|
-
const storeRes = this.currentStore?.res || this.currentStore?.ctx?.set;
|
|
127
|
-
if (storeRes && typeof storeRes.status === 'function') {
|
|
128
|
-
return storeRes.status(500).send(errorPayload);
|
|
129
|
-
}
|
|
130
|
-
return errorPayload;
|
|
129
|
+
return { status: false, message: error.message };
|
|
131
130
|
}
|
|
132
131
|
}
|
|
133
132
|
}
|