ismx-nexo-node-app 0.4.35 → 0.4.36

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.
@@ -29,9 +29,10 @@ class HttpResponse {
29
29
  }
30
30
  exports.HttpResponse = HttpResponse;
31
31
  class Service {
32
- constructor(method, endpoint = "/") {
32
+ constructor(method, endpoint = "/", type = "json") {
33
33
  this.method = method;
34
34
  this.endpoint = endpoint;
35
+ this.type = type;
35
36
  Service.services.push(this);
36
37
  }
37
38
  serve(request) {
@@ -47,9 +47,9 @@ class BusinessServer extends Business_1.default {
47
47
  return __awaiter(this, void 0, void 0, function* () {
48
48
  yield Promise.resolve().then(() => __importStar(require("express"))).then((_a) => __awaiter(this, [_a], void 0, function* ({ default: express }) {
49
49
  yield Promise.resolve().then(() => __importStar(require("cors"))).then((_a) => __awaiter(this, [_a], void 0, function* ({ default: cors }) {
50
+ this.express = express;
50
51
  this.app = express();
51
52
  this.app.use(express.urlencoded({ extended: false }));
52
- this.app.use(express.json());
53
53
  this.app.use(cors());
54
54
  }));
55
55
  }));
@@ -77,24 +77,32 @@ class BusinessServer extends Business_1.default {
77
77
  this.onEnd = listener;
78
78
  }
79
79
  publish(service) {
80
+ const app = (() => {
81
+ switch (service.type) {
82
+ case 'json': return this.express.json();
83
+ case 'xml': return this.express.xml();
84
+ case 'stream': return this.express.raw({ type: 'application/octet-stream', limit: '10mb' });
85
+ default: return this.express.json();
86
+ }
87
+ })();
80
88
  switch (service.method) {
81
89
  case "GET":
82
- this.app.get(service.endpoint, this.run(service));
90
+ this.app.get(service.endpoint, app, this.run(service));
83
91
  break;
84
92
  case "POST":
85
- this.app.post(service.endpoint, this.run(service));
93
+ this.app.post(service.endpoint, app, this.run(service));
86
94
  break;
87
95
  case "PUT":
88
- this.app.put(service.endpoint, this.run(service));
96
+ this.app.put(service.endpoint, app, this.run(service));
89
97
  break;
90
98
  case "DELETE":
91
- this.app.delete(service.endpoint, this.run(service));
99
+ this.app.delete(service.endpoint, app, this.run(service));
92
100
  break;
93
101
  case "PATCH":
94
- this.app.patch(service.endpoint, this.run(service));
102
+ this.app.patch(service.endpoint, app, this.run(service));
95
103
  break;
96
104
  case "*":
97
- this.app.all(service.endpoint, this.run(service));
105
+ this.app.all(service.endpoint, app, this.run(service));
98
106
  break;
99
107
  }
100
108
  }
@@ -24,11 +24,13 @@ export declare class HttpResponse<T = any> {
24
24
  static ok<T = any>(content: T): HttpResponse<any>;
25
25
  static ko<T = any>(httpCode: number, content: T): HttpResponse<any>;
26
26
  }
27
+ export type ServiceType = "json" | "xml" | 'stream';
27
28
  export default abstract class Service<Req, Res> {
28
29
  static readonly services: Service<any, any>[];
29
30
  readonly method: string;
30
31
  readonly endpoint: string;
31
- protected constructor(method: string, endpoint?: string);
32
+ readonly type?: ServiceType;
33
+ protected constructor(method: string, endpoint?: string, type?: ServiceType);
32
34
  serve(request: HttpRequest<Req>): Promise<HttpResponse<Res>>;
33
35
  static clone<R, S>(request: HttpRequest<R>, clone: {
34
36
  params?: {
@@ -2,6 +2,7 @@ import Service, { HttpRequest, HttpResponse } from "../api/Service";
2
2
  import Business from "./Business";
3
3
  export default class BusinessServer extends Business {
4
4
  private services;
5
+ private express;
5
6
  private app;
6
7
  constructor();
7
8
  init(): Promise<void>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ismx-nexo-node-app",
3
- "version": "0.4.35",
3
+ "version": "0.4.36",
4
4
  "description": "",
5
5
  "scripts": {
6
6
  "build": "rm -rf ./dist && npx tsc",
@@ -29,6 +29,7 @@ export class HttpResponse<T = any> {
29
29
  }
30
30
  }
31
31
 
32
+ export type ServiceType = "json" | "xml" | 'stream';
32
33
  export default abstract class Service<Req, Res>
33
34
  {
34
35
  public static readonly services: Service<any, any>[] = []
@@ -37,9 +38,12 @@ export default abstract class Service<Req, Res>
37
38
 
38
39
  readonly endpoint: string;
39
40
 
40
- protected constructor(method: string, endpoint: string = "/") {
41
+ readonly type?: ServiceType;
42
+
43
+ protected constructor(method: string, endpoint: string = "/", type: ServiceType = "json") {
41
44
  this.method = method;
42
45
  this.endpoint = endpoint;
46
+ this.type = type;
43
47
  Service.services.push(this);
44
48
  }
45
49
 
@@ -5,6 +5,8 @@ export default class BusinessServer extends Business
5
5
  {
6
6
  private services: Service<any, any>[] = []
7
7
 
8
+ private express: any;
9
+
8
10
  private app: any;
9
11
 
10
12
  constructor() {
@@ -15,9 +17,9 @@ export default class BusinessServer extends Business
15
17
  async init() {
16
18
  await import("express").then(async ({ default: express }) => {
17
19
  await import("cors").then(async ({ default: cors }) => {
20
+ this.express = express;
18
21
  this.app = express();
19
22
  this.app.use(express.urlencoded({ extended: false }));
20
- this.app.use(express.json());
21
23
  this.app.use(cors());
22
24
  })
23
25
  });
@@ -57,13 +59,21 @@ export default class BusinessServer extends Business
57
59
  }
58
60
 
59
61
  private publish<Req=any, Res=any>(service: Service<Req, Res>) {
62
+
63
+ const app = (() => { switch (service.type) {
64
+ case 'json': return this.express.json();
65
+ case 'xml': return this.express.xml();
66
+ case 'stream': return this.express.raw({ type: 'application/octet-stream', limit: '10mb' });
67
+ default: return this.express.json();
68
+ }})();
69
+
60
70
  switch (service.method) {
61
- case "GET": this.app.get(service.endpoint, this.run(service)); break;
62
- case "POST": this.app.post(service.endpoint, this.run(service)); break;
63
- case "PUT": this.app.put(service.endpoint, this.run(service)); break;
64
- case "DELETE": this.app.delete(service.endpoint, this.run(service)); break;
65
- case "PATCH": this.app.patch(service.endpoint, this.run(service)); break;
66
- case "*": this.app.all(service.endpoint, this.run(service)); break;
71
+ case "GET": this.app.get(service.endpoint, app, this.run(service)); break;
72
+ case "POST": this.app.post(service.endpoint, app, this.run(service)); break;
73
+ case "PUT": this.app.put(service.endpoint, app, this.run(service)); break;
74
+ case "DELETE": this.app.delete(service.endpoint, app, this.run(service)); break;
75
+ case "PATCH": this.app.patch(service.endpoint, app, this.run(service)); break;
76
+ case "*": this.app.all(service.endpoint, app, this.run(service)); break;
67
77
  }
68
78
  }
69
79