ismx-nexo-node-app 0.4.34 → 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
  }
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ const BusinessState_1 = __importDefault(require("./BusinessState"));
16
+ class BusinessThreadState extends BusinessState_1.default {
17
+ constructor(runImmediately = false) {
18
+ super();
19
+ this.runImmediately = runImmediately;
20
+ }
21
+ init() {
22
+ return __awaiter(this, void 0, void 0, function* () {
23
+ if (this.runImmediately)
24
+ try {
25
+ yield this.run();
26
+ }
27
+ catch (err) {
28
+ this.onError(err);
29
+ }
30
+ this.intervalId = setInterval(() => __awaiter(this, void 0, void 0, function* () {
31
+ try {
32
+ yield this.run();
33
+ }
34
+ catch (err) {
35
+ this.onError(err);
36
+ }
37
+ }), this.interval());
38
+ });
39
+ }
40
+ terminate() {
41
+ clearInterval(this.intervalId);
42
+ }
43
+ onError(err) { }
44
+ }
45
+ exports.default = BusinessThreadState;
package/dist/js/index.js CHANGED
@@ -26,7 +26,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
26
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.RestUtils = exports.QueryUtils = exports.RepositoryRestFormalTemplate = exports.RepositoryRestFormal = exports.RepositoryRest = exports.RepositoryDatabasePostgres = exports.RepositoryDatabase = exports.Repository = exports.ArrayUtils = exports.StringUtils = exports.NumberUtils = exports.CryptoUtils = exports.DateUtils = exports.BusinessLogger = exports.FormalError = exports.BusinessErrors = exports.BusinessThread = exports.BusinessServer = exports.BusinessProxy = exports.BusinessState = exports.Business = exports.ColorUtils = exports.ServiceRestFormalTemplate = exports.ServiceRestFormal = exports.ServiceRest = exports.HttpResponse = exports.Service = void 0;
29
+ exports.RestUtils = exports.QueryUtils = exports.RepositoryRestFormalTemplate = exports.RepositoryRestFormal = exports.RepositoryRest = exports.RepositoryDatabasePostgres = exports.RepositoryDatabase = exports.Repository = exports.ArrayUtils = exports.StringUtils = exports.NumberUtils = exports.CryptoUtils = exports.DateUtils = exports.BusinessLogger = exports.FormalError = exports.BusinessErrors = exports.BusinessThreadState = exports.BusinessThread = exports.BusinessServer = exports.BusinessProxy = exports.BusinessState = exports.Business = exports.ColorUtils = exports.ServiceRestFormalTemplate = exports.ServiceRestFormal = exports.ServiceRest = exports.HttpResponse = exports.Service = void 0;
30
30
  const Service_1 = __importStar(require("./api/Service"));
31
31
  class Service extends Service_1.default {
32
32
  }
@@ -71,6 +71,10 @@ const BusinessThread_1 = __importDefault(require("./business/BusinessThread"));
71
71
  class BusinessThread extends BusinessThread_1.default {
72
72
  }
73
73
  exports.BusinessThread = BusinessThread;
74
+ const BusinessThreadState_1 = __importDefault(require("./business/BusinessThreadState"));
75
+ class BusinessThreadState extends BusinessThreadState_1.default {
76
+ }
77
+ exports.BusinessThreadState = BusinessThreadState;
74
78
  const BusinessErrors_1 = __importStar(require("./business/BusinessErrors"));
75
79
  class BusinessErrors extends BusinessErrors_1.default {
76
80
  }
@@ -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>;
@@ -1,12 +1,11 @@
1
1
  import Business from "./Business";
2
2
  export default abstract class BusinessThread extends Business {
3
- private runImmediately;
3
+ private readonly runImmediately;
4
4
  private intervalId?;
5
5
  protected constructor(runImmediately?: boolean);
6
6
  init(): Promise<void>;
7
7
  terminate(): void;
8
8
  abstract run(): Promise<void>;
9
9
  abstract interval(): number;
10
- abstract delay?(): number;
11
10
  onError(err: Error): void;
12
11
  }
@@ -0,0 +1,11 @@
1
+ import BusinessState from "./BusinessState";
2
+ export default abstract class BusinessThreadState<Model> extends BusinessState<Model> {
3
+ private readonly runImmediately;
4
+ private intervalId?;
5
+ protected constructor(runImmediately?: boolean);
6
+ init(): Promise<void>;
7
+ terminate(): void;
8
+ abstract run(): Promise<void>;
9
+ abstract interval(): number;
10
+ onError(err: Error): void;
11
+ }
@@ -37,6 +37,9 @@ export declare class BusinessServer extends _BusinessServer {
37
37
  import BaseBusinessThread from "./business/BusinessThread";
38
38
  export declare abstract class BusinessThread extends BaseBusinessThread {
39
39
  }
40
+ import BaseBusinessThreadState from "./business/BusinessThreadState";
41
+ export declare abstract class BusinessThreadState<Model = any> extends BaseBusinessThreadState<Model> {
42
+ }
40
43
  import BaseBusinessErrors, { ErrorType as _ErrorType, FormalError as _FormalError } from "./business/BusinessErrors";
41
44
  export declare class BusinessErrors extends BaseBusinessErrors {
42
45
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ismx-nexo-node-app",
3
- "version": "0.4.34",
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
 
@@ -2,7 +2,7 @@ import Business from "./Business";
2
2
 
3
3
  export default abstract class BusinessThread extends Business
4
4
  {
5
- private runImmediately: boolean;
5
+ private readonly runImmediately: boolean;
6
6
  private intervalId?: NodeJS.Timeout;
7
7
 
8
8
  protected constructor(runImmediately: boolean = false) {
@@ -26,6 +26,5 @@ export default abstract class BusinessThread extends Business
26
26
 
27
27
  abstract run(): Promise<void>
28
28
  abstract interval(): number
29
- abstract delay?(): number
30
29
  onError(err: Error) {}
31
30
  }
@@ -0,0 +1,29 @@
1
+ import BusinessState from "./BusinessState";
2
+
3
+ export default abstract class BusinessThreadState<Model> extends BusinessState<Model>
4
+ {
5
+ private readonly runImmediately: boolean;
6
+ private intervalId?: NodeJS.Timeout;
7
+
8
+ protected constructor(runImmediately: boolean = false) {
9
+ super();
10
+ this.runImmediately = runImmediately;
11
+ }
12
+
13
+ async init() {
14
+ if (this.runImmediately)
15
+ try { await this.run() } catch (err) { this.onError(err as Error) }
16
+
17
+ this.intervalId = setInterval(async () => {
18
+ try { await this.run() } catch (err) { this.onError(err as Error) }
19
+ }, this.interval());
20
+ }
21
+
22
+ terminate() {
23
+ clearInterval(this.intervalId);
24
+ }
25
+
26
+ abstract run(): Promise<void>
27
+ abstract interval(): number
28
+ onError(err: Error) {}
29
+ }
@@ -34,6 +34,9 @@ export class BusinessServer extends _BusinessServer {}
34
34
  import BaseBusinessThread from "./business/BusinessThread";
35
35
  export abstract class BusinessThread extends BaseBusinessThread {}
36
36
 
37
+ import BaseBusinessThreadState from "./business/BusinessThreadState";
38
+ export abstract class BusinessThreadState<Model=any> extends BaseBusinessThreadState<Model> {}
39
+
37
40
  import BaseBusinessErrors, { ErrorType as _ErrorType, FormalError as _FormalError} from "./business/BusinessErrors";
38
41
  export class BusinessErrors extends BaseBusinessErrors {}
39
42
  export interface ErrorType extends _ErrorType {}