badmfck-api-server 1.2.6 → 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>;
@@ -42,7 +42,7 @@ async function Initializer(services) {
42
42
  exports.Initializer = Initializer;
43
43
  class APIService extends BaseService_1.BaseService {
44
44
  static nextLogID = 0;
45
- version = "1.2.3";
45
+ version = "1.2.9";
46
46
  options;
47
47
  netLog = [];
48
48
  constructor(options) {
@@ -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 = BaseEndpoint_1.BaseEndpoint.getEntryPoint() + j;
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,17 +1,23 @@
1
1
  import { HTTPRequestVO, TransferPacketVO } from "./structures/Interfaces";
2
2
  export interface IBaseEndpoint {
3
- endpoints: string[];
3
+ endpoints?: IEndpointHandler[];
4
4
  execute: (req: HTTPRequestVO) => Promise<TransferPacketVO<any>>;
5
5
  init: () => Promise<void>;
6
6
  ignoreHttpLogging: boolean;
7
7
  }
8
+ export interface IEndpointHandler {
9
+ endpoint: string;
10
+ handler: (req: HTTPRequestVO) => Promise<TransferPacketVO<any>>;
11
+ }
8
12
  export declare class BaseEndpoint implements IBaseEndpoint {
13
+ endpoints?: IEndpointHandler[];
9
14
  ignoreHttpLogging: boolean;
10
15
  private static entrypoint;
16
+ private endpoint;
11
17
  static setEntryPoint: (ep: string) => void;
12
18
  static getEntryPoint: () => string;
13
- endpoints: string[];
14
- constructor(ep: string | string[]);
19
+ constructor(endpoint: string);
20
+ registerEndpoints(endpoints: IEndpointHandler[]): void;
15
21
  init(): Promise<void>;
16
22
  execute(req: HTTPRequestVO): Promise<TransferPacketVO<any>>;
17
23
  }
@@ -6,8 +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 = "/";
12
+ endpoint = "";
11
13
  static setEntryPoint = (ep) => {
12
14
  this.entrypoint = ep;
13
15
  if (!this.entrypoint.endsWith("/")) {
@@ -19,24 +21,39 @@ class BaseEndpoint {
19
21
  static getEntryPoint = () => {
20
22
  return this.entrypoint;
21
23
  };
22
- endpoints;
23
- constructor(ep) {
24
- if (typeof ep === "string")
25
- ep = [ep];
26
- let eps = [];
27
- for (let i of ep) {
28
- if (i.startsWith("/"))
29
- i = i.substring(1);
30
- eps.push(i);
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;
31
38
  }
32
- this.endpoints = eps;
39
+ this.endpoints = endpoints;
33
40
  }
34
41
  async init() {
42
+ if (!this.endpoints) {
43
+ console.error("No endpoints registered for " + this.endpoint);
44
+ return;
45
+ }
35
46
  for (let i of this.endpoints)
36
- console.log("endpoint: " + BaseEndpoint.entrypoint + i + " initalized");
47
+ console.log("endpoint: " + i.endpoint + " initalized");
37
48
  }
38
49
  ;
39
50
  async execute(req) {
51
+ if (this.endpoints && this.endpoints.length > 0) {
52
+ for (let i of this.endpoints) {
53
+ if (i.endpoint === req.endpoint)
54
+ return i.handler(req);
55
+ }
56
+ }
40
57
  return {
41
58
  error: DefaultErrors_1.default.NOT_IMPLEMENTED,
42
59
  data: null
@@ -20,6 +20,7 @@ export interface HTTPRequestVO {
20
20
  };
21
21
  headers: any;
22
22
  endpoint: string;
23
+ interceptorResult?: TransferPacketVO<any>;
23
24
  }
24
25
  export interface ErrorVO {
25
26
  code: number;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "badmfck-api-server",
3
- "version": "1.2.6",
3
+ "version": "1.2.8",
4
4
  "description": "Simple API http server based on express",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",