itrm-tools 1.3.2 → 1.3.5-viio.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/.github/workflows/node.js.yml +30 -0
- package/README.md +2 -2
- package/dist/api/APIRequest.js +22 -29
- package/dist/api/APIRequest.js.map +1 -1
- package/dist/api/APIRouter.js +3 -1
- package/dist/api/APIRouter.js.map +1 -1
- package/dist/api/APIService.js +11 -17
- package/dist/api/APIService.js.map +1 -1
- package/dist/api/HttpsAPIService.js +5 -13
- package/dist/api/HttpsAPIService.js.map +1 -1
- package/dist/api/alegra/AlegraConnectionManager.js +23 -50
- package/dist/api/alegra/AlegraConnectionManager.js.map +1 -1
- package/dist/api/channel/ServerSentEventsManager.js +2 -1
- package/dist/api/channel/ServerSentEventsManager.js.map +1 -1
- package/dist/api/check/APIChecker.js +11 -25
- package/dist/api/check/APIChecker.js.map +1 -1
- package/dist/api/check/CheckableAPIRequest.js +17 -29
- package/dist/api/check/CheckableAPIRequest.js.map +1 -1
- package/dist/api/check/CheckableAPIRouter.js +17 -29
- package/dist/api/check/CheckableAPIRouter.js.map +1 -1
- package/dist/api/check/request/CheckableFileUploadingPostRequest.js +2 -0
- package/dist/api/check/request/CheckableFileUploadingPostRequest.js.map +1 -1
- package/dist/api/check/route/CorsCheckApiRouter.js +2 -13
- package/dist/api/check/route/CorsCheckApiRouter.js.map +1 -1
- package/dist/api/check/route/SimpleCheckableAPIRouter.js +2 -13
- package/dist/api/check/route/SimpleCheckableAPIRouter.js.map +1 -1
- package/dist/api/external/ExternalApiSigner.js +2 -0
- package/dist/api/external/ExternalApiSigner.js.map +1 -1
- package/dist/api/external/cetus/CetusStrategyApiManager.js +8 -20
- package/dist/api/external/cetus/CetusStrategyApiManager.js.map +1 -1
- package/dist/api/request/RequestAxiosCall.js +20 -37
- package/dist/api/request/RequestAxiosCall.js.map +1 -1
- package/dist/api/request/crud/CrudServiceManager.js +6 -17
- package/dist/api/request/crud/CrudServiceManager.js.map +1 -1
- package/dist/api/request/docs/DocumentationRequest.js +4 -7
- package/dist/api/request/docs/DocumentationRequest.js.map +1 -1
- package/dist/api/route/SimpleAPIRouter.js +2 -13
- package/dist/api/route/SimpleAPIRouter.js.map +1 -1
- package/dist/database/mongoose/MongooseModelManager.d.ts +8 -40
- package/dist/database/mongoose/MongooseModelManager.js +5 -14
- package/dist/database/mongoose/MongooseModelManager.js.map +1 -1
- package/dist/database/redis/RedisConnection.js +1 -0
- package/dist/database/redis/RedisConnection.js.map +1 -1
- package/dist/database/redis/RedisModelManager.js +1 -0
- package/dist/database/redis/RedisModelManager.js.map +1 -1
- package/dist/database/sequelize/SequelizeConnection.js +1 -0
- package/dist/database/sequelize/SequelizeConnection.js.map +1 -1
- package/dist/database/sequelize/SequelizeModelManager.js +6 -15
- package/dist/database/sequelize/SequelizeModelManager.js.map +1 -1
- package/dist/index.d.ts +0 -13
- package/dist/index.js +1 -28
- package/dist/index.js.map +1 -1
- package/dist/log/Logger.js +2 -0
- package/dist/log/Logger.js.map +1 -1
- package/dist/log/LoggerManager.js +11 -21
- package/dist/log/LoggerManager.js.map +1 -1
- package/dist/log/LoggerWebSocket.js +4 -2
- package/dist/log/LoggerWebSocket.js.map +1 -1
- package/dist/web3/coingecko/CoingeckoPriceFinder.js +24 -36
- package/dist/web3/coingecko/CoingeckoPriceFinder.js.map +1 -1
- package/jest.config.js +7 -0
- package/package.json +68 -74
- package/sequelize.sqlite +0 -0
- package/src/algorithm/encryption/EncryptionAlgorithm.ts +4 -0
- package/src/api/APIRequest.ts +176 -0
- package/src/api/APIRequestBuilder.ts +28 -0
- package/src/api/APIRouter.ts +26 -0
- package/src/api/APIService.ts +117 -0
- package/src/api/ExpressConfiguration.ts +5 -0
- package/src/api/HttpsAPIService.ts +35 -0
- package/src/api/alegra/AlegraConnectionManager.ts +54 -0
- package/src/api/alegra/AlegraTypes.ts +150 -0
- package/src/api/builder/StandardGetRequestBuilder.ts +34 -0
- package/src/api/channel/ServerSentEventsManager.ts +28 -0
- package/src/api/check/APIChecker.ts +50 -0
- package/src/api/check/CheckableAPIRequest.ts +33 -0
- package/src/api/check/CheckableAPIRouter.ts +39 -0
- package/src/api/check/request/CheckableDeleteRequest.ts +17 -0
- package/src/api/check/request/CheckableFileUploadingPostRequest.ts +26 -0
- package/src/api/check/request/CheckableGetRequest.ts +17 -0
- package/src/api/check/request/CheckablePostRequest.ts +17 -0
- package/src/api/check/request/CheckablePutRequest.ts +17 -0
- package/src/api/check/route/CorsCheckApiRouter.ts +22 -0
- package/src/api/check/route/SimpleCheckableAPIRouter.ts +14 -0
- package/src/api/express/ExpressFileUploadingConfiguration.ts +22 -0
- package/src/api/express/ExpressLogableConfig.ts +11 -0
- package/src/api/express/ExpressStandardConfiguration.ts +25 -0
- package/src/api/external/ExternalApiSigner.ts +29 -0
- package/src/api/external/cetus/CetusStrategyApiManager.ts +48 -0
- package/src/api/request/DeleteRequest.ts +16 -0
- package/src/api/request/GetRequest.ts +16 -0
- package/src/api/request/PostRequest.ts +16 -0
- package/src/api/request/PutRequest.ts +16 -0
- package/src/api/request/RequestAxiosCall.ts +38 -0
- package/src/api/request/crud/CrudManager.ts +11 -0
- package/src/api/request/crud/CrudServiceManager.ts +117 -0
- package/src/api/request/docs/DocumentationModels.ts +121 -0
- package/src/api/request/docs/DocumentationRequest.ts +132 -0
- package/src/api/route/SimpleAPIRouter.ts +12 -0
- package/src/database/mongoose/MongooseModelManager.ts +65 -0
- package/src/database/redis/RedisConnection.ts +18 -0
- package/src/database/redis/RedisModelManager.ts +23 -0
- package/src/database/sequelize/SequelizeConnection.ts +45 -0
- package/src/database/sequelize/SequelizeModelManager.ts +71 -0
- package/src/index.ts +51 -0
- package/src/log/Logger.ts +27 -0
- package/src/log/LoggerManager.ts +50 -0
- package/src/log/LoggerWebSocket.ts +13 -0
- package/src/math/Utils.ts +4 -0
- package/src/types/payments.ts +811 -0
- package/src/web3/Coin.ts +18 -0
- package/src/web3/coingecko/CoingeckoPriceFinder.ts +75 -0
- package/test/api/APIRouter.test.ts +78 -0
- package/test/api/APIService.test.ts +50 -0
- package/test/api/APITestDefinitions.ts +7 -0
- package/test/api/CORS.test.ts +56 -0
- package/test/api/alegra/AlegraConnectionManager.test.ts +32 -0
- package/test/api/check/CheckDefinitions.ts +47 -0
- package/test/api/check/CheckableAPIRouter.test.ts +67 -0
- package/test/api/check/CheckableDeleteRequest.test.ts +92 -0
- package/test/api/check/CheckableGetRequest.test.ts +99 -0
- package/test/api/check/CheckablePostRequest.test.ts +108 -0
- package/test/api/check/CheckablePutRequest.test.ts +99 -0
- package/test/api/docs/DocumentationRequest.test.ts +192 -0
- package/test/api/request/DeleteRequest.test.ts +117 -0
- package/test/api/request/GetRequest.test.ts +140 -0
- package/test/api/request/PostRequest.test.ts +237 -0
- package/test/api/request/PutRequest.test.ts +166 -0
- package/test/database/mongoose/MongooseModelManager.test.ts +139 -0
- package/test/database/redis/RedisConnection.test.ts +7 -0
- package/test/database/sequelize/SequealizeDefinitions.ts +6 -0
- package/test/database/sequelize/SequelizeConnection.test.ts +30 -0
- package/test/database/sequelize/SequelizeModelManager.test.ts +206 -0
- package/test/log/Logger.test.ts +44 -0
- package/test/web3/coingecko/CoingeckoPriceFinder.test.ts +72 -0
- package/tsconfig.json +23 -0
- package/dist/api/check/RoleServicePermissionsChecker.d.ts +0 -13
- package/dist/api/check/RoleServicePermissionsChecker.js +0 -92
- package/dist/api/check/RoleServicePermissionsChecker.js.map +0 -1
- package/dist/api/check/ServiceSessionChecker.d.ts +0 -28
- package/dist/api/check/ServiceSessionChecker.js +0 -119
- package/dist/api/check/ServiceSessionChecker.js.map +0 -1
- package/dist/api/request/crud/common/ActivityHistoryManager.d.ts +0 -40
- package/dist/api/request/crud/common/ActivityHistoryManager.js +0 -86
- package/dist/api/request/crud/common/ActivityHistoryManager.js.map +0 -1
- package/dist/api/request/crud/common/ApplicationManager.d.ts +0 -39
- package/dist/api/request/crud/common/ApplicationManager.js +0 -72
- package/dist/api/request/crud/common/ApplicationManager.js.map +0 -1
- package/dist/api/request/crud/common/AvailablePlatformManager.d.ts +0 -39
- package/dist/api/request/crud/common/AvailablePlatformManager.js +0 -86
- package/dist/api/request/crud/common/AvailablePlatformManager.js.map +0 -1
- package/dist/api/request/crud/common/ComponentManager.d.ts +0 -56
- package/dist/api/request/crud/common/ComponentManager.js +0 -78
- package/dist/api/request/crud/common/ComponentManager.js.map +0 -1
- package/dist/api/request/crud/common/KeyManager.d.ts +0 -39
- package/dist/api/request/crud/common/KeyManager.js +0 -88
- package/dist/api/request/crud/common/KeyManager.js.map +0 -1
- package/dist/api/request/crud/common/PlatformManager.d.ts +0 -44
- package/dist/api/request/crud/common/PlatformManager.js +0 -75
- package/dist/api/request/crud/common/PlatformManager.js.map +0 -1
- package/dist/api/request/crud/common/RoleManager.d.ts +0 -56
- package/dist/api/request/crud/common/RoleManager.js +0 -75
- package/dist/api/request/crud/common/RoleManager.js.map +0 -1
- package/dist/api/request/crud/common/StatusManager.d.ts +0 -34
- package/dist/api/request/crud/common/StatusManager.js +0 -74
- package/dist/api/request/crud/common/StatusManager.js.map +0 -1
- package/dist/api/request/crud/common/TypeManager.d.ts +0 -33
- package/dist/api/request/crud/common/TypeManager.js +0 -76
- package/dist/api/request/crud/common/TypeManager.js.map +0 -1
- package/dist/api/request/crud/common/UserCredentialsManager.d.ts +0 -39
- package/dist/api/request/crud/common/UserCredentialsManager.js +0 -84
- package/dist/api/request/crud/common/UserCredentialsManager.js.map +0 -1
- package/dist/api/request/crud/common/UserManager.d.ts +0 -35
- package/dist/api/request/crud/common/UserManager.js +0 -80
- package/dist/api/request/crud/common/UserManager.js.map +0 -1
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { Request, Response, NextFunction } from "express";
|
|
2
|
+
|
|
3
|
+
export declare interface APICheckResult {
|
|
4
|
+
approved: boolean,
|
|
5
|
+
payload?: any,
|
|
6
|
+
rejection?: {
|
|
7
|
+
code: number,
|
|
8
|
+
payload: any
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export declare interface APICheckConfig {
|
|
13
|
+
check: string
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface APICheck {
|
|
17
|
+
getConfig(): APICheckConfig;
|
|
18
|
+
apply(config: APICheckConfig, req: Request): PromiseLike<APICheckResult>;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface Checkable {
|
|
22
|
+
addCheck(check: APICheck): void;
|
|
23
|
+
check(req: Request, res: Response, next: NextFunction): PromiseLike<void | Response>;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export class APIChecker {
|
|
27
|
+
private checks: APICheck[] = [];
|
|
28
|
+
|
|
29
|
+
public addCheck(check: APICheck) {
|
|
30
|
+
this.checks.push(check);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
public async applyChecks(req: Request): Promise<APICheckResult> {
|
|
34
|
+
if (!req.body || !req.body.checkResults)
|
|
35
|
+
req.body = { ...(req.body ?? {}), checkResults: {} };
|
|
36
|
+
for (let check of this.checks) {
|
|
37
|
+
let result = await check.apply(check.getConfig(), req);
|
|
38
|
+
req.body.checkResults[check.getConfig().check] = result.payload;
|
|
39
|
+
if (!result.approved)
|
|
40
|
+
return result;
|
|
41
|
+
}
|
|
42
|
+
return { approved: true };
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
public reject(res: Response, result: APICheckResult) {
|
|
46
|
+
if (result.rejection)
|
|
47
|
+
return res.status(result.rejection.code).json(result.rejection.payload);
|
|
48
|
+
return res.status(401).json({ message: 'Unknown reason' });
|
|
49
|
+
}
|
|
50
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { Request, Response, NextFunction } from "express";
|
|
2
|
+
import { APIRequest, APIRequestConfig, RequestMethod } from "../APIRequest";
|
|
3
|
+
import { ITRMAPIServiceGlobal } from "../APIService";
|
|
4
|
+
import { APICheckResult, APIChecker, APICheck, Checkable } from "./APIChecker";
|
|
5
|
+
|
|
6
|
+
export abstract class CheckableAPIRequest extends APIRequest implements Checkable {
|
|
7
|
+
private checker: APIChecker = new APIChecker();
|
|
8
|
+
|
|
9
|
+
constructor(method: RequestMethod, config: APIRequestConfig) {
|
|
10
|
+
super(method, config);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
public addCheck(check: APICheck) {
|
|
14
|
+
this.checker.addCheck(check);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
public async check(req: Request, res: Response | any, next: NextFunction) {
|
|
18
|
+
try {
|
|
19
|
+
let request: CheckableAPIRequest = ITRMAPIServiceGlobal.requests[APIRequest.buildKey(req)];
|
|
20
|
+
let result: APICheckResult = await request.checker.applyChecks(req);
|
|
21
|
+
if (result.approved)
|
|
22
|
+
return next();
|
|
23
|
+
return request.checker.reject(res, result);
|
|
24
|
+
} catch (error: any) {
|
|
25
|
+
console.log("> error:", error);
|
|
26
|
+
return await res.status(error?.response?.status ?? 500).json({
|
|
27
|
+
type: 'InternalRequestCheckError',
|
|
28
|
+
error: 'An error has ocurred when runing the checks for the request',
|
|
29
|
+
payload: error?.response?.data ?? error
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { RouterOptions, Request, Response, NextFunction } from "express";
|
|
2
|
+
import { APIRouter } from "../APIRouter";
|
|
3
|
+
import { ITRMAPIServiceGlobal } from "../APIService";
|
|
4
|
+
import { APICheck, APIChecker, APICheckResult, Checkable } from "./APIChecker";
|
|
5
|
+
|
|
6
|
+
export abstract class CheckableAPIRouter extends APIRouter implements Checkable {
|
|
7
|
+
private checker: APIChecker = new APIChecker();
|
|
8
|
+
|
|
9
|
+
constructor(path: string, options?: RouterOptions) {
|
|
10
|
+
super(path, options);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
public init() {
|
|
14
|
+
this.router.use(this.check);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
public addCheck(checker: APICheck) {
|
|
18
|
+
this.checker.addCheck(checker);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
public async check(req: Request, res: Response, next: NextFunction): Promise<void | Response | any> {
|
|
22
|
+
try {
|
|
23
|
+
let route: CheckableAPIRouter = ITRMAPIServiceGlobal.routers[req.baseUrl];
|
|
24
|
+
route.setHeaders(res);
|
|
25
|
+
let result: APICheckResult = await route.checker.applyChecks(req)
|
|
26
|
+
if (result.approved)
|
|
27
|
+
return route.runMiddleware(req, res, next);
|
|
28
|
+
return route.checker.reject(res, result);
|
|
29
|
+
} catch (error: any) {
|
|
30
|
+
return await res.status(error?.response?.status ?? 500).json({
|
|
31
|
+
type: 'InternalRouterCheckError',
|
|
32
|
+
error: 'An error has ocurred when runing the checks for the router',
|
|
33
|
+
payload: error?.response?.data ?? error
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
protected abstract setHeaders(res: Response) : void;
|
|
39
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Express, Router } from "express";
|
|
2
|
+
import { APIRequestConfig, RequestMethod } from "../../APIRequest";
|
|
3
|
+
import { CheckableAPIRequest } from "../CheckableAPIRequest";
|
|
4
|
+
|
|
5
|
+
export abstract class CheckableDeleteRequest extends CheckableAPIRequest {
|
|
6
|
+
constructor(config: APIRequestConfig) {
|
|
7
|
+
super(RequestMethod.DELETE, config);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
public addToApp(app: Express): void {
|
|
11
|
+
app.delete(this.path, this.check, this.process);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
public addToRouter(router: Router): void {
|
|
15
|
+
router.delete(this.path, this.check, this.process);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { Express, Router } from "express";
|
|
2
|
+
import { APIRequestConfig, RequestMethod } from "../../APIRequest";
|
|
3
|
+
import { CheckableAPIRequest } from "../CheckableAPIRequest";
|
|
4
|
+
import { Multer, StorageEngine } from "multer";
|
|
5
|
+
import { ITRMAPIServiceGlobal } from "../../APIService";
|
|
6
|
+
import { initializeFileUploadingGlobalVars } from "../../express/ExpressFileUploadingConfiguration";
|
|
7
|
+
|
|
8
|
+
export abstract class CheckableFileUploadingPostRequest extends CheckableAPIRequest {
|
|
9
|
+
protected upload: Multer;
|
|
10
|
+
protected storage: StorageEngine;
|
|
11
|
+
|
|
12
|
+
constructor(config: APIRequestConfig) {
|
|
13
|
+
super(RequestMethod.GET, config);
|
|
14
|
+
initializeFileUploadingGlobalVars();
|
|
15
|
+
this.upload = ITRMAPIServiceGlobal.tools.file.upload;
|
|
16
|
+
this.storage = ITRMAPIServiceGlobal.tools.file.storage;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
public addToApp(app: Express): void {
|
|
20
|
+
app.post(this.path, this.upload.single("file"), this.check, this.process);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
public addToRouter(router: Router): void {
|
|
24
|
+
router.post(this.path, this.upload.single("file"), this.check, this.process);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Express, Router } from "express";
|
|
2
|
+
import { APIRequestConfig, RequestMethod } from "../../APIRequest";
|
|
3
|
+
import { CheckableAPIRequest } from "../CheckableAPIRequest";
|
|
4
|
+
|
|
5
|
+
export abstract class CheckableGetRequest extends CheckableAPIRequest {
|
|
6
|
+
constructor(config: APIRequestConfig) {
|
|
7
|
+
super(RequestMethod.GET, config);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
public addToApp(app: Express): void {
|
|
11
|
+
app.get(this.path, this.check, this.process);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
public addToRouter(router: Router): void {
|
|
15
|
+
router.get(this.path, this.check, this.process);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Express, Router } from "express";
|
|
2
|
+
import { APIRequestConfig, RequestMethod } from "../../APIRequest";
|
|
3
|
+
import { CheckableAPIRequest } from "../CheckableAPIRequest";
|
|
4
|
+
|
|
5
|
+
export abstract class CheckablePostRequest extends CheckableAPIRequest {
|
|
6
|
+
constructor(config: APIRequestConfig) {
|
|
7
|
+
super(RequestMethod.POST, config);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
public addToApp(app: Express): void {
|
|
11
|
+
app.post(this.path, this.check, this.process);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
public addToRouter(router: Router): void {
|
|
15
|
+
router.post(this.path, this.check, this.process);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Express, Router } from "express";
|
|
2
|
+
import { APIRequestConfig, RequestMethod } from "../../APIRequest";
|
|
3
|
+
import { CheckableAPIRequest } from "../CheckableAPIRequest";
|
|
4
|
+
|
|
5
|
+
export abstract class CheckablePutRequest extends CheckableAPIRequest {
|
|
6
|
+
constructor(config: APIRequestConfig) {
|
|
7
|
+
super(RequestMethod.PUT, config);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
public addToApp(app: Express): void {
|
|
11
|
+
app.put(this.path, this.check, this.process);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
public addToRouter(router: Router): void {
|
|
15
|
+
router.put(this.path, this.check, this.process);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { NextFunction, Request, Response, RouterOptions } from "express";
|
|
2
|
+
import { CheckableAPIRouter } from "../CheckableAPIRouter";
|
|
3
|
+
|
|
4
|
+
export class CorsCheckApiRouter extends CheckableAPIRouter {
|
|
5
|
+
constructor(path: string, options?: RouterOptions) {
|
|
6
|
+
super(path, options);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
protected setHeaders(res: Response<any, Record<string, any>>): void {
|
|
10
|
+
res.setHeader("Access-Control-Allow-Origin", "*");
|
|
11
|
+
res.setHeader("Access-Control-Allow-Methods", "GET,POST,PUT,DELETE,OPTIONS");
|
|
12
|
+
res.setHeader("Access-Control-Allow-Headers", "Content-Type,Authorization,platform");
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
protected async runMiddleware(
|
|
16
|
+
req: Request,
|
|
17
|
+
res: Response,
|
|
18
|
+
next: NextFunction
|
|
19
|
+
): Promise<void | Response> {
|
|
20
|
+
return next();
|
|
21
|
+
}
|
|
22
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { NextFunction, Request, Response, RouterOptions } from "express";
|
|
2
|
+
import { CheckableAPIRouter } from "../CheckableAPIRouter";
|
|
3
|
+
|
|
4
|
+
export class SimpleCheckableAPIRouter extends CheckableAPIRouter {
|
|
5
|
+
constructor(path: string, options?: RouterOptions) {
|
|
6
|
+
super(path, options);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
protected setHeaders(res: Response<any, Record<string, any>>): void { }
|
|
10
|
+
|
|
11
|
+
protected async runMiddleware(req: Request, res: Response, next: NextFunction): Promise<void | Response> {
|
|
12
|
+
return next();
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { ExpressStandardConfiguration } from "./ExpressStandardConfiguration";
|
|
2
|
+
import { ITRMAPIServiceGlobal } from "../APIService";
|
|
3
|
+
import { Express } from "express";
|
|
4
|
+
import multer from "multer";
|
|
5
|
+
|
|
6
|
+
export function initializeFileUploadingGlobalVars() {
|
|
7
|
+
if (!ITRMAPIServiceGlobal.tools.file) {
|
|
8
|
+
const storage = multer.memoryStorage();
|
|
9
|
+
ITRMAPIServiceGlobal.tools.file = {
|
|
10
|
+
storage: storage,
|
|
11
|
+
upload: multer({ storage })
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export class ExpressFileUploadingConfiguration extends ExpressStandardConfiguration {
|
|
17
|
+
public apply(app: Express): void {
|
|
18
|
+
super.apply(app);
|
|
19
|
+
initializeFileUploadingGlobalVars();
|
|
20
|
+
app.use(ITRMAPIServiceGlobal.tools.file.upload.array('file'));
|
|
21
|
+
}
|
|
22
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Express } from "express";
|
|
2
|
+
import morgan from "morgan";
|
|
3
|
+
import { ExpressStandardConfiguration } from "./ExpressStandardConfiguration";
|
|
4
|
+
|
|
5
|
+
export class ExpressLogableConfig extends ExpressStandardConfiguration {
|
|
6
|
+
apply(app: Express): void {
|
|
7
|
+
super.apply(app);
|
|
8
|
+
|
|
9
|
+
app.use(morgan(':remote-addr - :remote-user [:date[web]] ":method :url HTTP/:http-version" :status :res[content-length]'));
|
|
10
|
+
}
|
|
11
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import express, { Express } from "express";
|
|
2
|
+
import { ExpressConfiguration } from "../ExpressConfiguration";
|
|
3
|
+
import { ITRMAPIServiceGlobal } from "../APIService";
|
|
4
|
+
import cors from 'cors';
|
|
5
|
+
|
|
6
|
+
export class ExpressStandardConfiguration implements ExpressConfiguration {
|
|
7
|
+
public apply(app: Express): void {
|
|
8
|
+
app.use(ExpressStandardConfiguration.getCorsConfiguration());
|
|
9
|
+
app.use(express.json({ limit: '50mb' }));
|
|
10
|
+
app.use(express.urlencoded({ limit: '50mb', extended: true }));
|
|
11
|
+
//app.use(urlencoded({ extended: true }));
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
public static getCorsConfiguration() {
|
|
15
|
+
return cors({
|
|
16
|
+
origin: function (origin: any, callback: any) {
|
|
17
|
+
let whitelist = ITRMAPIServiceGlobal.whitelist;
|
|
18
|
+
if (whitelist.length == 0 || (origin && whitelist.indexOf(origin) !== -1))
|
|
19
|
+
callback(null, true);
|
|
20
|
+
else
|
|
21
|
+
callback(new Error('Not allowed by CORS'), false);
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { RequestMethod } from "../APIRequest";
|
|
2
|
+
import { HmacSHA256, enc } from 'crypto-js';
|
|
3
|
+
|
|
4
|
+
export interface ApiKeyPair {
|
|
5
|
+
publicKey: string;
|
|
6
|
+
secretKey: string;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export class ExternalApiSigner {
|
|
10
|
+
private keys: ApiKeyPair;
|
|
11
|
+
private service: string;
|
|
12
|
+
|
|
13
|
+
constructor(service: string, keys: ApiKeyPair) {
|
|
14
|
+
this.service = service;
|
|
15
|
+
this.keys = keys;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
public sign(method: RequestMethod, path: string, body?: any) {
|
|
19
|
+
let nonce = Date.now();
|
|
20
|
+
let message = nonce + method + path.replace(/ /g, '%20') + (body ? JSON.stringify(body) : "{}");
|
|
21
|
+
const hash = HmacSHA256(message, this.keys.secretKey);
|
|
22
|
+
let signature = enc.Hex.stringify(hash);
|
|
23
|
+
return {
|
|
24
|
+
headers: {
|
|
25
|
+
Authorization: `${this.service} ${this.keys.publicKey}:${nonce}:${signature}`
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { AxiosRequestConfig } from "axios";
|
|
2
|
+
import { ApiKeyPair, ExternalApiSigner } from "../ExternalApiSigner";
|
|
3
|
+
import { RequestMethod } from "../../APIRequest";
|
|
4
|
+
import { RequestAxiosCall } from "../../request/RequestAxiosCall";
|
|
5
|
+
|
|
6
|
+
export interface CetusRate {
|
|
7
|
+
symbol: string;
|
|
8
|
+
source: string;
|
|
9
|
+
bid?: number;
|
|
10
|
+
ask?: number;
|
|
11
|
+
spot?: number;
|
|
12
|
+
prevDayClose?: number;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export enum CetusPair {
|
|
16
|
+
USDCOP = "USDCOP"
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface CetusStrategy {
|
|
20
|
+
id: number;
|
|
21
|
+
name: string;
|
|
22
|
+
settings: any;
|
|
23
|
+
memory: any;
|
|
24
|
+
account: string;
|
|
25
|
+
pair: string;
|
|
26
|
+
intermediary: string;
|
|
27
|
+
status: string;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export class CetusStrategyApiManager extends ExternalApiSigner {
|
|
31
|
+
private apiUrl: string;
|
|
32
|
+
|
|
33
|
+
constructor(keys: ApiKeyPair, apiUrl: string) {
|
|
34
|
+
super('cetus', keys);
|
|
35
|
+
this.apiUrl = apiUrl;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
public async getRates() {
|
|
39
|
+
let signature: AxiosRequestConfig = this.sign(RequestMethod.GET, '/v1/rates');
|
|
40
|
+
return ((await RequestAxiosCall.get(`${this.apiUrl}/v1/rates`, signature)) as { status: string; rates: CetusRate[]; }).rates;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
public async getStrategies(name: string, pair: CetusPair, account: string) {
|
|
44
|
+
let url = `/v1/strategies?name=${name}&pair=${pair}&account=${account}`;
|
|
45
|
+
let signature: AxiosRequestConfig = this.sign(RequestMethod.GET, url);
|
|
46
|
+
return (((await RequestAxiosCall.get(`${this.apiUrl}${url}`, signature)) as any).result) as CetusStrategy[];
|
|
47
|
+
}
|
|
48
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Express, Router } from "express";
|
|
2
|
+
import { APIRequest, APIRequestConfig, RequestMethod } from "../APIRequest";
|
|
3
|
+
|
|
4
|
+
export abstract class DeleteRequest extends APIRequest {
|
|
5
|
+
constructor(config: APIRequestConfig) {
|
|
6
|
+
super(RequestMethod.DELETE, config);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
public addToApp(app: Express): void {
|
|
10
|
+
app.delete(this.path, this.process);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
public addToRouter(router: Router): void {
|
|
14
|
+
router.delete(this.path, this.process);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Express, Router } from "express";
|
|
2
|
+
import { APIRequest, APIRequestConfig, RequestMethod } from "../APIRequest";
|
|
3
|
+
|
|
4
|
+
export abstract class GetRequest extends APIRequest {
|
|
5
|
+
constructor(config: APIRequestConfig) {
|
|
6
|
+
super(RequestMethod.GET, config);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
public addToApp(app: Express): void {
|
|
10
|
+
app.get(this.path, this.process);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
public addToRouter(router: Router): void {
|
|
14
|
+
router.get(this.path, this.process);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Express, Router } from "express";
|
|
2
|
+
import { APIRequest, APIRequestConfig, RequestMethod } from "../APIRequest";
|
|
3
|
+
|
|
4
|
+
export abstract class PostRequest extends APIRequest {
|
|
5
|
+
constructor(config: APIRequestConfig) {
|
|
6
|
+
super(RequestMethod.POST, config);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
public addToApp(app: Express): void {
|
|
10
|
+
app.post(this.path, this.process);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
public addToRouter(router: Router): void {
|
|
14
|
+
router.post(this.path, this.process);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Express, Router } from "express";
|
|
2
|
+
import { APIRequest, APIRequestConfig, RequestMethod } from "../APIRequest";
|
|
3
|
+
|
|
4
|
+
export abstract class PutRequest extends APIRequest {
|
|
5
|
+
constructor(config: APIRequestConfig) {
|
|
6
|
+
super(RequestMethod.PUT, config);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
public addToApp(app: Express): void {
|
|
10
|
+
app.put(this.path, this.process);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
public addToRouter(router: Router): void {
|
|
14
|
+
router.put(this.path, this.process);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import axios, { AxiosRequestConfig } from "axios";
|
|
2
|
+
import axiosRetry from 'axios-retry';
|
|
3
|
+
|
|
4
|
+
axiosRetry(axios, { retries: 3, retryDelay: (retryCount) => { return 2000; } });
|
|
5
|
+
|
|
6
|
+
export class RequestAxiosCall {
|
|
7
|
+
public static async get(url: string, config?: AxiosRequestConfig<any>) {
|
|
8
|
+
return new Promise((resolve, reject) => {
|
|
9
|
+
axios.get(url, config ?? { headers: { 'Content-Type': 'application/json' } })
|
|
10
|
+
.then(response => { resolve(response.data) })
|
|
11
|
+
.catch(err => { reject(err) });
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
public static async post(url: string, body: any, config?: AxiosRequestConfig) {
|
|
16
|
+
return new Promise((resolve, reject) => {
|
|
17
|
+
axios.post(url, body, config ?? { headers: { 'Content-Type': 'application/json' } })
|
|
18
|
+
.then(response => { resolve(response.data) })
|
|
19
|
+
.catch(err => { reject(err) });
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
public static async put(url: string, body: any, config?: AxiosRequestConfig) {
|
|
24
|
+
return new Promise((resolve, reject) => {
|
|
25
|
+
axios.put(url, body, config ?? { headers: { 'Content-Type': 'application/json' } })
|
|
26
|
+
.then(response => { resolve(response.data) })
|
|
27
|
+
.catch(err => { reject(err) });
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
public static async delete(url: string, config?: AxiosRequestConfig) {
|
|
32
|
+
return new Promise((resolve, reject) => {
|
|
33
|
+
axios.delete(url, config ?? { headers: { 'Content-Type': 'application/json' } })
|
|
34
|
+
.then(response => { resolve(response.data) })
|
|
35
|
+
.catch(err => { reject(err) });
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export interface CrudManager<A, D, I> {
|
|
2
|
+
find(details: D): Promise<I | undefined>;
|
|
3
|
+
findAll(details: D): Promise<I[] | undefined>;
|
|
4
|
+
findById(id: A): Promise<I | undefined>;
|
|
5
|
+
findOrCreate(details: D): Promise<I | undefined>;
|
|
6
|
+
create(details: D): Promise<I | undefined>;
|
|
7
|
+
createMany(details: D): Promise<I[] | undefined>;
|
|
8
|
+
count(details: D):Promise<{ result: number }>;
|
|
9
|
+
modify(item: I): Promise<any>;
|
|
10
|
+
destroy(item: I): Promise<any>;
|
|
11
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { AxiosRequestConfig } from "axios";
|
|
2
|
+
import { CrudManager } from "./CrudManager";
|
|
3
|
+
import { RequestAxiosCall } from "../RequestAxiosCall";
|
|
4
|
+
|
|
5
|
+
export interface StandardIdRequestDefinition {
|
|
6
|
+
id: number,
|
|
7
|
+
config?: AxiosRequestConfig
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export abstract class CrudServiceManager<D, I> implements CrudManager<StandardIdRequestDefinition, D, I> {
|
|
11
|
+
public async findOrCreate(details: D): Promise<I | undefined> {
|
|
12
|
+
let item: any = await this.find(details);
|
|
13
|
+
if (!item)
|
|
14
|
+
item = await this.create(details);
|
|
15
|
+
return item;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
protected extractItem(response: any, key: string) {
|
|
19
|
+
if (response[key] && response[key].length > 0)
|
|
20
|
+
return response[key][0];
|
|
21
|
+
return undefined;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
protected extractItems(response: any, key: string) {
|
|
25
|
+
let result = [];
|
|
26
|
+
if (response[key] && response[key].length > 0)
|
|
27
|
+
for (let item of response[key])
|
|
28
|
+
result.push(item);
|
|
29
|
+
return result;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
protected sendFinding(url: string, config?: AxiosRequestConfig) {
|
|
33
|
+
return new Promise<I | undefined>((resolve, reject) => {
|
|
34
|
+
RequestAxiosCall.get(url, config)
|
|
35
|
+
.then((response: any) => {
|
|
36
|
+
resolve(this.extractItem(response, 'result'));
|
|
37
|
+
}).catch((err: any) => {
|
|
38
|
+
reject(err);
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
protected sendAllFinding(url: string, config?: AxiosRequestConfig) {
|
|
44
|
+
return new Promise<I[] | undefined>((resolve, reject) => {
|
|
45
|
+
RequestAxiosCall.get(url, config)
|
|
46
|
+
.then((response: any) => {
|
|
47
|
+
resolve(this.extractItems(response, 'result'));
|
|
48
|
+
}).catch((err: any) => {
|
|
49
|
+
reject(err);
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
protected sendCreation(url: string, body: any, config?: AxiosRequestConfig) {
|
|
55
|
+
return new Promise<I | undefined>((resolve, reject) => {
|
|
56
|
+
RequestAxiosCall.post(url, body, config)
|
|
57
|
+
.then((response: any) => {
|
|
58
|
+
resolve(this.extractItem(response, 'items'));
|
|
59
|
+
}).catch((err: any) => {
|
|
60
|
+
reject(err);
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
protected sendCreationMany(url: string, values: any[], config?: AxiosRequestConfig) {
|
|
66
|
+
return new Promise<I[] | undefined>((resolve, reject) => {
|
|
67
|
+
RequestAxiosCall.post(url, { values: values }, config)
|
|
68
|
+
.then((response: any) => {
|
|
69
|
+
resolve(this.extractItems(response, 'items'));
|
|
70
|
+
}).catch((err: any) => {
|
|
71
|
+
reject(err);
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
protected sendCounting(url: string, config?: AxiosRequestConfig) {
|
|
77
|
+
return new Promise<{ result: number; } | undefined >((resolve, reject) => {
|
|
78
|
+
RequestAxiosCall.get(`${url}${url.indexOf('?') !== -1 ? '&' : '?'}count=true`, config)
|
|
79
|
+
.then((response: any) => {
|
|
80
|
+
resolve(response);
|
|
81
|
+
}).catch((err: any) => {
|
|
82
|
+
reject(err);
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
protected sendModification(url: string, modification: I, config?: AxiosRequestConfig) {
|
|
88
|
+
return new Promise<any>((resolve, reject) => {
|
|
89
|
+
RequestAxiosCall.put(url, { ...modification }, config)
|
|
90
|
+
.then((response: any) => {
|
|
91
|
+
resolve(response);
|
|
92
|
+
}).catch((err: any) => {
|
|
93
|
+
reject(err);
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
protected sendDestruction(url: string, config?: AxiosRequestConfig) {
|
|
99
|
+
return new Promise<any>((resolve, reject) => {
|
|
100
|
+
RequestAxiosCall.delete(url, config)
|
|
101
|
+
.then((response: any) => {
|
|
102
|
+
resolve(response);
|
|
103
|
+
}).catch((err: any) => {
|
|
104
|
+
reject(err);
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
public abstract find(details: D): Promise<I | undefined>;
|
|
110
|
+
public abstract findAll(details: D): Promise<I[] | undefined>;
|
|
111
|
+
public abstract findById(id: StandardIdRequestDefinition): Promise<I | undefined>;
|
|
112
|
+
public abstract create(details: D): Promise<I | undefined>;
|
|
113
|
+
public abstract createMany(details: D): Promise<I[] | undefined>;
|
|
114
|
+
public abstract count(details: D): Promise<{ result: number }>;
|
|
115
|
+
public abstract modify(item: I): Promise<any>;
|
|
116
|
+
public abstract destroy(item: I): Promise<any>;
|
|
117
|
+
}
|