ismx-nexo-node-app 0.3.44 → 0.3.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.
- package/dist/js/index.js +13 -1
- package/dist/js/repository/RepositoryRest.js +16 -16
- package/dist/js/repository/RepositoryRestFormal.js +13 -16
- package/dist/js/repository/RepositoryRestFormalTemplate.js +116 -0
- package/dist/types/index.d.ts +9 -0
- package/dist/types/repository/RepositoryRest.d.ts +7 -12
- package/dist/types/repository/RepositoryRestFormal.d.ts +4 -4
- package/dist/types/repository/RepositoryRestFormalTemplate.d.ts +24 -0
- package/package.json +1 -1
- package/src/main/node/index.ts +9 -0
- package/src/main/node/repository/RepositoryRest.ts +22 -29
- package/src/main/node/repository/RepositoryRestFormal.ts +17 -23
- package/src/main/node/repository/RepositoryRestFormalTemplate.ts +84 -0
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.QueryUtils = exports.RepositoryDatabasePostgres = exports.RepositoryDatabase = exports.Repository = exports.StringUtils = exports.NumberUtils = exports.CryptoUtils = exports.BusinessLogger = exports.BusinessErrors = exports.BusinessThread = exports.BusinessServer = exports.BusinessProxy = exports.BusinessState = exports.Business = exports.ServiceRestFormalTemplate = exports.ServiceRestFormal = exports.ServiceRest = exports.HttpResponse = exports.Service = void 0;
|
|
29
|
+
exports.QueryUtils = exports.RepositoryRestFormalTemplate = exports.RepositoryRestFormal = exports.RepositoryRest = exports.RepositoryDatabasePostgres = exports.RepositoryDatabase = exports.Repository = exports.StringUtils = exports.NumberUtils = exports.CryptoUtils = exports.BusinessLogger = exports.BusinessErrors = exports.BusinessThread = exports.BusinessServer = exports.BusinessProxy = exports.BusinessState = exports.Business = 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
|
}
|
|
@@ -92,6 +92,18 @@ const RepositoryDatabasePostgres_1 = __importDefault(require("./repository/Repos
|
|
|
92
92
|
class RepositoryDatabasePostgres extends RepositoryDatabasePostgres_1.default {
|
|
93
93
|
}
|
|
94
94
|
exports.RepositoryDatabasePostgres = RepositoryDatabasePostgres;
|
|
95
|
+
const RepositoryRest_1 = __importDefault(require("./repository/RepositoryRest"));
|
|
96
|
+
class RepositoryRest extends RepositoryRest_1.default {
|
|
97
|
+
}
|
|
98
|
+
exports.RepositoryRest = RepositoryRest;
|
|
99
|
+
const RepositoryRestFormal_1 = __importDefault(require("./repository/RepositoryRestFormal"));
|
|
100
|
+
class RepositoryRestFormal extends RepositoryRestFormal_1.default {
|
|
101
|
+
}
|
|
102
|
+
exports.RepositoryRestFormal = RepositoryRestFormal;
|
|
103
|
+
const RepositoryRestFormalTemplate_1 = __importDefault(require("./repository/RepositoryRestFormalTemplate"));
|
|
104
|
+
class RepositoryRestFormalTemplate extends RepositoryRestFormalTemplate_1.default {
|
|
105
|
+
}
|
|
106
|
+
exports.RepositoryRestFormalTemplate = RepositoryRestFormalTemplate;
|
|
95
107
|
const QueryUtils_1 = __importDefault(require("./repository/utils/QueryUtils"));
|
|
96
108
|
class QueryUtils extends QueryUtils_1.default {
|
|
97
109
|
}
|
|
@@ -15,17 +15,14 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
15
15
|
const Repository_1 = __importDefault(require("./Repository"));
|
|
16
16
|
const QueryUtils_1 = __importDefault(require("./utils/QueryUtils"));
|
|
17
17
|
class RepositoryRest extends Repository_1.default {
|
|
18
|
-
constructor(baseUrl, options) {
|
|
18
|
+
constructor(baseUrl, options = {}) {
|
|
19
19
|
super();
|
|
20
20
|
this.baseUrl = baseUrl;
|
|
21
|
-
this.
|
|
22
|
-
}
|
|
23
|
-
setOnRequestListener(listener) {
|
|
24
|
-
this.onRequestListener = listener;
|
|
21
|
+
this.options = options;
|
|
25
22
|
}
|
|
26
23
|
call() {
|
|
27
24
|
return __awaiter(this, arguments, void 0, function* (method = 'GET', endpoint = '/', request = {}) {
|
|
28
|
-
var _a;
|
|
25
|
+
var _a, _b, _c, _d, _e;
|
|
29
26
|
// Especifica el método de la llamada HTTP al recurso.
|
|
30
27
|
request.method = method;
|
|
31
28
|
// Completa la información de las cabeceras con cabeceras comunes por llamada a API.
|
|
@@ -33,20 +30,23 @@ class RepositoryRest extends Repository_1.default {
|
|
|
33
30
|
headers.set('Content-Type', 'application/json');
|
|
34
31
|
headers.set('Cache-Control', 'no-cache');
|
|
35
32
|
headers.set('Pragma', 'no-cache');
|
|
36
|
-
for (
|
|
37
|
-
headers.set(header, request.headers[header]);
|
|
33
|
+
for (const header of Object.keys(headers))
|
|
34
|
+
headers.set(header, (_b = (_a = request.headers) === null || _a === void 0 ? void 0 : _a[header]) !== null && _b !== void 0 ? _b : "");
|
|
38
35
|
// Notifica que una petición REST va a ser realizada
|
|
39
|
-
(
|
|
36
|
+
request = (_e = (_d = (_c = this.options).interceptor) === null || _d === void 0 ? void 0 : _d.call(_c, endpoint, request)) !== null && _e !== void 0 ? _e : request;
|
|
40
37
|
// Realiza la llamada a la API con las cabeceras y la URL base de la llamada.
|
|
41
|
-
let
|
|
42
|
-
let call = fetch(this.baseUrl + endpoint + QueryUtils_1.default.map(request.query), Object.assign(Object.assign({}, request), { headers }));
|
|
38
|
+
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 }));
|
|
43
39
|
// Añade información adicional a la respuesta y devuelve el resultado (o el error en su caso).
|
|
44
40
|
return call.then((res) => {
|
|
45
|
-
var _a;
|
|
46
|
-
let
|
|
47
|
-
|
|
48
|
-
response
|
|
49
|
-
|
|
41
|
+
var _a, _b;
|
|
42
|
+
let headers = {};
|
|
43
|
+
res.headers.forEach((value, key) => headers[key] = value);
|
|
44
|
+
let response = {
|
|
45
|
+
httpCode: res.status,
|
|
46
|
+
content: res.json(),
|
|
47
|
+
headers
|
|
48
|
+
};
|
|
49
|
+
(_b = (_a = this.options) === null || _a === void 0 ? void 0 : _a.onResponse) === null || _b === void 0 ? void 0 : _b.call(_a, response);
|
|
50
50
|
return response;
|
|
51
51
|
});
|
|
52
52
|
});
|
|
@@ -14,31 +14,28 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
15
|
const RepositoryRest_1 = __importDefault(require("./RepositoryRest"));
|
|
16
16
|
class RepositoryRestFormal extends RepositoryRest_1.default {
|
|
17
|
-
constructor(baseUrl,
|
|
18
|
-
super(baseUrl);
|
|
17
|
+
constructor(baseUrl, options) {
|
|
18
|
+
super(baseUrl, options);
|
|
19
19
|
}
|
|
20
|
-
|
|
20
|
+
callFormal() {
|
|
21
21
|
const _super = Object.create(null, {
|
|
22
22
|
call: { get: () => super.call }
|
|
23
23
|
});
|
|
24
24
|
return __awaiter(this, arguments, void 0, function* (method = 'GET', endpoint = '/', request = {}) {
|
|
25
|
-
request.body = JSON.stringify(request.body);
|
|
26
25
|
return _super.call.call(this, method, endpoint, request)
|
|
27
|
-
.then((
|
|
28
|
-
let
|
|
29
|
-
if (
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
26
|
+
.then((response) => __awaiter(this, void 0, void 0, function* () {
|
|
27
|
+
let content = response.content;
|
|
28
|
+
if (content) {
|
|
29
|
+
if (content.code === "0")
|
|
30
|
+
return content.data;
|
|
31
|
+
else
|
|
32
|
+
throw { code: content.code, description: content.description };
|
|
33
33
|
}
|
|
34
|
+
else
|
|
35
|
+
return {};
|
|
34
36
|
}))
|
|
35
37
|
.catch((error) => {
|
|
36
|
-
|
|
37
|
-
if (error instanceof FormalError)
|
|
38
|
-
throw error;
|
|
39
|
-
else { // @ts-ignore
|
|
40
|
-
throw new FormalError("-1", "ConnectionError", undefined, error);
|
|
41
|
-
}
|
|
38
|
+
throw { id: "-1", code: "-1", description: "ConnectionError", error };
|
|
42
39
|
});
|
|
43
40
|
});
|
|
44
41
|
}
|
|
@@ -0,0 +1,116 @@
|
|
|
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 RepositoryRestFormal_1 = __importDefault(require("./RepositoryRestFormal"));
|
|
16
|
+
class RepositoryRestFormalTemplate {
|
|
17
|
+
constructor(baseUrl, options) {
|
|
18
|
+
this.repository = new RepositoryRestFormal_1.default(baseUrl, options);
|
|
19
|
+
}
|
|
20
|
+
get(id_1) {
|
|
21
|
+
return __awaiter(this, arguments, void 0, function* (id, request = {}) {
|
|
22
|
+
request.query = Object.assign(Object.assign({}, request.query), { id });
|
|
23
|
+
return this.repository.callFormal("GET", "/", request);
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
getList() {
|
|
27
|
+
return __awaiter(this, arguments, void 0, function* (request = {}) {
|
|
28
|
+
var _a;
|
|
29
|
+
return (_a = (yield this.repository.callFormal("GET", "/list", request))) !== null && _a !== void 0 ? _a : [];
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
post(body_1) {
|
|
33
|
+
return __awaiter(this, arguments, void 0, function* (body, request = {}) {
|
|
34
|
+
var _a;
|
|
35
|
+
request.body = body;
|
|
36
|
+
return (_a = (yield this.repository.callFormal("POST", "/", request))) !== null && _a !== void 0 ? _a : body;
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
postList(body_1) {
|
|
40
|
+
return __awaiter(this, arguments, void 0, function* (body, request = {}) {
|
|
41
|
+
var _a;
|
|
42
|
+
request.body = body;
|
|
43
|
+
return (_a = (yield this.repository.callFormal("POST", "/list", request))) !== null && _a !== void 0 ? _a : body;
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
full(id_1) {
|
|
47
|
+
return __awaiter(this, arguments, void 0, function* (id, request = {}) {
|
|
48
|
+
request.query = Object.assign(Object.assign({}, request.query), { id });
|
|
49
|
+
return this.repository.callFormal("GET", "/full", request);
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
fullList() {
|
|
53
|
+
return __awaiter(this, arguments, void 0, function* (request = {}) {
|
|
54
|
+
var _a;
|
|
55
|
+
return (_a = (yield this.repository.callFormal("GET", "/full/list", request))) !== null && _a !== void 0 ? _a : [];
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
put(id_1, body_1) {
|
|
59
|
+
return __awaiter(this, arguments, void 0, function* (id, body, request = {}) {
|
|
60
|
+
var _a;
|
|
61
|
+
request.query = Object.assign(Object.assign({}, request.query), { id });
|
|
62
|
+
request.body = body;
|
|
63
|
+
return (_a = (yield this.repository.callFormal("PUT", "/", request))) !== null && _a !== void 0 ? _a : body;
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
del(id_1) {
|
|
67
|
+
return __awaiter(this, arguments, void 0, function* (id, request = {}) {
|
|
68
|
+
request.query = Object.assign(Object.assign({}, request.query), { id });
|
|
69
|
+
return this.repository.callFormal("DELETE", "/", request);
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
delete() {
|
|
73
|
+
return __awaiter(this, arguments, void 0, function* (request = {}) {
|
|
74
|
+
return this.repository.callFormal('DELETE', "/", request);
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
select() {
|
|
78
|
+
return __awaiter(this, arguments, void 0, function* (request = {}) {
|
|
79
|
+
var _a;
|
|
80
|
+
return (_a = (yield this.repository.callFormal("GET", "/select", request))) !== null && _a !== void 0 ? _a : [];
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
exist() {
|
|
84
|
+
return __awaiter(this, arguments, void 0, function* (request = {}) {
|
|
85
|
+
var _a;
|
|
86
|
+
return (_a = (yield this.repository.callFormal("GET", "/exist", request))) !== null && _a !== void 0 ? _a : false;
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
count() {
|
|
90
|
+
return __awaiter(this, arguments, void 0, function* (request = {}) {
|
|
91
|
+
var _a;
|
|
92
|
+
return (_a = (yield this.repository.callFormal("GET", "/count", request))) !== null && _a !== void 0 ? _a : 0;
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
page(results_1, page_1) {
|
|
96
|
+
return __awaiter(this, arguments, void 0, function* (results, page, request = {}) {
|
|
97
|
+
var _a, _b;
|
|
98
|
+
(_a = request.query) !== null && _a !== void 0 ? _a : (request.query = {});
|
|
99
|
+
request.query["maxResults"] = results.toString();
|
|
100
|
+
request.query["pageNumber"] = page.toString();
|
|
101
|
+
return (_b = (yield this.repository.callFormal("GET", "/page", request))) !== null && _b !== void 0 ? _b : { elements: [], total: 0 };
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
map() {
|
|
105
|
+
return __awaiter(this, arguments, void 0, function* (request = {}) {
|
|
106
|
+
var _a;
|
|
107
|
+
return (_a = (yield this.repository.callFormal("GET", "/map", request))) !== null && _a !== void 0 ? _a : {};
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
call(method, endpoint, request) {
|
|
111
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
112
|
+
return this.repository.callFormal(method, endpoint, request);
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
exports.default = RepositoryRestFormalTemplate;
|
package/dist/types/index.d.ts
CHANGED
|
@@ -57,6 +57,15 @@ export declare abstract class RepositoryDatabase extends _RepositoryDatabase {
|
|
|
57
57
|
import _RepositoryDatabasePostgres from "./repository/RepositoryDatabasePostgres";
|
|
58
58
|
export declare class RepositoryDatabasePostgres extends _RepositoryDatabasePostgres {
|
|
59
59
|
}
|
|
60
|
+
import _RepositoryRest from "./repository/RepositoryRest";
|
|
61
|
+
export declare class RepositoryRest<B = any, R = any> extends _RepositoryRest<B, R> {
|
|
62
|
+
}
|
|
63
|
+
import _RepositoryRestFormal from "./repository/RepositoryRestFormal";
|
|
64
|
+
export declare class RepositoryRestFormal<B = any, R = any> extends _RepositoryRestFormal<B, R> {
|
|
65
|
+
}
|
|
66
|
+
import _RepositoryRestFormalTemplate from "./repository/RepositoryRestFormalTemplate";
|
|
67
|
+
export declare class RepositoryRestFormalTemplate<M = any> extends _RepositoryRestFormalTemplate<M> {
|
|
68
|
+
}
|
|
60
69
|
import _QueryUtils from "./repository/utils/QueryUtils";
|
|
61
70
|
export declare abstract class QueryUtils extends _QueryUtils {
|
|
62
71
|
}
|
|
@@ -1,17 +1,12 @@
|
|
|
1
|
-
import Repository from
|
|
2
|
-
import { HttpRequest } from "../api/Service";
|
|
3
|
-
export interface RestResponse<E = any> extends Response {
|
|
4
|
-
timestamp?: Date;
|
|
5
|
-
duration?: number;
|
|
6
|
-
}
|
|
1
|
+
import Repository from "./Repository";
|
|
2
|
+
import { HttpRequest, HttpResponse } from "../api/Service";
|
|
7
3
|
export interface RestOptions {
|
|
8
|
-
interceptor
|
|
4
|
+
interceptor?: (endpoint: string, request: HttpRequest) => HttpRequest | undefined;
|
|
5
|
+
onResponse?: (response: HttpResponse) => void;
|
|
9
6
|
}
|
|
10
|
-
export default class RepositoryRest<
|
|
7
|
+
export default class RepositoryRest<Body = any, Res = any> extends Repository {
|
|
11
8
|
private readonly baseUrl;
|
|
12
|
-
private readonly
|
|
13
|
-
private onRequestListener?;
|
|
9
|
+
private readonly options;
|
|
14
10
|
constructor(baseUrl: string, options?: RestOptions);
|
|
15
|
-
|
|
16
|
-
call<E = S>(method?: string, endpoint?: string, request?: HttpRequest): Promise<RestResponse<E>>;
|
|
11
|
+
call<B = Body, E = Res>(method?: string, endpoint?: string, request?: HttpRequest<B>): Promise<HttpResponse<E>>;
|
|
17
12
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import RepositoryRest from "./RepositoryRest";
|
|
1
|
+
import RepositoryRest, { RestOptions } from "./RepositoryRest";
|
|
2
2
|
import { Wrapper } from "../api/ServiceRestFormal";
|
|
3
3
|
import { HttpRequest } from "../api/Service";
|
|
4
|
-
export default class RepositoryRestFormal<S = any> extends RepositoryRest<Wrapper<S>> {
|
|
5
|
-
constructor(baseUrl: string,
|
|
6
|
-
|
|
4
|
+
export default class RepositoryRestFormal<Body = any, S = any> extends RepositoryRest<Body, Wrapper<S>> {
|
|
5
|
+
constructor(baseUrl: string, options?: RestOptions);
|
|
6
|
+
callFormal<B = Body, E = S>(method?: string, endpoint?: string, request?: HttpRequest<B>): Promise<E | undefined>;
|
|
7
7
|
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { RestOptions } from "./RepositoryRest";
|
|
2
|
+
import { HttpRequest } from "../api/Service";
|
|
3
|
+
import { Pagination } from "./RepositoryDatabase";
|
|
4
|
+
export default class RepositoryRestFormalTemplate<Model> {
|
|
5
|
+
private readonly repository;
|
|
6
|
+
constructor(baseUrl: string, options?: RestOptions);
|
|
7
|
+
get(id: string, request?: HttpRequest): Promise<Model | undefined>;
|
|
8
|
+
getList(request?: HttpRequest): Promise<Model[]>;
|
|
9
|
+
post(body: Model, request?: HttpRequest): Promise<Model>;
|
|
10
|
+
postList(body: Model[], request?: HttpRequest): Promise<Model[]>;
|
|
11
|
+
full(id: string, request?: HttpRequest): Promise<Model | undefined>;
|
|
12
|
+
fullList(request?: HttpRequest): Promise<Model[]>;
|
|
13
|
+
put(id: string, body: Model, request?: HttpRequest): Promise<Model>;
|
|
14
|
+
del(id: string, request?: HttpRequest): Promise<Model | undefined>;
|
|
15
|
+
delete(request?: HttpRequest): Promise<any>;
|
|
16
|
+
select(request?: HttpRequest): Promise<string[]>;
|
|
17
|
+
exist(request?: HttpRequest): Promise<boolean>;
|
|
18
|
+
count(request?: HttpRequest): Promise<number>;
|
|
19
|
+
page(results: number, page: number, request?: HttpRequest): Promise<Pagination<Model>>;
|
|
20
|
+
map(request?: HttpRequest): Promise<{
|
|
21
|
+
[key: string]: Model;
|
|
22
|
+
}>;
|
|
23
|
+
call<B, E>(method: string, endpoint: string, request: HttpRequest<B>): Promise<E | undefined>;
|
|
24
|
+
}
|
package/package.json
CHANGED
package/src/main/node/index.ts
CHANGED
|
@@ -58,5 +58,14 @@ export abstract class RepositoryDatabase extends _RepositoryDatabase {}
|
|
|
58
58
|
import _RepositoryDatabasePostgres from "./repository/RepositoryDatabasePostgres";
|
|
59
59
|
export class RepositoryDatabasePostgres extends _RepositoryDatabasePostgres {}
|
|
60
60
|
|
|
61
|
+
import _RepositoryRest from "./repository/RepositoryRest";
|
|
62
|
+
export class RepositoryRest<B=any,R=any> extends _RepositoryRest<B,R> {}
|
|
63
|
+
|
|
64
|
+
import _RepositoryRestFormal from "./repository/RepositoryRestFormal";
|
|
65
|
+
export class RepositoryRestFormal<B=any,R=any> extends _RepositoryRestFormal<B,R> {}
|
|
66
|
+
|
|
67
|
+
import _RepositoryRestFormalTemplate from "./repository/RepositoryRestFormalTemplate";
|
|
68
|
+
export class RepositoryRestFormalTemplate<M=any> extends _RepositoryRestFormalTemplate<M> {}
|
|
69
|
+
|
|
61
70
|
import _QueryUtils from "./repository/utils/QueryUtils";
|
|
62
71
|
export abstract class QueryUtils extends _QueryUtils {}
|
|
@@ -1,39 +1,29 @@
|
|
|
1
|
-
import Repository from
|
|
1
|
+
import Repository from "./Repository";
|
|
2
2
|
import {HttpRequest, HttpResponse} from "../api/Service";
|
|
3
3
|
import QueryUtils from "./utils/QueryUtils";
|
|
4
4
|
|
|
5
|
-
export interface RestResponse<E=any> extends Response {
|
|
6
|
-
timestamp?: Date;
|
|
7
|
-
duration?: number;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
5
|
export interface RestOptions {
|
|
11
|
-
interceptor
|
|
6
|
+
interceptor?: (endpoint: string, request: HttpRequest) => HttpRequest | undefined
|
|
7
|
+
onResponse?: (response: HttpResponse) => void;
|
|
12
8
|
}
|
|
13
9
|
|
|
14
|
-
export default class RepositoryRest<
|
|
10
|
+
export default class RepositoryRest<Body=any,Res=any> extends Repository
|
|
15
11
|
{
|
|
16
12
|
private readonly baseUrl: string;
|
|
17
13
|
|
|
18
|
-
private readonly
|
|
19
|
-
|
|
20
|
-
private onRequestListener?: (method: string, endpoint: string, request: HttpRequest)=>void;
|
|
14
|
+
private readonly options: RestOptions
|
|
21
15
|
|
|
22
|
-
constructor(baseUrl: string, options
|
|
16
|
+
constructor(baseUrl: string, options: RestOptions={}) {
|
|
23
17
|
super();
|
|
24
18
|
this.baseUrl = baseUrl;
|
|
25
|
-
this.
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
setOnRequestListener(listener: (method: string, endpoint: string, request: HttpRequest)=>void) {
|
|
29
|
-
this.onRequestListener = listener;
|
|
19
|
+
this.options = options;
|
|
30
20
|
}
|
|
31
21
|
|
|
32
|
-
async call<E =
|
|
22
|
+
async call<B=Body, E = Res>(
|
|
33
23
|
method: string = 'GET',
|
|
34
24
|
endpoint: string = '/',
|
|
35
|
-
request: HttpRequest = {},
|
|
36
|
-
): Promise<
|
|
25
|
+
request: HttpRequest<B> = {},
|
|
26
|
+
): Promise<HttpResponse<E>>
|
|
37
27
|
{
|
|
38
28
|
// Especifica el método de la llamada HTTP al recurso.
|
|
39
29
|
request.method = method;
|
|
@@ -43,25 +33,28 @@ export default class RepositoryRest<S=any> extends Repository
|
|
|
43
33
|
headers.set('Content-Type', 'application/json');
|
|
44
34
|
headers.set('Cache-Control', 'no-cache');
|
|
45
35
|
headers.set('Pragma', 'no-cache');
|
|
46
|
-
for (
|
|
47
|
-
headers.set(header, request.headers[header])
|
|
36
|
+
for (const header of Object.keys(headers))
|
|
37
|
+
headers.set(header, request.headers?.[header] ?? "")
|
|
48
38
|
|
|
49
39
|
// Notifica que una petición REST va a ser realizada
|
|
50
|
-
this.
|
|
40
|
+
request = this.options.interceptor?.(endpoint, request) ?? request
|
|
51
41
|
|
|
52
42
|
// Realiza la llamada a la API con las cabeceras y la URL base de la llamada.
|
|
53
|
-
let timestamp = new Date();
|
|
54
43
|
let call = fetch(this.baseUrl + endpoint + QueryUtils.map(request.query), {
|
|
55
44
|
...request,
|
|
56
|
-
|
|
45
|
+
body: method === "GET" ? JSON.stringify(request.body) : undefined,
|
|
46
|
+
headers
|
|
57
47
|
});
|
|
58
48
|
|
|
59
49
|
// Añade información adicional a la respuesta y devuelve el resultado (o el error en su caso).
|
|
60
50
|
return call.then((res: Response) => {
|
|
61
|
-
let
|
|
62
|
-
response
|
|
63
|
-
|
|
64
|
-
|
|
51
|
+
let headers: any = {}; res.headers.forEach((value, key) => headers[key] = value);
|
|
52
|
+
let response: HttpResponse<E> = {
|
|
53
|
+
httpCode: res.status,
|
|
54
|
+
content: res.json() as E,
|
|
55
|
+
headers
|
|
56
|
+
};
|
|
57
|
+
this.options?.onResponse?.(response);
|
|
65
58
|
return response;
|
|
66
59
|
});
|
|
67
60
|
}
|
|
@@ -1,36 +1,30 @@
|
|
|
1
|
-
import RepositoryRest from "./RepositoryRest";
|
|
1
|
+
import RepositoryRest, {RestOptions} from "./RepositoryRest";
|
|
2
2
|
import {Wrapper} from "../api/ServiceRestFormal";
|
|
3
3
|
import {HttpRequest} from "../api/Service";
|
|
4
|
+
import { FormalError} from "../business/BusinessErrors";
|
|
4
5
|
|
|
5
|
-
export default class RepositoryRestFormal<S=any> extends RepositoryRest<Wrapper<S>> {
|
|
6
|
+
export default class RepositoryRestFormal<Body=any, S=any> extends RepositoryRest<Body, Wrapper<S>> {
|
|
6
7
|
|
|
7
|
-
constructor(baseUrl: string,
|
|
8
|
-
super(baseUrl);
|
|
8
|
+
constructor(baseUrl: string, options?: RestOptions) {
|
|
9
|
+
super(baseUrl, options);
|
|
9
10
|
}
|
|
10
11
|
|
|
11
|
-
async
|
|
12
|
+
async callFormal<B = Body, E = S>(
|
|
12
13
|
method: string = 'GET',
|
|
13
14
|
endpoint: string = '/',
|
|
14
|
-
request: HttpRequest = {},
|
|
15
|
-
): Promise<E> {
|
|
16
|
-
|
|
17
|
-
request.body = JSON.stringify(request.body);
|
|
18
|
-
|
|
19
|
-
return super.call<E>(method, endpoint, request)
|
|
20
|
-
.then(async (call) => {
|
|
21
|
-
let json = await call.json();
|
|
22
|
-
if (json.code === "0") return json.data;
|
|
23
|
-
else { // @ts-ignore
|
|
24
|
-
throw new FormalError(json.code, json.description, call);
|
|
25
|
-
}
|
|
15
|
+
request: HttpRequest<B> = {},
|
|
16
|
+
): Promise<E | undefined> {
|
|
26
17
|
|
|
18
|
+
return super.call<B, Wrapper<E>>(method, endpoint, request)
|
|
19
|
+
.then(async (response) => {
|
|
20
|
+
let content = response.content;
|
|
21
|
+
if (content) {
|
|
22
|
+
if (content.code === "0") return content.data;
|
|
23
|
+
else throw { code: content.code, description: content.description } as FormalError;
|
|
24
|
+
} else return {} as E;
|
|
27
25
|
})
|
|
28
|
-
.catch((error
|
|
29
|
-
|
|
30
|
-
if (error instanceof FormalError) throw error;
|
|
31
|
-
else { // @ts-ignore
|
|
32
|
-
throw new FormalError("-1", "ConnectionError", undefined, error)
|
|
33
|
-
}
|
|
26
|
+
.catch((error) => {
|
|
27
|
+
throw { id: "-1", code: "-1", description: "ConnectionError", error } as FormalError
|
|
34
28
|
});
|
|
35
29
|
}
|
|
36
30
|
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import {RestOptions} from "./RepositoryRest";
|
|
2
|
+
import RepositoryRestFormal from "./RepositoryRestFormal";
|
|
3
|
+
import {HttpRequest} from "../api/Service";
|
|
4
|
+
import {Pagination} from "./RepositoryDatabase";
|
|
5
|
+
|
|
6
|
+
export default class RepositoryRestFormalTemplate<Model> {
|
|
7
|
+
|
|
8
|
+
private readonly repository: RepositoryRestFormal;
|
|
9
|
+
|
|
10
|
+
constructor(baseUrl: string, options?: RestOptions) {
|
|
11
|
+
this.repository = new RepositoryRestFormal(baseUrl, options);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
async get(id: string, request: HttpRequest = {}): Promise<Model | undefined> {
|
|
15
|
+
request.query = { ...request.query, id }
|
|
16
|
+
return this.repository.callFormal<any, Model>("GET", "/", request);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
async getList(request: HttpRequest = {}): Promise<Model[]> {
|
|
20
|
+
return (await this.repository.callFormal("GET", "/list", request)) ?? [];
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
async post(body: Model, request: HttpRequest = {}): Promise<Model> {
|
|
24
|
+
request.body = body
|
|
25
|
+
return (await this.repository.callFormal("POST", "/", request)) ?? body;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
async postList(body: Model[], request: HttpRequest = {}): Promise<Model[]> {
|
|
29
|
+
request.body = body;
|
|
30
|
+
return (await this.repository.callFormal("POST", "/list", request)) ?? body;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async full(id: string, request: HttpRequest = {}): Promise<Model | undefined> {
|
|
34
|
+
request.query = { ...request.query, id }
|
|
35
|
+
return this.repository.callFormal("GET", "/full", request);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async fullList(request: HttpRequest = {}): Promise<Model[]> {
|
|
39
|
+
return (await this.repository.callFormal("GET", "/full/list", request)) ?? [];
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async put(id: string, body: Model, request: HttpRequest = {}): Promise<Model> {
|
|
43
|
+
request.query = { ...request.query, id };
|
|
44
|
+
request.body = body;
|
|
45
|
+
return (await this.repository.callFormal("PUT", "/", request)) ?? body;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
async del(id: string, request: HttpRequest = {}): Promise<Model | undefined> {
|
|
50
|
+
request.query = { ...request.query, id };
|
|
51
|
+
return this.repository.callFormal("DELETE", "/", request);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async delete(request: HttpRequest = {}) {
|
|
55
|
+
return this.repository.callFormal('DELETE', "/", request);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async select(request: HttpRequest={}): Promise<string[]> {
|
|
59
|
+
return (await this.repository.callFormal("GET", "/select", request)) ?? [];
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async exist(request: HttpRequest={}): Promise<boolean> {
|
|
63
|
+
return (await this.repository.callFormal("GET", "/exist", request)) ?? false;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
async count(request: HttpRequest={}): Promise<number> {
|
|
67
|
+
return (await this.repository.callFormal("GET", "/count", request)) ?? 0;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
async page(results: number, page: number, request: HttpRequest={}): Promise<Pagination<Model>> {
|
|
71
|
+
request.query ??= {};
|
|
72
|
+
request.query["maxResults"] = results.toString();
|
|
73
|
+
request.query["pageNumber"] = page.toString();
|
|
74
|
+
return (await this.repository.callFormal("GET", "/page", request)) ?? { elements: [], total: 0};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
async map(request: HttpRequest={}): Promise<{ [key: string]: Model }> {
|
|
78
|
+
return (await this.repository.callFormal("GET", "/map", request)) ?? {};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
async call<B, E>(method: string, endpoint: string, request: HttpRequest<B>): Promise<E | undefined> {
|
|
82
|
+
return this.repository.callFormal<B, E>(method, endpoint, request);
|
|
83
|
+
}
|
|
84
|
+
}
|