ismx-nexo-node-app 0.4.43 → 0.4.45
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.
|
@@ -40,15 +40,6 @@ class BusinessProxy {
|
|
|
40
40
|
setModules(modules) {
|
|
41
41
|
this.modules = modules;
|
|
42
42
|
}
|
|
43
|
-
callFormal(tag, method, endpoint, request) {
|
|
44
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
45
|
-
let response = yield this.call(tag, method, endpoint, request);
|
|
46
|
-
if ((response === null || response === void 0 ? void 0 : response.code) === "0")
|
|
47
|
-
return response.data;
|
|
48
|
-
else
|
|
49
|
-
throw response;
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
43
|
call(tag, method, endpoint, request) {
|
|
53
44
|
return __awaiter(this, void 0, void 0, function* () {
|
|
54
45
|
let module = this.modules[tag];
|
|
@@ -62,7 +53,7 @@ class BusinessProxy {
|
|
|
62
53
|
headers.set(header, request.headers[header]);
|
|
63
54
|
let response = null;
|
|
64
55
|
try {
|
|
65
|
-
|
|
56
|
+
return yield (0, node_fetch_1.default)(`${module.host}${endpoint}${this.map(request.query)}`, {
|
|
66
57
|
method: method,
|
|
67
58
|
body: method !== "GET" ? JSON.stringify(request.body) : undefined,
|
|
68
59
|
headers: headers,
|
|
@@ -71,14 +62,6 @@ class BusinessProxy {
|
|
|
71
62
|
catch (error) {
|
|
72
63
|
throw new Error(`Module ${tag} is not responding`);
|
|
73
64
|
}
|
|
74
|
-
if (response.ok) {
|
|
75
|
-
if (response.headers.get('Content-Type') === 'application/json')
|
|
76
|
-
return yield response.json();
|
|
77
|
-
else
|
|
78
|
-
return yield response.text();
|
|
79
|
-
}
|
|
80
|
-
else
|
|
81
|
-
throw yield response.json();
|
|
82
65
|
});
|
|
83
66
|
}
|
|
84
67
|
map(query = {}) {
|
|
@@ -41,17 +41,15 @@ class RepositoryRest extends Repository_1.default {
|
|
|
41
41
|
let call = fetch(this.baseUrl + endpoint + QueryUtils_1.default.map(request.query), Object.assign(Object.assign({}, request), { body: method !== "GET" ? JSON.stringify(request.body) : undefined, headers }));
|
|
42
42
|
// Añade información adicional a la respuesta y devuelve el resultado (o el error en su caso).
|
|
43
43
|
return call.then((res) => __awaiter(this, void 0, void 0, function* () {
|
|
44
|
-
var _a, _b, _c, _d;
|
|
44
|
+
var _a, _b, _c, _d, _e;
|
|
45
45
|
let headers = {};
|
|
46
46
|
res.headers.forEach((value, key) => headers[key] = value);
|
|
47
|
-
let
|
|
48
|
-
(_b = (_a = this.options).onRawResponse) === null || _b === void 0 ? void 0 : _b.call(_a, request,
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
};
|
|
54
|
-
(_d = (_c = this.options) === null || _c === void 0 ? void 0 : _c.onResponse) === null || _d === void 0 ? void 0 : _d.call(_c, request, response);
|
|
47
|
+
let content = yield res.text();
|
|
48
|
+
(_b = (_a = this.options).onRawResponse) === null || _b === void 0 ? void 0 : _b.call(_a, request, content);
|
|
49
|
+
if ((_c = headers['Content-Type']) === null || _c === void 0 ? void 0 : _c.startsWith('application/json'))
|
|
50
|
+
content = JSON.parse(content, this.reviver);
|
|
51
|
+
let response = { httpCode: res.status, content: content, headers };
|
|
52
|
+
(_e = (_d = this.options) === null || _d === void 0 ? void 0 : _d.onResponse) === null || _e === void 0 ? void 0 : _e.call(_d, request, response);
|
|
55
53
|
return response;
|
|
56
54
|
})).catch((error) => {
|
|
57
55
|
var _a, _b;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { HttpRequest } from "../api/Service";
|
|
2
|
+
import fetch from 'node-fetch';
|
|
2
3
|
export interface Module {
|
|
3
4
|
id: string;
|
|
4
5
|
tag: string;
|
|
@@ -9,7 +10,6 @@ export default class BusinessProxy {
|
|
|
9
10
|
setModules(modules: {
|
|
10
11
|
[key: string]: Module;
|
|
11
12
|
}): void;
|
|
12
|
-
|
|
13
|
-
call<Req, Res>(tag: string, method: string, endpoint: string, request: HttpRequest<Req>): Promise<Res>;
|
|
13
|
+
call<Req, Res>(tag: string, method: string, endpoint: string, request: HttpRequest<Req>): Promise<fetch.Response>;
|
|
14
14
|
private map;
|
|
15
15
|
}
|
package/package.json
CHANGED
|
@@ -16,13 +16,7 @@ export default class BusinessProxy
|
|
|
16
16
|
this.modules = modules
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
async
|
|
20
|
-
let response = await this.call<Req, Wrapper<Res>>(tag, method, endpoint, request);
|
|
21
|
-
if (response?.code === "0") return response.data;
|
|
22
|
-
else throw response;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
async call<Req, Res>(tag: string, method: string, endpoint: string, request: HttpRequest<Req>): Promise<Res> {
|
|
19
|
+
async call<Req, Res>(tag: string, method: string, endpoint: string, request: HttpRequest<Req>): Promise<fetch.Response> {
|
|
26
20
|
let module = this.modules[tag];
|
|
27
21
|
if (!module) throw new Error(`Module ${tag} does not exist`);
|
|
28
22
|
|
|
@@ -35,18 +29,12 @@ export default class BusinessProxy
|
|
|
35
29
|
|
|
36
30
|
let response = null;
|
|
37
31
|
try {
|
|
38
|
-
|
|
32
|
+
return await fetch(`${module.host}${endpoint}${this.map(request.query)}`, {
|
|
39
33
|
method: method,
|
|
40
34
|
body: method !== "GET" ? JSON.stringify(request.body) : undefined,
|
|
41
35
|
headers: headers,
|
|
42
36
|
});
|
|
43
37
|
} catch (error) { throw new Error(`Module ${tag} is not responding`); }
|
|
44
|
-
|
|
45
|
-
if (response.ok) {
|
|
46
|
-
if (response.headers.get('Content-Type') === 'application/json') return await response.json();
|
|
47
|
-
else return await response.text() as Res;
|
|
48
|
-
}
|
|
49
|
-
else throw await response.json();
|
|
50
38
|
}
|
|
51
39
|
|
|
52
40
|
private map(query: { [key:string]: string } = {}) {
|
|
@@ -56,15 +56,16 @@ export default class RepositoryRest<Body=any,Res=any> extends Repository
|
|
|
56
56
|
// Añade información adicional a la respuesta y devuelve el resultado (o el error en su caso).
|
|
57
57
|
return call.then(async (res: Response) => {
|
|
58
58
|
let headers: any = {}; res.headers.forEach((value, key) => headers[key] = value);
|
|
59
|
-
let
|
|
60
|
-
this.options.onRawResponse?.(request,
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
content
|
|
64
|
-
|
|
65
|
-
};
|
|
59
|
+
let content: string | E = await res.text();
|
|
60
|
+
this.options.onRawResponse?.(request, content);
|
|
61
|
+
|
|
62
|
+
if (headers['Content-Type']?.startsWith('application/json'))
|
|
63
|
+
content = JSON.parse(content, this.reviver) as E;
|
|
64
|
+
|
|
65
|
+
let response: HttpResponse<E> = { httpCode: res.status, content: content as E, headers };
|
|
66
66
|
this.options?.onResponse?.(request, response);
|
|
67
67
|
return response;
|
|
68
|
+
|
|
68
69
|
}).catch((error) => {
|
|
69
70
|
this.options.onError?.(request, error);
|
|
70
71
|
throw error;
|