@xrystal/core 3.5.8 → 3.6.1
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 +1 -1
- package/source/loader/logger/index.d.ts +1 -1
- package/source/loader/logger/index.js +31 -35
- package/x/docker/Dockerfile +1 -2
- package/x/dist/index.d.ts +0 -2
- package/x/dist/index.js +0 -8
- package/x/dist/loader/configs/index.d.ts +0 -13
- package/x/dist/loader/configs/index.js +0 -17
- package/x/dist/loader/events/index.d.ts +0 -6
- package/x/dist/loader/events/index.js +0 -25
- package/x/dist/loader/index.d.ts +0 -6
- package/x/dist/loader/index.js +0 -6
- package/x/dist/loader/localizations/index.d.ts +0 -14
- package/x/dist/loader/localizations/index.js +0 -32
- package/x/dist/loader/logger/index.d.ts +0 -28
- package/x/dist/loader/logger/index.js +0 -175
- package/x/dist/loader/system/index.d.ts +0 -8
- package/x/dist/loader/system/index.js +0 -14
- package/x/dist/project/index.d.ts +0 -6
- package/x/dist/project/index.js +0 -63
- package/x/dist/utils/constants/index.d.ts +0 -8
- package/x/dist/utils/constants/index.js +0 -10
- package/x/dist/utils/helpers/date/index.d.ts +0 -16
- package/x/dist/utils/helpers/date/index.js +0 -48
- package/x/dist/utils/helpers/filters/index.d.ts +0 -17
- package/x/dist/utils/helpers/filters/index.js +0 -44
- package/x/dist/utils/helpers/hash/crypto.d.ts +0 -3
- package/x/dist/utils/helpers/hash/crypto.js +0 -22
- package/x/dist/utils/helpers/id/index.d.ts +0 -13
- package/x/dist/utils/helpers/id/index.js +0 -24
- package/x/dist/utils/helpers/index.d.ts +0 -17
- package/x/dist/utils/helpers/index.js +0 -17
- package/x/dist/utils/helpers/ip/index.d.ts +0 -1
- package/x/dist/utils/helpers/ip/index.js +0 -3
- package/x/dist/utils/helpers/is/index.d.ts +0 -11
- package/x/dist/utils/helpers/is/index.js +0 -35
- package/x/dist/utils/helpers/locales/index.d.ts +0 -52
- package/x/dist/utils/helpers/locales/index.js +0 -161
- package/x/dist/utils/helpers/locales copy/index.d.ts +0 -52
- package/x/dist/utils/helpers/locales copy/index.js +0 -161
- package/x/dist/utils/helpers/math/index.d.ts +0 -2
- package/x/dist/utils/helpers/math/index.js +0 -14
- package/x/dist/utils/helpers/objects/index.d.ts +0 -1
- package/x/dist/utils/helpers/objects/index.js +0 -55
- package/x/dist/utils/helpers/path/index.d.ts +0 -2
- package/x/dist/utils/helpers/path/index.js +0 -4
- package/x/dist/utils/helpers/regex/checkSpecialRegexControl.d.ts +0 -1
- package/x/dist/utils/helpers/regex/checkSpecialRegexControl.js +0 -3
- package/x/dist/utils/helpers/string/index.d.ts +0 -1
- package/x/dist/utils/helpers/string/index.js +0 -9
- package/x/dist/utils/helpers/timer/index.d.ts +0 -3
- package/x/dist/utils/helpers/timer/index.js +0 -5
- package/x/dist/utils/helpers/tmp/index.d.ts +0 -8
- package/x/dist/utils/helpers/tmp/index.js +0 -109
- package/x/dist/utils/helpers/validates/index.d.ts +0 -5
- package/x/dist/utils/helpers/validates/index.js +0 -20
- package/x/dist/utils/index.d.ts +0 -3
- package/x/dist/utils/index.js +0 -3
- package/x/dist/utils/models/classes/class.controller.d.ts +0 -80
- package/x/dist/utils/models/classes/class.controller.js +0 -164
- package/x/dist/utils/models/classes/class.response.d.ts +0 -17
- package/x/dist/utils/models/classes/class.response.js +0 -37
- package/x/dist/utils/models/classes/class.services.d.ts +0 -129
- package/x/dist/utils/models/classes/class.services.js +0 -344
- package/x/dist/utils/models/classes/class.tmp-file-loader.d.ts +0 -8
- package/x/dist/utils/models/classes/class.tmp-file-loader.js +0 -38
- package/x/dist/utils/models/classes/class.x.d.ts +0 -12
- package/x/dist/utils/models/classes/class.x.js +0 -16
- package/x/dist/utils/models/enums/index.d.ts +0 -117
- package/x/dist/utils/models/enums/index.js +0 -133
- package/x/dist/utils/models/index.d.ts +0 -8
- package/x/dist/utils/models/index.js +0 -8
- package/x/dist/utils/models/types/index.d.ts +0 -3
- package/x/dist/utils/models/types/index.js +0 -2
|
@@ -1,164 +0,0 @@
|
|
|
1
|
-
import qs from 'qs';
|
|
2
|
-
import { LoggerService } from '../../../loader';
|
|
3
|
-
import { responseMessageHelper, ResponseSchema, x } from '../../index';
|
|
4
|
-
class Controller {
|
|
5
|
-
logger = x.get(LoggerService);
|
|
6
|
-
protocol = null;
|
|
7
|
-
req = null;
|
|
8
|
-
res = null;
|
|
9
|
-
constructor({ protocol, req, res, ctx }) {
|
|
10
|
-
this.protocol = protocol;
|
|
11
|
-
if (ctx) {
|
|
12
|
-
this.req = {
|
|
13
|
-
url: ctx.request.url,
|
|
14
|
-
method: ctx.request.method,
|
|
15
|
-
headers: Object.fromEntries(ctx.request.headers),
|
|
16
|
-
body: ctx.body,
|
|
17
|
-
params: ctx.params,
|
|
18
|
-
query: ctx.query || {},
|
|
19
|
-
accounts: ctx.user || ctx.accounts,
|
|
20
|
-
t: ctx.t
|
|
21
|
-
};
|
|
22
|
-
this.res = {
|
|
23
|
-
locals: {},
|
|
24
|
-
status(code) {
|
|
25
|
-
this.locals._code = code;
|
|
26
|
-
return this;
|
|
27
|
-
},
|
|
28
|
-
send(data) {
|
|
29
|
-
return new Response(JSON.stringify(data), {
|
|
30
|
-
status: this.locals._code || 200,
|
|
31
|
-
headers: { 'content-type': 'application/json' }
|
|
32
|
-
});
|
|
33
|
-
},
|
|
34
|
-
json(data) {
|
|
35
|
-
return this.send(data);
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
|
-
}
|
|
39
|
-
else {
|
|
40
|
-
this.req = {
|
|
41
|
-
url: req?.originalUrl || req?.url,
|
|
42
|
-
method: req?.method,
|
|
43
|
-
headers: req?.headers || {},
|
|
44
|
-
body: req?.body,
|
|
45
|
-
params: req?.params,
|
|
46
|
-
query: req?.query || {},
|
|
47
|
-
accounts: req?.accounts,
|
|
48
|
-
t: req?.t
|
|
49
|
-
};
|
|
50
|
-
this.res = {
|
|
51
|
-
locals: res?.locals || {},
|
|
52
|
-
status(code) {
|
|
53
|
-
res.status(code);
|
|
54
|
-
return this;
|
|
55
|
-
},
|
|
56
|
-
send(data) {
|
|
57
|
-
return res.send(data);
|
|
58
|
-
},
|
|
59
|
-
json(data) {
|
|
60
|
-
return res.json(data);
|
|
61
|
-
}
|
|
62
|
-
};
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
responseProtocolSwitch = async ({ protocol, req, res, resStatus = 200, context }) => {
|
|
66
|
-
const responseData = context({ localeLanguageConverter: req.t });
|
|
67
|
-
return res.status(resStatus).send(responseData);
|
|
68
|
-
};
|
|
69
|
-
parsedQuerys = (url) => {
|
|
70
|
-
const queryString = url.includes('?') ? url.split('?')[1] : '';
|
|
71
|
-
if (!queryString)
|
|
72
|
-
return {};
|
|
73
|
-
return qs.parse(queryString, { decoder: decodeURIComponent });
|
|
74
|
-
};
|
|
75
|
-
log = async (level, message) => {
|
|
76
|
-
this.logger.winston.log({ level, message });
|
|
77
|
-
};
|
|
78
|
-
}
|
|
79
|
-
export class ControllerSchema extends Controller {
|
|
80
|
-
async schema({ checks, logic, response, }) {
|
|
81
|
-
const payload = { req: this.req, res: this.res };
|
|
82
|
-
const convertedPayload = {
|
|
83
|
-
...payload,
|
|
84
|
-
parsedQuerys: this.parsedQuerys(this.req.url)
|
|
85
|
-
};
|
|
86
|
-
try {
|
|
87
|
-
if (checks) {
|
|
88
|
-
const checkResult = await checks({ payload, convertedPayload });
|
|
89
|
-
let isInvalid = false;
|
|
90
|
-
if (Array.isArray(checkResult)) {
|
|
91
|
-
isInvalid = !checkResult.every(c => c !== undefined && c !== null && c !== "");
|
|
92
|
-
}
|
|
93
|
-
else if (typeof checkResult === 'object' && checkResult.message) {
|
|
94
|
-
return await this.responseProtocolSwitch({
|
|
95
|
-
protocol: this.protocol,
|
|
96
|
-
req: this.req,
|
|
97
|
-
res: this.res,
|
|
98
|
-
context: () => new ResponseSchema({
|
|
99
|
-
status: checkResult.status || false,
|
|
100
|
-
message: checkResult.message,
|
|
101
|
-
payload: checkResult.payload,
|
|
102
|
-
code: checkResult.code
|
|
103
|
-
}).getResponse
|
|
104
|
-
});
|
|
105
|
-
}
|
|
106
|
-
if (isInvalid) {
|
|
107
|
-
return await this.responseProtocolSwitch({
|
|
108
|
-
protocol: this.protocol,
|
|
109
|
-
req: this.req,
|
|
110
|
-
res: this.res,
|
|
111
|
-
resStatus: 400,
|
|
112
|
-
context: ({ localeLanguageConverter }) => new ResponseSchema({
|
|
113
|
-
status: false,
|
|
114
|
-
message: responseMessageHelper.schemaMissingDataChecks(localeLanguageConverter)
|
|
115
|
-
}).getResponse
|
|
116
|
-
});
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
const logicResult = await logic({ payload, convertedPayload });
|
|
120
|
-
if (logicResult.response instanceof Function) {
|
|
121
|
-
return logicResult.response(logicResult.payload);
|
|
122
|
-
}
|
|
123
|
-
if (logicResult.message) {
|
|
124
|
-
return await this.responseProtocolSwitch({
|
|
125
|
-
protocol: this.protocol,
|
|
126
|
-
req: this.req,
|
|
127
|
-
res: this.res,
|
|
128
|
-
resStatus: 400,
|
|
129
|
-
context: () => new ResponseSchema({
|
|
130
|
-
status: logicResult.status || false,
|
|
131
|
-
message: logicResult.message,
|
|
132
|
-
payload: logicResult.payload,
|
|
133
|
-
code: logicResult.code
|
|
134
|
-
}).getResponse
|
|
135
|
-
});
|
|
136
|
-
}
|
|
137
|
-
if (response) {
|
|
138
|
-
const resResult = await response({ payload, convertedPayload });
|
|
139
|
-
if (resResult.response instanceof Function) {
|
|
140
|
-
return resResult.response(resResult);
|
|
141
|
-
}
|
|
142
|
-
return await this.responseProtocolSwitch({
|
|
143
|
-
protocol: this.protocol,
|
|
144
|
-
req: this.req,
|
|
145
|
-
res: this.res,
|
|
146
|
-
resStatus: 200,
|
|
147
|
-
context: ({ localeLanguageConverter }) => new ResponseSchema({
|
|
148
|
-
status: true,
|
|
149
|
-
message: responseMessageHelper.successFully(resResult.message[0], resResult.message[1], localeLanguageConverter),
|
|
150
|
-
payload: logicResult.payload
|
|
151
|
-
}).getResponse
|
|
152
|
-
});
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
catch (error) {
|
|
156
|
-
await this.log('error', `Schema Error: ${error.message}`);
|
|
157
|
-
return this.res.status(500).send({
|
|
158
|
-
status: false,
|
|
159
|
-
message: error.message
|
|
160
|
-
});
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
export { Controller };
|
|
@@ -1,17 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,37 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,129 +0,0 @@
|
|
|
1
|
-
import { AxiosInstance } from 'axios';
|
|
2
|
-
import nodemailer from 'nodemailer';
|
|
3
|
-
import { LoggerService } from '../../../loader/index';
|
|
4
|
-
export interface IAuth {
|
|
5
|
-
authentication: (params: any) => Promise<void> | any;
|
|
6
|
-
}
|
|
7
|
-
declare abstract class Service {
|
|
8
|
-
protected logger: LoggerService;
|
|
9
|
-
clientName: string;
|
|
10
|
-
protected baseURL: string;
|
|
11
|
-
protected version: string | null;
|
|
12
|
-
protected headers: any;
|
|
13
|
-
protected timeout: number;
|
|
14
|
-
/***************
|
|
15
|
-
* CONSTRUCTOR *
|
|
16
|
-
***************/
|
|
17
|
-
protected constructor({ clientName, baseURL, version, timeout, headers, }: {
|
|
18
|
-
clientName: string;
|
|
19
|
-
baseURL: string;
|
|
20
|
-
version?: string;
|
|
21
|
-
timeout?: number;
|
|
22
|
-
headers?: {
|
|
23
|
-
'Content-Type'?: string;
|
|
24
|
-
'Lang'?: string;
|
|
25
|
-
'Authorization'?: string;
|
|
26
|
-
};
|
|
27
|
-
});
|
|
28
|
-
/***********
|
|
29
|
-
* HELPERS *
|
|
30
|
-
***********/
|
|
31
|
-
protected interceptorErrorComplement(error: any): void;
|
|
32
|
-
routeSlashChecker(route: string): string;
|
|
33
|
-
static cryptoHashGenerate: ({ algorithm, input, digest }: {
|
|
34
|
-
algorithm: string;
|
|
35
|
-
input: string;
|
|
36
|
-
digest?: string;
|
|
37
|
-
}) => string;
|
|
38
|
-
static cryptoHashDecrypt: ({ algorithm, input, keyEncoding, key, initializationVector, inputEncoding, outputEncoding, }: {
|
|
39
|
-
algorithm: string;
|
|
40
|
-
input: string;
|
|
41
|
-
keyEncoding?: string;
|
|
42
|
-
key: string;
|
|
43
|
-
initializationVector: string;
|
|
44
|
-
inputEncoding?: BufferEncoding;
|
|
45
|
-
outputEncoding?: BufferEncoding;
|
|
46
|
-
}) => string;
|
|
47
|
-
static objectToFormData: (object: any) => FormData;
|
|
48
|
-
static generateRandomNumber: ({ prefix, suffix, hyphen, length, totalLength }: {
|
|
49
|
-
prefix?: string;
|
|
50
|
-
suffix?: string;
|
|
51
|
-
hyphen?: boolean;
|
|
52
|
-
length?: number;
|
|
53
|
-
totalLength?: number | null;
|
|
54
|
-
}) => string;
|
|
55
|
-
}
|
|
56
|
-
export declare class ServiceStore {
|
|
57
|
-
private static _instance;
|
|
58
|
-
private _store;
|
|
59
|
-
private constructor();
|
|
60
|
-
static create(): ServiceStore;
|
|
61
|
-
set setStore(callback: (store: Record<any, any>) => any);
|
|
62
|
-
get instance(): Record<any, any> | null;
|
|
63
|
-
get store(): Record<any, any>;
|
|
64
|
-
}
|
|
65
|
-
export declare class EmailClient extends Service {
|
|
66
|
-
private _port;
|
|
67
|
-
private _username;
|
|
68
|
-
private _password;
|
|
69
|
-
constructor({ clientName, baseURL, version, port, username, password, }: {
|
|
70
|
-
clientName: string;
|
|
71
|
-
baseURL: string;
|
|
72
|
-
version?: string;
|
|
73
|
-
port?: number;
|
|
74
|
-
username: string;
|
|
75
|
-
password: string;
|
|
76
|
-
});
|
|
77
|
-
/***********
|
|
78
|
-
* LOADERS *
|
|
79
|
-
***********/
|
|
80
|
-
nodemailerLoader({ host, port, username, password, secure, }: {
|
|
81
|
-
host?: string;
|
|
82
|
-
port?: number;
|
|
83
|
-
username?: string;
|
|
84
|
-
password?: string;
|
|
85
|
-
secure?: boolean;
|
|
86
|
-
}): nodemailer.Transporter<import("nodemailer/lib/smtp-transport").SentMessageInfo, import("nodemailer/lib/smtp-transport").Options>;
|
|
87
|
-
}
|
|
88
|
-
export declare class AxiosClient extends Service {
|
|
89
|
-
constructor({ clientName, baseURL, version }: {
|
|
90
|
-
clientName: string;
|
|
91
|
-
baseURL: string;
|
|
92
|
-
version?: string;
|
|
93
|
-
});
|
|
94
|
-
/***********
|
|
95
|
-
* LOADERS *
|
|
96
|
-
***********/
|
|
97
|
-
protected axiosLoader(): AxiosInstance;
|
|
98
|
-
}
|
|
99
|
-
export declare class SoapClient extends Service {
|
|
100
|
-
private client;
|
|
101
|
-
constructor({ clientName, baseURL, version }: {
|
|
102
|
-
clientName: string;
|
|
103
|
-
baseURL: string;
|
|
104
|
-
version?: string;
|
|
105
|
-
});
|
|
106
|
-
getAsyncBaseClient(): Promise<{
|
|
107
|
-
[key: string]: any;
|
|
108
|
-
}>;
|
|
109
|
-
createAsyncClient({ fullURL }: {
|
|
110
|
-
fullURL: string;
|
|
111
|
-
}): Promise<{
|
|
112
|
-
[key: string]: any;
|
|
113
|
-
}>;
|
|
114
|
-
getAsyncMethod(methodName: string, pathname: string, args?: {
|
|
115
|
-
[key: string | number]: any;
|
|
116
|
-
}): Promise<{
|
|
117
|
-
[key: string]: any;
|
|
118
|
-
}>;
|
|
119
|
-
}
|
|
120
|
-
export declare class BaseApiClient extends AxiosClient implements IAuth {
|
|
121
|
-
constructor({ clientName, baseURL, version }: {
|
|
122
|
-
clientName: string;
|
|
123
|
-
baseURL: string;
|
|
124
|
-
version: string;
|
|
125
|
-
});
|
|
126
|
-
protected axiosLoaderAuthMiddleware(): AxiosInstance;
|
|
127
|
-
authentication: () => Promise<import("axios").AxiosResponse<any, any, {}>>;
|
|
128
|
-
}
|
|
129
|
-
export default Service;
|
|
@@ -1,344 +0,0 @@
|
|
|
1
|
-
import path from 'path';
|
|
2
|
-
import axios from 'axios';
|
|
3
|
-
import soap from 'soap';
|
|
4
|
-
// => special
|
|
5
|
-
import nodemailer from 'nodemailer';
|
|
6
|
-
import hbs from 'nodemailer-express-handlebars';
|
|
7
|
-
//
|
|
8
|
-
import { LoggerService } from '../../../loader/index';
|
|
9
|
-
import { x, LoggerLayerEnum, TokensEnum } from '../../index';
|
|
10
|
-
// => for cryptography
|
|
11
|
-
import crypto from 'crypto';
|
|
12
|
-
class Service {
|
|
13
|
-
logger = x.get(LoggerService);
|
|
14
|
-
//private _instance: Record<string, any> = {}
|
|
15
|
-
clientName;
|
|
16
|
-
baseURL;
|
|
17
|
-
version = null;
|
|
18
|
-
headers; /* unknow */
|
|
19
|
-
timeout = 10000;
|
|
20
|
-
/***************
|
|
21
|
-
* CONSTRUCTOR *
|
|
22
|
-
***************/
|
|
23
|
-
constructor({ clientName, baseURL, version, timeout, headers, }) {
|
|
24
|
-
this.clientName = clientName;
|
|
25
|
-
this.baseURL = baseURL;
|
|
26
|
-
version && this.version;
|
|
27
|
-
if (timeout) {
|
|
28
|
-
this.timeout = timeout;
|
|
29
|
-
}
|
|
30
|
-
if (headers) {
|
|
31
|
-
this.headers = {
|
|
32
|
-
...headers
|
|
33
|
-
};
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
/***********
|
|
37
|
-
* HELPERS *
|
|
38
|
-
***********/
|
|
39
|
-
interceptorErrorComplement(error /* unknow */) {
|
|
40
|
-
if (error.response) {
|
|
41
|
-
this.logger.winston.info({
|
|
42
|
-
level: LoggerLayerEnum[LoggerLayerEnum.ERROR].toLowerCase(),
|
|
43
|
-
message: `${this.clientName} api service error: ${JSON.stringify(error.response)}`,
|
|
44
|
-
});
|
|
45
|
-
}
|
|
46
|
-
else {
|
|
47
|
-
this.logger.winston.info({
|
|
48
|
-
level: LoggerLayerEnum[LoggerLayerEnum.CRITICAL].toLowerCase(),
|
|
49
|
-
message: `${this.clientName} api service error: ${JSON.stringify(error.message)}`,
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
routeSlashChecker(route) {
|
|
54
|
-
const splash = route.split('/');
|
|
55
|
-
let withSplash;
|
|
56
|
-
if (splash[0] === '') {
|
|
57
|
-
withSplash = route.substring(1);
|
|
58
|
-
}
|
|
59
|
-
else {
|
|
60
|
-
withSplash = route;
|
|
61
|
-
}
|
|
62
|
-
return withSplash;
|
|
63
|
-
}
|
|
64
|
-
static cryptoHashGenerate = ({ algorithm, input, digest = 'string' }) => {
|
|
65
|
-
return crypto.createHash(algorithm).update(input).digest(digest);
|
|
66
|
-
};
|
|
67
|
-
static cryptoHashDecrypt = ({ algorithm, input, keyEncoding = 'hex', key, initializationVector, inputEncoding = 'base64', outputEncoding = 'utf8', }) => {
|
|
68
|
-
const decipher = crypto.createDecipheriv(algorithm, Buffer.from(key, keyEncoding), Buffer.from(initializationVector, inputEncoding));
|
|
69
|
-
let decrypted = decipher.update(input, inputEncoding, outputEncoding);
|
|
70
|
-
decrypted += decipher.final(outputEncoding);
|
|
71
|
-
return decrypted;
|
|
72
|
-
};
|
|
73
|
-
static objectToFormData = (object) => {
|
|
74
|
-
const formData = new FormData();
|
|
75
|
-
for (const [key, value] of Object.entries(object)) {
|
|
76
|
-
let changedValue = null;
|
|
77
|
-
changedValue = value;
|
|
78
|
-
if (Array.isArray(value)) {
|
|
79
|
-
changedValue = JSON.stringify(value);
|
|
80
|
-
}
|
|
81
|
-
formData.append(key, changedValue);
|
|
82
|
-
}
|
|
83
|
-
return formData;
|
|
84
|
-
};
|
|
85
|
-
static generateRandomNumber = ({ prefix, suffix, hyphen = true, length = 10, totalLength = null }) => {
|
|
86
|
-
if (!Number.isInteger(length) || length <= 0) {
|
|
87
|
-
throw new Error(`Invalid length.`);
|
|
88
|
-
}
|
|
89
|
-
if (totalLength) {
|
|
90
|
-
const prefixLength = prefix ? Number(prefix?.length) : null;
|
|
91
|
-
const suffixLength = suffix ? Number(suffix?.length) : null;
|
|
92
|
-
if (prefixLength && !Number.isInteger(prefixLength) || length <= 0 ||
|
|
93
|
-
suffixLength && !Number.isInteger(suffixLength) || length <= 0) {
|
|
94
|
-
throw new Error(`Invalid type.`);
|
|
95
|
-
}
|
|
96
|
-
if (prefixLength) {
|
|
97
|
-
totalLength -= prefixLength + 1;
|
|
98
|
-
}
|
|
99
|
-
if (suffixLength) {
|
|
100
|
-
totalLength -= suffixLength + 1;
|
|
101
|
-
}
|
|
102
|
-
length = totalLength;
|
|
103
|
-
}
|
|
104
|
-
let randomNumber = '';
|
|
105
|
-
for (let i = 0; i < length; i++) {
|
|
106
|
-
const randomDigit = Math.floor(Math.random() * 10);
|
|
107
|
-
randomNumber += randomDigit.toString();
|
|
108
|
-
}
|
|
109
|
-
return `${prefix && hyphen ? prefix + '-' : ''}${randomNumber}${suffix && hyphen ? '-' + suffix : ''}`;
|
|
110
|
-
};
|
|
111
|
-
}
|
|
112
|
-
export class ServiceStore {
|
|
113
|
-
static _instance = null;
|
|
114
|
-
_store = {};
|
|
115
|
-
constructor() { }
|
|
116
|
-
static create() {
|
|
117
|
-
if (!ServiceStore._instance) {
|
|
118
|
-
ServiceStore._instance = new ServiceStore();
|
|
119
|
-
}
|
|
120
|
-
return ServiceStore._instance;
|
|
121
|
-
}
|
|
122
|
-
set setStore(callback) {
|
|
123
|
-
const returnCallback = callback(this._store);
|
|
124
|
-
this._store = {
|
|
125
|
-
...this._store,
|
|
126
|
-
...returnCallback
|
|
127
|
-
};
|
|
128
|
-
}
|
|
129
|
-
get instance() {
|
|
130
|
-
return ServiceStore._instance;
|
|
131
|
-
}
|
|
132
|
-
get store() {
|
|
133
|
-
return this._store;
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
export class EmailClient extends Service {
|
|
137
|
-
_port = null;
|
|
138
|
-
_username = null;
|
|
139
|
-
_password = null;
|
|
140
|
-
constructor({ clientName, baseURL, version, port, username, password, }) {
|
|
141
|
-
super({ clientName, baseURL, version });
|
|
142
|
-
this._username = username;
|
|
143
|
-
this._password = password;
|
|
144
|
-
}
|
|
145
|
-
/***********
|
|
146
|
-
* LOADERS *
|
|
147
|
-
***********/
|
|
148
|
-
nodemailerLoader({ host, port, username, password, secure = true, }) {
|
|
149
|
-
const transporter = nodemailer.createTransport({
|
|
150
|
-
//@ts-ignore
|
|
151
|
-
host: host ? host : this.baseURL,
|
|
152
|
-
port: port ? port : this._port,
|
|
153
|
-
secure,
|
|
154
|
-
auth: {
|
|
155
|
-
user: username ? username : this._username,
|
|
156
|
-
pass: password ? password : this._password,
|
|
157
|
-
},
|
|
158
|
-
});
|
|
159
|
-
transporter.use('compile', hbs({
|
|
160
|
-
viewEngine: {
|
|
161
|
-
extname: '.hbs',
|
|
162
|
-
layoutsDir: path.resolve('source', 'static', 'email'),
|
|
163
|
-
defaultLayout: 'index',
|
|
164
|
-
partialsDir: path.resolve('source', 'static', 'email'),
|
|
165
|
-
},
|
|
166
|
-
viewPath: path.resolve('source', 'static', 'email', 'templates'),
|
|
167
|
-
extName: '.hbs'
|
|
168
|
-
}));
|
|
169
|
-
return transporter;
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
export class AxiosClient extends Service {
|
|
173
|
-
constructor({ clientName, baseURL, version }) {
|
|
174
|
-
super({ clientName, baseURL, version });
|
|
175
|
-
}
|
|
176
|
-
/***********
|
|
177
|
-
* LOADERS *
|
|
178
|
-
***********/
|
|
179
|
-
axiosLoader() {
|
|
180
|
-
const axiosInstance = axios.create({
|
|
181
|
-
baseURL: this.baseURL,
|
|
182
|
-
timeout: this.timeout,
|
|
183
|
-
headers: {
|
|
184
|
-
...this.headers
|
|
185
|
-
},
|
|
186
|
-
});
|
|
187
|
-
axiosInstance.interceptors.request.use((config /* unknow */) => {
|
|
188
|
-
return config;
|
|
189
|
-
}, (error) => {
|
|
190
|
-
this.logger.winston.info({
|
|
191
|
-
level: LoggerLayerEnum[LoggerLayerEnum.CRITICAL].toLowerCase(),
|
|
192
|
-
message: `${this.clientName} api service error: ${JSON.stringify(error)}`,
|
|
193
|
-
});
|
|
194
|
-
return Promise.reject(error);
|
|
195
|
-
});
|
|
196
|
-
return axiosInstance;
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
export class SoapClient extends Service {
|
|
200
|
-
client;
|
|
201
|
-
constructor({ clientName, baseURL, version }) {
|
|
202
|
-
super({ clientName, baseURL, version });
|
|
203
|
-
}
|
|
204
|
-
async getAsyncBaseClient() {
|
|
205
|
-
try {
|
|
206
|
-
this.client = await soap.createClientAsync(this.baseURL);
|
|
207
|
-
return this.client;
|
|
208
|
-
}
|
|
209
|
-
catch (exception) {
|
|
210
|
-
this.logger.winston.error({
|
|
211
|
-
level: LoggerLayerEnum[LoggerLayerEnum.ERROR].toLowerCase(),
|
|
212
|
-
message: `${this.clientName} - soap client error: ${exception}`,
|
|
213
|
-
});
|
|
214
|
-
return null;
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
async createAsyncClient({ fullURL }) {
|
|
218
|
-
try {
|
|
219
|
-
this.client = await soap.createClientAsync(fullURL);
|
|
220
|
-
//console.log(this.client)
|
|
221
|
-
return this.client;
|
|
222
|
-
}
|
|
223
|
-
catch (exception) {
|
|
224
|
-
this.logger.winston.error({
|
|
225
|
-
level: LoggerLayerEnum[LoggerLayerEnum.ERROR].toLowerCase(),
|
|
226
|
-
message: `${this.clientName} - soap client error: ${exception}`,
|
|
227
|
-
});
|
|
228
|
-
return null;
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
async getAsyncMethod(methodName, pathname, args) {
|
|
232
|
-
let client = null;
|
|
233
|
-
let result = null;
|
|
234
|
-
if (pathname) {
|
|
235
|
-
client = await this.createAsyncClient({ fullURL: `${this.baseURL}${pathname}` });
|
|
236
|
-
}
|
|
237
|
-
else {
|
|
238
|
-
client = await this.getAsyncBaseClient();
|
|
239
|
-
}
|
|
240
|
-
//console.log('client: ', client)
|
|
241
|
-
if (client) {
|
|
242
|
-
try {
|
|
243
|
-
let data = await client[`${methodName}Async`](args);
|
|
244
|
-
result = data[0];
|
|
245
|
-
}
|
|
246
|
-
catch (exception) {
|
|
247
|
-
this.logger.winston.error({
|
|
248
|
-
level: LoggerLayerEnum[LoggerLayerEnum.ERROR].toLowerCase(),
|
|
249
|
-
message: `${this.clientName} - soap client method error: ${exception}`,
|
|
250
|
-
});
|
|
251
|
-
return null;
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
else {
|
|
255
|
-
return null;
|
|
256
|
-
}
|
|
257
|
-
return result;
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
|
-
export class BaseApiClient extends AxiosClient {
|
|
261
|
-
constructor({ clientName, baseURL, version }) {
|
|
262
|
-
super({
|
|
263
|
-
clientName,
|
|
264
|
-
baseURL,
|
|
265
|
-
version
|
|
266
|
-
});
|
|
267
|
-
}
|
|
268
|
-
axiosLoaderAuthMiddleware() {
|
|
269
|
-
const axiosInstance = this.axiosLoader();
|
|
270
|
-
let _retry = false;
|
|
271
|
-
axiosInstance.interceptors.request.use((config) => {
|
|
272
|
-
const accessToken = (ServiceStore.create()).store?.[this.clientName]?.accessToken;
|
|
273
|
-
if (accessToken) {
|
|
274
|
-
config.headers['Cookie'] = `${TokensEnum.ACCESS_TOKEN}:${accessToken}`;
|
|
275
|
-
}
|
|
276
|
-
return config;
|
|
277
|
-
}, (error) => {
|
|
278
|
-
return Promise.reject(error);
|
|
279
|
-
});
|
|
280
|
-
axiosInstance.interceptors.response.use(async (response) => {
|
|
281
|
-
const originalRequest = response.config;
|
|
282
|
-
if (!_retry && response.data?.success === false && response.data?.status_code === 101) {
|
|
283
|
-
this.logger.winston.info({
|
|
284
|
-
level: LoggerLayerEnum[LoggerLayerEnum.INFO].toLowerCase(),
|
|
285
|
-
message: `${this.clientName} client - token refreshed!`,
|
|
286
|
-
});
|
|
287
|
-
try {
|
|
288
|
-
await this.authentication();
|
|
289
|
-
_retry = true;
|
|
290
|
-
originalRequest.headers['Cookie'] = `${TokensEnum.ACCESS_TOKEN}:${(ServiceStore.create()).store?.[this.clientName]?.accessToken}`;
|
|
291
|
-
//console.log(originalConfig)
|
|
292
|
-
return axiosInstance(originalRequest);
|
|
293
|
-
}
|
|
294
|
-
catch (error) {
|
|
295
|
-
this.logger.winston.info({
|
|
296
|
-
level: LoggerLayerEnum[LoggerLayerEnum.INFO].toLowerCase(),
|
|
297
|
-
message: `${this.clientName} client - token refresh request not initial!`,
|
|
298
|
-
});
|
|
299
|
-
return Promise.reject(error);
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
return response;
|
|
303
|
-
}, async (error) => {
|
|
304
|
-
const originalConfig = error.config;
|
|
305
|
-
if (error.response.status === 401 && !originalConfig._retry) {
|
|
306
|
-
this.logger.winston.info({
|
|
307
|
-
level: LoggerLayerEnum[LoggerLayerEnum.INFO].toLowerCase(),
|
|
308
|
-
message: `${this.clientName} client - token refreshed!`,
|
|
309
|
-
});
|
|
310
|
-
originalConfig._retry = true;
|
|
311
|
-
try {
|
|
312
|
-
await this.authentication();
|
|
313
|
-
const accessToken = (ServiceStore.create()).store?.[this.clientName]?.accessToken;
|
|
314
|
-
originalConfig.headers['Cookie'] = `${TokensEnum.ACCESS_TOKEN}:${accessToken}`;
|
|
315
|
-
//console.log(originalConfig)
|
|
316
|
-
return axiosInstance(originalConfig);
|
|
317
|
-
}
|
|
318
|
-
catch (error) {
|
|
319
|
-
this.logger.winston.info({
|
|
320
|
-
level: LoggerLayerEnum[LoggerLayerEnum.INFO].toLowerCase(),
|
|
321
|
-
message: `${this.clientName} client - token refresh request not initial!`,
|
|
322
|
-
});
|
|
323
|
-
return Promise.reject(error);
|
|
324
|
-
}
|
|
325
|
-
}
|
|
326
|
-
return Promise.reject(error);
|
|
327
|
-
});
|
|
328
|
-
return axiosInstance;
|
|
329
|
-
}
|
|
330
|
-
authentication = async () => {
|
|
331
|
-
const response = await this.axiosLoader().post(`/${this.version}/auth/sign-in`, {});
|
|
332
|
-
if (response.data.success !== true) {
|
|
333
|
-
throw new Error('Authorization not implemented');
|
|
334
|
-
}
|
|
335
|
-
const servicesStore = ServiceStore.create();
|
|
336
|
-
servicesStore.setStore = ((prevState => ({
|
|
337
|
-
[this.clientName]: {
|
|
338
|
-
accessToken: response.data?.payload?.data?.token
|
|
339
|
-
}
|
|
340
|
-
})));
|
|
341
|
-
return response;
|
|
342
|
-
};
|
|
343
|
-
}
|
|
344
|
-
export default Service;
|