@xrystal/core 3.7.1 → 3.7.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
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ProtocolEnum
|
|
1
|
+
import { ProtocolEnum } from '../../index';
|
|
2
2
|
export interface CustomRequest {
|
|
3
3
|
accounts?: any;
|
|
4
4
|
query: Record<string, any>;
|
|
@@ -43,38 +43,32 @@ declare abstract class Controller {
|
|
|
43
43
|
protected protocol: ProtocolEnum | null;
|
|
44
44
|
protected req: CustomRequest | null;
|
|
45
45
|
protected res: CustomResponse | null;
|
|
46
|
+
protected locals: Record<string, any>;
|
|
47
|
+
protected accounts: any;
|
|
48
|
+
protected startDate: string | null;
|
|
49
|
+
protected date: string | null;
|
|
50
|
+
protected dateStart: string | null;
|
|
51
|
+
protected endDate: string | null;
|
|
52
|
+
protected sort: Sort | null;
|
|
53
|
+
protected offset: string | null;
|
|
54
|
+
protected limit: string | null;
|
|
55
|
+
defaultLimitSize: number;
|
|
56
|
+
maxLimitSize: number;
|
|
46
57
|
constructor({ protocol, req, res, ctx }: {
|
|
47
58
|
protocol: ProtocolEnum;
|
|
48
59
|
req?: any;
|
|
49
60
|
res?: any;
|
|
50
61
|
ctx?: any;
|
|
51
62
|
});
|
|
52
|
-
protected responseProtocolSwitch: ({
|
|
53
|
-
protocol: ProtocolEnum;
|
|
54
|
-
req: CustomRequest;
|
|
55
|
-
res: CustomResponse;
|
|
56
|
-
resStatus?: number;
|
|
57
|
-
context: (helpers: {
|
|
58
|
-
localeLanguageConverter?: Function;
|
|
59
|
-
}) => ResponseSchema;
|
|
60
|
-
}) => Promise<any>;
|
|
63
|
+
protected responseProtocolSwitch: ({ res, resStatus, context, req }: any) => Promise<any>;
|
|
61
64
|
protected parsedQuerys: (url: string) => Record<string, any>;
|
|
62
65
|
protected log: (level: string, message: string) => Promise<void>;
|
|
63
66
|
}
|
|
64
67
|
export declare class ControllerSchema extends Controller {
|
|
65
68
|
schema({ checks, logic, response, }: {
|
|
66
|
-
checks?: (args:
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
}) => Promise<ReturnChecksCallbackFuncInterface | Array<any>>;
|
|
70
|
-
logic: (args: {
|
|
71
|
-
payload: any;
|
|
72
|
-
convertedPayload: any;
|
|
73
|
-
}) => Promise<ReturnLogicCallbackFuncInterface>;
|
|
74
|
-
response?: (args: {
|
|
75
|
-
payload: any;
|
|
76
|
-
convertedPayload: any;
|
|
77
|
-
}) => Promise<ReturnResponseCallbackFuncInterface>;
|
|
69
|
+
checks?: (args: any) => Promise<any>;
|
|
70
|
+
logic: (args: any) => Promise<any>;
|
|
71
|
+
response?: (args: any) => Promise<any>;
|
|
78
72
|
}): Promise<any>;
|
|
79
73
|
}
|
|
80
74
|
export { Controller };
|
|
@@ -1,18 +1,29 @@
|
|
|
1
1
|
import qs from 'qs';
|
|
2
2
|
import { LoggerService } from '../../../loader';
|
|
3
|
-
import { responseMessageHelper, ResponseSchema, x } from '../../index';
|
|
3
|
+
import { ProtocolEnum, responseMessageHelper, ResponseSchema, x } from '../../index';
|
|
4
4
|
class Controller {
|
|
5
5
|
logger = x.get(LoggerService);
|
|
6
6
|
protocol = null;
|
|
7
7
|
req = null;
|
|
8
8
|
res = null;
|
|
9
|
+
locals = {};
|
|
10
|
+
accounts = null;
|
|
11
|
+
startDate = null;
|
|
12
|
+
date = null;
|
|
13
|
+
dateStart = null;
|
|
14
|
+
endDate = null;
|
|
15
|
+
sort = null;
|
|
16
|
+
offset = null;
|
|
17
|
+
limit = null;
|
|
18
|
+
defaultLimitSize = 20;
|
|
19
|
+
maxLimitSize = 100;
|
|
9
20
|
constructor({ protocol, req, res, ctx }) {
|
|
10
21
|
this.protocol = protocol;
|
|
11
22
|
if (ctx) {
|
|
12
23
|
this.req = {
|
|
13
|
-
url: ctx.request
|
|
14
|
-
method: ctx.request
|
|
15
|
-
headers: Object.fromEntries(ctx.request.headers),
|
|
24
|
+
url: ctx.request?.url || '',
|
|
25
|
+
method: ctx.request?.method || '',
|
|
26
|
+
headers: ctx.headers || (ctx.request?.headers ? Object.fromEntries(ctx.request.headers) : {}),
|
|
16
27
|
body: ctx.body,
|
|
17
28
|
params: ctx.params,
|
|
18
29
|
query: ctx.query || {},
|
|
@@ -20,20 +31,21 @@ class Controller {
|
|
|
20
31
|
t: ctx.t
|
|
21
32
|
};
|
|
22
33
|
this.res = {
|
|
23
|
-
locals:
|
|
24
|
-
status(code) {
|
|
34
|
+
locals: this.locals,
|
|
35
|
+
status: (code) => {
|
|
25
36
|
this.locals._code = code;
|
|
26
|
-
return this;
|
|
37
|
+
return this.res;
|
|
27
38
|
},
|
|
28
|
-
send(data) {
|
|
39
|
+
send: (data) => {
|
|
40
|
+
if (this.protocol === ProtocolEnum.WEBSOCKET) {
|
|
41
|
+
return data;
|
|
42
|
+
}
|
|
29
43
|
return new Response(JSON.stringify(data), {
|
|
30
44
|
status: this.locals._code || 200,
|
|
31
45
|
headers: { 'content-type': 'application/json' }
|
|
32
46
|
});
|
|
33
47
|
},
|
|
34
|
-
json(data)
|
|
35
|
-
return this.send(data);
|
|
36
|
-
}
|
|
48
|
+
json: (data) => this.res.send(data)
|
|
37
49
|
};
|
|
38
50
|
}
|
|
39
51
|
else {
|
|
@@ -47,30 +59,37 @@ class Controller {
|
|
|
47
59
|
accounts: req?.accounts,
|
|
48
60
|
t: req?.t
|
|
49
61
|
};
|
|
62
|
+
this.locals = res?.locals || {};
|
|
50
63
|
this.res = {
|
|
51
|
-
locals:
|
|
52
|
-
status(code) {
|
|
53
|
-
res.status
|
|
54
|
-
|
|
64
|
+
locals: this.locals,
|
|
65
|
+
status: (code) => {
|
|
66
|
+
if (res && typeof res.status === 'function') {
|
|
67
|
+
res.status(code);
|
|
68
|
+
}
|
|
69
|
+
return this.res;
|
|
55
70
|
},
|
|
56
|
-
send(data) {
|
|
57
|
-
|
|
71
|
+
send: (data) => {
|
|
72
|
+
if (res && typeof res.send === 'function') {
|
|
73
|
+
return res.send(data);
|
|
74
|
+
}
|
|
75
|
+
return data;
|
|
58
76
|
},
|
|
59
|
-
json(data) {
|
|
60
|
-
|
|
77
|
+
json: (data) => {
|
|
78
|
+
if (res && typeof res.json === 'function') {
|
|
79
|
+
return res.json(data);
|
|
80
|
+
}
|
|
81
|
+
return data;
|
|
61
82
|
}
|
|
62
83
|
};
|
|
63
84
|
}
|
|
64
85
|
}
|
|
65
|
-
responseProtocolSwitch = async ({
|
|
86
|
+
responseProtocolSwitch = async ({ res, resStatus = 200, context, req }) => {
|
|
66
87
|
const responseData = context({ localeLanguageConverter: req.t });
|
|
67
88
|
return res.status(resStatus).send(responseData);
|
|
68
89
|
};
|
|
69
90
|
parsedQuerys = (url) => {
|
|
70
91
|
const queryString = url.includes('?') ? url.split('?')[1] : '';
|
|
71
|
-
|
|
72
|
-
return {};
|
|
73
|
-
return qs.parse(queryString, { decoder: decodeURIComponent });
|
|
92
|
+
return queryString ? qs.parse(queryString, { decoder: decodeURIComponent }) : {};
|
|
74
93
|
};
|
|
75
94
|
log = async (level, message) => {
|
|
76
95
|
this.logger.winston.log({ level, message });
|
|
@@ -92,7 +111,6 @@ export class ControllerSchema extends Controller {
|
|
|
92
111
|
}
|
|
93
112
|
else if (typeof checkResult === 'object' && checkResult.message) {
|
|
94
113
|
return await this.responseProtocolSwitch({
|
|
95
|
-
protocol: this.protocol,
|
|
96
114
|
req: this.req,
|
|
97
115
|
res: this.res,
|
|
98
116
|
context: () => new ResponseSchema({
|
|
@@ -105,7 +123,6 @@ export class ControllerSchema extends Controller {
|
|
|
105
123
|
}
|
|
106
124
|
if (isInvalid) {
|
|
107
125
|
return await this.responseProtocolSwitch({
|
|
108
|
-
protocol: this.protocol,
|
|
109
126
|
req: this.req,
|
|
110
127
|
res: this.res,
|
|
111
128
|
resStatus: 400,
|
|
@@ -122,7 +139,6 @@ export class ControllerSchema extends Controller {
|
|
|
122
139
|
}
|
|
123
140
|
if (logicResult.message) {
|
|
124
141
|
return await this.responseProtocolSwitch({
|
|
125
|
-
protocol: this.protocol,
|
|
126
142
|
req: this.req,
|
|
127
143
|
res: this.res,
|
|
128
144
|
resStatus: 400,
|
|
@@ -140,7 +156,6 @@ export class ControllerSchema extends Controller {
|
|
|
140
156
|
return resResult.response(resResult);
|
|
141
157
|
}
|
|
142
158
|
return await this.responseProtocolSwitch({
|
|
143
|
-
protocol: this.protocol,
|
|
144
159
|
req: this.req,
|
|
145
160
|
res: this.res,
|
|
146
161
|
resStatus: 200,
|
|
@@ -154,10 +169,7 @@ export class ControllerSchema extends Controller {
|
|
|
154
169
|
}
|
|
155
170
|
catch (error) {
|
|
156
171
|
await this.log('error', `Schema Error: ${error.message}`);
|
|
157
|
-
return this.res.status(500).send({
|
|
158
|
-
status: false,
|
|
159
|
-
message: error.message
|
|
160
|
-
});
|
|
172
|
+
return this.res.status(500).send({ status: false, message: error.message });
|
|
161
173
|
}
|
|
162
174
|
}
|
|
163
175
|
}
|