badmfck-api-server 1.2.6 → 1.2.7

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.
@@ -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) {
@@ -5,13 +5,18 @@ export interface IBaseEndpoint {
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 {
9
13
  ignoreHttpLogging: boolean;
10
14
  private static entrypoint;
15
+ private endpointHandlers;
11
16
  static setEntryPoint: (ep: string) => void;
12
17
  static getEntryPoint: () => string;
13
18
  endpoints: string[];
14
- constructor(ep: string | string[]);
19
+ constructor(ep: string | string[] | IEndpointHandler[]);
15
20
  init(): Promise<void>;
16
21
  execute(req: HTTPRequestVO): Promise<TransferPacketVO<any>>;
17
22
  }
@@ -8,6 +8,7 @@ const DefaultErrors_1 = __importDefault(require("./structures/DefaultErrors"));
8
8
  class BaseEndpoint {
9
9
  ignoreHttpLogging = false;
10
10
  static entrypoint = "/";
11
+ endpointHandlers = [];
11
12
  static setEntryPoint = (ep) => {
12
13
  this.entrypoint = ep;
13
14
  if (!this.entrypoint.endsWith("/")) {
@@ -25,9 +26,19 @@ class BaseEndpoint {
25
26
  ep = [ep];
26
27
  let eps = [];
27
28
  for (let i of ep) {
28
- if (i.startsWith("/"))
29
- i = i.substring(1);
30
- eps.push(i);
29
+ if (typeof i === "object" && "endpoint" in i && "handler" in i) {
30
+ if (i.endpoint.startsWith("/"))
31
+ i.endpoint = i.endpoint.substring(1);
32
+ eps.push(i.endpoint);
33
+ this.endpointHandlers.push(i);
34
+ continue;
35
+ }
36
+ if (typeof i === "string") {
37
+ if (i.startsWith("/"))
38
+ i = i.substring(1);
39
+ eps.push(i);
40
+ continue;
41
+ }
31
42
  }
32
43
  this.endpoints = eps;
33
44
  }
@@ -37,6 +48,12 @@ class BaseEndpoint {
37
48
  }
38
49
  ;
39
50
  async execute(req) {
51
+ if (this.endpointHandlers && this.endpointHandlers.length > 0) {
52
+ for (let i of this.endpointHandlers) {
53
+ if (BaseEndpoint.entrypoint + 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
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.7",
4
4
  "description": "Simple API http server based on express",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",