@xrystal/core 3.4.7 → 3.4.8
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,7 +1,6 @@
|
|
|
1
|
-
import { NextFunction, Response } from 'express';
|
|
2
1
|
import { ParsedQs } from 'qs';
|
|
3
2
|
import { ProtocolEnum, ResponseSchema } from '../../index';
|
|
4
|
-
export interface CustomRequest
|
|
3
|
+
export interface CustomRequest {
|
|
5
4
|
accounts?: any;
|
|
6
5
|
query: {
|
|
7
6
|
sort?: string;
|
|
@@ -11,8 +10,15 @@ export interface CustomRequest extends Request {
|
|
|
11
10
|
offset?: string;
|
|
12
11
|
limit?: string;
|
|
13
12
|
} & ParsedQs;
|
|
13
|
+
url: string;
|
|
14
|
+
method: string;
|
|
15
|
+
headers: Headers;
|
|
16
|
+
body?: any;
|
|
14
17
|
}
|
|
15
|
-
export interface CustomResponse
|
|
18
|
+
export interface CustomResponse {
|
|
19
|
+
status: (code: number) => CustomResponse;
|
|
20
|
+
send: (data: any) => Response;
|
|
21
|
+
json: (data: any) => Response;
|
|
16
22
|
locals: {
|
|
17
23
|
ID?: any;
|
|
18
24
|
user?: any;
|
|
@@ -31,11 +37,11 @@ export interface ReturnLogicCallbackFuncInterface {
|
|
|
31
37
|
message?: string;
|
|
32
38
|
payload?: any;
|
|
33
39
|
code?: number;
|
|
34
|
-
response?:
|
|
40
|
+
response?: CustomResponse | Function;
|
|
35
41
|
}
|
|
36
42
|
export interface ReturnResponseCallbackFuncInterface {
|
|
37
43
|
message: Array<string>;
|
|
38
|
-
response?:
|
|
44
|
+
response?: CustomResponse | Function;
|
|
39
45
|
}
|
|
40
46
|
export type Sort = {
|
|
41
47
|
key: string;
|
|
@@ -46,7 +52,7 @@ declare abstract class Controller {
|
|
|
46
52
|
protected protocol: ProtocolEnum | null;
|
|
47
53
|
protected socket: any | null;
|
|
48
54
|
protected req: CustomRequest | null | undefined;
|
|
49
|
-
protected res:
|
|
55
|
+
protected res: CustomResponse | null | undefined;
|
|
50
56
|
protected accounts: any;
|
|
51
57
|
protected startDate: string | null;
|
|
52
58
|
protected date: string | null;
|
|
@@ -57,29 +63,31 @@ declare abstract class Controller {
|
|
|
57
63
|
protected limit: string | null;
|
|
58
64
|
defaultLimitSize: number;
|
|
59
65
|
maxLimitSize: number;
|
|
66
|
+
private statusCode;
|
|
60
67
|
constructor({ protocol, socket, req, res }: {
|
|
61
68
|
protocol: ProtocolEnum;
|
|
62
69
|
socket?: any;
|
|
63
70
|
req?: CustomRequest;
|
|
64
|
-
res?:
|
|
71
|
+
res?: CustomResponse;
|
|
65
72
|
});
|
|
73
|
+
protected createResponse(data: any, statusCode?: number): Response;
|
|
66
74
|
protected payloadProtocolSwitch: ({ protocol, socket, req, res }: {
|
|
67
75
|
protocol: ProtocolEnum;
|
|
68
76
|
socket?: any;
|
|
69
77
|
req?: any;
|
|
70
|
-
res?:
|
|
78
|
+
res?: CustomResponse;
|
|
71
79
|
}) => Promise<any>;
|
|
72
80
|
protected convertedPayloadProtocolSwitch: ({ protocol, socket, req, res, }: {
|
|
73
81
|
protocol: ProtocolEnum;
|
|
74
82
|
socket?: any;
|
|
75
83
|
req?: any;
|
|
76
|
-
res?:
|
|
84
|
+
res?: CustomResponse;
|
|
77
85
|
}) => Promise<any>;
|
|
78
86
|
protected responseProtocolSwitch: ({ protocol, socket, req, res, resStatus, context }: {
|
|
79
87
|
protocol: ProtocolEnum;
|
|
80
88
|
socket?: any;
|
|
81
89
|
req?: any;
|
|
82
|
-
res?:
|
|
90
|
+
res?: CustomResponse;
|
|
83
91
|
resStatus?: number;
|
|
84
92
|
context: ({ localeLanguageConverter }: {
|
|
85
93
|
localeLanguageConverter?: Function;
|
|
@@ -101,7 +109,7 @@ declare class ControllerSchema extends Controller {
|
|
|
101
109
|
protocol: ProtocolEnum;
|
|
102
110
|
socket?: any;
|
|
103
111
|
req?: CustomRequest;
|
|
104
|
-
res?:
|
|
112
|
+
res?: CustomResponse;
|
|
105
113
|
});
|
|
106
114
|
schema({ checks, logic, response, }: {
|
|
107
115
|
checks?: ({ payload, convertedPayload }: {
|
|
@@ -3,16 +3,11 @@ import { LoggerService } from '../../../loader';
|
|
|
3
3
|
import { LoggerLayerEnum, ProtocolEnum, responseMessageHelper, ResponseSchema, x } from '../../index';
|
|
4
4
|
class Controller {
|
|
5
5
|
logger = x.get(LoggerService);
|
|
6
|
-
//private static logger: LogCallback | null = null
|
|
7
|
-
// => it wants properties for proccessing
|
|
8
6
|
protocol = null;
|
|
9
7
|
socket = null;
|
|
10
8
|
req = null;
|
|
11
9
|
res = null;
|
|
12
|
-
//
|
|
13
|
-
// => special properties
|
|
14
10
|
accounts = null;
|
|
15
|
-
// => special querys
|
|
16
11
|
startDate = null;
|
|
17
12
|
date = null;
|
|
18
13
|
dateStart = null;
|
|
@@ -20,31 +15,35 @@ class Controller {
|
|
|
20
15
|
sort = null;
|
|
21
16
|
offset = null;
|
|
22
17
|
limit = null;
|
|
23
|
-
//
|
|
24
|
-
// => options
|
|
25
18
|
defaultLimitSize = 20;
|
|
26
19
|
maxLimitSize = 100;
|
|
27
|
-
|
|
20
|
+
statusCode = 200;
|
|
28
21
|
constructor({ protocol, socket, req, res }) {
|
|
29
22
|
this.protocol = protocol;
|
|
30
23
|
this.socket = socket;
|
|
31
24
|
this.req = req;
|
|
32
25
|
this.res = res;
|
|
33
26
|
}
|
|
34
|
-
|
|
27
|
+
createResponse(data, statusCode = 200) {
|
|
28
|
+
return new Response(JSON.stringify(data), {
|
|
29
|
+
status: statusCode,
|
|
30
|
+
headers: {
|
|
31
|
+
'Content-Type': 'application/json'
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
35
|
payloadProtocolSwitch = async ({ protocol, socket, req, res }) => {
|
|
36
36
|
let payload = {};
|
|
37
37
|
switch (protocol) {
|
|
38
38
|
case ProtocolEnum.HTTP:
|
|
39
39
|
case ProtocolEnum.HTTPS:
|
|
40
40
|
if ((protocol === ProtocolEnum.HTTP || protocol === ProtocolEnum.HTTPS) && (!req || !res)) {
|
|
41
|
-
throw new Error('Req and res');
|
|
41
|
+
throw new Error('Req and res required');
|
|
42
42
|
}
|
|
43
43
|
this.req = req;
|
|
44
44
|
this.res = res;
|
|
45
|
-
const parsedQuerys = this.parsedQuerys(req?.
|
|
45
|
+
const parsedQuerys = this.parsedQuerys(req?.url ? req.url : '');
|
|
46
46
|
payload = {
|
|
47
|
-
//socket: this.socket,
|
|
48
47
|
req: this.req,
|
|
49
48
|
res: this.res,
|
|
50
49
|
accounts: this.req?.accounts && this.req.accounts,
|
|
@@ -67,26 +66,35 @@ class Controller {
|
|
|
67
66
|
switch (protocol) {
|
|
68
67
|
case ProtocolEnum.HTTP:
|
|
69
68
|
case ProtocolEnum.HTTPS:
|
|
70
|
-
const parsedQuerys = this.parsedQuerys(req?.
|
|
71
|
-
const parsedQuerysWithTrueType = this.parsedQuerysWithTrueType(req?.
|
|
69
|
+
const parsedQuerys = this.parsedQuerys(req?.url ? req.url : '');
|
|
70
|
+
const parsedQuerysWithTrueType = this.parsedQuerysWithTrueType(req?.url ? req.url : '');
|
|
72
71
|
convertedPayload = {
|
|
73
|
-
//socket: this.socket,
|
|
74
72
|
req: this.req,
|
|
75
73
|
res: this.res,
|
|
76
74
|
accounts: this.req?.accounts,
|
|
77
75
|
parsedQuerys: parsedQuerys,
|
|
78
76
|
parsedQuerysWithTrueType: parsedQuerysWithTrueType,
|
|
79
|
-
startDate: parsedQuerysWithTrueType?.['start-date']
|
|
80
|
-
|
|
77
|
+
startDate: parsedQuerysWithTrueType?.['start-date']
|
|
78
|
+
? this.dateNativeUTCToUTCConverter(parsedQuerysWithTrueType['start-date'])
|
|
79
|
+
: this.startDate,
|
|
80
|
+
startDateTimezone: parsedQuerysWithTrueType?.['start-date']
|
|
81
|
+
? parsedQuerysWithTrueType['start-date'].split('+')[1]
|
|
82
|
+
: this.startDate,
|
|
81
83
|
date: this.date ? this.dateNativeUTCToUTCConverter(parsedQuerysWithTrueType.date) : this.date,
|
|
82
84
|
dateTimezone: this.date ? parsedQuerysWithTrueType.date.split('+')[1] : this.date,
|
|
83
|
-
endDate: parsedQuerysWithTrueType?.['end-date']
|
|
84
|
-
|
|
85
|
+
endDate: parsedQuerysWithTrueType?.['end-date']
|
|
86
|
+
? this.dateNativeUTCToUTCConverter(parsedQuerysWithTrueType['end-date'])
|
|
87
|
+
: this.endDate,
|
|
88
|
+
endDateTimezone: parsedQuerysWithTrueType?.['end-date']
|
|
89
|
+
? parsedQuerysWithTrueType['end-date'].split('+')[1]
|
|
90
|
+
: this.endDate,
|
|
85
91
|
sort: parsedQuerysWithTrueType.sort ? this.sortParser(parsedQuerys.sort) : this.sort,
|
|
86
92
|
offset: parsedQuerysWithTrueType.offset ? parsedQuerys.offset : this.offset,
|
|
87
|
-
limit: parsedQuerysWithTrueType.limit
|
|
88
|
-
|
|
89
|
-
|
|
93
|
+
limit: parsedQuerysWithTrueType.limit
|
|
94
|
+
? parsedQuerysWithTrueType.limit > this.maxLimitSize
|
|
95
|
+
? String(this.maxLimitSize)
|
|
96
|
+
: parsedQuerys.limit
|
|
97
|
+
: this.defaultLimitSize
|
|
90
98
|
};
|
|
91
99
|
break;
|
|
92
100
|
case ProtocolEnum.WEBSOCKET:
|
|
@@ -110,9 +118,11 @@ class Controller {
|
|
|
110
118
|
if (!req || !res) {
|
|
111
119
|
throw new Error(`req or res not found`);
|
|
112
120
|
}
|
|
113
|
-
|
|
121
|
+
// Bun native Response döndür
|
|
122
|
+
const responseData = context({
|
|
114
123
|
localeLanguageConverter: req.t
|
|
115
|
-
})
|
|
124
|
+
});
|
|
125
|
+
return this.createResponse(responseData, resStatus);
|
|
116
126
|
case ProtocolEnum.WEBSOCKET:
|
|
117
127
|
return context({});
|
|
118
128
|
case ProtocolEnum.SOCKETIO:
|
|
@@ -123,8 +133,6 @@ class Controller {
|
|
|
123
133
|
throw new Error('Protocol not match.');
|
|
124
134
|
}
|
|
125
135
|
};
|
|
126
|
-
//
|
|
127
|
-
// => helper
|
|
128
136
|
parsedQuerys = (url) => {
|
|
129
137
|
const parseUrlForQuerys = url.split('?')?.[1];
|
|
130
138
|
let convertedQuerys;
|
|
@@ -168,7 +176,10 @@ class Controller {
|
|
|
168
176
|
if (String(sort)) {
|
|
169
177
|
const parsed = sort.split('+');
|
|
170
178
|
if (!parsed[0] || !parsed[1]) {
|
|
171
|
-
this.log({
|
|
179
|
+
this.log({
|
|
180
|
+
level: LoggerLayerEnum[LoggerLayerEnum.ERROR].toLowerCase(),
|
|
181
|
+
message: 'sort query parameter invalid.'
|
|
182
|
+
});
|
|
172
183
|
return null;
|
|
173
184
|
}
|
|
174
185
|
return {
|
|
@@ -208,7 +219,7 @@ class ControllerSchema extends Controller {
|
|
|
208
219
|
const { res, req } = this;
|
|
209
220
|
try {
|
|
210
221
|
let logicCallbackFunc;
|
|
211
|
-
//
|
|
222
|
+
// Checks
|
|
212
223
|
try {
|
|
213
224
|
if (checks) {
|
|
214
225
|
const checksCallbackFunc = await checks({
|
|
@@ -243,7 +254,7 @@ class ControllerSchema extends Controller {
|
|
|
243
254
|
checked = checksCallbackFunc.response();
|
|
244
255
|
}
|
|
245
256
|
else {
|
|
246
|
-
return
|
|
257
|
+
return checksCallbackFunc.response();
|
|
247
258
|
}
|
|
248
259
|
}
|
|
249
260
|
if (!checked) {
|
|
@@ -255,10 +266,11 @@ class ControllerSchema extends Controller {
|
|
|
255
266
|
resStatus: 400,
|
|
256
267
|
context: ({ localeLanguageConverter }) => new ResponseSchema({
|
|
257
268
|
status: false,
|
|
258
|
-
message: Array.isArray(checksCallbackFunc)
|
|
259
|
-
`${responseMessageHelper.schemaMissingDataChecks(localeLanguageConverter)}`
|
|
260
|
-
checksCallbackFunc?.invalid
|
|
261
|
-
`${responseMessageHelper.
|
|
269
|
+
message: Array.isArray(checksCallbackFunc)
|
|
270
|
+
? `${responseMessageHelper.schemaMissingDataChecks(localeLanguageConverter)}`
|
|
271
|
+
: checksCallbackFunc?.invalid
|
|
272
|
+
? `${responseMessageHelper.schemaInvalidDataChecks(localeLanguageConverter)}`
|
|
273
|
+
: `${responseMessageHelper.schemaMissingDataChecks(localeLanguageConverter)}`,
|
|
262
274
|
}).getResponse
|
|
263
275
|
});
|
|
264
276
|
}
|
|
@@ -267,7 +279,7 @@ class ControllerSchema extends Controller {
|
|
|
267
279
|
catch (error) {
|
|
268
280
|
this.log({
|
|
269
281
|
level: LoggerLayerEnum[LoggerLayerEnum.CRITICAL].toLowerCase(),
|
|
270
|
-
message: this.getErrorMessage(`Schema check controller handler: ${error.message}. Extra
|
|
282
|
+
message: this.getErrorMessage(`Schema check controller handler: ${error.message}. Extra: ${JSON.stringify(error.response?.data)}`),
|
|
271
283
|
});
|
|
272
284
|
return this.responseProtocolSwitch({
|
|
273
285
|
protocol: this.protocol,
|
|
@@ -281,9 +293,8 @@ class ControllerSchema extends Controller {
|
|
|
281
293
|
}).getResponse
|
|
282
294
|
});
|
|
283
295
|
}
|
|
284
|
-
//
|
|
285
|
-
|
|
286
|
-
try { // => (message and response) first rendering. If there is no message or data, it goes to the response scope.
|
|
296
|
+
// Logic
|
|
297
|
+
try {
|
|
287
298
|
logicCallbackFunc = await logic({
|
|
288
299
|
payload: this.payloadProtocolSwitch,
|
|
289
300
|
convertedPayload: await this.convertedPayloadProtocolSwitch({
|
|
@@ -317,10 +328,10 @@ class ControllerSchema extends Controller {
|
|
|
317
328
|
});
|
|
318
329
|
}
|
|
319
330
|
}
|
|
320
|
-
catch (error
|
|
331
|
+
catch (error) {
|
|
321
332
|
this.log({
|
|
322
333
|
level: LoggerLayerEnum[LoggerLayerEnum.CRITICAL].toLowerCase(),
|
|
323
|
-
message: this.getErrorMessage(`Schema logic controller handler: ${error.message}. Extra
|
|
334
|
+
message: this.getErrorMessage(`Schema logic controller handler: ${error.message}. Extra: ${JSON.stringify(error?.response?.data)}`),
|
|
324
335
|
});
|
|
325
336
|
return this.responseProtocolSwitch({
|
|
326
337
|
protocol: this.protocol,
|
|
@@ -334,8 +345,7 @@ class ControllerSchema extends Controller {
|
|
|
334
345
|
}).getResponse
|
|
335
346
|
});
|
|
336
347
|
}
|
|
337
|
-
//
|
|
338
|
-
// return response start
|
|
348
|
+
// Response
|
|
339
349
|
try {
|
|
340
350
|
if (response) {
|
|
341
351
|
const responseCallbackFunc = await response({
|
|
@@ -380,10 +390,10 @@ class ControllerSchema extends Controller {
|
|
|
380
390
|
throw new Error('Please use response scope.');
|
|
381
391
|
}
|
|
382
392
|
}
|
|
383
|
-
catch (error
|
|
393
|
+
catch (error) {
|
|
384
394
|
this.log({
|
|
385
395
|
level: LoggerLayerEnum[LoggerLayerEnum.CRITICAL].toLowerCase(),
|
|
386
|
-
message: this.getErrorMessage(`Schema response controller handler: ${error.message}. Extra
|
|
396
|
+
message: this.getErrorMessage(`Schema response controller handler: ${error.message}. Extra: ${JSON.stringify(error?.response?.data)}`),
|
|
387
397
|
});
|
|
388
398
|
return this.responseProtocolSwitch({
|
|
389
399
|
protocol: this.protocol,
|
|
@@ -393,16 +403,15 @@ class ControllerSchema extends Controller {
|
|
|
393
403
|
resStatus: 500,
|
|
394
404
|
context: ({ localeLanguageConverter }) => new ResponseSchema({
|
|
395
405
|
status: false,
|
|
396
|
-
message: `${responseMessageHelper.schemaUnknowErrorResponse(error.message, JSON.stringify(error?.response?.data), localeLanguageConverter)}`,
|
|
406
|
+
message: `${responseMessageHelper.schemaUnknowErrorResponse(error.message, JSON.stringify(error?.response?.data), localeLanguageConverter)}`,
|
|
397
407
|
}).getResponse
|
|
398
408
|
});
|
|
399
409
|
}
|
|
400
|
-
// end
|
|
401
410
|
}
|
|
402
|
-
catch (error
|
|
411
|
+
catch (error) {
|
|
403
412
|
this.log({
|
|
404
413
|
level: LoggerLayerEnum[LoggerLayerEnum.CRITICAL].toLowerCase(),
|
|
405
|
-
message: this.getErrorMessage(`Schema main controller handler: ${error.message}. Extra
|
|
414
|
+
message: this.getErrorMessage(`Schema main controller handler: ${error.message}. Extra: ${JSON.stringify(error?.response?.data)}`),
|
|
406
415
|
});
|
|
407
416
|
return this.responseProtocolSwitch({
|
|
408
417
|
protocol: this.protocol,
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import { NextFunction, Response } from 'express';
|
|
2
1
|
import { ParsedQs } from 'qs';
|
|
3
2
|
import { ProtocolEnum, ResponseSchema } from '../../index';
|
|
4
|
-
export interface CustomRequest
|
|
3
|
+
export interface CustomRequest {
|
|
5
4
|
accounts?: any;
|
|
6
5
|
query: {
|
|
7
6
|
sort?: string;
|
|
@@ -11,8 +10,15 @@ export interface CustomRequest extends Request {
|
|
|
11
10
|
offset?: string;
|
|
12
11
|
limit?: string;
|
|
13
12
|
} & ParsedQs;
|
|
13
|
+
url: string;
|
|
14
|
+
method: string;
|
|
15
|
+
headers: Headers;
|
|
16
|
+
body?: any;
|
|
14
17
|
}
|
|
15
|
-
export interface CustomResponse
|
|
18
|
+
export interface CustomResponse {
|
|
19
|
+
status: (code: number) => CustomResponse;
|
|
20
|
+
send: (data: any) => Response;
|
|
21
|
+
json: (data: any) => Response;
|
|
16
22
|
locals: {
|
|
17
23
|
ID?: any;
|
|
18
24
|
user?: any;
|
|
@@ -31,11 +37,11 @@ export interface ReturnLogicCallbackFuncInterface {
|
|
|
31
37
|
message?: string;
|
|
32
38
|
payload?: any;
|
|
33
39
|
code?: number;
|
|
34
|
-
response?:
|
|
40
|
+
response?: CustomResponse | Function;
|
|
35
41
|
}
|
|
36
42
|
export interface ReturnResponseCallbackFuncInterface {
|
|
37
43
|
message: Array<string>;
|
|
38
|
-
response?:
|
|
44
|
+
response?: CustomResponse | Function;
|
|
39
45
|
}
|
|
40
46
|
export type Sort = {
|
|
41
47
|
key: string;
|
|
@@ -46,7 +52,7 @@ declare abstract class Controller {
|
|
|
46
52
|
protected protocol: ProtocolEnum | null;
|
|
47
53
|
protected socket: any | null;
|
|
48
54
|
protected req: CustomRequest | null | undefined;
|
|
49
|
-
protected res:
|
|
55
|
+
protected res: CustomResponse | null | undefined;
|
|
50
56
|
protected accounts: any;
|
|
51
57
|
protected startDate: string | null;
|
|
52
58
|
protected date: string | null;
|
|
@@ -57,29 +63,31 @@ declare abstract class Controller {
|
|
|
57
63
|
protected limit: string | null;
|
|
58
64
|
defaultLimitSize: number;
|
|
59
65
|
maxLimitSize: number;
|
|
66
|
+
private statusCode;
|
|
60
67
|
constructor({ protocol, socket, req, res }: {
|
|
61
68
|
protocol: ProtocolEnum;
|
|
62
69
|
socket?: any;
|
|
63
70
|
req?: CustomRequest;
|
|
64
|
-
res?:
|
|
71
|
+
res?: CustomResponse;
|
|
65
72
|
});
|
|
73
|
+
protected createResponse(data: any, statusCode?: number): Response;
|
|
66
74
|
protected payloadProtocolSwitch: ({ protocol, socket, req, res }: {
|
|
67
75
|
protocol: ProtocolEnum;
|
|
68
76
|
socket?: any;
|
|
69
77
|
req?: any;
|
|
70
|
-
res?:
|
|
78
|
+
res?: CustomResponse;
|
|
71
79
|
}) => Promise<any>;
|
|
72
80
|
protected convertedPayloadProtocolSwitch: ({ protocol, socket, req, res, }: {
|
|
73
81
|
protocol: ProtocolEnum;
|
|
74
82
|
socket?: any;
|
|
75
83
|
req?: any;
|
|
76
|
-
res?:
|
|
84
|
+
res?: CustomResponse;
|
|
77
85
|
}) => Promise<any>;
|
|
78
86
|
protected responseProtocolSwitch: ({ protocol, socket, req, res, resStatus, context }: {
|
|
79
87
|
protocol: ProtocolEnum;
|
|
80
88
|
socket?: any;
|
|
81
89
|
req?: any;
|
|
82
|
-
res?:
|
|
90
|
+
res?: CustomResponse;
|
|
83
91
|
resStatus?: number;
|
|
84
92
|
context: ({ localeLanguageConverter }: {
|
|
85
93
|
localeLanguageConverter?: Function;
|
|
@@ -101,7 +109,7 @@ declare class ControllerSchema extends Controller {
|
|
|
101
109
|
protocol: ProtocolEnum;
|
|
102
110
|
socket?: any;
|
|
103
111
|
req?: CustomRequest;
|
|
104
|
-
res?:
|
|
112
|
+
res?: CustomResponse;
|
|
105
113
|
});
|
|
106
114
|
schema({ checks, logic, response, }: {
|
|
107
115
|
checks?: ({ payload, convertedPayload }: {
|
|
@@ -3,16 +3,11 @@ import { LoggerService } from '../../../loader';
|
|
|
3
3
|
import { LoggerLayerEnum, ProtocolEnum, responseMessageHelper, ResponseSchema, x } from '../../index';
|
|
4
4
|
class Controller {
|
|
5
5
|
logger = x.get(LoggerService);
|
|
6
|
-
//private static logger: LogCallback | null = null
|
|
7
|
-
// => it wants properties for proccessing
|
|
8
6
|
protocol = null;
|
|
9
7
|
socket = null;
|
|
10
8
|
req = null;
|
|
11
9
|
res = null;
|
|
12
|
-
//
|
|
13
|
-
// => special properties
|
|
14
10
|
accounts = null;
|
|
15
|
-
// => special querys
|
|
16
11
|
startDate = null;
|
|
17
12
|
date = null;
|
|
18
13
|
dateStart = null;
|
|
@@ -20,31 +15,35 @@ class Controller {
|
|
|
20
15
|
sort = null;
|
|
21
16
|
offset = null;
|
|
22
17
|
limit = null;
|
|
23
|
-
//
|
|
24
|
-
// => options
|
|
25
18
|
defaultLimitSize = 20;
|
|
26
19
|
maxLimitSize = 100;
|
|
27
|
-
|
|
20
|
+
statusCode = 200;
|
|
28
21
|
constructor({ protocol, socket, req, res }) {
|
|
29
22
|
this.protocol = protocol;
|
|
30
23
|
this.socket = socket;
|
|
31
24
|
this.req = req;
|
|
32
25
|
this.res = res;
|
|
33
26
|
}
|
|
34
|
-
|
|
27
|
+
createResponse(data, statusCode = 200) {
|
|
28
|
+
return new Response(JSON.stringify(data), {
|
|
29
|
+
status: statusCode,
|
|
30
|
+
headers: {
|
|
31
|
+
'Content-Type': 'application/json'
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
35
|
payloadProtocolSwitch = async ({ protocol, socket, req, res }) => {
|
|
36
36
|
let payload = {};
|
|
37
37
|
switch (protocol) {
|
|
38
38
|
case ProtocolEnum.HTTP:
|
|
39
39
|
case ProtocolEnum.HTTPS:
|
|
40
40
|
if ((protocol === ProtocolEnum.HTTP || protocol === ProtocolEnum.HTTPS) && (!req || !res)) {
|
|
41
|
-
throw new Error('Req and res');
|
|
41
|
+
throw new Error('Req and res required');
|
|
42
42
|
}
|
|
43
43
|
this.req = req;
|
|
44
44
|
this.res = res;
|
|
45
|
-
const parsedQuerys = this.parsedQuerys(req?.
|
|
45
|
+
const parsedQuerys = this.parsedQuerys(req?.url ? req.url : '');
|
|
46
46
|
payload = {
|
|
47
|
-
//socket: this.socket,
|
|
48
47
|
req: this.req,
|
|
49
48
|
res: this.res,
|
|
50
49
|
accounts: this.req?.accounts && this.req.accounts,
|
|
@@ -67,26 +66,35 @@ class Controller {
|
|
|
67
66
|
switch (protocol) {
|
|
68
67
|
case ProtocolEnum.HTTP:
|
|
69
68
|
case ProtocolEnum.HTTPS:
|
|
70
|
-
const parsedQuerys = this.parsedQuerys(req?.
|
|
71
|
-
const parsedQuerysWithTrueType = this.parsedQuerysWithTrueType(req?.
|
|
69
|
+
const parsedQuerys = this.parsedQuerys(req?.url ? req.url : '');
|
|
70
|
+
const parsedQuerysWithTrueType = this.parsedQuerysWithTrueType(req?.url ? req.url : '');
|
|
72
71
|
convertedPayload = {
|
|
73
|
-
//socket: this.socket,
|
|
74
72
|
req: this.req,
|
|
75
73
|
res: this.res,
|
|
76
74
|
accounts: this.req?.accounts,
|
|
77
75
|
parsedQuerys: parsedQuerys,
|
|
78
76
|
parsedQuerysWithTrueType: parsedQuerysWithTrueType,
|
|
79
|
-
startDate: parsedQuerysWithTrueType?.['start-date']
|
|
80
|
-
|
|
77
|
+
startDate: parsedQuerysWithTrueType?.['start-date']
|
|
78
|
+
? this.dateNativeUTCToUTCConverter(parsedQuerysWithTrueType['start-date'])
|
|
79
|
+
: this.startDate,
|
|
80
|
+
startDateTimezone: parsedQuerysWithTrueType?.['start-date']
|
|
81
|
+
? parsedQuerysWithTrueType['start-date'].split('+')[1]
|
|
82
|
+
: this.startDate,
|
|
81
83
|
date: this.date ? this.dateNativeUTCToUTCConverter(parsedQuerysWithTrueType.date) : this.date,
|
|
82
84
|
dateTimezone: this.date ? parsedQuerysWithTrueType.date.split('+')[1] : this.date,
|
|
83
|
-
endDate: parsedQuerysWithTrueType?.['end-date']
|
|
84
|
-
|
|
85
|
+
endDate: parsedQuerysWithTrueType?.['end-date']
|
|
86
|
+
? this.dateNativeUTCToUTCConverter(parsedQuerysWithTrueType['end-date'])
|
|
87
|
+
: this.endDate,
|
|
88
|
+
endDateTimezone: parsedQuerysWithTrueType?.['end-date']
|
|
89
|
+
? parsedQuerysWithTrueType['end-date'].split('+')[1]
|
|
90
|
+
: this.endDate,
|
|
85
91
|
sort: parsedQuerysWithTrueType.sort ? this.sortParser(parsedQuerys.sort) : this.sort,
|
|
86
92
|
offset: parsedQuerysWithTrueType.offset ? parsedQuerys.offset : this.offset,
|
|
87
|
-
limit: parsedQuerysWithTrueType.limit
|
|
88
|
-
|
|
89
|
-
|
|
93
|
+
limit: parsedQuerysWithTrueType.limit
|
|
94
|
+
? parsedQuerysWithTrueType.limit > this.maxLimitSize
|
|
95
|
+
? String(this.maxLimitSize)
|
|
96
|
+
: parsedQuerys.limit
|
|
97
|
+
: this.defaultLimitSize
|
|
90
98
|
};
|
|
91
99
|
break;
|
|
92
100
|
case ProtocolEnum.WEBSOCKET:
|
|
@@ -110,9 +118,11 @@ class Controller {
|
|
|
110
118
|
if (!req || !res) {
|
|
111
119
|
throw new Error(`req or res not found`);
|
|
112
120
|
}
|
|
113
|
-
|
|
121
|
+
// Bun native Response döndür
|
|
122
|
+
const responseData = context({
|
|
114
123
|
localeLanguageConverter: req.t
|
|
115
|
-
})
|
|
124
|
+
});
|
|
125
|
+
return this.createResponse(responseData, resStatus);
|
|
116
126
|
case ProtocolEnum.WEBSOCKET:
|
|
117
127
|
return context({});
|
|
118
128
|
case ProtocolEnum.SOCKETIO:
|
|
@@ -123,8 +133,6 @@ class Controller {
|
|
|
123
133
|
throw new Error('Protocol not match.');
|
|
124
134
|
}
|
|
125
135
|
};
|
|
126
|
-
//
|
|
127
|
-
// => helper
|
|
128
136
|
parsedQuerys = (url) => {
|
|
129
137
|
const parseUrlForQuerys = url.split('?')?.[1];
|
|
130
138
|
let convertedQuerys;
|
|
@@ -168,7 +176,10 @@ class Controller {
|
|
|
168
176
|
if (String(sort)) {
|
|
169
177
|
const parsed = sort.split('+');
|
|
170
178
|
if (!parsed[0] || !parsed[1]) {
|
|
171
|
-
this.log({
|
|
179
|
+
this.log({
|
|
180
|
+
level: LoggerLayerEnum[LoggerLayerEnum.ERROR].toLowerCase(),
|
|
181
|
+
message: 'sort query parameter invalid.'
|
|
182
|
+
});
|
|
172
183
|
return null;
|
|
173
184
|
}
|
|
174
185
|
return {
|
|
@@ -208,7 +219,7 @@ class ControllerSchema extends Controller {
|
|
|
208
219
|
const { res, req } = this;
|
|
209
220
|
try {
|
|
210
221
|
let logicCallbackFunc;
|
|
211
|
-
//
|
|
222
|
+
// Checks
|
|
212
223
|
try {
|
|
213
224
|
if (checks) {
|
|
214
225
|
const checksCallbackFunc = await checks({
|
|
@@ -243,7 +254,7 @@ class ControllerSchema extends Controller {
|
|
|
243
254
|
checked = checksCallbackFunc.response();
|
|
244
255
|
}
|
|
245
256
|
else {
|
|
246
|
-
return
|
|
257
|
+
return checksCallbackFunc.response();
|
|
247
258
|
}
|
|
248
259
|
}
|
|
249
260
|
if (!checked) {
|
|
@@ -255,10 +266,11 @@ class ControllerSchema extends Controller {
|
|
|
255
266
|
resStatus: 400,
|
|
256
267
|
context: ({ localeLanguageConverter }) => new ResponseSchema({
|
|
257
268
|
status: false,
|
|
258
|
-
message: Array.isArray(checksCallbackFunc)
|
|
259
|
-
`${responseMessageHelper.schemaMissingDataChecks(localeLanguageConverter)}`
|
|
260
|
-
checksCallbackFunc?.invalid
|
|
261
|
-
`${responseMessageHelper.
|
|
269
|
+
message: Array.isArray(checksCallbackFunc)
|
|
270
|
+
? `${responseMessageHelper.schemaMissingDataChecks(localeLanguageConverter)}`
|
|
271
|
+
: checksCallbackFunc?.invalid
|
|
272
|
+
? `${responseMessageHelper.schemaInvalidDataChecks(localeLanguageConverter)}`
|
|
273
|
+
: `${responseMessageHelper.schemaMissingDataChecks(localeLanguageConverter)}`,
|
|
262
274
|
}).getResponse
|
|
263
275
|
});
|
|
264
276
|
}
|
|
@@ -267,7 +279,7 @@ class ControllerSchema extends Controller {
|
|
|
267
279
|
catch (error) {
|
|
268
280
|
this.log({
|
|
269
281
|
level: LoggerLayerEnum[LoggerLayerEnum.CRITICAL].toLowerCase(),
|
|
270
|
-
message: this.getErrorMessage(`Schema check controller handler: ${error.message}. Extra
|
|
282
|
+
message: this.getErrorMessage(`Schema check controller handler: ${error.message}. Extra: ${JSON.stringify(error.response?.data)}`),
|
|
271
283
|
});
|
|
272
284
|
return this.responseProtocolSwitch({
|
|
273
285
|
protocol: this.protocol,
|
|
@@ -281,9 +293,8 @@ class ControllerSchema extends Controller {
|
|
|
281
293
|
}).getResponse
|
|
282
294
|
});
|
|
283
295
|
}
|
|
284
|
-
//
|
|
285
|
-
|
|
286
|
-
try { // => (message and response) first rendering. If there is no message or data, it goes to the response scope.
|
|
296
|
+
// Logic
|
|
297
|
+
try {
|
|
287
298
|
logicCallbackFunc = await logic({
|
|
288
299
|
payload: this.payloadProtocolSwitch,
|
|
289
300
|
convertedPayload: await this.convertedPayloadProtocolSwitch({
|
|
@@ -317,10 +328,10 @@ class ControllerSchema extends Controller {
|
|
|
317
328
|
});
|
|
318
329
|
}
|
|
319
330
|
}
|
|
320
|
-
catch (error
|
|
331
|
+
catch (error) {
|
|
321
332
|
this.log({
|
|
322
333
|
level: LoggerLayerEnum[LoggerLayerEnum.CRITICAL].toLowerCase(),
|
|
323
|
-
message: this.getErrorMessage(`Schema logic controller handler: ${error.message}. Extra
|
|
334
|
+
message: this.getErrorMessage(`Schema logic controller handler: ${error.message}. Extra: ${JSON.stringify(error?.response?.data)}`),
|
|
324
335
|
});
|
|
325
336
|
return this.responseProtocolSwitch({
|
|
326
337
|
protocol: this.protocol,
|
|
@@ -334,8 +345,7 @@ class ControllerSchema extends Controller {
|
|
|
334
345
|
}).getResponse
|
|
335
346
|
});
|
|
336
347
|
}
|
|
337
|
-
//
|
|
338
|
-
// return response start
|
|
348
|
+
// Response
|
|
339
349
|
try {
|
|
340
350
|
if (response) {
|
|
341
351
|
const responseCallbackFunc = await response({
|
|
@@ -380,10 +390,10 @@ class ControllerSchema extends Controller {
|
|
|
380
390
|
throw new Error('Please use response scope.');
|
|
381
391
|
}
|
|
382
392
|
}
|
|
383
|
-
catch (error
|
|
393
|
+
catch (error) {
|
|
384
394
|
this.log({
|
|
385
395
|
level: LoggerLayerEnum[LoggerLayerEnum.CRITICAL].toLowerCase(),
|
|
386
|
-
message: this.getErrorMessage(`Schema response controller handler: ${error.message}. Extra
|
|
396
|
+
message: this.getErrorMessage(`Schema response controller handler: ${error.message}. Extra: ${JSON.stringify(error?.response?.data)}`),
|
|
387
397
|
});
|
|
388
398
|
return this.responseProtocolSwitch({
|
|
389
399
|
protocol: this.protocol,
|
|
@@ -393,16 +403,15 @@ class ControllerSchema extends Controller {
|
|
|
393
403
|
resStatus: 500,
|
|
394
404
|
context: ({ localeLanguageConverter }) => new ResponseSchema({
|
|
395
405
|
status: false,
|
|
396
|
-
message: `${responseMessageHelper.schemaUnknowErrorResponse(error.message, JSON.stringify(error?.response?.data), localeLanguageConverter)}`,
|
|
406
|
+
message: `${responseMessageHelper.schemaUnknowErrorResponse(error.message, JSON.stringify(error?.response?.data), localeLanguageConverter)}`,
|
|
397
407
|
}).getResponse
|
|
398
408
|
});
|
|
399
409
|
}
|
|
400
|
-
// end
|
|
401
410
|
}
|
|
402
|
-
catch (error
|
|
411
|
+
catch (error) {
|
|
403
412
|
this.log({
|
|
404
413
|
level: LoggerLayerEnum[LoggerLayerEnum.CRITICAL].toLowerCase(),
|
|
405
|
-
message: this.getErrorMessage(`Schema main controller handler: ${error.message}. Extra
|
|
414
|
+
message: this.getErrorMessage(`Schema main controller handler: ${error.message}. Extra: ${JSON.stringify(error?.response?.data)}`),
|
|
406
415
|
});
|
|
407
416
|
return this.responseProtocolSwitch({
|
|
408
417
|
protocol: this.protocol,
|