@xrystal/core 3.23.0 → 3.23.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
|
@@ -31,7 +31,13 @@ export class BaseController {
|
|
|
31
31
|
get res() {
|
|
32
32
|
const store = this.currentStore;
|
|
33
33
|
const self = this;
|
|
34
|
-
const fallbackRes = {
|
|
34
|
+
const fallbackRes = {
|
|
35
|
+
status: function () { return this; },
|
|
36
|
+
send: (d) => d,
|
|
37
|
+
json: function (d) { return this.send(d); },
|
|
38
|
+
setRaw: function () { return this; },
|
|
39
|
+
locals: {}
|
|
40
|
+
};
|
|
35
41
|
if (!store)
|
|
36
42
|
return fallbackRes;
|
|
37
43
|
if (!store.metadata)
|
|
@@ -42,13 +48,22 @@ export class BaseController {
|
|
|
42
48
|
store.metadata._code = code;
|
|
43
49
|
return this;
|
|
44
50
|
},
|
|
51
|
+
setRaw(isRaw = true) {
|
|
52
|
+
store.metadata._isRaw = isRaw;
|
|
53
|
+
return this;
|
|
54
|
+
},
|
|
45
55
|
send(data) {
|
|
46
56
|
if (self.protocol === ProtocolEnum.WEBSOCKET)
|
|
47
57
|
return data;
|
|
48
58
|
if (data instanceof Response)
|
|
49
59
|
return data;
|
|
50
60
|
const status = store.metadata?._code || 200;
|
|
51
|
-
const
|
|
61
|
+
const isRaw = store.metadata?._isRaw;
|
|
62
|
+
if (isRaw) {
|
|
63
|
+
const contentType = store.metadata?._contentType || (typeof data === 'string' ? 'text/plain' : 'application/octet-stream');
|
|
64
|
+
return new Response(data, { status, headers: { 'content-type': contentType } });
|
|
65
|
+
}
|
|
66
|
+
const body = JSON.stringify(data?.getResponse ? data.getResponse : data);
|
|
52
67
|
return new Response(body, { status, headers: { 'content-type': 'application/json' } });
|
|
53
68
|
},
|
|
54
69
|
json(data) { return this.send(data); }
|
|
@@ -100,27 +115,15 @@ export default class Controller extends BaseController {
|
|
|
100
115
|
const isObj = checkRes && typeof checkRes === 'object';
|
|
101
116
|
const code = isObj ? checkRes.code || 400 : 400;
|
|
102
117
|
const rawMsg = isObj ? checkRes.message : (typeof checkRes === 'string' ? checkRes : '@schemaMissingDataChecks');
|
|
103
|
-
const { message, status, code: _c, ...rest } = isObj ? checkRes : {};
|
|
104
|
-
|
|
105
|
-
let errExtras = { ...rest };
|
|
106
|
-
if (rest.data !== undefined || rest.payload !== undefined) {
|
|
107
|
-
delete errExtras.data;
|
|
108
|
-
delete errExtras.payload;
|
|
109
|
-
}
|
|
110
|
-
else {
|
|
111
|
-
errCoreData = Object.keys(rest).length > 0 ? rest : undefined;
|
|
112
|
-
errExtras = {};
|
|
113
|
-
}
|
|
118
|
+
const { message: _m, status: _s, code: _c, data, payload, ...rest } = isObj ? checkRes : {};
|
|
119
|
+
const errCoreData = data ?? payload ?? (Object.keys(rest).length > 0 ? rest : undefined);
|
|
114
120
|
const errRes = {
|
|
115
121
|
status: false,
|
|
116
122
|
message: this.parseMessage(rawMsg, t),
|
|
117
123
|
code
|
|
118
124
|
};
|
|
119
|
-
if (errCoreData !== undefined
|
|
120
|
-
errRes.payload = {
|
|
121
|
-
...(errCoreData !== undefined && { data: errCoreData }),
|
|
122
|
-
...(Object.keys(errExtras).length > 0 && { extraData: errExtras })
|
|
123
|
-
};
|
|
125
|
+
if (errCoreData !== undefined) {
|
|
126
|
+
errRes.payload = { data: errCoreData };
|
|
124
127
|
}
|
|
125
128
|
return this.res.status(code).send(new ResponseSchema(errRes).getResponse);
|
|
126
129
|
}
|
|
@@ -132,43 +135,41 @@ export default class Controller extends BaseController {
|
|
|
132
135
|
let finalRes = response ? await response({ ...p, logicResult: logicRes }) : logicRes;
|
|
133
136
|
if (typeof finalRes === 'function')
|
|
134
137
|
finalRes = await finalRes(p);
|
|
135
|
-
if (Array.isArray(finalRes)) {
|
|
136
|
-
finalRes = { message: `@successfully ${finalRes.join(' ')}`, data: logicRes };
|
|
137
|
-
}
|
|
138
|
-
else if (typeof finalRes === 'string' && !finalRes.startsWith('@')) {
|
|
139
|
-
finalRes = { message: `@successfully ${finalRes}`, data: logicRes };
|
|
140
|
-
}
|
|
141
138
|
if (finalRes instanceof Response || finalRes?.constructor?.name === 'Response')
|
|
142
139
|
return finalRes;
|
|
140
|
+
if (store?.metadata?._isRaw)
|
|
141
|
+
return this.res.send(finalRes);
|
|
143
142
|
const isSuccess = finalRes?.status !== false;
|
|
144
143
|
const finalCode = finalRes?.code || store?.metadata?._code || 200;
|
|
145
144
|
const msgSource = finalRes?.message || (typeof finalRes === 'string' ? finalRes : '@successfully');
|
|
146
|
-
const rawOutput = (finalRes && typeof finalRes === 'object' && !Array.isArray(finalRes))
|
|
147
|
-
? finalRes
|
|
148
|
-
: { data: finalRes || logicRes };
|
|
149
|
-
const { message: _m, status: _s, code: _co, data, payload, ...restExtras } = rawOutput;
|
|
150
145
|
let coreData;
|
|
151
|
-
let extraData;
|
|
152
|
-
if (
|
|
153
|
-
|
|
154
|
-
|
|
146
|
+
let extraData = {};
|
|
147
|
+
if (finalRes && typeof finalRes === 'object' && !Array.isArray(finalRes)) {
|
|
148
|
+
const { message: _m, status: _s, code: _co, data, payload, ...rest } = finalRes;
|
|
149
|
+
coreData = data ?? payload;
|
|
150
|
+
if (coreData === undefined) {
|
|
151
|
+
coreData = Object.keys(rest).length > 0 ? rest : logicRes;
|
|
152
|
+
}
|
|
153
|
+
else {
|
|
154
|
+
extraData = rest;
|
|
155
|
+
}
|
|
155
156
|
}
|
|
156
157
|
else {
|
|
157
|
-
coreData =
|
|
158
|
-
extraData = {};
|
|
158
|
+
coreData = finalRes ?? logicRes;
|
|
159
159
|
}
|
|
160
160
|
const responseData = {
|
|
161
161
|
status: isSuccess,
|
|
162
|
-
message: this.parseMessage(msgSource, t)
|
|
162
|
+
message: this.parseMessage(msgSource, t),
|
|
163
|
+
code: finalCode
|
|
163
164
|
};
|
|
164
|
-
const
|
|
165
|
+
const payloadContainer = {};
|
|
165
166
|
if (coreData !== undefined && coreData !== null)
|
|
166
|
-
|
|
167
|
+
payloadContainer.data = coreData;
|
|
167
168
|
if (Object.keys(extraData).length > 0)
|
|
168
|
-
|
|
169
|
-
if (Object.keys(
|
|
170
|
-
responseData.payload =
|
|
171
|
-
|
|
169
|
+
payloadContainer.extraData = extraData;
|
|
170
|
+
if (Object.keys(payloadContainer).length > 0) {
|
|
171
|
+
responseData.payload = payloadContainer;
|
|
172
|
+
}
|
|
172
173
|
return this.res.status(finalCode).send(new ResponseSchema(responseData).getResponse);
|
|
173
174
|
}
|
|
174
175
|
catch (error) {
|