badmfck-api-server 1.2.7 → 1.2.8
Sign up to get free protection for your applications and to get access to all the features.
@@ -24,6 +24,7 @@ export interface APIServiceOptions {
|
|
24
24
|
onNetworkLog?: ((log: APIServiceNetworkLogItem) => void) | null;
|
25
25
|
onError?: ((...rest: any[]) => void) | null;
|
26
26
|
isProductionEnvironment: boolean;
|
27
|
+
interceptor?: IBaseEndpoint;
|
27
28
|
}
|
28
29
|
export declare function getDefaultOptions(): APIServiceOptions;
|
29
30
|
export declare const REQ_CREATE_NET_LOG: Req<void, APIServiceNetworkLogItem>;
|
@@ -108,8 +108,10 @@ class APIService extends BaseService_1.BaseService {
|
|
108
108
|
BaseEndpoint_1.BaseEndpoint.setEntryPoint(this.options.baseEndPoint);
|
109
109
|
for (let i of this.options.endpoints) {
|
110
110
|
await i.init();
|
111
|
+
if (!i.endpoints)
|
112
|
+
continue;
|
111
113
|
for (let j of i.endpoints) {
|
112
|
-
const ep =
|
114
|
+
const ep = j.endpoint;
|
113
115
|
app.all(ep, async (req, res) => {
|
114
116
|
const tme = +new Date();
|
115
117
|
let log = null;
|
@@ -130,10 +132,19 @@ class APIService extends BaseService_1.BaseService {
|
|
130
132
|
data: req.body,
|
131
133
|
params: req.params,
|
132
134
|
headers: req.headers,
|
133
|
-
endpoint: ep
|
135
|
+
endpoint: ep,
|
134
136
|
};
|
135
137
|
let result;
|
136
138
|
try {
|
139
|
+
let interceptorResult;
|
140
|
+
if (this.options.interceptor) {
|
141
|
+
interceptorResult = await this.options.interceptor.execute(httpRequest);
|
142
|
+
if (interceptorResult.error) {
|
143
|
+
this.sendResponse(res, interceptorResult, tme, ep, log);
|
144
|
+
return;
|
145
|
+
}
|
146
|
+
httpRequest.interceptorResult = interceptorResult;
|
147
|
+
}
|
137
148
|
result = await i.execute(httpRequest);
|
138
149
|
}
|
139
150
|
catch (e) {
|
@@ -1,6 +1,6 @@
|
|
1
1
|
import { HTTPRequestVO, TransferPacketVO } from "./structures/Interfaces";
|
2
2
|
export interface IBaseEndpoint {
|
3
|
-
endpoints
|
3
|
+
endpoints?: IEndpointHandler[];
|
4
4
|
execute: (req: HTTPRequestVO) => Promise<TransferPacketVO<any>>;
|
5
5
|
init: () => Promise<void>;
|
6
6
|
ignoreHttpLogging: boolean;
|
@@ -10,13 +10,14 @@ export interface IEndpointHandler {
|
|
10
10
|
handler: (req: HTTPRequestVO) => Promise<TransferPacketVO<any>>;
|
11
11
|
}
|
12
12
|
export declare class BaseEndpoint implements IBaseEndpoint {
|
13
|
+
endpoints?: IEndpointHandler[];
|
13
14
|
ignoreHttpLogging: boolean;
|
14
15
|
private static entrypoint;
|
15
|
-
private
|
16
|
+
private endpoint;
|
16
17
|
static setEntryPoint: (ep: string) => void;
|
17
18
|
static getEntryPoint: () => string;
|
18
|
-
|
19
|
-
|
19
|
+
constructor(endpoint: string);
|
20
|
+
registerEndpoints(endpoints: IEndpointHandler[]): void;
|
20
21
|
init(): Promise<void>;
|
21
22
|
execute(req: HTTPRequestVO): Promise<TransferPacketVO<any>>;
|
22
23
|
}
|
@@ -6,9 +6,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.BaseEndpoint = void 0;
|
7
7
|
const DefaultErrors_1 = __importDefault(require("./structures/DefaultErrors"));
|
8
8
|
class BaseEndpoint {
|
9
|
+
endpoints;
|
9
10
|
ignoreHttpLogging = false;
|
10
11
|
static entrypoint = "/";
|
11
|
-
|
12
|
+
endpoint = "";
|
12
13
|
static setEntryPoint = (ep) => {
|
13
14
|
this.entrypoint = ep;
|
14
15
|
if (!this.entrypoint.endsWith("/")) {
|
@@ -20,37 +21,36 @@ class BaseEndpoint {
|
|
20
21
|
static getEntryPoint = () => {
|
21
22
|
return this.entrypoint;
|
22
23
|
};
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
if (i.startsWith("/"))
|
38
|
-
i = i.substring(1);
|
39
|
-
eps.push(i);
|
40
|
-
continue;
|
41
|
-
}
|
24
|
+
constructor(endpoint) {
|
25
|
+
if (endpoint.startsWith("/"))
|
26
|
+
endpoint = endpoint.substring(1);
|
27
|
+
if (!endpoint.endsWith("/"))
|
28
|
+
endpoint = endpoint + "/";
|
29
|
+
this.endpoint = endpoint;
|
30
|
+
}
|
31
|
+
registerEndpoints(endpoints) {
|
32
|
+
for (let i of endpoints) {
|
33
|
+
if (i.endpoint.startsWith("/"))
|
34
|
+
i.endpoint = i.endpoint.substring(1);
|
35
|
+
if (i.endpoint.endsWith("/"))
|
36
|
+
i.endpoint = i.endpoint.substring(0, i.endpoint.length - 1);
|
37
|
+
i.endpoint = BaseEndpoint.entrypoint + this.endpoint + i.endpoint;
|
42
38
|
}
|
43
|
-
this.endpoints =
|
39
|
+
this.endpoints = endpoints;
|
44
40
|
}
|
45
41
|
async init() {
|
42
|
+
if (!this.endpoints) {
|
43
|
+
console.error("No endpoints registered for " + this.endpoint);
|
44
|
+
return;
|
45
|
+
}
|
46
46
|
for (let i of this.endpoints)
|
47
|
-
console.log("endpoint: " +
|
47
|
+
console.log("endpoint: " + i.endpoint + " initalized");
|
48
48
|
}
|
49
49
|
;
|
50
50
|
async execute(req) {
|
51
|
-
if (this.
|
52
|
-
for (let i of this.
|
53
|
-
if (
|
51
|
+
if (this.endpoints && this.endpoints.length > 0) {
|
52
|
+
for (let i of this.endpoints) {
|
53
|
+
if (i.endpoint === req.endpoint)
|
54
54
|
return i.handler(req);
|
55
55
|
}
|
56
56
|
}
|