@xrystal/core 3.2.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/README.md +1 -0
- package/bin/constants/index.mjs +14 -0
- package/bin/helpers/index.mjs +83 -0
- package/bin/main-cli.js +179 -0
- package/package.json +113 -0
- package/source/index.d.ts +2 -0
- package/source/index.js +8 -0
- package/source/loader/configs/index.d.ts +13 -0
- package/source/loader/configs/index.js +17 -0
- package/source/loader/events/index.d.ts +6 -0
- package/source/loader/events/index.js +25 -0
- package/source/loader/index.d.ts +6 -0
- package/source/loader/index.js +6 -0
- package/source/loader/localizations/index.d.ts +14 -0
- package/source/loader/localizations/index.js +32 -0
- package/source/loader/logger/index.d.ts +22 -0
- package/source/loader/logger/index.js +130 -0
- package/source/loader/system/index.d.ts +8 -0
- package/source/loader/system/index.js +14 -0
- package/source/project/index.d.ts +7 -0
- package/source/project/index.js +94 -0
- package/source/utils/constants/index.d.ts +8 -0
- package/source/utils/constants/index.js +10 -0
- package/source/utils/helpers/date/index.d.ts +16 -0
- package/source/utils/helpers/date/index.js +48 -0
- package/source/utils/helpers/filters/index.d.ts +17 -0
- package/source/utils/helpers/filters/index.js +44 -0
- package/source/utils/helpers/hash/crypto.d.ts +3 -0
- package/source/utils/helpers/hash/crypto.js +22 -0
- package/source/utils/helpers/id/index.d.ts +13 -0
- package/source/utils/helpers/id/index.js +24 -0
- package/source/utils/helpers/index.d.ts +16 -0
- package/source/utils/helpers/index.js +16 -0
- package/source/utils/helpers/ip/index.d.ts +1 -0
- package/source/utils/helpers/ip/index.js +3 -0
- package/source/utils/helpers/is/index.d.ts +11 -0
- package/source/utils/helpers/is/index.js +35 -0
- package/source/utils/helpers/locales/index.d.ts +52 -0
- package/source/utils/helpers/locales/index.js +161 -0
- package/source/utils/helpers/locales copy/index.d.ts +52 -0
- package/source/utils/helpers/locales copy/index.js +161 -0
- package/source/utils/helpers/math/index.d.ts +2 -0
- package/source/utils/helpers/math/index.js +14 -0
- package/source/utils/helpers/objects/index.d.ts +1 -0
- package/source/utils/helpers/objects/index.js +55 -0
- package/source/utils/helpers/path/index.d.ts +2 -0
- package/source/utils/helpers/path/index.js +4 -0
- package/source/utils/helpers/regex/checkSpecialRegexControl.d.ts +1 -0
- package/source/utils/helpers/regex/checkSpecialRegexControl.js +3 -0
- package/source/utils/helpers/string/index.d.ts +1 -0
- package/source/utils/helpers/string/index.js +9 -0
- package/source/utils/helpers/timer/index.d.ts +3 -0
- package/source/utils/helpers/timer/index.js +5 -0
- package/source/utils/helpers/tmp/index.d.ts +8 -0
- package/source/utils/helpers/tmp/index.js +109 -0
- package/source/utils/helpers/validates/index.d.ts +5 -0
- package/source/utils/helpers/validates/index.js +20 -0
- package/source/utils/index.d.ts +3 -0
- package/source/utils/index.js +3 -0
- package/source/utils/models/classes/class.controller.d.ts +121 -0
- package/source/utils/models/classes/class.controller.js +421 -0
- package/source/utils/models/classes/class.response.d.ts +17 -0
- package/source/utils/models/classes/class.response.js +37 -0
- package/source/utils/models/classes/class.services.d.ts +129 -0
- package/source/utils/models/classes/class.services.js +344 -0
- package/source/utils/models/classes/class.tmp-file-loader.d.ts +11 -0
- package/source/utils/models/classes/class.tmp-file-loader.js +38 -0
- package/source/utils/models/classes/class.x.d.ts +12 -0
- package/source/utils/models/classes/class.x.js +16 -0
- package/source/utils/models/enums/index.d.ts +116 -0
- package/source/utils/models/enums/index.js +132 -0
- package/source/utils/models/index.d.ts +8 -0
- package/source/utils/models/index.js +8 -0
- package/source/utils/models/types/index.d.ts +3 -0
- package/source/utils/models/types/index.js +2 -0
- package/x/tmp.yml +26 -0
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { NextFunction, Response } from 'express';
|
|
2
|
+
import { ParsedQs } from 'qs';
|
|
3
|
+
import { ProtocolEnum, ResponseSchema } from '../../index';
|
|
4
|
+
export interface CustomRequest extends Request {
|
|
5
|
+
accounts?: any;
|
|
6
|
+
query: {
|
|
7
|
+
sort?: string;
|
|
8
|
+
['start-date']?: string;
|
|
9
|
+
date?: string;
|
|
10
|
+
['end-date']?: string;
|
|
11
|
+
offset?: string;
|
|
12
|
+
limit?: string;
|
|
13
|
+
} & ParsedQs;
|
|
14
|
+
}
|
|
15
|
+
export interface CustomResponse extends Response {
|
|
16
|
+
locals: {
|
|
17
|
+
ID?: any;
|
|
18
|
+
user?: any;
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
export interface ReturnChecksCallbackFuncInterface {
|
|
22
|
+
status?: boolean;
|
|
23
|
+
message?: string;
|
|
24
|
+
payload?: any;
|
|
25
|
+
code?: number;
|
|
26
|
+
response?: Function;
|
|
27
|
+
invalid?: boolean;
|
|
28
|
+
}
|
|
29
|
+
export interface ReturnLogicCallbackFuncInterface {
|
|
30
|
+
status?: boolean;
|
|
31
|
+
message?: string;
|
|
32
|
+
payload?: any;
|
|
33
|
+
code?: number;
|
|
34
|
+
response?: Response | NextFunction | Function;
|
|
35
|
+
}
|
|
36
|
+
export interface ReturnResponseCallbackFuncInterface {
|
|
37
|
+
message: Array<string>;
|
|
38
|
+
response?: Response;
|
|
39
|
+
}
|
|
40
|
+
export type Sort = {
|
|
41
|
+
key: string;
|
|
42
|
+
value: number;
|
|
43
|
+
};
|
|
44
|
+
declare abstract class Controller {
|
|
45
|
+
private logger;
|
|
46
|
+
protected protocol: ProtocolEnum | null;
|
|
47
|
+
protected socket: any | null;
|
|
48
|
+
protected req: CustomRequest | null | undefined;
|
|
49
|
+
protected res: Response | null | undefined;
|
|
50
|
+
protected accounts: any;
|
|
51
|
+
protected startDate: string | null;
|
|
52
|
+
protected date: string | null;
|
|
53
|
+
protected dateStart: string | null;
|
|
54
|
+
protected endDate: string | null;
|
|
55
|
+
protected sort: Sort | null;
|
|
56
|
+
protected offset: string | null;
|
|
57
|
+
protected limit: string | null;
|
|
58
|
+
defaultLimitSize: number;
|
|
59
|
+
maxLimitSize: number;
|
|
60
|
+
constructor({ protocol, socket, req, res }: {
|
|
61
|
+
protocol: ProtocolEnum;
|
|
62
|
+
socket?: any;
|
|
63
|
+
req?: CustomRequest;
|
|
64
|
+
res?: Response;
|
|
65
|
+
});
|
|
66
|
+
protected payloadProtocolSwitch: ({ protocol, socket, req, res }: {
|
|
67
|
+
protocol: ProtocolEnum;
|
|
68
|
+
socket?: any;
|
|
69
|
+
req?: any;
|
|
70
|
+
res?: Response;
|
|
71
|
+
}) => Promise<any>;
|
|
72
|
+
protected convertedPayloadProtocolSwitch: ({ protocol, socket, req, res, }: {
|
|
73
|
+
protocol: ProtocolEnum;
|
|
74
|
+
socket?: any;
|
|
75
|
+
req?: any;
|
|
76
|
+
res?: Response;
|
|
77
|
+
}) => Promise<any>;
|
|
78
|
+
protected responseProtocolSwitch: ({ protocol, socket, req, res, resStatus, context }: {
|
|
79
|
+
protocol: ProtocolEnum;
|
|
80
|
+
socket?: any;
|
|
81
|
+
req?: any;
|
|
82
|
+
res?: Response;
|
|
83
|
+
resStatus?: number;
|
|
84
|
+
context: ({ localeLanguageConverter }: {
|
|
85
|
+
localeLanguageConverter?: Function;
|
|
86
|
+
}) => ResponseSchema;
|
|
87
|
+
}) => Promise<Response | ResponseSchema>;
|
|
88
|
+
protected parsedQuerys: (url: string) => Record<any, any>;
|
|
89
|
+
protected parsedQuerysWithTrueType: (url: string) => Record<string, any>;
|
|
90
|
+
protected dateNativeUTCToUTCConverter: (date: string) => string;
|
|
91
|
+
protected sortParser: (sort: string) => Sort | null;
|
|
92
|
+
protected aprotator(value: any): boolean;
|
|
93
|
+
protected log: ({ level, message }: {
|
|
94
|
+
level: string;
|
|
95
|
+
message: string;
|
|
96
|
+
}) => Promise<void>;
|
|
97
|
+
protected getErrorMessage(error: unknown): string;
|
|
98
|
+
}
|
|
99
|
+
declare class ControllerSchema extends Controller {
|
|
100
|
+
constructor({ protocol, socket, req, res }: {
|
|
101
|
+
protocol: ProtocolEnum;
|
|
102
|
+
socket?: any;
|
|
103
|
+
req?: CustomRequest;
|
|
104
|
+
res?: Response;
|
|
105
|
+
});
|
|
106
|
+
schema({ checks, logic, response, }: {
|
|
107
|
+
checks?: ({ payload, convertedPayload }: {
|
|
108
|
+
payload?: any;
|
|
109
|
+
convertedPayload?: any;
|
|
110
|
+
}) => Promise<ReturnChecksCallbackFuncInterface | Array<any>>;
|
|
111
|
+
logic: ({ payload, convertedPayload }: {
|
|
112
|
+
payload?: any;
|
|
113
|
+
convertedPayload?: any;
|
|
114
|
+
}) => Promise<ReturnLogicCallbackFuncInterface>;
|
|
115
|
+
response?: ({ payload, convertedPayload }: {
|
|
116
|
+
payload?: any;
|
|
117
|
+
convertedPayload?: any;
|
|
118
|
+
}) => Promise<ReturnResponseCallbackFuncInterface>;
|
|
119
|
+
}): Promise<Response | ResponseSchema>;
|
|
120
|
+
}
|
|
121
|
+
export { Controller, ControllerSchema };
|
|
@@ -0,0 +1,421 @@
|
|
|
1
|
+
import qs from 'qs';
|
|
2
|
+
import { LoggerService } from '../../../loader';
|
|
3
|
+
import { LoggerLayerEnum, ProtocolEnum, responseMessageHelper, ResponseSchema, x } from '../../index';
|
|
4
|
+
class Controller {
|
|
5
|
+
logger = x.get(LoggerService);
|
|
6
|
+
//private static logger: LogCallback | null = null
|
|
7
|
+
// => it wants properties for proccessing
|
|
8
|
+
protocol = null;
|
|
9
|
+
socket = null;
|
|
10
|
+
req = null;
|
|
11
|
+
res = null;
|
|
12
|
+
//
|
|
13
|
+
// => special properties
|
|
14
|
+
accounts = null;
|
|
15
|
+
// => special querys
|
|
16
|
+
startDate = null;
|
|
17
|
+
date = null;
|
|
18
|
+
dateStart = null;
|
|
19
|
+
endDate = null;
|
|
20
|
+
sort = null;
|
|
21
|
+
offset = null;
|
|
22
|
+
limit = null;
|
|
23
|
+
//
|
|
24
|
+
// => options
|
|
25
|
+
defaultLimitSize = 20;
|
|
26
|
+
maxLimitSize = 100;
|
|
27
|
+
//
|
|
28
|
+
constructor({ protocol, socket, req, res }) {
|
|
29
|
+
this.protocol = protocol;
|
|
30
|
+
this.socket = socket;
|
|
31
|
+
this.req = req;
|
|
32
|
+
this.res = res;
|
|
33
|
+
}
|
|
34
|
+
// => to switch
|
|
35
|
+
payloadProtocolSwitch = async ({ protocol, socket, req, res }) => {
|
|
36
|
+
let payload = {};
|
|
37
|
+
switch (protocol) {
|
|
38
|
+
case ProtocolEnum.HTTP:
|
|
39
|
+
case ProtocolEnum.HTTPS:
|
|
40
|
+
if ((protocol === ProtocolEnum.HTTP || protocol === ProtocolEnum.HTTPS) && (!req || !res)) {
|
|
41
|
+
throw new Error('Req and res');
|
|
42
|
+
}
|
|
43
|
+
this.req = req;
|
|
44
|
+
this.res = res;
|
|
45
|
+
const parsedQuerys = this.parsedQuerys(req?.originalUrl ? req?.originalUrl : '');
|
|
46
|
+
payload = {
|
|
47
|
+
//socket: this.socket,
|
|
48
|
+
req: this.req,
|
|
49
|
+
res: this.res,
|
|
50
|
+
accounts: this.req?.accounts && this.req.accounts,
|
|
51
|
+
parsedQuerys: parsedQuerys,
|
|
52
|
+
};
|
|
53
|
+
break;
|
|
54
|
+
case ProtocolEnum.SOCKETIO:
|
|
55
|
+
payload = {};
|
|
56
|
+
break;
|
|
57
|
+
case ProtocolEnum.NONE:
|
|
58
|
+
payload = {};
|
|
59
|
+
break;
|
|
60
|
+
default:
|
|
61
|
+
throw new Error('Protocol not match.');
|
|
62
|
+
}
|
|
63
|
+
return payload;
|
|
64
|
+
};
|
|
65
|
+
convertedPayloadProtocolSwitch = async ({ protocol, socket, req, res, }) => {
|
|
66
|
+
let convertedPayload = {};
|
|
67
|
+
switch (protocol) {
|
|
68
|
+
case ProtocolEnum.HTTP:
|
|
69
|
+
case ProtocolEnum.HTTPS:
|
|
70
|
+
const parsedQuerys = this.parsedQuerys(req?.originalUrl ? req.originalUrl : '');
|
|
71
|
+
const parsedQuerysWithTrueType = this.parsedQuerysWithTrueType(req?.originalUrl ? req.originalUrl : '');
|
|
72
|
+
convertedPayload = {
|
|
73
|
+
//socket: this.socket,
|
|
74
|
+
req: this.req,
|
|
75
|
+
res: this.res,
|
|
76
|
+
accounts: this.req?.accounts,
|
|
77
|
+
parsedQuerys: parsedQuerys,
|
|
78
|
+
parsedQuerysWithTrueType: parsedQuerysWithTrueType,
|
|
79
|
+
startDate: parsedQuerysWithTrueType?.['start-date'] ? this.dateNativeUTCToUTCConverter(parsedQuerysWithTrueType['start-date']) : this.startDate,
|
|
80
|
+
startDateTimezone: parsedQuerysWithTrueType?.['start-date'] ? parsedQuerysWithTrueType['start-date'].split('+')[1] : this.startDate,
|
|
81
|
+
date: this.date ? this.dateNativeUTCToUTCConverter(parsedQuerysWithTrueType.date) : this.date,
|
|
82
|
+
dateTimezone: this.date ? parsedQuerysWithTrueType.date.split('+')[1] : this.date,
|
|
83
|
+
endDate: parsedQuerysWithTrueType?.['end-date'] ? this.dateNativeUTCToUTCConverter(parsedQuerysWithTrueType['end-date']) : this.endDate,
|
|
84
|
+
endDateTimezone: parsedQuerysWithTrueType?.['end-date'] ? parsedQuerysWithTrueType['end-date'].split('+')[1] : this.endDate,
|
|
85
|
+
sort: parsedQuerysWithTrueType.sort ? this.sortParser(parsedQuerys.sort) : this.sort,
|
|
86
|
+
offset: parsedQuerysWithTrueType.offset ? parsedQuerys.offset : this.offset,
|
|
87
|
+
limit: parsedQuerysWithTrueType.limit ? parsedQuerysWithTrueType.limit > this.maxLimitSize ?
|
|
88
|
+
String(this.maxLimitSize) :
|
|
89
|
+
parsedQuerys.limit : this.defaultLimitSize
|
|
90
|
+
};
|
|
91
|
+
break;
|
|
92
|
+
case ProtocolEnum.WEBSOCKET:
|
|
93
|
+
convertedPayload = {};
|
|
94
|
+
break;
|
|
95
|
+
case ProtocolEnum.SOCKETIO:
|
|
96
|
+
convertedPayload = {};
|
|
97
|
+
break;
|
|
98
|
+
case ProtocolEnum.NONE:
|
|
99
|
+
convertedPayload = {};
|
|
100
|
+
break;
|
|
101
|
+
default:
|
|
102
|
+
throw new Error('Protocol not match.');
|
|
103
|
+
}
|
|
104
|
+
return convertedPayload;
|
|
105
|
+
};
|
|
106
|
+
responseProtocolSwitch = async ({ protocol, socket, req, res, resStatus = 200, context }) => {
|
|
107
|
+
switch (protocol) {
|
|
108
|
+
case ProtocolEnum.HTTP:
|
|
109
|
+
case ProtocolEnum.HTTPS:
|
|
110
|
+
if (!req || !res) {
|
|
111
|
+
throw new Error(`req or res not found`);
|
|
112
|
+
}
|
|
113
|
+
return res.status(resStatus).send(context({
|
|
114
|
+
localeLanguageConverter: req.t
|
|
115
|
+
}));
|
|
116
|
+
case ProtocolEnum.WEBSOCKET:
|
|
117
|
+
return context({});
|
|
118
|
+
case ProtocolEnum.SOCKETIO:
|
|
119
|
+
return context({});
|
|
120
|
+
case ProtocolEnum.NONE:
|
|
121
|
+
return context({});
|
|
122
|
+
default:
|
|
123
|
+
throw new Error('Protocol not match.');
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
//
|
|
127
|
+
// => helper
|
|
128
|
+
parsedQuerys = (url) => {
|
|
129
|
+
const parseUrlForQuerys = url.split('?')?.[1];
|
|
130
|
+
let convertedQuerys;
|
|
131
|
+
if (parseUrlForQuerys) {
|
|
132
|
+
convertedQuerys = qs.parse(parseUrlForQuerys, { decoder: decodeURIComponent });
|
|
133
|
+
}
|
|
134
|
+
else {
|
|
135
|
+
convertedQuerys = {};
|
|
136
|
+
}
|
|
137
|
+
return convertedQuerys;
|
|
138
|
+
};
|
|
139
|
+
parsedQuerysWithTrueType = (url) => {
|
|
140
|
+
const parsedParams = {};
|
|
141
|
+
Object.entries(this.parsedQuerys(url))?.forEach(([key, value]) => {
|
|
142
|
+
if (value === 'undefined') {
|
|
143
|
+
parsedParams[key] = undefined;
|
|
144
|
+
}
|
|
145
|
+
else if (value === 'null') {
|
|
146
|
+
parsedParams[key] = null;
|
|
147
|
+
}
|
|
148
|
+
else if (value === '') {
|
|
149
|
+
parsedParams[key] = '';
|
|
150
|
+
}
|
|
151
|
+
else if (!isNaN(value)) {
|
|
152
|
+
parsedParams[key] = Number(value);
|
|
153
|
+
}
|
|
154
|
+
else if (value?.toLowerCase() === 'true' || value?.toLowerCase() === 'false') {
|
|
155
|
+
parsedParams[key] = value?.toLowerCase() === 'true';
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
parsedParams[key] = value;
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
return parsedParams;
|
|
162
|
+
};
|
|
163
|
+
dateNativeUTCToUTCConverter = (date) => {
|
|
164
|
+
const convertedDate = (new Date(date)).toISOString();
|
|
165
|
+
return convertedDate;
|
|
166
|
+
};
|
|
167
|
+
sortParser = (sort) => {
|
|
168
|
+
if (String(sort)) {
|
|
169
|
+
const parsed = sort.split('+');
|
|
170
|
+
if (!parsed[0] || !parsed[1]) {
|
|
171
|
+
this.log({ level: LoggerLayerEnum[LoggerLayerEnum.ERROR].toLowerCase(), message: 'sort query parameter invalid.' });
|
|
172
|
+
return null;
|
|
173
|
+
}
|
|
174
|
+
return {
|
|
175
|
+
key: parsed[0],
|
|
176
|
+
value: Number(parsed[1])
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
else {
|
|
180
|
+
return null;
|
|
181
|
+
}
|
|
182
|
+
};
|
|
183
|
+
aprotator(value) {
|
|
184
|
+
return value === 0 ? true : !!value;
|
|
185
|
+
}
|
|
186
|
+
log = async ({ level, message }) => {
|
|
187
|
+
this.logger.winston.log({
|
|
188
|
+
level: level,
|
|
189
|
+
message,
|
|
190
|
+
});
|
|
191
|
+
};
|
|
192
|
+
getErrorMessage(error) {
|
|
193
|
+
if (error instanceof Error)
|
|
194
|
+
return error.message;
|
|
195
|
+
return String(error);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
class ControllerSchema extends Controller {
|
|
199
|
+
constructor({ protocol, socket, req, res }) {
|
|
200
|
+
super({
|
|
201
|
+
protocol,
|
|
202
|
+
socket,
|
|
203
|
+
req,
|
|
204
|
+
res
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
async schema({ checks, logic, response, }) {
|
|
208
|
+
const { res, req } = this;
|
|
209
|
+
try {
|
|
210
|
+
let logicCallbackFunc;
|
|
211
|
+
// controlling all properties start
|
|
212
|
+
try {
|
|
213
|
+
if (checks) {
|
|
214
|
+
const checksCallbackFunc = await checks({
|
|
215
|
+
payload: this.payloadProtocolSwitch,
|
|
216
|
+
convertedPayload: await this.convertedPayloadProtocolSwitch({
|
|
217
|
+
protocol: this.protocol,
|
|
218
|
+
socket: this.socket,
|
|
219
|
+
req: req,
|
|
220
|
+
res: res
|
|
221
|
+
})
|
|
222
|
+
});
|
|
223
|
+
let checked = false;
|
|
224
|
+
if (Array.isArray(checksCallbackFunc)) {
|
|
225
|
+
checked = checksCallbackFunc.every((check) => check !== undefined && check !== null && check !== "");
|
|
226
|
+
}
|
|
227
|
+
else if (!Array.isArray(checksCallbackFunc) && checksCallbackFunc?.message) {
|
|
228
|
+
return this.responseProtocolSwitch({
|
|
229
|
+
protocol: this.protocol,
|
|
230
|
+
req: this.req,
|
|
231
|
+
res: this.res,
|
|
232
|
+
resStatus: 200,
|
|
233
|
+
context: ({ localeLanguageConverter }) => new ResponseSchema({
|
|
234
|
+
status: checksCallbackFunc.status || false,
|
|
235
|
+
message: checksCallbackFunc?.message,
|
|
236
|
+
...(this.aprotator(checksCallbackFunc?.payload) && { payload: checksCallbackFunc?.payload }),
|
|
237
|
+
code: checksCallbackFunc?.code
|
|
238
|
+
}).getResponse
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
else if (typeof checksCallbackFunc === 'object' && typeof checksCallbackFunc?.response === 'function') {
|
|
242
|
+
if (typeof checksCallbackFunc.response() === 'boolean') {
|
|
243
|
+
checked = checksCallbackFunc.response();
|
|
244
|
+
}
|
|
245
|
+
else {
|
|
246
|
+
return checked = checksCallbackFunc.response();
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
if (!checked) {
|
|
250
|
+
return this.responseProtocolSwitch({
|
|
251
|
+
protocol: this.protocol,
|
|
252
|
+
socket: this.socket,
|
|
253
|
+
req: this.req,
|
|
254
|
+
res: this.res,
|
|
255
|
+
resStatus: 400,
|
|
256
|
+
context: ({ localeLanguageConverter }) => new ResponseSchema({
|
|
257
|
+
status: false,
|
|
258
|
+
message: Array.isArray(checksCallbackFunc) ?
|
|
259
|
+
`${responseMessageHelper.schemaMissingDataChecks(localeLanguageConverter)}` :
|
|
260
|
+
checksCallbackFunc?.invalid ? `${responseMessageHelper.schemaInvalidDataChecks(localeLanguageConverter)}` :
|
|
261
|
+
`${responseMessageHelper.schemaMissingDataChecks(localeLanguageConverter)}`,
|
|
262
|
+
}).getResponse
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
catch (error) {
|
|
268
|
+
this.log({
|
|
269
|
+
level: LoggerLayerEnum[LoggerLayerEnum.CRITICAL].toLowerCase(),
|
|
270
|
+
message: this.getErrorMessage(`Schema check controller handler: ${error.message}. Extra explanation: ${JSON.stringify(error.response?.data)}`),
|
|
271
|
+
});
|
|
272
|
+
return this.responseProtocolSwitch({
|
|
273
|
+
protocol: this.protocol,
|
|
274
|
+
socket: this.socket,
|
|
275
|
+
req: this.req,
|
|
276
|
+
res: this.res,
|
|
277
|
+
resStatus: 500,
|
|
278
|
+
context: ({ localeLanguageConverter }) => new ResponseSchema({
|
|
279
|
+
status: false,
|
|
280
|
+
message: `${responseMessageHelper.schemaUnknowErrorChecks(error.message, JSON.stringify(error.response?.data), localeLanguageConverter)}`,
|
|
281
|
+
}).getResponse
|
|
282
|
+
});
|
|
283
|
+
}
|
|
284
|
+
//end
|
|
285
|
+
// logic response start
|
|
286
|
+
try { // => (message and response) first rendering. If there is no message or data, it goes to the response scope.
|
|
287
|
+
logicCallbackFunc = await logic({
|
|
288
|
+
payload: this.payloadProtocolSwitch,
|
|
289
|
+
convertedPayload: await this.convertedPayloadProtocolSwitch({
|
|
290
|
+
protocol: this.protocol,
|
|
291
|
+
socket: this.socket,
|
|
292
|
+
req: req,
|
|
293
|
+
res: res
|
|
294
|
+
})
|
|
295
|
+
});
|
|
296
|
+
if (logicCallbackFunc?.response) {
|
|
297
|
+
if (logicCallbackFunc.response instanceof Function) {
|
|
298
|
+
return logicCallbackFunc.response(logicCallbackFunc?.payload);
|
|
299
|
+
}
|
|
300
|
+
else {
|
|
301
|
+
throw new Error('Invalid type for logicCallbackFunc.response');
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
if (logicCallbackFunc?.message) {
|
|
305
|
+
return this.responseProtocolSwitch({
|
|
306
|
+
protocol: this.protocol,
|
|
307
|
+
socket: this.socket,
|
|
308
|
+
req: this.req,
|
|
309
|
+
res: this.res,
|
|
310
|
+
resStatus: 400,
|
|
311
|
+
context: ({ localeLanguageConverter }) => new ResponseSchema({
|
|
312
|
+
status: logicCallbackFunc?.status || false,
|
|
313
|
+
message: logicCallbackFunc?.message,
|
|
314
|
+
...(this.aprotator(logicCallbackFunc?.payload) && { payload: logicCallbackFunc?.payload }),
|
|
315
|
+
code: logicCallbackFunc?.code
|
|
316
|
+
}).getResponse
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
catch (error /* unknow */) {
|
|
321
|
+
this.log({
|
|
322
|
+
level: LoggerLayerEnum[LoggerLayerEnum.CRITICAL].toLowerCase(),
|
|
323
|
+
message: this.getErrorMessage(`Schema logic controller handler: ${error.message}. Extra explanation: ${JSON.stringify(error?.response?.data)}`),
|
|
324
|
+
});
|
|
325
|
+
return this.responseProtocolSwitch({
|
|
326
|
+
protocol: this.protocol,
|
|
327
|
+
socket: this.socket,
|
|
328
|
+
req: this.req,
|
|
329
|
+
res: this.res,
|
|
330
|
+
resStatus: 500,
|
|
331
|
+
context: ({ localeLanguageConverter }) => new ResponseSchema({
|
|
332
|
+
status: false,
|
|
333
|
+
message: `${responseMessageHelper.schemaUnknowErrorLogic(error.message, JSON.stringify(error?.response?.data), localeLanguageConverter)}`,
|
|
334
|
+
}).getResponse
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
//end
|
|
338
|
+
// return response start
|
|
339
|
+
try {
|
|
340
|
+
if (response) {
|
|
341
|
+
const responseCallbackFunc = await response({
|
|
342
|
+
payload: this.payloadProtocolSwitch,
|
|
343
|
+
convertedPayload: await this.convertedPayloadProtocolSwitch({
|
|
344
|
+
protocol: this.protocol,
|
|
345
|
+
socket: this.socket,
|
|
346
|
+
req: req,
|
|
347
|
+
res: res
|
|
348
|
+
})
|
|
349
|
+
});
|
|
350
|
+
if (!responseCallbackFunc?.message) {
|
|
351
|
+
throw new Error('Message required: Please add the responseObj.message');
|
|
352
|
+
}
|
|
353
|
+
if (responseCallbackFunc?.response) {
|
|
354
|
+
if (responseCallbackFunc.response instanceof Function) {
|
|
355
|
+
return responseCallbackFunc.response(responseCallbackFunc);
|
|
356
|
+
}
|
|
357
|
+
else {
|
|
358
|
+
throw new Error('Invalid type for responseCallbackFunc.response');
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
if (logicCallbackFunc) {
|
|
362
|
+
return this.responseProtocolSwitch({
|
|
363
|
+
protocol: this.protocol,
|
|
364
|
+
socket: this.socket,
|
|
365
|
+
req: this.req,
|
|
366
|
+
res: this.res,
|
|
367
|
+
resStatus: 200,
|
|
368
|
+
context: ({ localeLanguageConverter }) => new ResponseSchema({
|
|
369
|
+
status: true,
|
|
370
|
+
message: `${responseMessageHelper.successFully(responseCallbackFunc.message?.[0], responseCallbackFunc.message?.[1], localeLanguageConverter)}`,
|
|
371
|
+
...(this.aprotator(logicCallbackFunc?.payload) && { payload: logicCallbackFunc?.payload })
|
|
372
|
+
}).getResponse
|
|
373
|
+
});
|
|
374
|
+
}
|
|
375
|
+
else {
|
|
376
|
+
throw new Error("logicCallbackFunc values not found.");
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
else {
|
|
380
|
+
throw new Error('Please use response scope.');
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
catch (error /* unknow */) {
|
|
384
|
+
this.log({
|
|
385
|
+
level: LoggerLayerEnum[LoggerLayerEnum.CRITICAL].toLowerCase(),
|
|
386
|
+
message: this.getErrorMessage(`Schema response controller handler: ${error.message}. Extra explanation: ${JSON.stringify(error?.response?.data)}`),
|
|
387
|
+
});
|
|
388
|
+
return this.responseProtocolSwitch({
|
|
389
|
+
protocol: this.protocol,
|
|
390
|
+
socket: this.socket,
|
|
391
|
+
req: this.req,
|
|
392
|
+
res: this.res,
|
|
393
|
+
resStatus: 500,
|
|
394
|
+
context: ({ localeLanguageConverter }) => new ResponseSchema({
|
|
395
|
+
status: false,
|
|
396
|
+
message: `${responseMessageHelper.schemaUnknowErrorResponse(error.message, JSON.stringify(error?.response?.data), localeLanguageConverter)}`, // logic içersinde db dışında hata fırlatan data da olabilir düzenleme gerekecek
|
|
397
|
+
}).getResponse
|
|
398
|
+
});
|
|
399
|
+
}
|
|
400
|
+
// end
|
|
401
|
+
}
|
|
402
|
+
catch (error /* unknow */) {
|
|
403
|
+
this.log({
|
|
404
|
+
level: LoggerLayerEnum[LoggerLayerEnum.CRITICAL].toLowerCase(),
|
|
405
|
+
message: this.getErrorMessage(`Schema main controller handler: ${error.message}. Extra explanation: ${JSON.stringify(error?.response?.data)}`),
|
|
406
|
+
});
|
|
407
|
+
return this.responseProtocolSwitch({
|
|
408
|
+
protocol: this.protocol,
|
|
409
|
+
socket: this.socket,
|
|
410
|
+
req: this.req,
|
|
411
|
+
res: this.res,
|
|
412
|
+
resStatus: 500,
|
|
413
|
+
context: ({ localeLanguageConverter }) => new ResponseSchema({
|
|
414
|
+
status: false,
|
|
415
|
+
message: `${responseMessageHelper.schemaUnknowErrorMain(error.message, JSON.stringify(error?.response?.data), localeLanguageConverter)}`,
|
|
416
|
+
}).getResponse
|
|
417
|
+
});
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
export { Controller, ControllerSchema };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export interface PayloadSchemaInterface {
|
|
2
|
+
status: boolean;
|
|
3
|
+
message: string;
|
|
4
|
+
payload?: any;
|
|
5
|
+
[key: string]: any;
|
|
6
|
+
code?: number;
|
|
7
|
+
}
|
|
8
|
+
export declare class ResponseSchema {
|
|
9
|
+
constructor({ status, message, payload, code }: PayloadSchemaInterface);
|
|
10
|
+
status: boolean;
|
|
11
|
+
message: string;
|
|
12
|
+
payload: any;
|
|
13
|
+
code: number;
|
|
14
|
+
validData(): any;
|
|
15
|
+
statusCodeGenerator(): number;
|
|
16
|
+
get getResponse(): ResponseSchema;
|
|
17
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export class ResponseSchema {
|
|
2
|
+
constructor({ status = false, message = '', payload = null, code = 0 }) {
|
|
3
|
+
this.status = status;
|
|
4
|
+
this.message = message;
|
|
5
|
+
this.payload = payload;
|
|
6
|
+
this.code = code;
|
|
7
|
+
}
|
|
8
|
+
status;
|
|
9
|
+
message;
|
|
10
|
+
payload;
|
|
11
|
+
code;
|
|
12
|
+
validData() {
|
|
13
|
+
return this.payload; // => ileride burada değiştirmeler olabilir.
|
|
14
|
+
}
|
|
15
|
+
statusCodeGenerator() {
|
|
16
|
+
if (this.status === false && this.code === 401 || this.code === 403) {
|
|
17
|
+
return Number(this.code);
|
|
18
|
+
}
|
|
19
|
+
else if (this.status === true) {
|
|
20
|
+
return this.code = 0;
|
|
21
|
+
}
|
|
22
|
+
else {
|
|
23
|
+
return this.code = 1;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
get getResponse() {
|
|
27
|
+
let response = {
|
|
28
|
+
status: this.status,
|
|
29
|
+
message: this.message,
|
|
30
|
+
};
|
|
31
|
+
if (this.payload !== null) {
|
|
32
|
+
response.payload = this.validData();
|
|
33
|
+
}
|
|
34
|
+
response.code = this.statusCodeGenerator();
|
|
35
|
+
return { ...response };
|
|
36
|
+
}
|
|
37
|
+
}
|