@xrystal/core 3.16.2 → 3.16.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
|
@@ -23,7 +23,7 @@ class Controller {
|
|
|
23
23
|
if (!store)
|
|
24
24
|
return { url: '', method: '', headers: {}, params: {}, query: {}, t: identityT };
|
|
25
25
|
const { ctx, req: nativeReq } = store;
|
|
26
|
-
const reqSource = nativeReq
|
|
26
|
+
const reqSource = nativeReq || ctx?.request || ctx?.data?.request;
|
|
27
27
|
return {
|
|
28
28
|
url: reqSource?.url || '',
|
|
29
29
|
method: reqSource?.method || 'GET',
|
|
@@ -34,22 +34,23 @@ class Controller {
|
|
|
34
34
|
params: ctx?.params || nativeReq?.params || {},
|
|
35
35
|
query: ctx?.query || nativeReq?.query || {},
|
|
36
36
|
accounts: ctx?.user || ctx?.data?.user || nativeReq?.accounts,
|
|
37
|
-
t: ctx?.t || nativeReq?.t || identityT
|
|
37
|
+
t: ctx?.t || ctx?.data?.t || nativeReq?.t || identityT
|
|
38
38
|
};
|
|
39
39
|
}
|
|
40
40
|
get res() {
|
|
41
41
|
const store = this.currentStore;
|
|
42
|
+
const fallbackRes = {
|
|
43
|
+
status: function () { return this; },
|
|
44
|
+
send: (d) => new Response(typeof d === 'string' ? d : JSON.stringify(d), { status: 500, headers: { 'content-type': 'application/json' } }),
|
|
45
|
+
json: function (d) { return this.send(d); },
|
|
46
|
+
locals: {}
|
|
47
|
+
};
|
|
42
48
|
if (!store)
|
|
43
|
-
return
|
|
44
|
-
status: function () { return this; },
|
|
45
|
-
send: (data) => new Response(JSON.stringify(data), { status: 500, headers: { 'content-type': 'application/json' } }),
|
|
46
|
-
json: function (data) { return this.send(data); },
|
|
47
|
-
locals: {}
|
|
48
|
-
};
|
|
49
|
-
const currentProtocol = this.protocol;
|
|
49
|
+
return fallbackRes;
|
|
50
50
|
if (store.ctx || store.req) {
|
|
51
51
|
if (!store.metadata)
|
|
52
52
|
store.metadata = { locals: {} };
|
|
53
|
+
const currentProtocol = this.protocol;
|
|
53
54
|
return {
|
|
54
55
|
locals: store.metadata.locals,
|
|
55
56
|
status(code) {
|
|
@@ -57,11 +58,12 @@ class Controller {
|
|
|
57
58
|
return this;
|
|
58
59
|
},
|
|
59
60
|
send: (data) => {
|
|
60
|
-
const isRaw = data instanceof Blob || data instanceof Uint8Array || store.metadata?._isRaw;
|
|
61
|
-
const contentType = store.metadata?._contentType || (isRaw ? 'application/octet-stream' : 'application/json');
|
|
62
61
|
if (currentProtocol === ProtocolEnum.WEBSOCKET)
|
|
63
62
|
return data;
|
|
64
|
-
const
|
|
63
|
+
const isBinary = data instanceof Blob || data instanceof Uint8Array || data instanceof ArrayBuffer;
|
|
64
|
+
const isRaw = store.metadata?._isRaw || isBinary;
|
|
65
|
+
const contentType = store.metadata?._contentType || (isBinary ? 'application/octet-stream' : 'application/json');
|
|
66
|
+
const body = isRaw ? data : JSON.stringify(data && typeof data === 'object' ? (data.getResponse || data) : data);
|
|
65
67
|
return new Response(body, {
|
|
66
68
|
status: store.metadata?._code || 200,
|
|
67
69
|
headers: { 'content-type': contentType }
|
|
@@ -70,7 +72,7 @@ class Controller {
|
|
|
70
72
|
json(data) { return this.send(data); }
|
|
71
73
|
};
|
|
72
74
|
}
|
|
73
|
-
return store.res;
|
|
75
|
+
return store.res || fallbackRes;
|
|
74
76
|
}
|
|
75
77
|
responseProtocolSwitch = async ({ res, resStatus = 200, context }) => {
|
|
76
78
|
const responseData = context({ localeLanguageConverter: this.req?.t });
|
|
@@ -106,9 +108,9 @@ export class ControllerService extends Controller {
|
|
|
106
108
|
}
|
|
107
109
|
}
|
|
108
110
|
const logicResult = await logic({ payload, convertedPayload });
|
|
109
|
-
if (logicResult
|
|
111
|
+
if (logicResult?.response instanceof Function)
|
|
110
112
|
return logicResult.response(logicResult.payload);
|
|
111
|
-
if (logicResult
|
|
113
|
+
if (logicResult?.message) {
|
|
112
114
|
return await this.responseProtocolSwitch({ res: currentRes, resStatus: 400, context: () => new ResponseSchema(logicResult).getResponse });
|
|
113
115
|
}
|
|
114
116
|
if (response) {
|
|
@@ -118,15 +120,14 @@ export class ControllerService extends Controller {
|
|
|
118
120
|
message: Array.isArray(resResult.message)
|
|
119
121
|
? responseMessageHelper.successFully(resResult.message[0], resResult.message[1], currentReq.t)
|
|
120
122
|
: resResult.message,
|
|
121
|
-
payload: logicResult
|
|
123
|
+
payload: logicResult?.payload || logicResult
|
|
122
124
|
};
|
|
123
125
|
return await this.responseProtocolSwitch({ res: currentRes, resStatus: 200, context: () => new ResponseSchema(successObj).getResponse });
|
|
124
126
|
}
|
|
125
|
-
|
|
126
|
-
return finalData && typeof finalData === 'object' ? JSON.parse(JSON.stringify(finalData)) : finalData;
|
|
127
|
+
return currentRes.send(logicResult?.payload !== undefined ? logicResult.payload : logicResult);
|
|
127
128
|
}
|
|
128
129
|
catch (error) {
|
|
129
|
-
return { status: false, message: error.message };
|
|
130
|
+
return this.res.status(500).send({ status: false, message: error.message });
|
|
130
131
|
}
|
|
131
132
|
}
|
|
132
133
|
}
|