@xrystal/core 3.15.1 → 3.15.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
|
@@ -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,9 +9,7 @@ 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)) {
|
|
@@ -21,46 +19,45 @@ class Controller {
|
|
|
21
19
|
}
|
|
22
20
|
get req() {
|
|
23
21
|
const store = this.currentStore;
|
|
22
|
+
const identityT = (k) => k;
|
|
24
23
|
if (!store)
|
|
25
|
-
return {};
|
|
26
|
-
|
|
27
|
-
|
|
24
|
+
return { url: '', method: '', headers: {}, params: {}, query: {}, t: identityT };
|
|
25
|
+
const { ctx, req } = store;
|
|
26
|
+
if (ctx) {
|
|
27
|
+
const request = ctx.request || (ctx.data?.request);
|
|
28
28
|
return {
|
|
29
|
-
url:
|
|
30
|
-
method:
|
|
29
|
+
url: request?.url || '',
|
|
30
|
+
method: request?.method || '',
|
|
31
31
|
headers: ctx.headers || {},
|
|
32
32
|
body: ctx.body,
|
|
33
33
|
params: ctx.params || {},
|
|
34
34
|
query: ctx.query || {},
|
|
35
|
-
accounts: ctx.user || ctx.accounts,
|
|
36
|
-
t: ctx.t
|
|
35
|
+
accounts: ctx.user || ctx.data?.user || ctx.accounts,
|
|
36
|
+
t: ctx.t || identityT
|
|
37
37
|
};
|
|
38
38
|
}
|
|
39
39
|
return {
|
|
40
|
-
url:
|
|
41
|
-
method:
|
|
42
|
-
headers:
|
|
43
|
-
body:
|
|
44
|
-
params:
|
|
45
|
-
query:
|
|
46
|
-
accounts:
|
|
47
|
-
t:
|
|
40
|
+
url: req?.originalUrl || req?.url || '',
|
|
41
|
+
method: req?.method || 'GET',
|
|
42
|
+
headers: req?.headers || {},
|
|
43
|
+
body: req?.body,
|
|
44
|
+
params: req?.params || {},
|
|
45
|
+
query: req?.query || {},
|
|
46
|
+
accounts: req?.accounts,
|
|
47
|
+
t: req?.t || identityT
|
|
48
48
|
};
|
|
49
49
|
}
|
|
50
50
|
get res() {
|
|
51
51
|
const store = this.currentStore;
|
|
52
52
|
if (!store)
|
|
53
|
-
return {};
|
|
54
|
-
const currentProtocol = this.
|
|
53
|
+
return { status: () => { }, send: () => { }, json: () => { }, locals: {} };
|
|
54
|
+
const currentProtocol = this.controllerType || ProtocolEnum.HTTP;
|
|
55
55
|
if (store.ctx) {
|
|
56
56
|
return {
|
|
57
57
|
locals: {},
|
|
58
|
-
status(code) {
|
|
59
|
-
this.locals._code = code;
|
|
60
|
-
return this;
|
|
61
|
-
},
|
|
58
|
+
status(code) { this.locals._code = code; return this; },
|
|
62
59
|
send(data) {
|
|
63
|
-
const plainData = JSON.parse(JSON.stringify(data));
|
|
60
|
+
const plainData = data && typeof data === 'object' ? JSON.parse(JSON.stringify(data)) : data;
|
|
64
61
|
if (currentProtocol === ProtocolEnum.WEBSOCKET)
|
|
65
62
|
return plainData;
|
|
66
63
|
return new Response(JSON.stringify(plainData), {
|
|
@@ -71,27 +68,10 @@ class Controller {
|
|
|
71
68
|
json(data) { return this.send(data); }
|
|
72
69
|
};
|
|
73
70
|
}
|
|
74
|
-
return
|
|
75
|
-
locals: store.res?.locals || {},
|
|
76
|
-
status(code) {
|
|
77
|
-
if (store.res?.status)
|
|
78
|
-
store.res.status(code);
|
|
79
|
-
return this;
|
|
80
|
-
},
|
|
81
|
-
send(data) {
|
|
82
|
-
if (store.res?.send)
|
|
83
|
-
return store.res.send(data);
|
|
84
|
-
return data;
|
|
85
|
-
},
|
|
86
|
-
json(data) {
|
|
87
|
-
if (store.res?.json)
|
|
88
|
-
return store.res.json(data);
|
|
89
|
-
return data;
|
|
90
|
-
}
|
|
91
|
-
};
|
|
71
|
+
return store.res;
|
|
92
72
|
}
|
|
93
|
-
responseProtocolSwitch = async ({ res, resStatus = 200, context
|
|
94
|
-
const responseData = context({ localeLanguageConverter: req
|
|
73
|
+
responseProtocolSwitch = async ({ res, resStatus = 200, context }) => {
|
|
74
|
+
const responseData = context({ localeLanguageConverter: this.req?.t });
|
|
95
75
|
return res.status(resStatus).send(responseData);
|
|
96
76
|
};
|
|
97
77
|
parsedQuerys = (url) => {
|
|
@@ -117,7 +97,7 @@ export class ControllerService extends Controller {
|
|
|
117
97
|
const checkResult = await checks({ payload, convertedPayload });
|
|
118
98
|
if (checkResult?.message) {
|
|
119
99
|
const errorObj = new ResponseSchema(checkResult).getResponse;
|
|
120
|
-
return await this.responseProtocolSwitch({
|
|
100
|
+
return await this.responseProtocolSwitch({ res: currentRes, context: () => errorObj });
|
|
121
101
|
}
|
|
122
102
|
}
|
|
123
103
|
const logicResult = await logic({ payload, convertedPayload });
|
|
@@ -125,7 +105,7 @@ export class ControllerService extends Controller {
|
|
|
125
105
|
return logicResult.response(logicResult.payload);
|
|
126
106
|
if (logicResult.message) {
|
|
127
107
|
const logicErrorObj = new ResponseSchema(logicResult).getResponse;
|
|
128
|
-
return await this.responseProtocolSwitch({
|
|
108
|
+
return await this.responseProtocolSwitch({ res: currentRes, resStatus: 400, context: () => logicErrorObj });
|
|
129
109
|
}
|
|
130
110
|
if (response) {
|
|
131
111
|
const resResult = await response({ payload, convertedPayload, logicResult });
|
|
@@ -136,9 +116,10 @@ export class ControllerService extends Controller {
|
|
|
136
116
|
: resResult.message,
|
|
137
117
|
payload: logicResult.payload
|
|
138
118
|
}).getResponse;
|
|
139
|
-
return await this.responseProtocolSwitch({
|
|
119
|
+
return await this.responseProtocolSwitch({ res: currentRes, resStatus: 200, context: () => successObj });
|
|
140
120
|
}
|
|
141
|
-
|
|
121
|
+
const finalPayload = logicResult.payload !== undefined ? logicResult.payload : logicResult;
|
|
122
|
+
return finalPayload && typeof finalPayload === 'object' ? JSON.parse(JSON.stringify(finalPayload)) : finalPayload;
|
|
142
123
|
}
|
|
143
124
|
catch (error) {
|
|
144
125
|
const errorPayload = { status: false, message: error.message };
|