@xrystal/core 3.4.7 → 3.4.9
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,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"author": "Yusuf Yasir KAYGUSUZ",
|
|
3
3
|
"name": "@xrystal/core",
|
|
4
|
-
"version": "3.4.
|
|
4
|
+
"version": "3.4.9",
|
|
5
5
|
"description": "Project core for xrystal",
|
|
6
6
|
"publishConfig": {
|
|
7
7
|
"access": "public",
|
|
@@ -43,6 +43,7 @@
|
|
|
43
43
|
"chalk": "^5.6.2",
|
|
44
44
|
"commander": "^13.0.0",
|
|
45
45
|
"ejs": "^3.1.9",
|
|
46
|
+
"elysia": "^1.4.21",
|
|
46
47
|
"handlebars": "^4.7.8",
|
|
47
48
|
"i18next": "^25.6.3",
|
|
48
49
|
"i18next-fs-backend": "^2.6.1",
|
|
@@ -67,6 +68,7 @@
|
|
|
67
68
|
"yaml": "^2.5.0"
|
|
68
69
|
},
|
|
69
70
|
"devDependencies": {
|
|
71
|
+
"@types/bun": "^1.3.5",
|
|
70
72
|
"@types/express": "^5.0.6",
|
|
71
73
|
"@types/minimist": "^1.2.5",
|
|
72
74
|
"@types/node": "^25.0.6",
|
|
@@ -1,7 +1,14 @@
|
|
|
1
|
-
import { NextFunction, Response } from 'express';
|
|
2
1
|
import { ParsedQs } from 'qs';
|
|
3
2
|
import { ProtocolEnum, ResponseSchema } from '../../index';
|
|
4
|
-
export interface
|
|
3
|
+
export interface ElysiaContext {
|
|
4
|
+
request: Request;
|
|
5
|
+
body?: any;
|
|
6
|
+
params?: Record<string, string>;
|
|
7
|
+
query?: Record<string, any>;
|
|
8
|
+
headers?: Headers;
|
|
9
|
+
[key: string]: any;
|
|
10
|
+
}
|
|
11
|
+
export interface CustomRequest {
|
|
5
12
|
accounts?: any;
|
|
6
13
|
query: {
|
|
7
14
|
sort?: string;
|
|
@@ -11,8 +18,17 @@ export interface CustomRequest extends Request {
|
|
|
11
18
|
offset?: string;
|
|
12
19
|
limit?: string;
|
|
13
20
|
} & ParsedQs;
|
|
21
|
+
url: string;
|
|
22
|
+
method: string;
|
|
23
|
+
headers: Record<string, string>;
|
|
24
|
+
body?: any;
|
|
25
|
+
params?: Record<string, string>;
|
|
26
|
+
t?: Function;
|
|
14
27
|
}
|
|
15
|
-
export interface CustomResponse
|
|
28
|
+
export interface CustomResponse {
|
|
29
|
+
status: (code: number) => CustomResponse;
|
|
30
|
+
send: (data: any) => Response;
|
|
31
|
+
json: (data: any) => Response;
|
|
16
32
|
locals: {
|
|
17
33
|
ID?: any;
|
|
18
34
|
user?: any;
|
|
@@ -31,11 +47,11 @@ export interface ReturnLogicCallbackFuncInterface {
|
|
|
31
47
|
message?: string;
|
|
32
48
|
payload?: any;
|
|
33
49
|
code?: number;
|
|
34
|
-
response?:
|
|
50
|
+
response?: CustomResponse | Function;
|
|
35
51
|
}
|
|
36
52
|
export interface ReturnResponseCallbackFuncInterface {
|
|
37
53
|
message: Array<string>;
|
|
38
|
-
response?:
|
|
54
|
+
response?: CustomResponse | Function;
|
|
39
55
|
}
|
|
40
56
|
export type Sort = {
|
|
41
57
|
key: string;
|
|
@@ -46,7 +62,7 @@ declare abstract class Controller {
|
|
|
46
62
|
protected protocol: ProtocolEnum | null;
|
|
47
63
|
protected socket: any | null;
|
|
48
64
|
protected req: CustomRequest | null | undefined;
|
|
49
|
-
protected res:
|
|
65
|
+
protected res: CustomResponse | null | undefined;
|
|
50
66
|
protected accounts: any;
|
|
51
67
|
protected startDate: string | null;
|
|
52
68
|
protected date: string | null;
|
|
@@ -57,29 +73,31 @@ declare abstract class Controller {
|
|
|
57
73
|
protected limit: string | null;
|
|
58
74
|
defaultLimitSize: number;
|
|
59
75
|
maxLimitSize: number;
|
|
60
|
-
constructor({ protocol, socket, req, res }: {
|
|
76
|
+
constructor({ protocol, socket, req, res, ctx }: {
|
|
61
77
|
protocol: ProtocolEnum;
|
|
62
78
|
socket?: any;
|
|
63
79
|
req?: CustomRequest;
|
|
64
|
-
res?:
|
|
80
|
+
res?: CustomResponse;
|
|
81
|
+
ctx?: any;
|
|
65
82
|
});
|
|
83
|
+
protected createResponse(data: any, statusCode?: number): Response;
|
|
66
84
|
protected payloadProtocolSwitch: ({ protocol, socket, req, res }: {
|
|
67
85
|
protocol: ProtocolEnum;
|
|
68
86
|
socket?: any;
|
|
69
87
|
req?: any;
|
|
70
|
-
res?:
|
|
88
|
+
res?: CustomResponse;
|
|
71
89
|
}) => Promise<any>;
|
|
72
90
|
protected convertedPayloadProtocolSwitch: ({ protocol, socket, req, res, }: {
|
|
73
91
|
protocol: ProtocolEnum;
|
|
74
92
|
socket?: any;
|
|
75
93
|
req?: any;
|
|
76
|
-
res?:
|
|
94
|
+
res?: CustomResponse;
|
|
77
95
|
}) => Promise<any>;
|
|
78
96
|
protected responseProtocolSwitch: ({ protocol, socket, req, res, resStatus, context }: {
|
|
79
97
|
protocol: ProtocolEnum;
|
|
80
98
|
socket?: any;
|
|
81
99
|
req?: any;
|
|
82
|
-
res?:
|
|
100
|
+
res?: CustomResponse;
|
|
83
101
|
resStatus?: number;
|
|
84
102
|
context: ({ localeLanguageConverter }: {
|
|
85
103
|
localeLanguageConverter?: Function;
|
|
@@ -97,11 +115,12 @@ declare abstract class Controller {
|
|
|
97
115
|
protected getErrorMessage(error: unknown): string;
|
|
98
116
|
}
|
|
99
117
|
declare class ControllerSchema extends Controller {
|
|
100
|
-
constructor({ protocol, socket, req, res }: {
|
|
118
|
+
constructor({ protocol, socket, req, res, ctx }: {
|
|
101
119
|
protocol: ProtocolEnum;
|
|
102
120
|
socket?: any;
|
|
103
121
|
req?: CustomRequest;
|
|
104
|
-
res?:
|
|
122
|
+
res?: CustomResponse;
|
|
123
|
+
ctx?: any;
|
|
105
124
|
});
|
|
106
125
|
schema({ checks, logic, response, }: {
|
|
107
126
|
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,53 @@ 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
|
-
|
|
28
|
-
|
|
20
|
+
constructor({ protocol, socket, req, res, ctx }) {
|
|
21
|
+
// Eğer ctx varsa, otomatik req/res'e çevir
|
|
22
|
+
if (ctx) {
|
|
23
|
+
req = {
|
|
24
|
+
url: ctx.request.url,
|
|
25
|
+
method: ctx.request.method,
|
|
26
|
+
headers: Object.fromEntries(ctx.request.headers),
|
|
27
|
+
body: ctx.body,
|
|
28
|
+
params: ctx.params,
|
|
29
|
+
query: ctx.query,
|
|
30
|
+
accounts: ctx.accounts || ctx.user,
|
|
31
|
+
t: ctx.t
|
|
32
|
+
};
|
|
33
|
+
res = {
|
|
34
|
+
status: (code) => res,
|
|
35
|
+
send: (data) => data,
|
|
36
|
+
json: (data) => data,
|
|
37
|
+
locals: {}
|
|
38
|
+
};
|
|
39
|
+
}
|
|
29
40
|
this.protocol = protocol;
|
|
30
41
|
this.socket = socket;
|
|
31
42
|
this.req = req;
|
|
32
43
|
this.res = res;
|
|
33
44
|
}
|
|
34
|
-
|
|
45
|
+
createResponse(data, statusCode = 200) {
|
|
46
|
+
return new Response(JSON.stringify(data), {
|
|
47
|
+
status: statusCode,
|
|
48
|
+
headers: {
|
|
49
|
+
'Content-Type': 'application/json'
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
}
|
|
35
53
|
payloadProtocolSwitch = async ({ protocol, socket, req, res }) => {
|
|
36
54
|
let payload = {};
|
|
37
55
|
switch (protocol) {
|
|
38
56
|
case ProtocolEnum.HTTP:
|
|
39
57
|
case ProtocolEnum.HTTPS:
|
|
40
58
|
if ((protocol === ProtocolEnum.HTTP || protocol === ProtocolEnum.HTTPS) && (!req || !res)) {
|
|
41
|
-
throw new Error('Req and res');
|
|
59
|
+
throw new Error('Req and res required');
|
|
42
60
|
}
|
|
43
61
|
this.req = req;
|
|
44
62
|
this.res = res;
|
|
45
|
-
const parsedQuerys = this.parsedQuerys(req?.
|
|
63
|
+
const parsedQuerys = this.parsedQuerys(req?.url ? req.url : '');
|
|
46
64
|
payload = {
|
|
47
|
-
//socket: this.socket,
|
|
48
65
|
req: this.req,
|
|
49
66
|
res: this.res,
|
|
50
67
|
accounts: this.req?.accounts && this.req.accounts,
|
|
@@ -67,26 +84,35 @@ class Controller {
|
|
|
67
84
|
switch (protocol) {
|
|
68
85
|
case ProtocolEnum.HTTP:
|
|
69
86
|
case ProtocolEnum.HTTPS:
|
|
70
|
-
const parsedQuerys = this.parsedQuerys(req?.
|
|
71
|
-
const parsedQuerysWithTrueType = this.parsedQuerysWithTrueType(req?.
|
|
87
|
+
const parsedQuerys = this.parsedQuerys(req?.url ? req.url : '');
|
|
88
|
+
const parsedQuerysWithTrueType = this.parsedQuerysWithTrueType(req?.url ? req.url : '');
|
|
72
89
|
convertedPayload = {
|
|
73
|
-
//socket: this.socket,
|
|
74
90
|
req: this.req,
|
|
75
91
|
res: this.res,
|
|
76
92
|
accounts: this.req?.accounts,
|
|
77
93
|
parsedQuerys: parsedQuerys,
|
|
78
94
|
parsedQuerysWithTrueType: parsedQuerysWithTrueType,
|
|
79
|
-
startDate: parsedQuerysWithTrueType?.['start-date']
|
|
80
|
-
|
|
95
|
+
startDate: parsedQuerysWithTrueType?.['start-date']
|
|
96
|
+
? this.dateNativeUTCToUTCConverter(parsedQuerysWithTrueType['start-date'])
|
|
97
|
+
: this.startDate,
|
|
98
|
+
startDateTimezone: parsedQuerysWithTrueType?.['start-date']
|
|
99
|
+
? parsedQuerysWithTrueType['start-date'].split('+')[1]
|
|
100
|
+
: this.startDate,
|
|
81
101
|
date: this.date ? this.dateNativeUTCToUTCConverter(parsedQuerysWithTrueType.date) : this.date,
|
|
82
102
|
dateTimezone: this.date ? parsedQuerysWithTrueType.date.split('+')[1] : this.date,
|
|
83
|
-
endDate: parsedQuerysWithTrueType?.['end-date']
|
|
84
|
-
|
|
103
|
+
endDate: parsedQuerysWithTrueType?.['end-date']
|
|
104
|
+
? this.dateNativeUTCToUTCConverter(parsedQuerysWithTrueType['end-date'])
|
|
105
|
+
: this.endDate,
|
|
106
|
+
endDateTimezone: parsedQuerysWithTrueType?.['end-date']
|
|
107
|
+
? parsedQuerysWithTrueType['end-date'].split('+')[1]
|
|
108
|
+
: this.endDate,
|
|
85
109
|
sort: parsedQuerysWithTrueType.sort ? this.sortParser(parsedQuerys.sort) : this.sort,
|
|
86
110
|
offset: parsedQuerysWithTrueType.offset ? parsedQuerys.offset : this.offset,
|
|
87
|
-
limit: parsedQuerysWithTrueType.limit
|
|
88
|
-
|
|
89
|
-
|
|
111
|
+
limit: parsedQuerysWithTrueType.limit
|
|
112
|
+
? parsedQuerysWithTrueType.limit > this.maxLimitSize
|
|
113
|
+
? String(this.maxLimitSize)
|
|
114
|
+
: parsedQuerys.limit
|
|
115
|
+
: this.defaultLimitSize
|
|
90
116
|
};
|
|
91
117
|
break;
|
|
92
118
|
case ProtocolEnum.WEBSOCKET:
|
|
@@ -110,9 +136,10 @@ class Controller {
|
|
|
110
136
|
if (!req || !res) {
|
|
111
137
|
throw new Error(`req or res not found`);
|
|
112
138
|
}
|
|
113
|
-
|
|
139
|
+
const responseData = context({
|
|
114
140
|
localeLanguageConverter: req.t
|
|
115
|
-
})
|
|
141
|
+
});
|
|
142
|
+
return this.createResponse(responseData, resStatus);
|
|
116
143
|
case ProtocolEnum.WEBSOCKET:
|
|
117
144
|
return context({});
|
|
118
145
|
case ProtocolEnum.SOCKETIO:
|
|
@@ -123,8 +150,6 @@ class Controller {
|
|
|
123
150
|
throw new Error('Protocol not match.');
|
|
124
151
|
}
|
|
125
152
|
};
|
|
126
|
-
//
|
|
127
|
-
// => helper
|
|
128
153
|
parsedQuerys = (url) => {
|
|
129
154
|
const parseUrlForQuerys = url.split('?')?.[1];
|
|
130
155
|
let convertedQuerys;
|
|
@@ -168,7 +193,10 @@ class Controller {
|
|
|
168
193
|
if (String(sort)) {
|
|
169
194
|
const parsed = sort.split('+');
|
|
170
195
|
if (!parsed[0] || !parsed[1]) {
|
|
171
|
-
this.log({
|
|
196
|
+
this.log({
|
|
197
|
+
level: LoggerLayerEnum[LoggerLayerEnum.ERROR].toLowerCase(),
|
|
198
|
+
message: 'sort query parameter invalid.'
|
|
199
|
+
});
|
|
172
200
|
return null;
|
|
173
201
|
}
|
|
174
202
|
return {
|
|
@@ -196,19 +224,20 @@ class Controller {
|
|
|
196
224
|
}
|
|
197
225
|
}
|
|
198
226
|
class ControllerSchema extends Controller {
|
|
199
|
-
constructor({ protocol, socket, req, res }) {
|
|
227
|
+
constructor({ protocol, socket, req, res, ctx }) {
|
|
200
228
|
super({
|
|
201
229
|
protocol,
|
|
202
230
|
socket,
|
|
203
231
|
req,
|
|
204
|
-
res
|
|
232
|
+
res,
|
|
233
|
+
ctx
|
|
205
234
|
});
|
|
206
235
|
}
|
|
207
236
|
async schema({ checks, logic, response, }) {
|
|
208
237
|
const { res, req } = this;
|
|
209
238
|
try {
|
|
210
239
|
let logicCallbackFunc;
|
|
211
|
-
//
|
|
240
|
+
// Checks
|
|
212
241
|
try {
|
|
213
242
|
if (checks) {
|
|
214
243
|
const checksCallbackFunc = await checks({
|
|
@@ -243,7 +272,7 @@ class ControllerSchema extends Controller {
|
|
|
243
272
|
checked = checksCallbackFunc.response();
|
|
244
273
|
}
|
|
245
274
|
else {
|
|
246
|
-
return
|
|
275
|
+
return checksCallbackFunc.response();
|
|
247
276
|
}
|
|
248
277
|
}
|
|
249
278
|
if (!checked) {
|
|
@@ -255,10 +284,11 @@ class ControllerSchema extends Controller {
|
|
|
255
284
|
resStatus: 400,
|
|
256
285
|
context: ({ localeLanguageConverter }) => new ResponseSchema({
|
|
257
286
|
status: false,
|
|
258
|
-
message: Array.isArray(checksCallbackFunc)
|
|
259
|
-
`${responseMessageHelper.schemaMissingDataChecks(localeLanguageConverter)}`
|
|
260
|
-
checksCallbackFunc?.invalid
|
|
261
|
-
`${responseMessageHelper.
|
|
287
|
+
message: Array.isArray(checksCallbackFunc)
|
|
288
|
+
? `${responseMessageHelper.schemaMissingDataChecks(localeLanguageConverter)}`
|
|
289
|
+
: checksCallbackFunc?.invalid
|
|
290
|
+
? `${responseMessageHelper.schemaInvalidDataChecks(localeLanguageConverter)}`
|
|
291
|
+
: `${responseMessageHelper.schemaMissingDataChecks(localeLanguageConverter)}`,
|
|
262
292
|
}).getResponse
|
|
263
293
|
});
|
|
264
294
|
}
|
|
@@ -267,7 +297,7 @@ class ControllerSchema extends Controller {
|
|
|
267
297
|
catch (error) {
|
|
268
298
|
this.log({
|
|
269
299
|
level: LoggerLayerEnum[LoggerLayerEnum.CRITICAL].toLowerCase(),
|
|
270
|
-
message: this.getErrorMessage(`Schema check controller handler: ${error.message}. Extra
|
|
300
|
+
message: this.getErrorMessage(`Schema check controller handler: ${error.message}. Extra: ${JSON.stringify(error.response?.data)}`),
|
|
271
301
|
});
|
|
272
302
|
return this.responseProtocolSwitch({
|
|
273
303
|
protocol: this.protocol,
|
|
@@ -281,9 +311,8 @@ class ControllerSchema extends Controller {
|
|
|
281
311
|
}).getResponse
|
|
282
312
|
});
|
|
283
313
|
}
|
|
284
|
-
//
|
|
285
|
-
|
|
286
|
-
try { // => (message and response) first rendering. If there is no message or data, it goes to the response scope.
|
|
314
|
+
// Logic
|
|
315
|
+
try {
|
|
287
316
|
logicCallbackFunc = await logic({
|
|
288
317
|
payload: this.payloadProtocolSwitch,
|
|
289
318
|
convertedPayload: await this.convertedPayloadProtocolSwitch({
|
|
@@ -317,10 +346,10 @@ class ControllerSchema extends Controller {
|
|
|
317
346
|
});
|
|
318
347
|
}
|
|
319
348
|
}
|
|
320
|
-
catch (error
|
|
349
|
+
catch (error) {
|
|
321
350
|
this.log({
|
|
322
351
|
level: LoggerLayerEnum[LoggerLayerEnum.CRITICAL].toLowerCase(),
|
|
323
|
-
message: this.getErrorMessage(`Schema logic controller handler: ${error.message}. Extra
|
|
352
|
+
message: this.getErrorMessage(`Schema logic controller handler: ${error.message}. Extra: ${JSON.stringify(error?.response?.data)}`),
|
|
324
353
|
});
|
|
325
354
|
return this.responseProtocolSwitch({
|
|
326
355
|
protocol: this.protocol,
|
|
@@ -334,8 +363,7 @@ class ControllerSchema extends Controller {
|
|
|
334
363
|
}).getResponse
|
|
335
364
|
});
|
|
336
365
|
}
|
|
337
|
-
//
|
|
338
|
-
// return response start
|
|
366
|
+
// Response
|
|
339
367
|
try {
|
|
340
368
|
if (response) {
|
|
341
369
|
const responseCallbackFunc = await response({
|
|
@@ -380,10 +408,10 @@ class ControllerSchema extends Controller {
|
|
|
380
408
|
throw new Error('Please use response scope.');
|
|
381
409
|
}
|
|
382
410
|
}
|
|
383
|
-
catch (error
|
|
411
|
+
catch (error) {
|
|
384
412
|
this.log({
|
|
385
413
|
level: LoggerLayerEnum[LoggerLayerEnum.CRITICAL].toLowerCase(),
|
|
386
|
-
message: this.getErrorMessage(`Schema response controller handler: ${error.message}. Extra
|
|
414
|
+
message: this.getErrorMessage(`Schema response controller handler: ${error.message}. Extra: ${JSON.stringify(error?.response?.data)}`),
|
|
387
415
|
});
|
|
388
416
|
return this.responseProtocolSwitch({
|
|
389
417
|
protocol: this.protocol,
|
|
@@ -393,16 +421,15 @@ class ControllerSchema extends Controller {
|
|
|
393
421
|
resStatus: 500,
|
|
394
422
|
context: ({ localeLanguageConverter }) => new ResponseSchema({
|
|
395
423
|
status: false,
|
|
396
|
-
message: `${responseMessageHelper.schemaUnknowErrorResponse(error.message, JSON.stringify(error?.response?.data), localeLanguageConverter)}`,
|
|
424
|
+
message: `${responseMessageHelper.schemaUnknowErrorResponse(error.message, JSON.stringify(error?.response?.data), localeLanguageConverter)}`,
|
|
397
425
|
}).getResponse
|
|
398
426
|
});
|
|
399
427
|
}
|
|
400
|
-
// end
|
|
401
428
|
}
|
|
402
|
-
catch (error
|
|
429
|
+
catch (error) {
|
|
403
430
|
this.log({
|
|
404
431
|
level: LoggerLayerEnum[LoggerLayerEnum.CRITICAL].toLowerCase(),
|
|
405
|
-
message: this.getErrorMessage(`Schema main controller handler: ${error.message}. Extra
|
|
432
|
+
message: this.getErrorMessage(`Schema main controller handler: ${error.message}. Extra: ${JSON.stringify(error?.response?.data)}`),
|
|
406
433
|
});
|
|
407
434
|
return this.responseProtocolSwitch({
|
|
408
435
|
protocol: this.protocol,
|
|
@@ -1,7 +1,14 @@
|
|
|
1
|
-
import { NextFunction, Response } from 'express';
|
|
2
1
|
import { ParsedQs } from 'qs';
|
|
3
2
|
import { ProtocolEnum, ResponseSchema } from '../../index';
|
|
4
|
-
export interface
|
|
3
|
+
export interface ElysiaContext {
|
|
4
|
+
request: Request;
|
|
5
|
+
body?: any;
|
|
6
|
+
params?: Record<string, string>;
|
|
7
|
+
query?: Record<string, any>;
|
|
8
|
+
headers?: Headers;
|
|
9
|
+
[key: string]: any;
|
|
10
|
+
}
|
|
11
|
+
export interface CustomRequest {
|
|
5
12
|
accounts?: any;
|
|
6
13
|
query: {
|
|
7
14
|
sort?: string;
|
|
@@ -11,8 +18,17 @@ export interface CustomRequest extends Request {
|
|
|
11
18
|
offset?: string;
|
|
12
19
|
limit?: string;
|
|
13
20
|
} & ParsedQs;
|
|
21
|
+
url: string;
|
|
22
|
+
method: string;
|
|
23
|
+
headers: Record<string, string>;
|
|
24
|
+
body?: any;
|
|
25
|
+
params?: Record<string, string>;
|
|
26
|
+
t?: Function;
|
|
14
27
|
}
|
|
15
|
-
export interface CustomResponse
|
|
28
|
+
export interface CustomResponse {
|
|
29
|
+
status: (code: number) => CustomResponse;
|
|
30
|
+
send: (data: any) => Response;
|
|
31
|
+
json: (data: any) => Response;
|
|
16
32
|
locals: {
|
|
17
33
|
ID?: any;
|
|
18
34
|
user?: any;
|
|
@@ -31,11 +47,11 @@ export interface ReturnLogicCallbackFuncInterface {
|
|
|
31
47
|
message?: string;
|
|
32
48
|
payload?: any;
|
|
33
49
|
code?: number;
|
|
34
|
-
response?:
|
|
50
|
+
response?: CustomResponse | Function;
|
|
35
51
|
}
|
|
36
52
|
export interface ReturnResponseCallbackFuncInterface {
|
|
37
53
|
message: Array<string>;
|
|
38
|
-
response?:
|
|
54
|
+
response?: CustomResponse | Function;
|
|
39
55
|
}
|
|
40
56
|
export type Sort = {
|
|
41
57
|
key: string;
|
|
@@ -46,7 +62,7 @@ declare abstract class Controller {
|
|
|
46
62
|
protected protocol: ProtocolEnum | null;
|
|
47
63
|
protected socket: any | null;
|
|
48
64
|
protected req: CustomRequest | null | undefined;
|
|
49
|
-
protected res:
|
|
65
|
+
protected res: CustomResponse | null | undefined;
|
|
50
66
|
protected accounts: any;
|
|
51
67
|
protected startDate: string | null;
|
|
52
68
|
protected date: string | null;
|
|
@@ -57,29 +73,31 @@ declare abstract class Controller {
|
|
|
57
73
|
protected limit: string | null;
|
|
58
74
|
defaultLimitSize: number;
|
|
59
75
|
maxLimitSize: number;
|
|
60
|
-
constructor({ protocol, socket, req, res }: {
|
|
76
|
+
constructor({ protocol, socket, req, res, ctx }: {
|
|
61
77
|
protocol: ProtocolEnum;
|
|
62
78
|
socket?: any;
|
|
63
79
|
req?: CustomRequest;
|
|
64
|
-
res?:
|
|
80
|
+
res?: CustomResponse;
|
|
81
|
+
ctx?: any;
|
|
65
82
|
});
|
|
83
|
+
protected createResponse(data: any, statusCode?: number): Response;
|
|
66
84
|
protected payloadProtocolSwitch: ({ protocol, socket, req, res }: {
|
|
67
85
|
protocol: ProtocolEnum;
|
|
68
86
|
socket?: any;
|
|
69
87
|
req?: any;
|
|
70
|
-
res?:
|
|
88
|
+
res?: CustomResponse;
|
|
71
89
|
}) => Promise<any>;
|
|
72
90
|
protected convertedPayloadProtocolSwitch: ({ protocol, socket, req, res, }: {
|
|
73
91
|
protocol: ProtocolEnum;
|
|
74
92
|
socket?: any;
|
|
75
93
|
req?: any;
|
|
76
|
-
res?:
|
|
94
|
+
res?: CustomResponse;
|
|
77
95
|
}) => Promise<any>;
|
|
78
96
|
protected responseProtocolSwitch: ({ protocol, socket, req, res, resStatus, context }: {
|
|
79
97
|
protocol: ProtocolEnum;
|
|
80
98
|
socket?: any;
|
|
81
99
|
req?: any;
|
|
82
|
-
res?:
|
|
100
|
+
res?: CustomResponse;
|
|
83
101
|
resStatus?: number;
|
|
84
102
|
context: ({ localeLanguageConverter }: {
|
|
85
103
|
localeLanguageConverter?: Function;
|
|
@@ -97,11 +115,12 @@ declare abstract class Controller {
|
|
|
97
115
|
protected getErrorMessage(error: unknown): string;
|
|
98
116
|
}
|
|
99
117
|
declare class ControllerSchema extends Controller {
|
|
100
|
-
constructor({ protocol, socket, req, res }: {
|
|
118
|
+
constructor({ protocol, socket, req, res, ctx }: {
|
|
101
119
|
protocol: ProtocolEnum;
|
|
102
120
|
socket?: any;
|
|
103
121
|
req?: CustomRequest;
|
|
104
|
-
res?:
|
|
122
|
+
res?: CustomResponse;
|
|
123
|
+
ctx?: any;
|
|
105
124
|
});
|
|
106
125
|
schema({ checks, logic, response, }: {
|
|
107
126
|
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,53 @@ 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
|
-
|
|
28
|
-
|
|
20
|
+
constructor({ protocol, socket, req, res, ctx }) {
|
|
21
|
+
// Eğer ctx varsa, otomatik req/res'e çevir
|
|
22
|
+
if (ctx) {
|
|
23
|
+
req = {
|
|
24
|
+
url: ctx.request.url,
|
|
25
|
+
method: ctx.request.method,
|
|
26
|
+
headers: Object.fromEntries(ctx.request.headers),
|
|
27
|
+
body: ctx.body,
|
|
28
|
+
params: ctx.params,
|
|
29
|
+
query: ctx.query,
|
|
30
|
+
accounts: ctx.accounts || ctx.user,
|
|
31
|
+
t: ctx.t
|
|
32
|
+
};
|
|
33
|
+
res = {
|
|
34
|
+
status: (code) => res,
|
|
35
|
+
send: (data) => data,
|
|
36
|
+
json: (data) => data,
|
|
37
|
+
locals: {}
|
|
38
|
+
};
|
|
39
|
+
}
|
|
29
40
|
this.protocol = protocol;
|
|
30
41
|
this.socket = socket;
|
|
31
42
|
this.req = req;
|
|
32
43
|
this.res = res;
|
|
33
44
|
}
|
|
34
|
-
|
|
45
|
+
createResponse(data, statusCode = 200) {
|
|
46
|
+
return new Response(JSON.stringify(data), {
|
|
47
|
+
status: statusCode,
|
|
48
|
+
headers: {
|
|
49
|
+
'Content-Type': 'application/json'
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
}
|
|
35
53
|
payloadProtocolSwitch = async ({ protocol, socket, req, res }) => {
|
|
36
54
|
let payload = {};
|
|
37
55
|
switch (protocol) {
|
|
38
56
|
case ProtocolEnum.HTTP:
|
|
39
57
|
case ProtocolEnum.HTTPS:
|
|
40
58
|
if ((protocol === ProtocolEnum.HTTP || protocol === ProtocolEnum.HTTPS) && (!req || !res)) {
|
|
41
|
-
throw new Error('Req and res');
|
|
59
|
+
throw new Error('Req and res required');
|
|
42
60
|
}
|
|
43
61
|
this.req = req;
|
|
44
62
|
this.res = res;
|
|
45
|
-
const parsedQuerys = this.parsedQuerys(req?.
|
|
63
|
+
const parsedQuerys = this.parsedQuerys(req?.url ? req.url : '');
|
|
46
64
|
payload = {
|
|
47
|
-
//socket: this.socket,
|
|
48
65
|
req: this.req,
|
|
49
66
|
res: this.res,
|
|
50
67
|
accounts: this.req?.accounts && this.req.accounts,
|
|
@@ -67,26 +84,35 @@ class Controller {
|
|
|
67
84
|
switch (protocol) {
|
|
68
85
|
case ProtocolEnum.HTTP:
|
|
69
86
|
case ProtocolEnum.HTTPS:
|
|
70
|
-
const parsedQuerys = this.parsedQuerys(req?.
|
|
71
|
-
const parsedQuerysWithTrueType = this.parsedQuerysWithTrueType(req?.
|
|
87
|
+
const parsedQuerys = this.parsedQuerys(req?.url ? req.url : '');
|
|
88
|
+
const parsedQuerysWithTrueType = this.parsedQuerysWithTrueType(req?.url ? req.url : '');
|
|
72
89
|
convertedPayload = {
|
|
73
|
-
//socket: this.socket,
|
|
74
90
|
req: this.req,
|
|
75
91
|
res: this.res,
|
|
76
92
|
accounts: this.req?.accounts,
|
|
77
93
|
parsedQuerys: parsedQuerys,
|
|
78
94
|
parsedQuerysWithTrueType: parsedQuerysWithTrueType,
|
|
79
|
-
startDate: parsedQuerysWithTrueType?.['start-date']
|
|
80
|
-
|
|
95
|
+
startDate: parsedQuerysWithTrueType?.['start-date']
|
|
96
|
+
? this.dateNativeUTCToUTCConverter(parsedQuerysWithTrueType['start-date'])
|
|
97
|
+
: this.startDate,
|
|
98
|
+
startDateTimezone: parsedQuerysWithTrueType?.['start-date']
|
|
99
|
+
? parsedQuerysWithTrueType['start-date'].split('+')[1]
|
|
100
|
+
: this.startDate,
|
|
81
101
|
date: this.date ? this.dateNativeUTCToUTCConverter(parsedQuerysWithTrueType.date) : this.date,
|
|
82
102
|
dateTimezone: this.date ? parsedQuerysWithTrueType.date.split('+')[1] : this.date,
|
|
83
|
-
endDate: parsedQuerysWithTrueType?.['end-date']
|
|
84
|
-
|
|
103
|
+
endDate: parsedQuerysWithTrueType?.['end-date']
|
|
104
|
+
? this.dateNativeUTCToUTCConverter(parsedQuerysWithTrueType['end-date'])
|
|
105
|
+
: this.endDate,
|
|
106
|
+
endDateTimezone: parsedQuerysWithTrueType?.['end-date']
|
|
107
|
+
? parsedQuerysWithTrueType['end-date'].split('+')[1]
|
|
108
|
+
: this.endDate,
|
|
85
109
|
sort: parsedQuerysWithTrueType.sort ? this.sortParser(parsedQuerys.sort) : this.sort,
|
|
86
110
|
offset: parsedQuerysWithTrueType.offset ? parsedQuerys.offset : this.offset,
|
|
87
|
-
limit: parsedQuerysWithTrueType.limit
|
|
88
|
-
|
|
89
|
-
|
|
111
|
+
limit: parsedQuerysWithTrueType.limit
|
|
112
|
+
? parsedQuerysWithTrueType.limit > this.maxLimitSize
|
|
113
|
+
? String(this.maxLimitSize)
|
|
114
|
+
: parsedQuerys.limit
|
|
115
|
+
: this.defaultLimitSize
|
|
90
116
|
};
|
|
91
117
|
break;
|
|
92
118
|
case ProtocolEnum.WEBSOCKET:
|
|
@@ -110,9 +136,10 @@ class Controller {
|
|
|
110
136
|
if (!req || !res) {
|
|
111
137
|
throw new Error(`req or res not found`);
|
|
112
138
|
}
|
|
113
|
-
|
|
139
|
+
const responseData = context({
|
|
114
140
|
localeLanguageConverter: req.t
|
|
115
|
-
})
|
|
141
|
+
});
|
|
142
|
+
return this.createResponse(responseData, resStatus);
|
|
116
143
|
case ProtocolEnum.WEBSOCKET:
|
|
117
144
|
return context({});
|
|
118
145
|
case ProtocolEnum.SOCKETIO:
|
|
@@ -123,8 +150,6 @@ class Controller {
|
|
|
123
150
|
throw new Error('Protocol not match.');
|
|
124
151
|
}
|
|
125
152
|
};
|
|
126
|
-
//
|
|
127
|
-
// => helper
|
|
128
153
|
parsedQuerys = (url) => {
|
|
129
154
|
const parseUrlForQuerys = url.split('?')?.[1];
|
|
130
155
|
let convertedQuerys;
|
|
@@ -168,7 +193,10 @@ class Controller {
|
|
|
168
193
|
if (String(sort)) {
|
|
169
194
|
const parsed = sort.split('+');
|
|
170
195
|
if (!parsed[0] || !parsed[1]) {
|
|
171
|
-
this.log({
|
|
196
|
+
this.log({
|
|
197
|
+
level: LoggerLayerEnum[LoggerLayerEnum.ERROR].toLowerCase(),
|
|
198
|
+
message: 'sort query parameter invalid.'
|
|
199
|
+
});
|
|
172
200
|
return null;
|
|
173
201
|
}
|
|
174
202
|
return {
|
|
@@ -196,19 +224,20 @@ class Controller {
|
|
|
196
224
|
}
|
|
197
225
|
}
|
|
198
226
|
class ControllerSchema extends Controller {
|
|
199
|
-
constructor({ protocol, socket, req, res }) {
|
|
227
|
+
constructor({ protocol, socket, req, res, ctx }) {
|
|
200
228
|
super({
|
|
201
229
|
protocol,
|
|
202
230
|
socket,
|
|
203
231
|
req,
|
|
204
|
-
res
|
|
232
|
+
res,
|
|
233
|
+
ctx
|
|
205
234
|
});
|
|
206
235
|
}
|
|
207
236
|
async schema({ checks, logic, response, }) {
|
|
208
237
|
const { res, req } = this;
|
|
209
238
|
try {
|
|
210
239
|
let logicCallbackFunc;
|
|
211
|
-
//
|
|
240
|
+
// Checks
|
|
212
241
|
try {
|
|
213
242
|
if (checks) {
|
|
214
243
|
const checksCallbackFunc = await checks({
|
|
@@ -243,7 +272,7 @@ class ControllerSchema extends Controller {
|
|
|
243
272
|
checked = checksCallbackFunc.response();
|
|
244
273
|
}
|
|
245
274
|
else {
|
|
246
|
-
return
|
|
275
|
+
return checksCallbackFunc.response();
|
|
247
276
|
}
|
|
248
277
|
}
|
|
249
278
|
if (!checked) {
|
|
@@ -255,10 +284,11 @@ class ControllerSchema extends Controller {
|
|
|
255
284
|
resStatus: 400,
|
|
256
285
|
context: ({ localeLanguageConverter }) => new ResponseSchema({
|
|
257
286
|
status: false,
|
|
258
|
-
message: Array.isArray(checksCallbackFunc)
|
|
259
|
-
`${responseMessageHelper.schemaMissingDataChecks(localeLanguageConverter)}`
|
|
260
|
-
checksCallbackFunc?.invalid
|
|
261
|
-
`${responseMessageHelper.
|
|
287
|
+
message: Array.isArray(checksCallbackFunc)
|
|
288
|
+
? `${responseMessageHelper.schemaMissingDataChecks(localeLanguageConverter)}`
|
|
289
|
+
: checksCallbackFunc?.invalid
|
|
290
|
+
? `${responseMessageHelper.schemaInvalidDataChecks(localeLanguageConverter)}`
|
|
291
|
+
: `${responseMessageHelper.schemaMissingDataChecks(localeLanguageConverter)}`,
|
|
262
292
|
}).getResponse
|
|
263
293
|
});
|
|
264
294
|
}
|
|
@@ -267,7 +297,7 @@ class ControllerSchema extends Controller {
|
|
|
267
297
|
catch (error) {
|
|
268
298
|
this.log({
|
|
269
299
|
level: LoggerLayerEnum[LoggerLayerEnum.CRITICAL].toLowerCase(),
|
|
270
|
-
message: this.getErrorMessage(`Schema check controller handler: ${error.message}. Extra
|
|
300
|
+
message: this.getErrorMessage(`Schema check controller handler: ${error.message}. Extra: ${JSON.stringify(error.response?.data)}`),
|
|
271
301
|
});
|
|
272
302
|
return this.responseProtocolSwitch({
|
|
273
303
|
protocol: this.protocol,
|
|
@@ -281,9 +311,8 @@ class ControllerSchema extends Controller {
|
|
|
281
311
|
}).getResponse
|
|
282
312
|
});
|
|
283
313
|
}
|
|
284
|
-
//
|
|
285
|
-
|
|
286
|
-
try { // => (message and response) first rendering. If there is no message or data, it goes to the response scope.
|
|
314
|
+
// Logic
|
|
315
|
+
try {
|
|
287
316
|
logicCallbackFunc = await logic({
|
|
288
317
|
payload: this.payloadProtocolSwitch,
|
|
289
318
|
convertedPayload: await this.convertedPayloadProtocolSwitch({
|
|
@@ -317,10 +346,10 @@ class ControllerSchema extends Controller {
|
|
|
317
346
|
});
|
|
318
347
|
}
|
|
319
348
|
}
|
|
320
|
-
catch (error
|
|
349
|
+
catch (error) {
|
|
321
350
|
this.log({
|
|
322
351
|
level: LoggerLayerEnum[LoggerLayerEnum.CRITICAL].toLowerCase(),
|
|
323
|
-
message: this.getErrorMessage(`Schema logic controller handler: ${error.message}. Extra
|
|
352
|
+
message: this.getErrorMessage(`Schema logic controller handler: ${error.message}. Extra: ${JSON.stringify(error?.response?.data)}`),
|
|
324
353
|
});
|
|
325
354
|
return this.responseProtocolSwitch({
|
|
326
355
|
protocol: this.protocol,
|
|
@@ -334,8 +363,7 @@ class ControllerSchema extends Controller {
|
|
|
334
363
|
}).getResponse
|
|
335
364
|
});
|
|
336
365
|
}
|
|
337
|
-
//
|
|
338
|
-
// return response start
|
|
366
|
+
// Response
|
|
339
367
|
try {
|
|
340
368
|
if (response) {
|
|
341
369
|
const responseCallbackFunc = await response({
|
|
@@ -380,10 +408,10 @@ class ControllerSchema extends Controller {
|
|
|
380
408
|
throw new Error('Please use response scope.');
|
|
381
409
|
}
|
|
382
410
|
}
|
|
383
|
-
catch (error
|
|
411
|
+
catch (error) {
|
|
384
412
|
this.log({
|
|
385
413
|
level: LoggerLayerEnum[LoggerLayerEnum.CRITICAL].toLowerCase(),
|
|
386
|
-
message: this.getErrorMessage(`Schema response controller handler: ${error.message}. Extra
|
|
414
|
+
message: this.getErrorMessage(`Schema response controller handler: ${error.message}. Extra: ${JSON.stringify(error?.response?.data)}`),
|
|
387
415
|
});
|
|
388
416
|
return this.responseProtocolSwitch({
|
|
389
417
|
protocol: this.protocol,
|
|
@@ -393,16 +421,15 @@ class ControllerSchema extends Controller {
|
|
|
393
421
|
resStatus: 500,
|
|
394
422
|
context: ({ localeLanguageConverter }) => new ResponseSchema({
|
|
395
423
|
status: false,
|
|
396
|
-
message: `${responseMessageHelper.schemaUnknowErrorResponse(error.message, JSON.stringify(error?.response?.data), localeLanguageConverter)}`,
|
|
424
|
+
message: `${responseMessageHelper.schemaUnknowErrorResponse(error.message, JSON.stringify(error?.response?.data), localeLanguageConverter)}`,
|
|
397
425
|
}).getResponse
|
|
398
426
|
});
|
|
399
427
|
}
|
|
400
|
-
// end
|
|
401
428
|
}
|
|
402
|
-
catch (error
|
|
429
|
+
catch (error) {
|
|
403
430
|
this.log({
|
|
404
431
|
level: LoggerLayerEnum[LoggerLayerEnum.CRITICAL].toLowerCase(),
|
|
405
|
-
message: this.getErrorMessage(`Schema main controller handler: ${error.message}. Extra
|
|
432
|
+
message: this.getErrorMessage(`Schema main controller handler: ${error.message}. Extra: ${JSON.stringify(error?.response?.data)}`),
|
|
406
433
|
});
|
|
407
434
|
return this.responseProtocolSwitch({
|
|
408
435
|
protocol: this.protocol,
|