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