ng2-rest 13.2.7 → 13.2.8
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/README.md +135 -135
- package/assets/shared/shared_folder_info.txt +1 -1
- package/browser/README.md +24 -24
- package/browser/esm2020/lib/content-type.mjs +12 -12
- package/browser/esm2020/lib/cookie.mjs +28 -28
- package/browser/esm2020/lib/helpers.mjs +23 -23
- package/browser/esm2020/lib/index.mjs +12 -12
- package/browser/esm2020/lib/mapping.mjs +238 -238
- package/browser/esm2020/lib/models.mjs +159 -159
- package/browser/esm2020/lib/other/simple-resource.mjs +116 -116
- package/browser/esm2020/lib/params.mjs +278 -278
- package/browser/esm2020/lib/request-cache.mjs +100 -100
- package/browser/esm2020/lib/resource.service.mjs +213 -213
- package/browser/esm2020/lib/rest-headers.mjs +128 -128
- package/browser/esm2020/lib/rest-request.mjs +321 -321
- package/browser/esm2020/lib/rest.class.mjs +115 -115
- package/browser/esm2020/ng2-rest.mjs +4 -4
- package/browser/esm2020/public-api.mjs +1 -1
- package/browser/fesm2015/ng2-rest.mjs +1677 -1677
- package/browser/fesm2020/ng2-rest.mjs +1675 -1675
- package/browser/lib/content-type.d.ts +4 -4
- package/browser/lib/cookie.d.ts +7 -7
- package/browser/lib/helpers.d.ts +10 -10
- package/browser/lib/index.d.ts +9 -9
- package/browser/lib/mapping.d.ts +12 -12
- package/browser/lib/models.d.ts +157 -157
- package/browser/lib/other/simple-resource.d.ts +29 -29
- package/browser/lib/params.d.ts +23 -23
- package/browser/lib/request-cache.d.ts +17 -17
- package/browser/lib/resource.service.d.ts +43 -43
- package/browser/lib/rest-headers.d.ts +57 -57
- package/browser/lib/rest-request.d.ts +21 -21
- package/browser/lib/rest.class.d.ts +36 -36
- package/browser/ng2-rest.d.ts +4 -4
- package/client/README.md +24 -24
- package/client/esm2020/lib/content-type.mjs +12 -12
- package/client/esm2020/lib/cookie.mjs +28 -28
- package/client/esm2020/lib/helpers.mjs +23 -23
- package/client/esm2020/lib/index.mjs +12 -12
- package/client/esm2020/lib/mapping.mjs +238 -238
- package/client/esm2020/lib/models.mjs +159 -159
- package/client/esm2020/lib/other/simple-resource.mjs +116 -116
- package/client/esm2020/lib/params.mjs +278 -278
- package/client/esm2020/lib/request-cache.mjs +100 -100
- package/client/esm2020/lib/resource.service.mjs +213 -213
- package/client/esm2020/lib/rest-headers.mjs +128 -128
- package/client/esm2020/lib/rest-request.mjs +321 -321
- package/client/esm2020/lib/rest.class.mjs +115 -115
- package/client/esm2020/ng2-rest.mjs +4 -4
- package/client/esm2020/public-api.mjs +1 -1
- package/client/fesm2015/ng2-rest.mjs +1677 -1677
- package/client/fesm2020/ng2-rest.mjs +1675 -1675
- package/client/lib/content-type.d.ts +4 -4
- package/client/lib/cookie.d.ts +7 -7
- package/client/lib/helpers.d.ts +10 -10
- package/client/lib/index.d.ts +9 -9
- package/client/lib/mapping.d.ts +12 -12
- package/client/lib/models.d.ts +157 -157
- package/client/lib/other/simple-resource.d.ts +29 -29
- package/client/lib/params.d.ts +23 -23
- package/client/lib/request-cache.d.ts +17 -17
- package/client/lib/resource.service.d.ts +43 -43
- package/client/lib/rest-headers.d.ts +57 -57
- package/client/lib/rest-request.d.ts +21 -21
- package/client/lib/rest.class.d.ts +36 -36
- package/client/ng2-rest.d.ts +4 -4
- package/client/package.json +28 -28
- package/index.d.ts +1 -1
- package/index.js.map +1 -1
- package/lib/content-type.d.ts +5 -5
- package/lib/content-type.js.map +1 -1
- package/lib/cookie.d.ts +8 -8
- package/lib/cookie.js.map +1 -1
- package/lib/helpers.d.ts +11 -11
- package/lib/helpers.js.map +1 -1
- package/lib/index.d.ts +10 -10
- package/lib/index.js.map +1 -1
- package/lib/mapping.d.ts +13 -13
- package/lib/mapping.js.map +1 -1
- package/lib/models.d.ts +158 -158
- package/lib/models.js.map +1 -1
- package/lib/other/simple-resource.d.ts +30 -30
- package/lib/other/simple-resource.js.map +1 -1
- package/lib/params.d.ts +24 -24
- package/lib/params.js.map +1 -1
- package/lib/request-cache.d.ts +18 -18
- package/lib/request-cache.js.map +1 -1
- package/lib/resource.service.d.ts +44 -44
- package/lib/resource.service.js.map +1 -1
- package/lib/rest-headers.d.ts +58 -58
- package/lib/rest-headers.js.map +1 -1
- package/lib/rest-request.d.ts +22 -22
- package/lib/rest-request.js.map +1 -1
- package/lib/rest.class.d.ts +37 -37
- package/lib/rest.class.js.map +1 -1
- package/package.json +4 -4
- package/package.json_devDependencies.json +217 -217
- package/package.json_tnp.json5 +53 -50
- package/tmp-environment.json +34 -33
- package/websql/README.md +24 -24
- package/websql/esm2020/lib/content-type.mjs +12 -12
- package/websql/esm2020/lib/cookie.mjs +28 -28
- package/websql/esm2020/lib/helpers.mjs +23 -23
- package/websql/esm2020/lib/index.mjs +12 -12
- package/websql/esm2020/lib/mapping.mjs +238 -238
- package/websql/esm2020/lib/models.mjs +159 -159
- package/websql/esm2020/lib/other/simple-resource.mjs +116 -116
- package/websql/esm2020/lib/params.mjs +278 -278
- package/websql/esm2020/lib/request-cache.mjs +100 -100
- package/websql/esm2020/lib/resource.service.mjs +213 -213
- package/websql/esm2020/lib/rest-headers.mjs +128 -128
- package/websql/esm2020/lib/rest-request.mjs +321 -321
- package/websql/esm2020/lib/rest.class.mjs +115 -115
- package/websql/esm2020/ng2-rest.mjs +4 -4
- package/websql/esm2020/public-api.mjs +1 -1
- package/websql/fesm2015/ng2-rest.mjs +1677 -1677
- package/websql/fesm2020/ng2-rest.mjs +1675 -1675
- package/websql/lib/content-type.d.ts +4 -4
- package/websql/lib/cookie.d.ts +7 -7
- package/websql/lib/helpers.d.ts +10 -10
- package/websql/lib/index.d.ts +9 -9
- package/websql/lib/mapping.d.ts +12 -12
- package/websql/lib/models.d.ts +157 -157
- package/websql/lib/other/simple-resource.d.ts +29 -29
- package/websql/lib/params.d.ts +23 -23
- package/websql/lib/request-cache.d.ts +17 -17
- package/websql/lib/resource.service.d.ts +43 -43
- package/websql/lib/rest-headers.d.ts +57 -57
- package/websql/lib/rest-request.d.ts +21 -21
- package/websql/lib/rest.class.d.ts +36 -36
- package/websql/ng2-rest.d.ts +4 -4
|
@@ -7,1729 +7,1729 @@ import { CLASS, SYMBOL, Models as Models$1 } from 'typescript-class-helpers/webs
|
|
|
7
7
|
import { JSON10 } from 'json10/websql';
|
|
8
8
|
import axios from 'axios';
|
|
9
9
|
|
|
10
|
-
var Mapping;
|
|
11
|
-
(function (Mapping) {
|
|
12
|
-
function decode(json, autodetect = false) {
|
|
13
|
-
if (_.isUndefined(json)) {
|
|
14
|
-
return void 0;
|
|
15
|
-
}
|
|
16
|
-
let mapping = decodeFromDecorator(_.isArray(json) ? _.first(json) : json, !autodetect);
|
|
17
|
-
if (autodetect) {
|
|
18
|
-
mapping = _.merge(getMappingNaive(json), mapping);
|
|
19
|
-
}
|
|
20
|
-
return mapping;
|
|
21
|
-
}
|
|
22
|
-
Mapping.decode = decode;
|
|
23
|
-
function encode(json, mapping, circular = []) {
|
|
24
|
-
if (_.isString(json) || _.isBoolean(json) || _.isNumber(json)) {
|
|
25
|
-
return json;
|
|
26
|
-
}
|
|
27
|
-
if (mapping['']) {
|
|
28
|
-
const decoratorMapping = getModelsMapping(CLASS.getBy(mapping['']));
|
|
29
|
-
mapping = _.merge(mapping, decoratorMapping);
|
|
30
|
-
}
|
|
31
|
-
let res;
|
|
32
|
-
if (_.isArray(circular) && circular.length > 0) {
|
|
33
|
-
res = setMappingCirc(json, mapping, circular);
|
|
34
|
-
}
|
|
35
|
-
else {
|
|
36
|
-
res = setMapping(json, mapping);
|
|
37
|
-
}
|
|
38
|
-
return res;
|
|
39
|
-
}
|
|
40
|
-
Mapping.encode = encode;
|
|
41
|
-
function decodeFromDecorator(json, production = false) {
|
|
42
|
-
const entityClass = CLASS.getFromObject(json);
|
|
43
|
-
const mappings = getModelsMapping(entityClass);
|
|
44
|
-
return mappings;
|
|
45
|
-
}
|
|
46
|
-
function getModelsMapping(entity) {
|
|
47
|
-
if (!_.isFunction(entity) || entity === Object) {
|
|
48
|
-
return {};
|
|
49
|
-
}
|
|
50
|
-
const className = CLASS.getName(entity);
|
|
51
|
-
let enityOWnMapping = _.isArray(entity[SYMBOL.MODELS_MAPPING]) ?
|
|
52
|
-
entity[SYMBOL.MODELS_MAPPING] : [{ '': className }];
|
|
53
|
-
let res = {};
|
|
54
|
-
let parents = enityOWnMapping
|
|
55
|
-
.filter(m => !_.isUndefined(m['']) && m[''] !== className)
|
|
56
|
-
.map(m => m['']);
|
|
57
|
-
enityOWnMapping.reverse().forEach(m => {
|
|
58
|
-
m = _.cloneDeep(m);
|
|
59
|
-
Object.keys(m).forEach(key => {
|
|
60
|
-
const v = m[key];
|
|
61
|
-
const isArr = _.isArray(v);
|
|
62
|
-
const model = isArr ? _.first(v) : v;
|
|
63
|
-
if (parents.includes(model)) {
|
|
64
|
-
m[key] = isArr ? [className] : className;
|
|
65
|
-
}
|
|
66
|
-
});
|
|
67
|
-
res = _.merge(res, m);
|
|
68
|
-
});
|
|
69
|
-
res[''] = className;
|
|
70
|
-
return res;
|
|
71
|
-
}
|
|
72
|
-
Mapping.getModelsMapping = getModelsMapping;
|
|
73
|
-
function add(o, path, mapping = {}) {
|
|
74
|
-
if (!o || Array.isArray(o) || typeof o !== 'object')
|
|
75
|
-
return;
|
|
76
|
-
const objectClassName = CLASS.getName(Object.getPrototypeOf(o).constructor);
|
|
77
|
-
const resolveClass = CLASS.getBy(objectClassName);
|
|
78
|
-
if (!resolveClass) {
|
|
79
|
-
if (objectClassName !== 'Object') {
|
|
80
|
-
if (Helpers$1.isBrowser) {
|
|
81
|
-
console.error(`Cannot resolve class "${objectClassName}" while mapping.`);
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
return;
|
|
85
|
-
}
|
|
86
|
-
if (!mapping[path])
|
|
87
|
-
mapping[path] = CLASS.getName(resolveClass);
|
|
88
|
-
;
|
|
89
|
-
}
|
|
90
|
-
/**
|
|
91
|
-
* USE ONLY IN DEVELOPMENT
|
|
92
|
-
* @param c
|
|
93
|
-
* @param path
|
|
94
|
-
* @param mapping
|
|
95
|
-
* @param level
|
|
96
|
-
*/
|
|
97
|
-
function getMappingNaive(c, path = '', mapping = {}, level = 0) {
|
|
98
|
-
if (Array.isArray(c)) {
|
|
99
|
-
c.forEach(c => getMappingNaive(c, path, mapping, level));
|
|
100
|
-
return mapping;
|
|
101
|
-
}
|
|
102
|
-
if (++level === 16)
|
|
103
|
-
return;
|
|
104
|
-
add(c, path, mapping);
|
|
105
|
-
for (var p in c) {
|
|
106
|
-
if (c.hasOwnProperty(p)) {
|
|
107
|
-
const v = c[p];
|
|
108
|
-
if (Array.isArray(v) && v.length > 0) { // reducer as impovement
|
|
109
|
-
v.forEach((elem, i) => {
|
|
110
|
-
const currentPaht = [path, p].filter(c => c.trim() != '').join('.');
|
|
111
|
-
getMappingNaive(elem, currentPaht, mapping, level);
|
|
112
|
-
});
|
|
113
|
-
}
|
|
114
|
-
else if (typeof v === 'object') {
|
|
115
|
-
const currentPaht = [path, p].filter(c => c.trim() != '').join('.');
|
|
116
|
-
add(v, currentPaht, mapping);
|
|
117
|
-
getMappingNaive(v, currentPaht, mapping, level);
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
return mapping;
|
|
122
|
-
}
|
|
123
|
-
function getMappingPathFrom(pathLodhas) {
|
|
124
|
-
if (!_.isString(pathLodhas)) {
|
|
125
|
-
return void 0;
|
|
126
|
-
}
|
|
127
|
-
const regex = /\[([0-9a-zA-Z]|\'|\")*\]/g;
|
|
128
|
-
pathLodhas = pathLodhas
|
|
129
|
-
.replace(regex, '')
|
|
130
|
-
.replace('..', '.');
|
|
131
|
-
if (pathLodhas.startsWith('.')) {
|
|
132
|
-
pathLodhas = pathLodhas.slice(1);
|
|
133
|
-
}
|
|
134
|
-
return pathLodhas;
|
|
135
|
-
}
|
|
136
|
-
function setMappingCirc(json, mapping = {}, circular = []) {
|
|
137
|
-
const mainClassFn = !_.isArray(json) && CLASS.getBy(mapping['']);
|
|
138
|
-
walk.Object(json, (v, lodashPath, changeValue) => {
|
|
139
|
-
if (!_.isUndefined(v) && !_.isNull(v)) {
|
|
140
|
-
const mappingPath = getMappingPathFrom(lodashPath);
|
|
141
|
-
if (!_.isUndefined(mapping[mappingPath])) {
|
|
142
|
-
const isArray = _.isArray(mapping[mappingPath]);
|
|
143
|
-
if (!isArray) {
|
|
144
|
-
const className = isArray ? _.first(mapping[mappingPath]) : mapping[mappingPath];
|
|
145
|
-
const classFN = CLASS.getBy(className);
|
|
146
|
-
if (_.isFunction(classFN)) {
|
|
147
|
-
changeValue(_.merge(new classFN(), v));
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
});
|
|
153
|
-
circular.forEach(c => {
|
|
154
|
-
const ref = _.get(json, c.circuralTargetPath);
|
|
155
|
-
_.set(json, c.pathToObj, ref);
|
|
156
|
-
});
|
|
157
|
-
if (_.isFunction(mainClassFn)) {
|
|
158
|
-
json = _.merge(new mainClassFn(), json);
|
|
159
|
-
}
|
|
160
|
-
return json;
|
|
161
|
-
}
|
|
162
|
-
function setMapping(json, mapping = {}) {
|
|
163
|
-
if (Array.isArray(json)) {
|
|
164
|
-
return json.map(j => {
|
|
165
|
-
return setMapping(j, mapping);
|
|
166
|
-
});
|
|
167
|
-
}
|
|
168
|
-
const mainClassFn = CLASS.getBy(mapping['']);
|
|
169
|
-
for (const key in json) {
|
|
170
|
-
if (json.hasOwnProperty(key)) {
|
|
171
|
-
if (_.isArray(json[key])) {
|
|
172
|
-
json[key] = json[key].map(arrObj => {
|
|
173
|
-
const objMapping = getModelsMapping(CLASS.getBy(mapping[key]));
|
|
174
|
-
return setMapping(arrObj, objMapping);
|
|
175
|
-
});
|
|
176
|
-
}
|
|
177
|
-
else if (_.isObject(json[key])) {
|
|
178
|
-
const objMapping = getModelsMapping(CLASS.getBy(mapping[key]));
|
|
179
|
-
json[key] = setMapping(json[key], objMapping);
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
Object
|
|
184
|
-
.keys(mapping)
|
|
185
|
-
.filter(key => key !== '' && key.split('.').length >= 2)
|
|
186
|
-
.forEach(lodasPath => {
|
|
187
|
-
const objMapping = getModelsMapping(CLASS.getBy(mapping[lodasPath]));
|
|
188
|
-
const input = _.get(json, lodasPath);
|
|
189
|
-
if (!_.isUndefined(input)) {
|
|
190
|
-
const res = setMapping(input, objMapping);
|
|
191
|
-
_.set(json, lodasPath, res);
|
|
192
|
-
}
|
|
193
|
-
});
|
|
194
|
-
if (!mainClassFn) {
|
|
195
|
-
return json;
|
|
196
|
-
}
|
|
197
|
-
return _.merge(new mainClassFn(), json);
|
|
198
|
-
}
|
|
199
|
-
function DefaultModelWithMapping(defaultModelValues, mapping) {
|
|
200
|
-
return function (target) {
|
|
201
|
-
if (!_.isArray(target[SYMBOL.MODELS_MAPPING])) {
|
|
202
|
-
target[SYMBOL.MODELS_MAPPING] = [];
|
|
203
|
-
}
|
|
204
|
-
target[SYMBOL.MODELS_MAPPING].push({ '': CLASS.getName(target) });
|
|
205
|
-
if (_.isObject(mapping)) {
|
|
206
|
-
target[SYMBOL.MODELS_MAPPING] = target[SYMBOL.MODELS_MAPPING].concat(mapping);
|
|
207
|
-
Object.keys(mapping)
|
|
208
|
-
.forEach(key => {
|
|
209
|
-
const v = mapping;
|
|
210
|
-
if (_.isUndefined(v) || _.isFunction(v)) {
|
|
10
|
+
var Mapping;
|
|
11
|
+
(function (Mapping) {
|
|
12
|
+
function decode(json, autodetect = false) {
|
|
13
|
+
if (_.isUndefined(json)) {
|
|
14
|
+
return void 0;
|
|
15
|
+
}
|
|
16
|
+
let mapping = decodeFromDecorator(_.isArray(json) ? _.first(json) : json, !autodetect);
|
|
17
|
+
if (autodetect) {
|
|
18
|
+
mapping = _.merge(getMappingNaive(json), mapping);
|
|
19
|
+
}
|
|
20
|
+
return mapping;
|
|
21
|
+
}
|
|
22
|
+
Mapping.decode = decode;
|
|
23
|
+
function encode(json, mapping, circular = []) {
|
|
24
|
+
if (_.isString(json) || _.isBoolean(json) || _.isNumber(json)) {
|
|
25
|
+
return json;
|
|
26
|
+
}
|
|
27
|
+
if (mapping['']) {
|
|
28
|
+
const decoratorMapping = getModelsMapping(CLASS.getBy(mapping['']));
|
|
29
|
+
mapping = _.merge(mapping, decoratorMapping);
|
|
30
|
+
}
|
|
31
|
+
let res;
|
|
32
|
+
if (_.isArray(circular) && circular.length > 0) {
|
|
33
|
+
res = setMappingCirc(json, mapping, circular);
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
res = setMapping(json, mapping);
|
|
37
|
+
}
|
|
38
|
+
return res;
|
|
39
|
+
}
|
|
40
|
+
Mapping.encode = encode;
|
|
41
|
+
function decodeFromDecorator(json, production = false) {
|
|
42
|
+
const entityClass = CLASS.getFromObject(json);
|
|
43
|
+
const mappings = getModelsMapping(entityClass);
|
|
44
|
+
return mappings;
|
|
45
|
+
}
|
|
46
|
+
function getModelsMapping(entity) {
|
|
47
|
+
if (!_.isFunction(entity) || entity === Object) {
|
|
48
|
+
return {};
|
|
49
|
+
}
|
|
50
|
+
const className = CLASS.getName(entity);
|
|
51
|
+
let enityOWnMapping = _.isArray(entity[SYMBOL.MODELS_MAPPING]) ?
|
|
52
|
+
entity[SYMBOL.MODELS_MAPPING] : [{ '': className }];
|
|
53
|
+
let res = {};
|
|
54
|
+
let parents = enityOWnMapping
|
|
55
|
+
.filter(m => !_.isUndefined(m['']) && m[''] !== className)
|
|
56
|
+
.map(m => m['']);
|
|
57
|
+
enityOWnMapping.reverse().forEach(m => {
|
|
58
|
+
m = _.cloneDeep(m);
|
|
59
|
+
Object.keys(m).forEach(key => {
|
|
60
|
+
const v = m[key];
|
|
61
|
+
const isArr = _.isArray(v);
|
|
62
|
+
const model = isArr ? _.first(v) : v;
|
|
63
|
+
if (parents.includes(model)) {
|
|
64
|
+
m[key] = isArr ? [className] : className;
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
res = _.merge(res, m);
|
|
68
|
+
});
|
|
69
|
+
res[''] = className;
|
|
70
|
+
return res;
|
|
71
|
+
}
|
|
72
|
+
Mapping.getModelsMapping = getModelsMapping;
|
|
73
|
+
function add(o, path, mapping = {}) {
|
|
74
|
+
if (!o || Array.isArray(o) || typeof o !== 'object')
|
|
75
|
+
return;
|
|
76
|
+
const objectClassName = CLASS.getName(Object.getPrototypeOf(o).constructor);
|
|
77
|
+
const resolveClass = CLASS.getBy(objectClassName);
|
|
78
|
+
if (!resolveClass) {
|
|
79
|
+
if (objectClassName !== 'Object') {
|
|
80
|
+
if (Helpers$1.isBrowser) {
|
|
81
|
+
console.error(`Cannot resolve class "${objectClassName}" while mapping.`);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
if (!mapping[path])
|
|
87
|
+
mapping[path] = CLASS.getName(resolveClass);
|
|
88
|
+
;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* USE ONLY IN DEVELOPMENT
|
|
92
|
+
* @param c
|
|
93
|
+
* @param path
|
|
94
|
+
* @param mapping
|
|
95
|
+
* @param level
|
|
96
|
+
*/
|
|
97
|
+
function getMappingNaive(c, path = '', mapping = {}, level = 0) {
|
|
98
|
+
if (Array.isArray(c)) {
|
|
99
|
+
c.forEach(c => getMappingNaive(c, path, mapping, level));
|
|
100
|
+
return mapping;
|
|
101
|
+
}
|
|
102
|
+
if (++level === 16)
|
|
103
|
+
return;
|
|
104
|
+
add(c, path, mapping);
|
|
105
|
+
for (var p in c) {
|
|
106
|
+
if (c.hasOwnProperty(p)) {
|
|
107
|
+
const v = c[p];
|
|
108
|
+
if (Array.isArray(v) && v.length > 0) { // reducer as impovement
|
|
109
|
+
v.forEach((elem, i) => {
|
|
110
|
+
const currentPaht = [path, p].filter(c => c.trim() != '').join('.');
|
|
111
|
+
getMappingNaive(elem, currentPaht, mapping, level);
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
else if (typeof v === 'object') {
|
|
115
|
+
const currentPaht = [path, p].filter(c => c.trim() != '').join('.');
|
|
116
|
+
add(v, currentPaht, mapping);
|
|
117
|
+
getMappingNaive(v, currentPaht, mapping, level);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return mapping;
|
|
122
|
+
}
|
|
123
|
+
function getMappingPathFrom(pathLodhas) {
|
|
124
|
+
if (!_.isString(pathLodhas)) {
|
|
125
|
+
return void 0;
|
|
126
|
+
}
|
|
127
|
+
const regex = /\[([0-9a-zA-Z]|\'|\")*\]/g;
|
|
128
|
+
pathLodhas = pathLodhas
|
|
129
|
+
.replace(regex, '')
|
|
130
|
+
.replace('..', '.');
|
|
131
|
+
if (pathLodhas.startsWith('.')) {
|
|
132
|
+
pathLodhas = pathLodhas.slice(1);
|
|
133
|
+
}
|
|
134
|
+
return pathLodhas;
|
|
135
|
+
}
|
|
136
|
+
function setMappingCirc(json, mapping = {}, circular = []) {
|
|
137
|
+
const mainClassFn = !_.isArray(json) && CLASS.getBy(mapping['']);
|
|
138
|
+
walk.Object(json, (v, lodashPath, changeValue) => {
|
|
139
|
+
if (!_.isUndefined(v) && !_.isNull(v)) {
|
|
140
|
+
const mappingPath = getMappingPathFrom(lodashPath);
|
|
141
|
+
if (!_.isUndefined(mapping[mappingPath])) {
|
|
142
|
+
const isArray = _.isArray(mapping[mappingPath]);
|
|
143
|
+
if (!isArray) {
|
|
144
|
+
const className = isArray ? _.first(mapping[mappingPath]) : mapping[mappingPath];
|
|
145
|
+
const classFN = CLASS.getBy(className);
|
|
146
|
+
if (_.isFunction(classFN)) {
|
|
147
|
+
changeValue(_.merge(new classFN(), v));
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
circular.forEach(c => {
|
|
154
|
+
const ref = _.get(json, c.circuralTargetPath);
|
|
155
|
+
_.set(json, c.pathToObj, ref);
|
|
156
|
+
});
|
|
157
|
+
if (_.isFunction(mainClassFn)) {
|
|
158
|
+
json = _.merge(new mainClassFn(), json);
|
|
159
|
+
}
|
|
160
|
+
return json;
|
|
161
|
+
}
|
|
162
|
+
function setMapping(json, mapping = {}) {
|
|
163
|
+
if (Array.isArray(json)) {
|
|
164
|
+
return json.map(j => {
|
|
165
|
+
return setMapping(j, mapping);
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
const mainClassFn = CLASS.getBy(mapping['']);
|
|
169
|
+
for (const key in json) {
|
|
170
|
+
if (json.hasOwnProperty(key)) {
|
|
171
|
+
if (_.isArray(json[key])) {
|
|
172
|
+
json[key] = json[key].map(arrObj => {
|
|
173
|
+
const objMapping = getModelsMapping(CLASS.getBy(mapping[key]));
|
|
174
|
+
return setMapping(arrObj, objMapping);
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
else if (_.isObject(json[key])) {
|
|
178
|
+
const objMapping = getModelsMapping(CLASS.getBy(mapping[key]));
|
|
179
|
+
json[key] = setMapping(json[key], objMapping);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
Object
|
|
184
|
+
.keys(mapping)
|
|
185
|
+
.filter(key => key !== '' && key.split('.').length >= 2)
|
|
186
|
+
.forEach(lodasPath => {
|
|
187
|
+
const objMapping = getModelsMapping(CLASS.getBy(mapping[lodasPath]));
|
|
188
|
+
const input = _.get(json, lodasPath);
|
|
189
|
+
if (!_.isUndefined(input)) {
|
|
190
|
+
const res = setMapping(input, objMapping);
|
|
191
|
+
_.set(json, lodasPath, res);
|
|
192
|
+
}
|
|
193
|
+
});
|
|
194
|
+
if (!mainClassFn) {
|
|
195
|
+
return json;
|
|
196
|
+
}
|
|
197
|
+
return _.merge(new mainClassFn(), json);
|
|
198
|
+
}
|
|
199
|
+
function DefaultModelWithMapping(defaultModelValues, mapping) {
|
|
200
|
+
return function (target) {
|
|
201
|
+
if (!_.isArray(target[SYMBOL.MODELS_MAPPING])) {
|
|
202
|
+
target[SYMBOL.MODELS_MAPPING] = [];
|
|
203
|
+
}
|
|
204
|
+
target[SYMBOL.MODELS_MAPPING].push({ '': CLASS.getName(target) });
|
|
205
|
+
if (_.isObject(mapping)) {
|
|
206
|
+
target[SYMBOL.MODELS_MAPPING] = target[SYMBOL.MODELS_MAPPING].concat(mapping);
|
|
207
|
+
Object.keys(mapping)
|
|
208
|
+
.forEach(key => {
|
|
209
|
+
const v = mapping;
|
|
210
|
+
if (_.isUndefined(v) || _.isFunction(v)) {
|
|
211
211
|
throw `
|
|
212
212
|
|
|
213
213
|
|
|
214
214
|
Class: '${target.name}'
|
|
215
215
|
[ng2rest] Bad mapping value for path: ${key} , please use type: <string> or [<string>]
|
|
216
|
-
`;
|
|
217
|
-
}
|
|
218
|
-
});
|
|
219
|
-
}
|
|
220
|
-
if (_.isObject(defaultModelValues)) {
|
|
221
|
-
const toMerge = {};
|
|
222
|
-
const describedTarget = CLASS
|
|
223
|
-
.describeProperites(target)
|
|
224
|
-
.filter(prop => /^([a-zA-Z0-9]|\_|\#)+$/.test(prop));
|
|
225
|
-
describedTarget.forEach(propDefInConstr => {
|
|
226
|
-
if (defaultModelValues[propDefInConstr]) {
|
|
216
|
+
`;
|
|
217
|
+
}
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
if (_.isObject(defaultModelValues)) {
|
|
221
|
+
const toMerge = {};
|
|
222
|
+
const describedTarget = CLASS
|
|
223
|
+
.describeProperites(target)
|
|
224
|
+
.filter(prop => /^([a-zA-Z0-9]|\_|\#)+$/.test(prop));
|
|
225
|
+
describedTarget.forEach(propDefInConstr => {
|
|
226
|
+
if (defaultModelValues[propDefInConstr]) {
|
|
227
227
|
console.warn(`
|
|
228
228
|
|
|
229
229
|
CONFLICT: default value for property: "${propDefInConstr}"
|
|
230
230
|
in class "${target.name}" already defined as typescript
|
|
231
231
|
default class proprty value.
|
|
232
232
|
|
|
233
|
-
`);
|
|
234
|
-
}
|
|
235
|
-
else {
|
|
236
|
-
toMerge[propDefInConstr] = null; // TODO from toString I can't know that
|
|
237
|
-
}
|
|
238
|
-
});
|
|
239
|
-
target[SYMBOL.DEFAULT_MODEL] = _.merge(toMerge, defaultModelValues);
|
|
240
|
-
const propsToOmmit = Object
|
|
241
|
-
.keys(target[SYMBOL.DEFAULT_MODEL])
|
|
242
|
-
.filter(key => {
|
|
243
|
-
const descriptor = Object
|
|
244
|
-
.getOwnPropertyDescriptor(target.prototype, key);
|
|
245
|
-
return !!descriptor;
|
|
246
|
-
});
|
|
247
|
-
_.merge(target.prototype, _.omit(target[SYMBOL.DEFAULT_MODEL], propsToOmmit));
|
|
248
|
-
}
|
|
249
|
-
};
|
|
250
|
-
}
|
|
251
|
-
Mapping.DefaultModelWithMapping = DefaultModelWithMapping;
|
|
252
|
-
})(Mapping || (Mapping = {}));
|
|
253
|
-
;
|
|
233
|
+
`);
|
|
234
|
+
}
|
|
235
|
+
else {
|
|
236
|
+
toMerge[propDefInConstr] = null; // TODO from toString I can't know that
|
|
237
|
+
}
|
|
238
|
+
});
|
|
239
|
+
target[SYMBOL.DEFAULT_MODEL] = _.merge(toMerge, defaultModelValues);
|
|
240
|
+
const propsToOmmit = Object
|
|
241
|
+
.keys(target[SYMBOL.DEFAULT_MODEL])
|
|
242
|
+
.filter(key => {
|
|
243
|
+
const descriptor = Object
|
|
244
|
+
.getOwnPropertyDescriptor(target.prototype, key);
|
|
245
|
+
return !!descriptor;
|
|
246
|
+
});
|
|
247
|
+
_.merge(target.prototype, _.omit(target[SYMBOL.DEFAULT_MODEL], propsToOmmit));
|
|
248
|
+
}
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
Mapping.DefaultModelWithMapping = DefaultModelWithMapping;
|
|
252
|
+
})(Mapping || (Mapping = {}));
|
|
253
|
+
;
|
|
254
254
|
({}); // @--end-of-file-for-module=ng2-rest lib/mapping.ts
|
|
255
255
|
|
|
256
|
-
// @ts-ignore
|
|
257
|
-
class Helpers extends CoreHelpers {
|
|
258
|
-
static get Mapping() {
|
|
259
|
-
return {
|
|
260
|
-
encode(json, mapping) {
|
|
261
|
-
return Mapping.encode(json, mapping);
|
|
262
|
-
},
|
|
263
|
-
decode(json, autodetect = false) {
|
|
264
|
-
return Mapping.decode(json, autodetect);
|
|
265
|
-
}
|
|
266
|
-
};
|
|
267
|
-
}
|
|
268
|
-
static checkValidUrl(url) {
|
|
269
|
-
let regex = /(http|https):\/\/(\w+:{0,1}\w*)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%!\-\/]))?/;
|
|
270
|
-
return regex.test(url);
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
Helpers.JSON = JSON10;
|
|
274
|
-
;
|
|
256
|
+
// @ts-ignore
|
|
257
|
+
class Helpers extends CoreHelpers {
|
|
258
|
+
static get Mapping() {
|
|
259
|
+
return {
|
|
260
|
+
encode(json, mapping) {
|
|
261
|
+
return Mapping.encode(json, mapping);
|
|
262
|
+
},
|
|
263
|
+
decode(json, autodetect = false) {
|
|
264
|
+
return Mapping.decode(json, autodetect);
|
|
265
|
+
}
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
static checkValidUrl(url) {
|
|
269
|
+
let regex = /(http|https):\/\/(\w+:{0,1}\w*)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%!\-\/]))?/;
|
|
270
|
+
return regex.test(url);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
Helpers.JSON = JSON10;
|
|
274
|
+
;
|
|
275
275
|
({}); // @--end-of-file-for-module=ng2-rest lib/helpers.ts
|
|
276
276
|
|
|
277
|
-
const log$3 = Log.create('[ng2-rest] params', Level.__NOTHING);
|
|
278
|
-
/** check if string is a valid pattern */
|
|
279
|
-
function isValid(pattern) {
|
|
280
|
-
return (new RegExp('\/:[a-zA-Z]*', 'g')).test(pattern.replace('://', ''));
|
|
281
|
-
}
|
|
282
|
-
function check(url, pattern) {
|
|
283
|
-
if (!Helpers.checkValidUrl(url)) {
|
|
284
|
-
log$3.error(`Incorrect url: ${url}`);
|
|
285
|
-
return false;
|
|
286
|
-
}
|
|
287
|
-
if (url.charAt(url.length - 1) === '/')
|
|
288
|
-
url = url.slice(0, url.length - 2);
|
|
289
|
-
if (pattern.charAt(pattern.length - 1) === '/')
|
|
290
|
-
pattern = pattern.slice(0, url.length - 2);
|
|
291
|
-
pattern = pattern.replace(/\//g, '\/');
|
|
292
|
-
pattern = pattern.replace(new RegExp('\/:[a-zA-Z]*', 'g'), '.+');
|
|
293
|
-
let reg = new RegExp(pattern, 'g');
|
|
294
|
-
return reg.test(url);
|
|
295
|
-
}
|
|
296
|
-
function getModels(pattern) {
|
|
297
|
-
let m = pattern.match(new RegExp('[a-z-A-Z]*\/:', 'g'));
|
|
298
|
-
return m.map(p => p.replace('/:', ''));
|
|
299
|
-
}
|
|
300
|
-
function getRestPramsNames(pattern) {
|
|
301
|
-
if (pattern.charAt(pattern.length - 1) !== '/')
|
|
302
|
-
pattern = `${pattern}/`;
|
|
303
|
-
let m = pattern.match(new RegExp(':[a-zA-Z]*\/', 'g'));
|
|
304
|
-
let res = m.map(p => p.replace(':', '').replace('/', ''));
|
|
305
|
-
return res.filter(p => p.trim() !== '');
|
|
306
|
-
}
|
|
307
|
-
function containsModels(url, models) {
|
|
308
|
-
if (url.charAt(0) !== '/')
|
|
309
|
-
url = '/' + url;
|
|
310
|
-
let res = models.filter(m => {
|
|
311
|
-
let word = '/' + m;
|
|
312
|
-
let iii = url.indexOf(word);
|
|
313
|
-
if (iii + word.length < url.length && url.charAt(iii + word.length) !== '/') {
|
|
314
|
-
return false;
|
|
315
|
-
}
|
|
316
|
-
if (iii !== -1) {
|
|
317
|
-
url = url.replace(new RegExp('\/' + m, 'g'), '');
|
|
318
|
-
return true;
|
|
319
|
-
}
|
|
320
|
-
return false;
|
|
321
|
-
}).length;
|
|
322
|
-
return res === models.length;
|
|
323
|
-
}
|
|
324
|
-
function stars(n) {
|
|
325
|
-
let res = '';
|
|
326
|
-
for (let i = 0; i < n; i++)
|
|
327
|
-
res += '*';
|
|
328
|
-
return res;
|
|
329
|
-
}
|
|
330
|
-
function getRestParams(url, pattern) {
|
|
331
|
-
let res = {};
|
|
332
|
-
let models = getRestPramsNames(pattern);
|
|
333
|
-
models.forEach(m => {
|
|
334
|
-
pattern = pattern.replace(`:${m}`, stars(m.length));
|
|
335
|
-
});
|
|
336
|
-
let currentModel = void 0;
|
|
337
|
-
diffChars(pattern, url).forEach(d => {
|
|
338
|
-
if (d.added) {
|
|
339
|
-
if (!isNaN(Number(d.value)))
|
|
340
|
-
res[currentModel] = Number(d.value);
|
|
341
|
-
else if (d.value.trim() === 'true')
|
|
342
|
-
res[currentModel] = true;
|
|
343
|
-
else if (d.value.trim() === 'false')
|
|
344
|
-
res[currentModel] = false;
|
|
345
|
-
else
|
|
346
|
-
res[currentModel] = decodeURIComponent(d.value);
|
|
347
|
-
currentModel = void 0;
|
|
348
|
-
}
|
|
349
|
-
let m = d.value.replace(':', "");
|
|
350
|
-
if (d.removed) {
|
|
351
|
-
currentModel = models.shift();
|
|
352
|
-
}
|
|
353
|
-
});
|
|
354
|
-
return res;
|
|
355
|
-
}
|
|
356
|
-
const regexisPath = /[^\..]+(\.[^\..]+)+/g;
|
|
357
|
-
/**
|
|
358
|
-
* Models like books/:id
|
|
359
|
-
*/
|
|
360
|
-
function cutUrlModel(params, models, output) {
|
|
361
|
-
if (models.length === 0)
|
|
362
|
-
return output.join('\/');
|
|
363
|
-
let m = models.pop();
|
|
364
|
-
let param = m.match(/:[a-zA-Z0-9\.]+/)[0].replace(':', '');
|
|
365
|
-
const paramIsPath = regexisPath.test(param);
|
|
366
|
-
let model = m.match(/[a-zA-Z0-9]+\//)[0].replace('\/', '');
|
|
367
|
-
if (params === void 0 ||
|
|
368
|
-
(paramIsPath ? _.get(params, param) === void 0 : params[param] === void 0) ||
|
|
369
|
-
param === 'undefined') {
|
|
370
|
-
output.length = 0;
|
|
371
|
-
output.unshift(model);
|
|
372
|
-
return cutUrlModel(params, models, output);
|
|
373
|
-
}
|
|
374
|
-
else {
|
|
375
|
-
if (paramIsPath) {
|
|
376
|
-
let mrep = m.replace(new RegExp(`:${param}`, 'g'), `${_.get(params, param)}`);
|
|
377
|
-
output.unshift(mrep);
|
|
378
|
-
return cutUrlModel(params, models, output);
|
|
379
|
-
}
|
|
380
|
-
else {
|
|
381
|
-
let mrep = m.replace(new RegExp(`:${param}`, 'g'), `${params[param]}`);
|
|
382
|
-
output.unshift(mrep);
|
|
383
|
-
return cutUrlModel(params, models, output);
|
|
384
|
-
}
|
|
385
|
-
}
|
|
386
|
-
}
|
|
387
|
-
function interpolateParamsToUrl(params, url) {
|
|
388
|
-
const regexInt = /\[\[([^\..]+\.[^\..]+)+\]\]/g;
|
|
389
|
-
url = url.split('/').map(p => {
|
|
390
|
-
let isParam = p.startsWith(':');
|
|
391
|
-
if (isParam) {
|
|
392
|
-
let part = p.slice(1);
|
|
393
|
-
if (regexInt.test(part)) {
|
|
394
|
-
part = part.replace('[[', '');
|
|
395
|
-
part = part.replace(']]', '');
|
|
396
|
-
}
|
|
397
|
-
return `:${part}`;
|
|
398
|
-
}
|
|
399
|
-
return p;
|
|
400
|
-
}).join('/');
|
|
401
|
-
let slash = {
|
|
402
|
-
start: url.charAt(0) === '\/',
|
|
403
|
-
end: url.charAt(url.length - 1) === '\/'
|
|
404
|
-
};
|
|
405
|
-
let morePramsOnEnd = url.match(/(\/:[a-zA-Z0-9\.]+){2,10}/g);
|
|
406
|
-
if (morePramsOnEnd && (Array.isArray(morePramsOnEnd) && morePramsOnEnd.length === 1)) {
|
|
407
|
-
let m = morePramsOnEnd[0];
|
|
408
|
-
let match = m.match(/\/:[a-zA-Z0-9\.]+/g);
|
|
409
|
-
match.forEach(e => {
|
|
410
|
-
let c = e.replace('\/:', '');
|
|
411
|
-
if (regexisPath.test(c)) {
|
|
412
|
-
url = url.replace(e, `/${_.get(params, c)}`);
|
|
413
|
-
}
|
|
414
|
-
else {
|
|
415
|
-
url = url.replace(e, `/${params[c]}`);
|
|
416
|
-
}
|
|
417
|
-
});
|
|
418
|
-
return url;
|
|
419
|
-
}
|
|
420
|
-
let nestedParams = url.match(/[a-zA-Z0-9]+\/:[a-zA-Z0-9\.]+/g);
|
|
421
|
-
if (!nestedParams || (Array.isArray(nestedParams) && nestedParams.length === 0))
|
|
422
|
-
return url;
|
|
423
|
-
if (!slash.end)
|
|
424
|
-
url = `${url}/`;
|
|
425
|
-
let addUndefinedForAlone = (!/:[a-zA-Z0-9\.]+\/$/g.test(url) && /[a-zA-Z0-9]+\/$/g.test(url));
|
|
426
|
-
let replace = (nestedParams.length > 1 ? nestedParams.join('\/') : nestedParams[0]) +
|
|
427
|
-
(addUndefinedForAlone ? '\/' + url.match(/[a-zA-Z0-9]+\/$/g)[0] : '\/');
|
|
428
|
-
let beginHref = url.replace(replace, '');
|
|
429
|
-
if (addUndefinedForAlone) {
|
|
430
|
-
url = url.replace(/\/$/g, '/:undefined');
|
|
431
|
-
nestedParams = url.match(/[a-zA-Z0-9]+\/:[a-zA-Z0-9\.]+/g);
|
|
432
|
-
url = cutUrlModel(params, nestedParams, []);
|
|
433
|
-
}
|
|
434
|
-
else {
|
|
435
|
-
url = cutUrlModel(params, nestedParams, []);
|
|
436
|
-
}
|
|
437
|
-
url = beginHref + url;
|
|
438
|
-
if (url.charAt(url.length - 1) !== '/' && slash.end)
|
|
439
|
-
url = `${url}/`;
|
|
440
|
-
if (url.charAt(0) !== '\/' && slash.start)
|
|
441
|
-
url = `/${url}`;
|
|
442
|
-
return url;
|
|
443
|
-
}
|
|
444
|
-
/**
|
|
445
|
-
* Get query params from url, like 'ex' in /api/books?ex=value
|
|
446
|
-
*/
|
|
447
|
-
function decodeUrl(url) {
|
|
448
|
-
let regex = /[?&]([^=#]+)=([^&#]*)/g, params = {}, match;
|
|
449
|
-
while (match = regex.exec(url)) {
|
|
450
|
-
params[decodeURIComponent(match[1])] = decodeURIComponent(match[2]);
|
|
451
|
-
}
|
|
452
|
-
let paramsObject = params;
|
|
453
|
-
for (let p in paramsObject) {
|
|
454
|
-
if (paramsObject[p] === void 0) {
|
|
455
|
-
delete paramsObject[p];
|
|
456
|
-
continue;
|
|
457
|
-
}
|
|
458
|
-
if (paramsObject.hasOwnProperty(p)) {
|
|
459
|
-
let n = Number(params[p]);
|
|
460
|
-
if (!isNaN(n)) {
|
|
461
|
-
params[p] = n;
|
|
462
|
-
continue;
|
|
463
|
-
}
|
|
464
|
-
if (typeof params[p] === 'string') {
|
|
465
|
-
let json;
|
|
466
|
-
try {
|
|
467
|
-
json = JSON.parse(params[p]);
|
|
468
|
-
}
|
|
469
|
-
catch (error) { }
|
|
470
|
-
if (json !== void 0) {
|
|
471
|
-
params[p] = json;
|
|
472
|
-
continue;
|
|
473
|
-
}
|
|
474
|
-
}
|
|
475
|
-
}
|
|
476
|
-
}
|
|
477
|
-
return params;
|
|
478
|
-
}
|
|
479
|
-
/**
|
|
480
|
-
* Create query params string for url
|
|
481
|
-
*
|
|
482
|
-
* @export
|
|
483
|
-
* @param {UrlParams[]} params
|
|
484
|
-
* @returns {string}
|
|
485
|
-
*/
|
|
486
|
-
function getParamsUrl(params, doNotSerialize = false) {
|
|
487
|
-
let urlparts = [];
|
|
488
|
-
if (!params)
|
|
489
|
-
return '';
|
|
490
|
-
if (!(params instanceof Array))
|
|
491
|
-
return '';
|
|
492
|
-
if (params.length === 0)
|
|
493
|
-
return '';
|
|
494
|
-
params.forEach(urlparam => {
|
|
495
|
-
if (JSON.stringify(urlparam) !== '{}') {
|
|
496
|
-
let parameters = [];
|
|
497
|
-
let paramObject = urlparam;
|
|
498
|
-
for (let p in paramObject) {
|
|
499
|
-
if (paramObject[p] === void 0)
|
|
500
|
-
delete paramObject[p];
|
|
501
|
-
if (paramObject.hasOwnProperty(p) && typeof p === 'string' && p !== 'regex' && !(paramObject[p] instanceof RegExp)) {
|
|
502
|
-
if (p.length > 0 && p[0] === '/') {
|
|
503
|
-
let newName = p.slice(1, p.length - 1);
|
|
504
|
-
urlparam[newName] = urlparam[p];
|
|
505
|
-
urlparam[p] = void 0;
|
|
506
|
-
p = newName;
|
|
507
|
-
}
|
|
508
|
-
if (p.length > 0 && p[p.length - 1] === '/') {
|
|
509
|
-
let newName = p.slice(0, p.length - 2);
|
|
510
|
-
urlparam[newName] = urlparam[p];
|
|
511
|
-
urlparam[p] = void 0;
|
|
512
|
-
p = newName;
|
|
513
|
-
}
|
|
514
|
-
let v = urlparam[p];
|
|
515
|
-
if (v instanceof Object) {
|
|
516
|
-
urlparam[p] = JSON.stringify(urlparam[p]);
|
|
517
|
-
}
|
|
518
|
-
urlparam[p] = doNotSerialize ? urlparam[p] : encodeURIComponent(urlparam[p]);
|
|
519
|
-
if (urlparam.regex !== void 0 && urlparam.regex instanceof RegExp) {
|
|
520
|
-
if (!urlparam.regex.test(urlparam[p])) {
|
|
521
|
-
console.warn(`Data: ${urlparam[p]} incostistent with regex ${urlparam.regex.source}`);
|
|
522
|
-
}
|
|
523
|
-
}
|
|
524
|
-
parameters.push(`${p}=${urlparam[p]}`);
|
|
525
|
-
}
|
|
526
|
-
}
|
|
527
|
-
urlparts.push(parameters.join('&'));
|
|
528
|
-
}
|
|
529
|
-
});
|
|
530
|
-
let join = urlparts.join().trim();
|
|
531
|
-
if (join.trim() === '')
|
|
532
|
-
return '';
|
|
533
|
-
return `?${urlparts.join('&')}`;
|
|
534
|
-
}
|
|
535
|
-
function transform(o) {
|
|
536
|
-
if (typeof o === 'object') {
|
|
537
|
-
return encodeURIComponent(JSON.stringify(o));
|
|
538
|
-
}
|
|
539
|
-
return o;
|
|
540
|
-
}
|
|
541
|
-
function prepareUrlOldWay(params) {
|
|
542
|
-
if (!params)
|
|
543
|
-
return this.endpoint;
|
|
544
|
-
if (typeof params === 'object') {
|
|
545
|
-
params = transform(params);
|
|
546
|
-
}
|
|
547
|
-
return this.endpoint + '/' + params;
|
|
548
|
-
}
|
|
549
|
-
;
|
|
277
|
+
const log$3 = Log.create('[ng2-rest] params', Level.__NOTHING);
|
|
278
|
+
/** check if string is a valid pattern */
|
|
279
|
+
function isValid(pattern) {
|
|
280
|
+
return (new RegExp('\/:[a-zA-Z]*', 'g')).test(pattern.replace('://', ''));
|
|
281
|
+
}
|
|
282
|
+
function check(url, pattern) {
|
|
283
|
+
if (!Helpers.checkValidUrl(url)) {
|
|
284
|
+
log$3.error(`Incorrect url: ${url}`);
|
|
285
|
+
return false;
|
|
286
|
+
}
|
|
287
|
+
if (url.charAt(url.length - 1) === '/')
|
|
288
|
+
url = url.slice(0, url.length - 2);
|
|
289
|
+
if (pattern.charAt(pattern.length - 1) === '/')
|
|
290
|
+
pattern = pattern.slice(0, url.length - 2);
|
|
291
|
+
pattern = pattern.replace(/\//g, '\/');
|
|
292
|
+
pattern = pattern.replace(new RegExp('\/:[a-zA-Z]*', 'g'), '.+');
|
|
293
|
+
let reg = new RegExp(pattern, 'g');
|
|
294
|
+
return reg.test(url);
|
|
295
|
+
}
|
|
296
|
+
function getModels(pattern) {
|
|
297
|
+
let m = pattern.match(new RegExp('[a-z-A-Z]*\/:', 'g'));
|
|
298
|
+
return m.map(p => p.replace('/:', ''));
|
|
299
|
+
}
|
|
300
|
+
function getRestPramsNames(pattern) {
|
|
301
|
+
if (pattern.charAt(pattern.length - 1) !== '/')
|
|
302
|
+
pattern = `${pattern}/`;
|
|
303
|
+
let m = pattern.match(new RegExp(':[a-zA-Z]*\/', 'g'));
|
|
304
|
+
let res = m.map(p => p.replace(':', '').replace('/', ''));
|
|
305
|
+
return res.filter(p => p.trim() !== '');
|
|
306
|
+
}
|
|
307
|
+
function containsModels(url, models) {
|
|
308
|
+
if (url.charAt(0) !== '/')
|
|
309
|
+
url = '/' + url;
|
|
310
|
+
let res = models.filter(m => {
|
|
311
|
+
let word = '/' + m;
|
|
312
|
+
let iii = url.indexOf(word);
|
|
313
|
+
if (iii + word.length < url.length && url.charAt(iii + word.length) !== '/') {
|
|
314
|
+
return false;
|
|
315
|
+
}
|
|
316
|
+
if (iii !== -1) {
|
|
317
|
+
url = url.replace(new RegExp('\/' + m, 'g'), '');
|
|
318
|
+
return true;
|
|
319
|
+
}
|
|
320
|
+
return false;
|
|
321
|
+
}).length;
|
|
322
|
+
return res === models.length;
|
|
323
|
+
}
|
|
324
|
+
function stars(n) {
|
|
325
|
+
let res = '';
|
|
326
|
+
for (let i = 0; i < n; i++)
|
|
327
|
+
res += '*';
|
|
328
|
+
return res;
|
|
329
|
+
}
|
|
330
|
+
function getRestParams(url, pattern) {
|
|
331
|
+
let res = {};
|
|
332
|
+
let models = getRestPramsNames(pattern);
|
|
333
|
+
models.forEach(m => {
|
|
334
|
+
pattern = pattern.replace(`:${m}`, stars(m.length));
|
|
335
|
+
});
|
|
336
|
+
let currentModel = void 0;
|
|
337
|
+
diffChars(pattern, url).forEach(d => {
|
|
338
|
+
if (d.added) {
|
|
339
|
+
if (!isNaN(Number(d.value)))
|
|
340
|
+
res[currentModel] = Number(d.value);
|
|
341
|
+
else if (d.value.trim() === 'true')
|
|
342
|
+
res[currentModel] = true;
|
|
343
|
+
else if (d.value.trim() === 'false')
|
|
344
|
+
res[currentModel] = false;
|
|
345
|
+
else
|
|
346
|
+
res[currentModel] = decodeURIComponent(d.value);
|
|
347
|
+
currentModel = void 0;
|
|
348
|
+
}
|
|
349
|
+
let m = d.value.replace(':', "");
|
|
350
|
+
if (d.removed) {
|
|
351
|
+
currentModel = models.shift();
|
|
352
|
+
}
|
|
353
|
+
});
|
|
354
|
+
return res;
|
|
355
|
+
}
|
|
356
|
+
const regexisPath = /[^\..]+(\.[^\..]+)+/g;
|
|
357
|
+
/**
|
|
358
|
+
* Models like books/:id
|
|
359
|
+
*/
|
|
360
|
+
function cutUrlModel(params, models, output) {
|
|
361
|
+
if (models.length === 0)
|
|
362
|
+
return output.join('\/');
|
|
363
|
+
let m = models.pop();
|
|
364
|
+
let param = m.match(/:[a-zA-Z0-9\.]+/)[0].replace(':', '');
|
|
365
|
+
const paramIsPath = regexisPath.test(param);
|
|
366
|
+
let model = m.match(/[a-zA-Z0-9]+\//)[0].replace('\/', '');
|
|
367
|
+
if (params === void 0 ||
|
|
368
|
+
(paramIsPath ? _.get(params, param) === void 0 : params[param] === void 0) ||
|
|
369
|
+
param === 'undefined') {
|
|
370
|
+
output.length = 0;
|
|
371
|
+
output.unshift(model);
|
|
372
|
+
return cutUrlModel(params, models, output);
|
|
373
|
+
}
|
|
374
|
+
else {
|
|
375
|
+
if (paramIsPath) {
|
|
376
|
+
let mrep = m.replace(new RegExp(`:${param}`, 'g'), `${_.get(params, param)}`);
|
|
377
|
+
output.unshift(mrep);
|
|
378
|
+
return cutUrlModel(params, models, output);
|
|
379
|
+
}
|
|
380
|
+
else {
|
|
381
|
+
let mrep = m.replace(new RegExp(`:${param}`, 'g'), `${params[param]}`);
|
|
382
|
+
output.unshift(mrep);
|
|
383
|
+
return cutUrlModel(params, models, output);
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
function interpolateParamsToUrl(params, url) {
|
|
388
|
+
const regexInt = /\[\[([^\..]+\.[^\..]+)+\]\]/g;
|
|
389
|
+
url = url.split('/').map(p => {
|
|
390
|
+
let isParam = p.startsWith(':');
|
|
391
|
+
if (isParam) {
|
|
392
|
+
let part = p.slice(1);
|
|
393
|
+
if (regexInt.test(part)) {
|
|
394
|
+
part = part.replace('[[', '');
|
|
395
|
+
part = part.replace(']]', '');
|
|
396
|
+
}
|
|
397
|
+
return `:${part}`;
|
|
398
|
+
}
|
|
399
|
+
return p;
|
|
400
|
+
}).join('/');
|
|
401
|
+
let slash = {
|
|
402
|
+
start: url.charAt(0) === '\/',
|
|
403
|
+
end: url.charAt(url.length - 1) === '\/'
|
|
404
|
+
};
|
|
405
|
+
let morePramsOnEnd = url.match(/(\/:[a-zA-Z0-9\.]+){2,10}/g);
|
|
406
|
+
if (morePramsOnEnd && (Array.isArray(morePramsOnEnd) && morePramsOnEnd.length === 1)) {
|
|
407
|
+
let m = morePramsOnEnd[0];
|
|
408
|
+
let match = m.match(/\/:[a-zA-Z0-9\.]+/g);
|
|
409
|
+
match.forEach(e => {
|
|
410
|
+
let c = e.replace('\/:', '');
|
|
411
|
+
if (regexisPath.test(c)) {
|
|
412
|
+
url = url.replace(e, `/${_.get(params, c)}`);
|
|
413
|
+
}
|
|
414
|
+
else {
|
|
415
|
+
url = url.replace(e, `/${params[c]}`);
|
|
416
|
+
}
|
|
417
|
+
});
|
|
418
|
+
return url;
|
|
419
|
+
}
|
|
420
|
+
let nestedParams = url.match(/[a-zA-Z0-9]+\/:[a-zA-Z0-9\.]+/g);
|
|
421
|
+
if (!nestedParams || (Array.isArray(nestedParams) && nestedParams.length === 0))
|
|
422
|
+
return url;
|
|
423
|
+
if (!slash.end)
|
|
424
|
+
url = `${url}/`;
|
|
425
|
+
let addUndefinedForAlone = (!/:[a-zA-Z0-9\.]+\/$/g.test(url) && /[a-zA-Z0-9]+\/$/g.test(url));
|
|
426
|
+
let replace = (nestedParams.length > 1 ? nestedParams.join('\/') : nestedParams[0]) +
|
|
427
|
+
(addUndefinedForAlone ? '\/' + url.match(/[a-zA-Z0-9]+\/$/g)[0] : '\/');
|
|
428
|
+
let beginHref = url.replace(replace, '');
|
|
429
|
+
if (addUndefinedForAlone) {
|
|
430
|
+
url = url.replace(/\/$/g, '/:undefined');
|
|
431
|
+
nestedParams = url.match(/[a-zA-Z0-9]+\/:[a-zA-Z0-9\.]+/g);
|
|
432
|
+
url = cutUrlModel(params, nestedParams, []);
|
|
433
|
+
}
|
|
434
|
+
else {
|
|
435
|
+
url = cutUrlModel(params, nestedParams, []);
|
|
436
|
+
}
|
|
437
|
+
url = beginHref + url;
|
|
438
|
+
if (url.charAt(url.length - 1) !== '/' && slash.end)
|
|
439
|
+
url = `${url}/`;
|
|
440
|
+
if (url.charAt(0) !== '\/' && slash.start)
|
|
441
|
+
url = `/${url}`;
|
|
442
|
+
return url;
|
|
443
|
+
}
|
|
444
|
+
/**
|
|
445
|
+
* Get query params from url, like 'ex' in /api/books?ex=value
|
|
446
|
+
*/
|
|
447
|
+
function decodeUrl(url) {
|
|
448
|
+
let regex = /[?&]([^=#]+)=([^&#]*)/g, params = {}, match;
|
|
449
|
+
while (match = regex.exec(url)) {
|
|
450
|
+
params[decodeURIComponent(match[1])] = decodeURIComponent(match[2]);
|
|
451
|
+
}
|
|
452
|
+
let paramsObject = params;
|
|
453
|
+
for (let p in paramsObject) {
|
|
454
|
+
if (paramsObject[p] === void 0) {
|
|
455
|
+
delete paramsObject[p];
|
|
456
|
+
continue;
|
|
457
|
+
}
|
|
458
|
+
if (paramsObject.hasOwnProperty(p)) {
|
|
459
|
+
let n = Number(params[p]);
|
|
460
|
+
if (!isNaN(n)) {
|
|
461
|
+
params[p] = n;
|
|
462
|
+
continue;
|
|
463
|
+
}
|
|
464
|
+
if (typeof params[p] === 'string') {
|
|
465
|
+
let json;
|
|
466
|
+
try {
|
|
467
|
+
json = JSON.parse(params[p]);
|
|
468
|
+
}
|
|
469
|
+
catch (error) { }
|
|
470
|
+
if (json !== void 0) {
|
|
471
|
+
params[p] = json;
|
|
472
|
+
continue;
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
return params;
|
|
478
|
+
}
|
|
479
|
+
/**
|
|
480
|
+
* Create query params string for url
|
|
481
|
+
*
|
|
482
|
+
* @export
|
|
483
|
+
* @param {UrlParams[]} params
|
|
484
|
+
* @returns {string}
|
|
485
|
+
*/
|
|
486
|
+
function getParamsUrl(params, doNotSerialize = false) {
|
|
487
|
+
let urlparts = [];
|
|
488
|
+
if (!params)
|
|
489
|
+
return '';
|
|
490
|
+
if (!(params instanceof Array))
|
|
491
|
+
return '';
|
|
492
|
+
if (params.length === 0)
|
|
493
|
+
return '';
|
|
494
|
+
params.forEach(urlparam => {
|
|
495
|
+
if (JSON.stringify(urlparam) !== '{}') {
|
|
496
|
+
let parameters = [];
|
|
497
|
+
let paramObject = urlparam;
|
|
498
|
+
for (let p in paramObject) {
|
|
499
|
+
if (paramObject[p] === void 0)
|
|
500
|
+
delete paramObject[p];
|
|
501
|
+
if (paramObject.hasOwnProperty(p) && typeof p === 'string' && p !== 'regex' && !(paramObject[p] instanceof RegExp)) {
|
|
502
|
+
if (p.length > 0 && p[0] === '/') {
|
|
503
|
+
let newName = p.slice(1, p.length - 1);
|
|
504
|
+
urlparam[newName] = urlparam[p];
|
|
505
|
+
urlparam[p] = void 0;
|
|
506
|
+
p = newName;
|
|
507
|
+
}
|
|
508
|
+
if (p.length > 0 && p[p.length - 1] === '/') {
|
|
509
|
+
let newName = p.slice(0, p.length - 2);
|
|
510
|
+
urlparam[newName] = urlparam[p];
|
|
511
|
+
urlparam[p] = void 0;
|
|
512
|
+
p = newName;
|
|
513
|
+
}
|
|
514
|
+
let v = urlparam[p];
|
|
515
|
+
if (v instanceof Object) {
|
|
516
|
+
urlparam[p] = JSON.stringify(urlparam[p]);
|
|
517
|
+
}
|
|
518
|
+
urlparam[p] = doNotSerialize ? urlparam[p] : encodeURIComponent(urlparam[p]);
|
|
519
|
+
if (urlparam.regex !== void 0 && urlparam.regex instanceof RegExp) {
|
|
520
|
+
if (!urlparam.regex.test(urlparam[p])) {
|
|
521
|
+
console.warn(`Data: ${urlparam[p]} incostistent with regex ${urlparam.regex.source}`);
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
parameters.push(`${p}=${urlparam[p]}`);
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
urlparts.push(parameters.join('&'));
|
|
528
|
+
}
|
|
529
|
+
});
|
|
530
|
+
let join = urlparts.join().trim();
|
|
531
|
+
if (join.trim() === '')
|
|
532
|
+
return '';
|
|
533
|
+
return `?${urlparts.join('&')}`;
|
|
534
|
+
}
|
|
535
|
+
function transform(o) {
|
|
536
|
+
if (typeof o === 'object') {
|
|
537
|
+
return encodeURIComponent(JSON.stringify(o));
|
|
538
|
+
}
|
|
539
|
+
return o;
|
|
540
|
+
}
|
|
541
|
+
function prepareUrlOldWay(params) {
|
|
542
|
+
if (!params)
|
|
543
|
+
return this.endpoint;
|
|
544
|
+
if (typeof params === 'object') {
|
|
545
|
+
params = transform(params);
|
|
546
|
+
}
|
|
547
|
+
return this.endpoint + '/' + params;
|
|
548
|
+
}
|
|
549
|
+
;
|
|
550
550
|
({}); // @--end-of-file-for-module=ng2-rest lib/params.ts
|
|
551
551
|
|
|
552
|
-
class RestHeaders {
|
|
553
|
-
constructor(headers) {
|
|
554
|
-
/** @internal header names are lower case */
|
|
555
|
-
this._headers = new Map();
|
|
556
|
-
/** @internal map lower case names to actual names */
|
|
557
|
-
this._normalizedNames = new Map();
|
|
558
|
-
if (headers instanceof RestHeaders) {
|
|
559
|
-
headers.forEach((values, name) => {
|
|
560
|
-
values.forEach(value => this.set(name, value));
|
|
561
|
-
});
|
|
562
|
-
}
|
|
563
|
-
else {
|
|
564
|
-
Object.keys(headers).forEach((name) => {
|
|
565
|
-
const values = (Array.isArray(headers[name]) ? headers[name] : [headers[name]]);
|
|
566
|
-
this.delete(name);
|
|
567
|
-
values.forEach(value => this.set(name, value));
|
|
568
|
-
});
|
|
569
|
-
}
|
|
570
|
-
}
|
|
571
|
-
static from(headers) {
|
|
572
|
-
if (!headers) {
|
|
573
|
-
return void 0;
|
|
574
|
-
}
|
|
575
|
-
return new RestHeaders(headers);
|
|
576
|
-
}
|
|
577
|
-
/**
|
|
578
|
-
* Returns a new RestHeaders instance from the given DOMString of Response RestHeaders
|
|
579
|
-
*/
|
|
580
|
-
static fromResponseHeaderString(headersString) {
|
|
581
|
-
const headers = new RestHeaders();
|
|
582
|
-
headersString.split('\n').forEach(line => {
|
|
583
|
-
const index = line.indexOf(':');
|
|
584
|
-
if (index > 0) {
|
|
585
|
-
const name = line.slice(0, index);
|
|
586
|
-
const value = line.slice(index + 1).trim();
|
|
587
|
-
headers.set(name, value);
|
|
588
|
-
}
|
|
589
|
-
});
|
|
590
|
-
return headers;
|
|
591
|
-
}
|
|
592
|
-
/**
|
|
593
|
-
* Appends a header to existing list of header values for a given header name.
|
|
594
|
-
*/
|
|
595
|
-
append(name, value) {
|
|
596
|
-
const values = this.getAll(name);
|
|
597
|
-
if (values === null) {
|
|
598
|
-
this.set(name, value);
|
|
599
|
-
}
|
|
600
|
-
else {
|
|
601
|
-
values.push(value);
|
|
602
|
-
}
|
|
603
|
-
}
|
|
604
|
-
/**
|
|
605
|
-
* Deletes all header values for the given name.
|
|
606
|
-
*/
|
|
607
|
-
delete(name) {
|
|
608
|
-
const lcName = name.toLowerCase();
|
|
609
|
-
this._normalizedNames.delete(lcName);
|
|
610
|
-
this._headers.delete(lcName);
|
|
611
|
-
}
|
|
612
|
-
forEach(fn) {
|
|
613
|
-
this._headers.forEach((values, lcName) => fn(values, this._normalizedNames.get(lcName), this._headers));
|
|
614
|
-
}
|
|
615
|
-
/**
|
|
616
|
-
* Returns first header that matches given name.
|
|
617
|
-
*/
|
|
618
|
-
get(name) {
|
|
619
|
-
const values = this.getAll(name);
|
|
620
|
-
if (values === null) {
|
|
621
|
-
return null;
|
|
622
|
-
}
|
|
623
|
-
return values.length > 0 ? values[0] : null;
|
|
624
|
-
}
|
|
625
|
-
/**
|
|
626
|
-
* Checks for existence of header by given name.
|
|
627
|
-
*/
|
|
628
|
-
has(name) { return this._headers.has(name.toLowerCase()); }
|
|
629
|
-
/**
|
|
630
|
-
* Returns the names of the headers
|
|
631
|
-
*/
|
|
632
|
-
keys() { return Array.from(this._normalizedNames.values()); }
|
|
633
|
-
/**
|
|
634
|
-
* Sets or overrides header value for given name.
|
|
635
|
-
*/
|
|
636
|
-
set(name, value) {
|
|
637
|
-
if (Array.isArray(value)) {
|
|
638
|
-
if (value.length) {
|
|
639
|
-
this._headers.set(name.toLowerCase(), [value.join(',')]);
|
|
640
|
-
}
|
|
641
|
-
}
|
|
642
|
-
else {
|
|
643
|
-
this._headers.set(name.toLowerCase(), [value]);
|
|
644
|
-
}
|
|
645
|
-
this.mayBeSetNormalizedName(name);
|
|
646
|
-
}
|
|
647
|
-
/**
|
|
648
|
-
* Returns values of all headers.
|
|
649
|
-
*/
|
|
650
|
-
values() { return Array.from(this._headers.values()); }
|
|
651
|
-
/**
|
|
652
|
-
* Returns string of all headers.
|
|
653
|
-
*/
|
|
654
|
-
toJSON() {
|
|
655
|
-
const serialized = {};
|
|
656
|
-
if (!this._headers) {
|
|
657
|
-
}
|
|
658
|
-
this._headers.forEach((values, name) => {
|
|
659
|
-
const split = [];
|
|
660
|
-
values.forEach(v => split.push(...v.split(',')));
|
|
661
|
-
serialized[this._normalizedNames.get(name)] = split;
|
|
662
|
-
});
|
|
663
|
-
return serialized;
|
|
664
|
-
}
|
|
665
|
-
/**
|
|
666
|
-
* Returns list of header values for a given name.
|
|
667
|
-
*/
|
|
668
|
-
getAll(name) {
|
|
669
|
-
return this.has(name) ? this._headers.get(name.toLowerCase()) : null;
|
|
670
|
-
}
|
|
671
|
-
mayBeSetNormalizedName(name) {
|
|
672
|
-
const lcName = name.toLowerCase();
|
|
673
|
-
if (!this._normalizedNames.has(lcName)) {
|
|
674
|
-
this._normalizedNames.set(lcName, name);
|
|
675
|
-
}
|
|
676
|
-
}
|
|
677
|
-
}
|
|
678
|
-
;
|
|
552
|
+
class RestHeaders {
|
|
553
|
+
constructor(headers) {
|
|
554
|
+
/** @internal header names are lower case */
|
|
555
|
+
this._headers = new Map();
|
|
556
|
+
/** @internal map lower case names to actual names */
|
|
557
|
+
this._normalizedNames = new Map();
|
|
558
|
+
if (headers instanceof RestHeaders) {
|
|
559
|
+
headers.forEach((values, name) => {
|
|
560
|
+
values.forEach(value => this.set(name, value));
|
|
561
|
+
});
|
|
562
|
+
}
|
|
563
|
+
else {
|
|
564
|
+
Object.keys(headers).forEach((name) => {
|
|
565
|
+
const values = (Array.isArray(headers[name]) ? headers[name] : [headers[name]]);
|
|
566
|
+
this.delete(name);
|
|
567
|
+
values.forEach(value => this.set(name, value));
|
|
568
|
+
});
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
static from(headers) {
|
|
572
|
+
if (!headers) {
|
|
573
|
+
return void 0;
|
|
574
|
+
}
|
|
575
|
+
return new RestHeaders(headers);
|
|
576
|
+
}
|
|
577
|
+
/**
|
|
578
|
+
* Returns a new RestHeaders instance from the given DOMString of Response RestHeaders
|
|
579
|
+
*/
|
|
580
|
+
static fromResponseHeaderString(headersString) {
|
|
581
|
+
const headers = new RestHeaders();
|
|
582
|
+
headersString.split('\n').forEach(line => {
|
|
583
|
+
const index = line.indexOf(':');
|
|
584
|
+
if (index > 0) {
|
|
585
|
+
const name = line.slice(0, index);
|
|
586
|
+
const value = line.slice(index + 1).trim();
|
|
587
|
+
headers.set(name, value);
|
|
588
|
+
}
|
|
589
|
+
});
|
|
590
|
+
return headers;
|
|
591
|
+
}
|
|
592
|
+
/**
|
|
593
|
+
* Appends a header to existing list of header values for a given header name.
|
|
594
|
+
*/
|
|
595
|
+
append(name, value) {
|
|
596
|
+
const values = this.getAll(name);
|
|
597
|
+
if (values === null) {
|
|
598
|
+
this.set(name, value);
|
|
599
|
+
}
|
|
600
|
+
else {
|
|
601
|
+
values.push(value);
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
/**
|
|
605
|
+
* Deletes all header values for the given name.
|
|
606
|
+
*/
|
|
607
|
+
delete(name) {
|
|
608
|
+
const lcName = name.toLowerCase();
|
|
609
|
+
this._normalizedNames.delete(lcName);
|
|
610
|
+
this._headers.delete(lcName);
|
|
611
|
+
}
|
|
612
|
+
forEach(fn) {
|
|
613
|
+
this._headers.forEach((values, lcName) => fn(values, this._normalizedNames.get(lcName), this._headers));
|
|
614
|
+
}
|
|
615
|
+
/**
|
|
616
|
+
* Returns first header that matches given name.
|
|
617
|
+
*/
|
|
618
|
+
get(name) {
|
|
619
|
+
const values = this.getAll(name);
|
|
620
|
+
if (values === null) {
|
|
621
|
+
return null;
|
|
622
|
+
}
|
|
623
|
+
return values.length > 0 ? values[0] : null;
|
|
624
|
+
}
|
|
625
|
+
/**
|
|
626
|
+
* Checks for existence of header by given name.
|
|
627
|
+
*/
|
|
628
|
+
has(name) { return this._headers.has(name.toLowerCase()); }
|
|
629
|
+
/**
|
|
630
|
+
* Returns the names of the headers
|
|
631
|
+
*/
|
|
632
|
+
keys() { return Array.from(this._normalizedNames.values()); }
|
|
633
|
+
/**
|
|
634
|
+
* Sets or overrides header value for given name.
|
|
635
|
+
*/
|
|
636
|
+
set(name, value) {
|
|
637
|
+
if (Array.isArray(value)) {
|
|
638
|
+
if (value.length) {
|
|
639
|
+
this._headers.set(name.toLowerCase(), [value.join(',')]);
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
else {
|
|
643
|
+
this._headers.set(name.toLowerCase(), [value]);
|
|
644
|
+
}
|
|
645
|
+
this.mayBeSetNormalizedName(name);
|
|
646
|
+
}
|
|
647
|
+
/**
|
|
648
|
+
* Returns values of all headers.
|
|
649
|
+
*/
|
|
650
|
+
values() { return Array.from(this._headers.values()); }
|
|
651
|
+
/**
|
|
652
|
+
* Returns string of all headers.
|
|
653
|
+
*/
|
|
654
|
+
toJSON() {
|
|
655
|
+
const serialized = {};
|
|
656
|
+
if (!this._headers) {
|
|
657
|
+
}
|
|
658
|
+
this._headers.forEach((values, name) => {
|
|
659
|
+
const split = [];
|
|
660
|
+
values.forEach(v => split.push(...v.split(',')));
|
|
661
|
+
serialized[this._normalizedNames.get(name)] = split;
|
|
662
|
+
});
|
|
663
|
+
return serialized;
|
|
664
|
+
}
|
|
665
|
+
/**
|
|
666
|
+
* Returns list of header values for a given name.
|
|
667
|
+
*/
|
|
668
|
+
getAll(name) {
|
|
669
|
+
return this.has(name) ? this._headers.get(name.toLowerCase()) : null;
|
|
670
|
+
}
|
|
671
|
+
mayBeSetNormalizedName(name) {
|
|
672
|
+
const lcName = name.toLowerCase();
|
|
673
|
+
if (!this._normalizedNames.has(lcName)) {
|
|
674
|
+
this._normalizedNames.set(lcName, name);
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
;
|
|
679
679
|
({}); // @--end-of-file-for-module=ng2-rest lib/rest-headers.ts
|
|
680
680
|
|
|
681
|
-
const CONTENT_TYPE = {
|
|
682
|
-
APPLICATION_JSON: RestHeaders.from({
|
|
683
|
-
'Content-Type': 'application/json',
|
|
684
|
-
'Accept': 'application/json'
|
|
685
|
-
}),
|
|
686
|
-
APPLICATINO_VND_API_JSON: RestHeaders.from({
|
|
687
|
-
'Content-Type': 'application/vnd.api+json',
|
|
688
|
-
'Accept': 'application/vnd.api+json'
|
|
689
|
-
}),
|
|
690
|
-
};
|
|
681
|
+
const CONTENT_TYPE = {
|
|
682
|
+
APPLICATION_JSON: RestHeaders.from({
|
|
683
|
+
'Content-Type': 'application/json',
|
|
684
|
+
'Accept': 'application/json'
|
|
685
|
+
}),
|
|
686
|
+
APPLICATINO_VND_API_JSON: RestHeaders.from({
|
|
687
|
+
'Content-Type': 'application/vnd.api+json',
|
|
688
|
+
'Accept': 'application/vnd.api+json'
|
|
689
|
+
}),
|
|
690
|
+
};
|
|
691
691
|
({}); // @--end-of-file-for-module=ng2-rest lib/content-type.ts
|
|
692
692
|
|
|
693
|
-
class Rest {
|
|
694
|
-
constructor(endpoint, request, meta, customContentType) {
|
|
695
|
-
this.request = request;
|
|
696
|
-
this.meta = meta;
|
|
697
|
-
this.customContentType = customContentType;
|
|
698
|
-
this._headers = RestHeaders.from(CONTENT_TYPE.APPLICATION_JSON);
|
|
699
|
-
this.array = {
|
|
700
|
-
get: (params = void 0, doNotSerializeParams) => {
|
|
701
|
-
return this.req('get', void 0, params, doNotSerializeParams, true);
|
|
702
|
-
},
|
|
703
|
-
head: (params = void 0, doNotSerializeParams) => {
|
|
704
|
-
return this.req('head', void 0, params, doNotSerializeParams, true);
|
|
705
|
-
},
|
|
706
|
-
post: (item, params, doNotSerializeParams) => {
|
|
707
|
-
return this.req('post', item, params, doNotSerializeParams, true);
|
|
708
|
-
},
|
|
709
|
-
put: (item, params, doNotSerializeParams) => {
|
|
710
|
-
return this.req('put', item, params, doNotSerializeParams, true);
|
|
711
|
-
},
|
|
712
|
-
patch: (item, params, doNotSerializeParams) => {
|
|
713
|
-
return this.req('patch', item, params, doNotSerializeParams, true);
|
|
714
|
-
},
|
|
715
|
-
delete: (params, doNotSerializeParams) => {
|
|
716
|
-
return this.req('delete', void 0, params, doNotSerializeParams, true);
|
|
717
|
-
},
|
|
718
|
-
jsonp: (params, doNotSerializeParams) => {
|
|
719
|
-
return this.req('jsonp', void 0, params, doNotSerializeParams, true);
|
|
720
|
-
}
|
|
721
|
-
};
|
|
722
|
-
this.__meta_endpoint = endpoint;
|
|
723
|
-
}
|
|
724
|
-
mock(mock) {
|
|
725
|
-
if ((typeof mock === 'function') || (typeof mock === 'object')) {
|
|
726
|
-
this.mockHttp = mock;
|
|
727
|
-
}
|
|
728
|
-
else {
|
|
693
|
+
class Rest {
|
|
694
|
+
constructor(endpoint, request, meta, customContentType) {
|
|
695
|
+
this.request = request;
|
|
696
|
+
this.meta = meta;
|
|
697
|
+
this.customContentType = customContentType;
|
|
698
|
+
this._headers = RestHeaders.from(CONTENT_TYPE.APPLICATION_JSON);
|
|
699
|
+
this.array = {
|
|
700
|
+
get: (params = void 0, doNotSerializeParams) => {
|
|
701
|
+
return this.req('get', void 0, params, doNotSerializeParams, true);
|
|
702
|
+
},
|
|
703
|
+
head: (params = void 0, doNotSerializeParams) => {
|
|
704
|
+
return this.req('head', void 0, params, doNotSerializeParams, true);
|
|
705
|
+
},
|
|
706
|
+
post: (item, params, doNotSerializeParams) => {
|
|
707
|
+
return this.req('post', item, params, doNotSerializeParams, true);
|
|
708
|
+
},
|
|
709
|
+
put: (item, params, doNotSerializeParams) => {
|
|
710
|
+
return this.req('put', item, params, doNotSerializeParams, true);
|
|
711
|
+
},
|
|
712
|
+
patch: (item, params, doNotSerializeParams) => {
|
|
713
|
+
return this.req('patch', item, params, doNotSerializeParams, true);
|
|
714
|
+
},
|
|
715
|
+
delete: (params, doNotSerializeParams) => {
|
|
716
|
+
return this.req('delete', void 0, params, doNotSerializeParams, true);
|
|
717
|
+
},
|
|
718
|
+
jsonp: (params, doNotSerializeParams) => {
|
|
719
|
+
return this.req('jsonp', void 0, params, doNotSerializeParams, true);
|
|
720
|
+
}
|
|
721
|
+
};
|
|
722
|
+
this.__meta_endpoint = endpoint;
|
|
723
|
+
}
|
|
724
|
+
mock(mock) {
|
|
725
|
+
if ((typeof mock === 'function') || (typeof mock === 'object')) {
|
|
726
|
+
this.mockHttp = mock;
|
|
727
|
+
}
|
|
728
|
+
else {
|
|
729
729
|
throw `[ng2-rest]
|
|
730
730
|
.model(...)
|
|
731
731
|
.mock( < BAD MOCK DATA > )
|
|
732
732
|
...
|
|
733
|
-
`;
|
|
734
|
-
}
|
|
735
|
-
return this;
|
|
736
|
-
}
|
|
737
|
-
get endpoint() {
|
|
738
|
-
let e = this.__meta_endpoint;
|
|
739
|
-
if (this.restQueryParams !== void 0 && this._endpointRest !== void 0
|
|
740
|
-
&& typeof this._endpointRest === 'string' && this._endpointRest.trim() !== '')
|
|
741
|
-
e = this._endpointRest;
|
|
742
|
-
return e;
|
|
743
|
-
}
|
|
744
|
-
set __rest_endpoint(endpoint) {
|
|
745
|
-
this._endpointRest = endpoint;
|
|
746
|
-
if (endpoint === void 0) {
|
|
747
|
-
this.restQueryParams = void 0;
|
|
748
|
-
}
|
|
749
|
-
else {
|
|
750
|
-
this.restQueryParams = getRestParams(endpoint, this.__meta_endpoint);
|
|
751
|
-
}
|
|
752
|
-
}
|
|
753
|
-
creatUrl(params, doNotSerializeParams = false) {
|
|
754
|
-
return `${this.endpoint}${getParamsUrl(params, doNotSerializeParams)}`;
|
|
755
|
-
}
|
|
756
|
-
get headers() {
|
|
757
|
-
return this._headers;
|
|
758
|
-
}
|
|
759
|
-
req(method, requestBody, params, doNotSerializeParams = false, isArray = false) {
|
|
760
|
-
const modelUrl = this.creatUrl(params, doNotSerializeParams);
|
|
761
|
-
const body = (CLASS.getNameFromObject(requestBody) === 'FormData')
|
|
762
|
-
? requestBody
|
|
763
|
-
: (requestBody ? JSON.stringify(requestBody) : void 0);
|
|
764
|
-
if (this.customContentType) {
|
|
765
|
-
const customHeaderKeys = this.customContentType.keys();
|
|
766
|
-
const currentHeaderKeys = this._headers.keys();
|
|
767
|
-
currentHeaderKeys
|
|
768
|
-
.filter(key => !customHeaderKeys.includes(key))
|
|
769
|
-
.forEach(key => {
|
|
770
|
-
this.customContentType.set(key, this._headers.get(key));
|
|
771
|
-
});
|
|
772
|
-
this._headers = this.customContentType;
|
|
773
|
-
}
|
|
774
|
-
else {
|
|
775
|
-
this._headers = RestHeaders.from(CONTENT_TYPE.APPLICATION_JSON);
|
|
776
|
-
}
|
|
777
|
-
const result = this.request[method.toLowerCase()](modelUrl, body, this.headers, this.meta, isArray, this.mockHttp);
|
|
778
|
-
this.mockHttp = void 0;
|
|
779
|
-
return result;
|
|
780
|
-
}
|
|
781
|
-
replay(method) {
|
|
782
|
-
this.request.replay(method, this.meta);
|
|
783
|
-
}
|
|
784
|
-
get(params, doNotSerializeParams = false) {
|
|
785
|
-
return this.req('get', void 0, params, doNotSerializeParams);
|
|
786
|
-
}
|
|
787
|
-
head(params, doNotSerializeParams = false) {
|
|
788
|
-
return this.req('head', void 0, params, doNotSerializeParams);
|
|
789
|
-
}
|
|
790
|
-
post(item, params, doNotSerializeParams = false) {
|
|
791
|
-
return this.req('post', item, params, doNotSerializeParams);
|
|
792
|
-
}
|
|
793
|
-
put(item, params, doNotSerializeParams = false) {
|
|
794
|
-
return this.req('put', item, params, doNotSerializeParams);
|
|
795
|
-
}
|
|
796
|
-
patch(item, params, doNotSerializeParams = false) {
|
|
797
|
-
return this.req('patch', item, params, doNotSerializeParams);
|
|
798
|
-
}
|
|
799
|
-
delete(params, doNotSerializeParams = false) {
|
|
800
|
-
return this.req('delete', void 0, params, doNotSerializeParams);
|
|
801
|
-
}
|
|
802
|
-
jsonp(params, doNotSerializeParams = false) {
|
|
803
|
-
return this.req('jsonp', void 0, params, doNotSerializeParams);
|
|
804
|
-
}
|
|
805
|
-
}
|
|
806
|
-
;
|
|
733
|
+
`;
|
|
734
|
+
}
|
|
735
|
+
return this;
|
|
736
|
+
}
|
|
737
|
+
get endpoint() {
|
|
738
|
+
let e = this.__meta_endpoint;
|
|
739
|
+
if (this.restQueryParams !== void 0 && this._endpointRest !== void 0
|
|
740
|
+
&& typeof this._endpointRest === 'string' && this._endpointRest.trim() !== '')
|
|
741
|
+
e = this._endpointRest;
|
|
742
|
+
return e;
|
|
743
|
+
}
|
|
744
|
+
set __rest_endpoint(endpoint) {
|
|
745
|
+
this._endpointRest = endpoint;
|
|
746
|
+
if (endpoint === void 0) {
|
|
747
|
+
this.restQueryParams = void 0;
|
|
748
|
+
}
|
|
749
|
+
else {
|
|
750
|
+
this.restQueryParams = getRestParams(endpoint, this.__meta_endpoint);
|
|
751
|
+
}
|
|
752
|
+
}
|
|
753
|
+
creatUrl(params, doNotSerializeParams = false) {
|
|
754
|
+
return `${this.endpoint}${getParamsUrl(params, doNotSerializeParams)}`;
|
|
755
|
+
}
|
|
756
|
+
get headers() {
|
|
757
|
+
return this._headers;
|
|
758
|
+
}
|
|
759
|
+
req(method, requestBody, params, doNotSerializeParams = false, isArray = false) {
|
|
760
|
+
const modelUrl = this.creatUrl(params, doNotSerializeParams);
|
|
761
|
+
const body = (CLASS.getNameFromObject(requestBody) === 'FormData')
|
|
762
|
+
? requestBody
|
|
763
|
+
: (requestBody ? JSON.stringify(requestBody) : void 0);
|
|
764
|
+
if (this.customContentType) {
|
|
765
|
+
const customHeaderKeys = this.customContentType.keys();
|
|
766
|
+
const currentHeaderKeys = this._headers.keys();
|
|
767
|
+
currentHeaderKeys
|
|
768
|
+
.filter(key => !customHeaderKeys.includes(key))
|
|
769
|
+
.forEach(key => {
|
|
770
|
+
this.customContentType.set(key, this._headers.get(key));
|
|
771
|
+
});
|
|
772
|
+
this._headers = this.customContentType;
|
|
773
|
+
}
|
|
774
|
+
else {
|
|
775
|
+
this._headers = RestHeaders.from(CONTENT_TYPE.APPLICATION_JSON);
|
|
776
|
+
}
|
|
777
|
+
const result = this.request[method.toLowerCase()](modelUrl, body, this.headers, this.meta, isArray, this.mockHttp);
|
|
778
|
+
this.mockHttp = void 0;
|
|
779
|
+
return result;
|
|
780
|
+
}
|
|
781
|
+
replay(method) {
|
|
782
|
+
this.request.replay(method, this.meta);
|
|
783
|
+
}
|
|
784
|
+
get(params, doNotSerializeParams = false) {
|
|
785
|
+
return this.req('get', void 0, params, doNotSerializeParams);
|
|
786
|
+
}
|
|
787
|
+
head(params, doNotSerializeParams = false) {
|
|
788
|
+
return this.req('head', void 0, params, doNotSerializeParams);
|
|
789
|
+
}
|
|
790
|
+
post(item, params, doNotSerializeParams = false) {
|
|
791
|
+
return this.req('post', item, params, doNotSerializeParams);
|
|
792
|
+
}
|
|
793
|
+
put(item, params, doNotSerializeParams = false) {
|
|
794
|
+
return this.req('put', item, params, doNotSerializeParams);
|
|
795
|
+
}
|
|
796
|
+
patch(item, params, doNotSerializeParams = false) {
|
|
797
|
+
return this.req('patch', item, params, doNotSerializeParams);
|
|
798
|
+
}
|
|
799
|
+
delete(params, doNotSerializeParams = false) {
|
|
800
|
+
return this.req('delete', void 0, params, doNotSerializeParams);
|
|
801
|
+
}
|
|
802
|
+
jsonp(params, doNotSerializeParams = false) {
|
|
803
|
+
return this.req('jsonp', void 0, params, doNotSerializeParams);
|
|
804
|
+
}
|
|
805
|
+
}
|
|
806
|
+
;
|
|
807
807
|
({}); // @--end-of-file-for-module=ng2-rest lib/rest.class.ts
|
|
808
808
|
|
|
809
|
-
class Cookie {
|
|
810
|
-
constructor() {
|
|
811
|
-
}
|
|
812
|
-
static get Instance() {
|
|
813
|
-
if (!Cookie.__instance) {
|
|
814
|
-
Cookie.__instance = new Cookie();
|
|
815
|
-
}
|
|
816
|
-
return Cookie.__instance;
|
|
817
|
-
}
|
|
818
|
-
read(name) {
|
|
819
|
-
var result = new RegExp('(?:^|; )' + encodeURIComponent(name) + '=([^;]*)').exec(document.cookie);
|
|
820
|
-
return result ? result[1] : null;
|
|
821
|
-
}
|
|
822
|
-
write(name, value, days) {
|
|
823
|
-
if (!days) {
|
|
824
|
-
days = 365 * 20;
|
|
825
|
-
}
|
|
826
|
-
var date = new Date();
|
|
827
|
-
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
|
|
828
|
-
var expires = "; expires=" + date.toUTCString();
|
|
829
|
-
document.cookie = name + "=" + value + expires + "; path=/";
|
|
830
|
-
}
|
|
831
|
-
remove(name) {
|
|
832
|
-
this.write(name, "", -1);
|
|
833
|
-
}
|
|
834
|
-
}
|
|
835
|
-
;
|
|
809
|
+
class Cookie {
|
|
810
|
+
constructor() {
|
|
811
|
+
}
|
|
812
|
+
static get Instance() {
|
|
813
|
+
if (!Cookie.__instance) {
|
|
814
|
+
Cookie.__instance = new Cookie();
|
|
815
|
+
}
|
|
816
|
+
return Cookie.__instance;
|
|
817
|
+
}
|
|
818
|
+
read(name) {
|
|
819
|
+
var result = new RegExp('(?:^|; )' + encodeURIComponent(name) + '=([^;]*)').exec(document.cookie);
|
|
820
|
+
return result ? result[1] : null;
|
|
821
|
+
}
|
|
822
|
+
write(name, value, days) {
|
|
823
|
+
if (!days) {
|
|
824
|
+
days = 365 * 20;
|
|
825
|
+
}
|
|
826
|
+
var date = new Date();
|
|
827
|
+
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
|
|
828
|
+
var expires = "; expires=" + date.toUTCString();
|
|
829
|
+
document.cookie = name + "=" + value + expires + "; path=/";
|
|
830
|
+
}
|
|
831
|
+
remove(name) {
|
|
832
|
+
this.write(name, "", -1);
|
|
833
|
+
}
|
|
834
|
+
}
|
|
835
|
+
;
|
|
836
836
|
({}); // @--end-of-file-for-module=ng2-rest lib/cookie.ts
|
|
837
837
|
|
|
838
|
-
const log$2 = Log.create('request-cache', Level.__NOTHING);
|
|
839
|
-
/**
|
|
840
|
-
* @deprecated
|
|
841
|
-
* there is Cache API for that
|
|
842
|
-
*/
|
|
843
|
-
class RequestCache {
|
|
844
|
-
constructor(response) {
|
|
845
|
-
this.response = response;
|
|
846
|
-
}
|
|
847
|
-
static restoreFromLocalStorage() {
|
|
848
|
-
if (Helpers$1.isNode) {
|
|
849
|
-
return;
|
|
850
|
-
}
|
|
851
|
-
if (!RequestCache.isRestoredFromLocalStorage) {
|
|
852
|
-
RequestCache.isRestoredFromLocalStorage = true;
|
|
853
|
-
const data = localStorage.getItem(RequestCache.LOCAL_STORAGE_KEY);
|
|
854
|
-
let requests = [];
|
|
855
|
-
if (data) {
|
|
856
|
-
try {
|
|
857
|
-
requests = JSON.parse(data);
|
|
858
|
-
}
|
|
859
|
-
catch (error) {
|
|
860
|
-
}
|
|
861
|
-
const restored = requests.map(r => {
|
|
862
|
-
let { sourceRequest, responseText, body, headers, circular, entity, isArray, cookies, statusCode } = r.response;
|
|
863
|
-
r.response = new Models.HttpResponse(sourceRequest, responseText, RestHeaders.from(headers), statusCode, entity, circular, -1, // jobid from local storage TODO
|
|
864
|
-
isArray);
|
|
865
|
-
r = new RequestCache(r.response);
|
|
866
|
-
r.response.rq = r;
|
|
867
|
-
return r;
|
|
868
|
-
});
|
|
869
|
-
log$2.i('RESTORED FROM LOCAL STORAGE', restored);
|
|
870
|
-
RequestCache.cached = restored;
|
|
871
|
-
}
|
|
872
|
-
}
|
|
873
|
-
}
|
|
874
|
-
static findBy(sourceRequest) {
|
|
875
|
-
log$2.i('findby', sourceRequest);
|
|
876
|
-
log$2.i('RequestCache.cached', RequestCache.cached);
|
|
877
|
-
RequestCache.restoreFromLocalStorage();
|
|
878
|
-
return RequestCache.cached.find(c => {
|
|
879
|
-
const a = c.response.sourceRequest;
|
|
880
|
-
const b = sourceRequest;
|
|
881
|
-
return (a.isArray === b.isArray &&
|
|
882
|
-
a.url === b.url &&
|
|
883
|
-
a.method === b.method &&
|
|
884
|
-
a.body === b.body);
|
|
885
|
-
});
|
|
886
|
-
}
|
|
887
|
-
get containsCache() {
|
|
888
|
-
RequestCache.restoreFromLocalStorage();
|
|
889
|
-
return RequestCache.cached.includes(this);
|
|
890
|
-
}
|
|
891
|
-
persistsInLocalStorage() {
|
|
892
|
-
localStorage.setItem(RequestCache.LOCAL_STORAGE_KEY, JSON.stringify(RequestCache.cached.map(r => {
|
|
893
|
-
return {
|
|
894
|
-
response: {
|
|
895
|
-
sourceRequest: r.response.sourceRequest,
|
|
896
|
-
responseText: r.response.responseText,
|
|
897
|
-
headers: r.response.headers,
|
|
898
|
-
statusCode: r.response.statusCode,
|
|
899
|
-
entity: r.response.entity,
|
|
900
|
-
circular: r.response.circular,
|
|
901
|
-
isArray: r.response.isArray,
|
|
902
|
-
}
|
|
903
|
-
};
|
|
904
|
-
})));
|
|
905
|
-
}
|
|
906
|
-
store() {
|
|
907
|
-
RequestCache.restoreFromLocalStorage();
|
|
908
|
-
if (!this.containsCache) {
|
|
909
|
-
RequestCache.cached.push(this);
|
|
910
|
-
this.persistsInLocalStorage();
|
|
911
|
-
}
|
|
912
|
-
else {
|
|
913
|
-
console.log('already stored');
|
|
914
|
-
}
|
|
915
|
-
return this;
|
|
916
|
-
}
|
|
917
|
-
remove() {
|
|
918
|
-
RequestCache.restoreFromLocalStorage();
|
|
919
|
-
const index = RequestCache.cached.indexOf(this);
|
|
920
|
-
if (index !== -1) {
|
|
921
|
-
RequestCache.cached.splice(index, 1);
|
|
922
|
-
this.persistsInLocalStorage();
|
|
923
|
-
}
|
|
924
|
-
else {
|
|
925
|
-
console.log('already removed');
|
|
926
|
-
}
|
|
927
|
-
}
|
|
928
|
-
}
|
|
929
|
-
RequestCache.LOCAL_STORAGE_KEY = 'ng2restrequestcache';
|
|
930
|
-
RequestCache.cached = [];
|
|
931
|
-
RequestCache.isRestoredFromLocalStorage = false;
|
|
932
|
-
;
|
|
838
|
+
const log$2 = Log.create('request-cache', Level.__NOTHING);
|
|
839
|
+
/**
|
|
840
|
+
* @deprecated
|
|
841
|
+
* there is Cache API for that
|
|
842
|
+
*/
|
|
843
|
+
class RequestCache {
|
|
844
|
+
constructor(response) {
|
|
845
|
+
this.response = response;
|
|
846
|
+
}
|
|
847
|
+
static restoreFromLocalStorage() {
|
|
848
|
+
if (Helpers$1.isNode) {
|
|
849
|
+
return;
|
|
850
|
+
}
|
|
851
|
+
if (!RequestCache.isRestoredFromLocalStorage) {
|
|
852
|
+
RequestCache.isRestoredFromLocalStorage = true;
|
|
853
|
+
const data = localStorage.getItem(RequestCache.LOCAL_STORAGE_KEY);
|
|
854
|
+
let requests = [];
|
|
855
|
+
if (data) {
|
|
856
|
+
try {
|
|
857
|
+
requests = JSON.parse(data);
|
|
858
|
+
}
|
|
859
|
+
catch (error) {
|
|
860
|
+
}
|
|
861
|
+
const restored = requests.map(r => {
|
|
862
|
+
let { sourceRequest, responseText, body, headers, circular, entity, isArray, cookies, statusCode } = r.response;
|
|
863
|
+
r.response = new Models.HttpResponse(sourceRequest, responseText, RestHeaders.from(headers), statusCode, entity, circular, -1, // jobid from local storage TODO
|
|
864
|
+
isArray);
|
|
865
|
+
r = new RequestCache(r.response);
|
|
866
|
+
r.response.rq = r;
|
|
867
|
+
return r;
|
|
868
|
+
});
|
|
869
|
+
log$2.i('RESTORED FROM LOCAL STORAGE', restored);
|
|
870
|
+
RequestCache.cached = restored;
|
|
871
|
+
}
|
|
872
|
+
}
|
|
873
|
+
}
|
|
874
|
+
static findBy(sourceRequest) {
|
|
875
|
+
log$2.i('findby', sourceRequest);
|
|
876
|
+
log$2.i('RequestCache.cached', RequestCache.cached);
|
|
877
|
+
RequestCache.restoreFromLocalStorage();
|
|
878
|
+
return RequestCache.cached.find(c => {
|
|
879
|
+
const a = c.response.sourceRequest;
|
|
880
|
+
const b = sourceRequest;
|
|
881
|
+
return (a.isArray === b.isArray &&
|
|
882
|
+
a.url === b.url &&
|
|
883
|
+
a.method === b.method &&
|
|
884
|
+
a.body === b.body);
|
|
885
|
+
});
|
|
886
|
+
}
|
|
887
|
+
get containsCache() {
|
|
888
|
+
RequestCache.restoreFromLocalStorage();
|
|
889
|
+
return RequestCache.cached.includes(this);
|
|
890
|
+
}
|
|
891
|
+
persistsInLocalStorage() {
|
|
892
|
+
localStorage.setItem(RequestCache.LOCAL_STORAGE_KEY, JSON.stringify(RequestCache.cached.map(r => {
|
|
893
|
+
return {
|
|
894
|
+
response: {
|
|
895
|
+
sourceRequest: r.response.sourceRequest,
|
|
896
|
+
responseText: r.response.responseText,
|
|
897
|
+
headers: r.response.headers,
|
|
898
|
+
statusCode: r.response.statusCode,
|
|
899
|
+
entity: r.response.entity,
|
|
900
|
+
circular: r.response.circular,
|
|
901
|
+
isArray: r.response.isArray,
|
|
902
|
+
}
|
|
903
|
+
};
|
|
904
|
+
})));
|
|
905
|
+
}
|
|
906
|
+
store() {
|
|
907
|
+
RequestCache.restoreFromLocalStorage();
|
|
908
|
+
if (!this.containsCache) {
|
|
909
|
+
RequestCache.cached.push(this);
|
|
910
|
+
this.persistsInLocalStorage();
|
|
911
|
+
}
|
|
912
|
+
else {
|
|
913
|
+
console.log('already stored');
|
|
914
|
+
}
|
|
915
|
+
return this;
|
|
916
|
+
}
|
|
917
|
+
remove() {
|
|
918
|
+
RequestCache.restoreFromLocalStorage();
|
|
919
|
+
const index = RequestCache.cached.indexOf(this);
|
|
920
|
+
if (index !== -1) {
|
|
921
|
+
RequestCache.cached.splice(index, 1);
|
|
922
|
+
this.persistsInLocalStorage();
|
|
923
|
+
}
|
|
924
|
+
else {
|
|
925
|
+
console.log('already removed');
|
|
926
|
+
}
|
|
927
|
+
}
|
|
928
|
+
}
|
|
929
|
+
RequestCache.LOCAL_STORAGE_KEY = 'ng2restrequestcache';
|
|
930
|
+
RequestCache.cached = [];
|
|
931
|
+
RequestCache.isRestoredFromLocalStorage = false;
|
|
932
|
+
;
|
|
933
933
|
({}); // @--end-of-file-for-module=ng2-rest lib/request-cache.ts
|
|
934
934
|
|
|
935
|
-
var Models;
|
|
936
|
-
(function (Models) {
|
|
937
|
-
Models.MethodConfig = Models$1.MethodConfig;
|
|
938
|
-
Models.ClassConfig = Models$1.ClassConfig;
|
|
939
|
-
Models.ParamConfig = Models$1.ParamConfig;
|
|
940
|
-
;
|
|
941
|
-
;
|
|
942
|
-
[];
|
|
943
|
-
class BaseBody {
|
|
944
|
-
toJSON(data, isJSONArray = false) {
|
|
945
|
-
let r = isJSONArray ? [] : {};
|
|
946
|
-
if (typeof data === 'string') {
|
|
947
|
-
try {
|
|
948
|
-
r = JSON.parse(data);
|
|
949
|
-
}
|
|
950
|
-
catch (e) { }
|
|
951
|
-
}
|
|
952
|
-
else if (typeof data === 'object') {
|
|
953
|
-
return data;
|
|
954
|
-
}
|
|
955
|
-
return r;
|
|
956
|
-
}
|
|
957
|
-
}
|
|
958
|
-
Models.BaseBody = BaseBody;
|
|
959
|
-
class HttpBody extends BaseBody {
|
|
960
|
-
constructor(responseText, isArray = false, entity, circular) {
|
|
961
|
-
super();
|
|
962
|
-
this.responseText = responseText;
|
|
963
|
-
this.isArray = isArray;
|
|
964
|
-
this.entity = entity;
|
|
965
|
-
this.circular = circular;
|
|
966
|
-
}
|
|
967
|
-
get blob() {
|
|
968
|
-
return this.responseText;
|
|
969
|
-
}
|
|
970
|
-
get booleanValue() {
|
|
971
|
-
if (!Helpers$1.isBlob(this.responseText)) {
|
|
972
|
-
return ['ok', 'true'].includes(this.responseText.trim());
|
|
973
|
-
}
|
|
974
|
-
}
|
|
975
|
-
get rawJson() {
|
|
976
|
-
if (!Helpers$1.isBlob(this.responseText)) {
|
|
977
|
-
let res = this.toJSON(this.responseText, this.isArray);
|
|
978
|
-
if (this.circular && Array.isArray(this.circular)) {
|
|
979
|
-
res = JSON10.parse(JSON.stringify(res), this.circular);
|
|
980
|
-
}
|
|
981
|
-
return res;
|
|
982
|
-
}
|
|
983
|
-
}
|
|
984
|
-
get json() {
|
|
985
|
-
if (!Helpers$1.isBlob(this.responseText)) {
|
|
986
|
-
if (this.entity && typeof this.entity === 'function') {
|
|
987
|
-
return this.entity(); // @LAST
|
|
988
|
-
}
|
|
989
|
-
if (this.entity && typeof this.entity === 'object') {
|
|
990
|
-
const json = this.toJSON(this.responseText, this.isArray);
|
|
991
|
-
return Mapping.encode(json, this.entity, this.circular);
|
|
992
|
-
}
|
|
993
|
-
let res = this.toJSON(this.responseText, this.isArray);
|
|
994
|
-
if (this.circular && Array.isArray(this.circular)) {
|
|
995
|
-
res = JSON10.parse(JSON.stringify(res), this.circular);
|
|
996
|
-
}
|
|
997
|
-
return res;
|
|
998
|
-
}
|
|
999
|
-
}
|
|
1000
|
-
get text() {
|
|
1001
|
-
if (!Helpers$1.isBlob(this.responseText)) {
|
|
1002
|
-
return this.responseText.replace(/^\"/, '').replace(/\"$/, '');
|
|
1003
|
-
}
|
|
1004
|
-
}
|
|
1005
|
-
}
|
|
1006
|
-
Models.HttpBody = HttpBody;
|
|
1007
|
-
class ErrorBody extends BaseBody {
|
|
1008
|
-
constructor(data) {
|
|
1009
|
-
super();
|
|
1010
|
-
this.data = data;
|
|
1011
|
-
}
|
|
1012
|
-
get json() {
|
|
1013
|
-
return this.toJSON(this.data);
|
|
1014
|
-
}
|
|
1015
|
-
get text() {
|
|
1016
|
-
return this.data;
|
|
1017
|
-
}
|
|
1018
|
-
}
|
|
1019
|
-
Models.ErrorBody = ErrorBody;
|
|
1020
|
-
class BaseResponse {
|
|
1021
|
-
constructor(responseText, headers, statusCode, isArray = false) {
|
|
1022
|
-
this.responseText = responseText;
|
|
1023
|
-
this.headers = headers;
|
|
1024
|
-
this.statusCode = statusCode;
|
|
1025
|
-
this.isArray = isArray;
|
|
1026
|
-
}
|
|
1027
|
-
get cookies() {
|
|
1028
|
-
return BaseResponse.cookies;
|
|
1029
|
-
}
|
|
1030
|
-
}
|
|
1031
|
-
BaseResponse.cookies = Cookie.Instance;
|
|
1032
|
-
Models.BaseResponse = BaseResponse;
|
|
1033
|
-
class HttpResponse extends BaseResponse {
|
|
1034
|
-
constructor(sourceRequest, responseText, headers, statusCode, entity, circular, jobid, isArray = false) {
|
|
1035
|
-
super(responseText, headers, statusCode, isArray);
|
|
1036
|
-
this.sourceRequest = sourceRequest;
|
|
1037
|
-
this.responseText = responseText;
|
|
1038
|
-
this.headers = headers;
|
|
1039
|
-
this.statusCode = statusCode;
|
|
1040
|
-
this.entity = entity;
|
|
1041
|
-
this.circular = circular;
|
|
1042
|
-
this.jobid = jobid;
|
|
1043
|
-
this.isArray = isArray;
|
|
1044
|
-
this.init();
|
|
1045
|
-
}
|
|
1046
|
-
init() {
|
|
1047
|
-
if (typeof this.entity === 'string') {
|
|
1048
|
-
let entityJSON = this.headers.getAll(this.entity);
|
|
1049
|
-
if (!!entityJSON) {
|
|
1050
|
-
this.entity = JSON.parse(entityJSON.join());
|
|
1051
|
-
}
|
|
1052
|
-
}
|
|
1053
|
-
if (typeof this.circular === 'string') {
|
|
1054
|
-
let circuralJSON = this.headers.getAll(this.circular);
|
|
1055
|
-
if (!!circuralJSON) {
|
|
1056
|
-
this.circular = JSON.parse(circuralJSON.join());
|
|
1057
|
-
}
|
|
1058
|
-
}
|
|
1059
|
-
this.body = new HttpBody(this.responseText, this.isArray, this.entity, this.circular);
|
|
1060
|
-
}
|
|
1061
|
-
get cache() {
|
|
1062
|
-
if (_.isUndefined(this.rq)) {
|
|
1063
|
-
this.rq = new RequestCache(this);
|
|
1064
|
-
}
|
|
1065
|
-
return new RequestCache(this);
|
|
1066
|
-
}
|
|
1067
|
-
}
|
|
1068
|
-
Models.HttpResponse = HttpResponse;
|
|
1069
|
-
class HttpResponseError extends BaseResponse {
|
|
1070
|
-
constructor(message, responseText, headers, statusCode, jobid) {
|
|
1071
|
-
super(responseText, headers, statusCode);
|
|
1072
|
-
this.message = message;
|
|
1073
|
-
this.jobid = jobid;
|
|
1074
|
-
this.body = new ErrorBody(responseText);
|
|
1075
|
-
}
|
|
1076
|
-
}
|
|
1077
|
-
Models.HttpResponseError = HttpResponseError;
|
|
1078
|
-
/* */
|
|
1079
|
-
/* */
|
|
1080
|
-
/* */
|
|
1081
|
-
/* */
|
|
1082
|
-
/* */
|
|
1083
|
-
/* */
|
|
1084
|
-
})(Models || (Models = {}));
|
|
1085
|
-
;
|
|
935
|
+
var Models;
|
|
936
|
+
(function (Models) {
|
|
937
|
+
Models.MethodConfig = Models$1.MethodConfig;
|
|
938
|
+
Models.ClassConfig = Models$1.ClassConfig;
|
|
939
|
+
Models.ParamConfig = Models$1.ParamConfig;
|
|
940
|
+
;
|
|
941
|
+
;
|
|
942
|
+
[];
|
|
943
|
+
class BaseBody {
|
|
944
|
+
toJSON(data, isJSONArray = false) {
|
|
945
|
+
let r = isJSONArray ? [] : {};
|
|
946
|
+
if (typeof data === 'string') {
|
|
947
|
+
try {
|
|
948
|
+
r = JSON.parse(data);
|
|
949
|
+
}
|
|
950
|
+
catch (e) { }
|
|
951
|
+
}
|
|
952
|
+
else if (typeof data === 'object') {
|
|
953
|
+
return data;
|
|
954
|
+
}
|
|
955
|
+
return r;
|
|
956
|
+
}
|
|
957
|
+
}
|
|
958
|
+
Models.BaseBody = BaseBody;
|
|
959
|
+
class HttpBody extends BaseBody {
|
|
960
|
+
constructor(responseText, isArray = false, entity, circular) {
|
|
961
|
+
super();
|
|
962
|
+
this.responseText = responseText;
|
|
963
|
+
this.isArray = isArray;
|
|
964
|
+
this.entity = entity;
|
|
965
|
+
this.circular = circular;
|
|
966
|
+
}
|
|
967
|
+
get blob() {
|
|
968
|
+
return this.responseText;
|
|
969
|
+
}
|
|
970
|
+
get booleanValue() {
|
|
971
|
+
if (!Helpers$1.isBlob(this.responseText)) {
|
|
972
|
+
return ['ok', 'true'].includes(this.responseText.trim());
|
|
973
|
+
}
|
|
974
|
+
}
|
|
975
|
+
get rawJson() {
|
|
976
|
+
if (!Helpers$1.isBlob(this.responseText)) {
|
|
977
|
+
let res = this.toJSON(this.responseText, this.isArray);
|
|
978
|
+
if (this.circular && Array.isArray(this.circular)) {
|
|
979
|
+
res = JSON10.parse(JSON.stringify(res), this.circular);
|
|
980
|
+
}
|
|
981
|
+
return res;
|
|
982
|
+
}
|
|
983
|
+
}
|
|
984
|
+
get json() {
|
|
985
|
+
if (!Helpers$1.isBlob(this.responseText)) {
|
|
986
|
+
if (this.entity && typeof this.entity === 'function') {
|
|
987
|
+
return this.entity(); // @LAST
|
|
988
|
+
}
|
|
989
|
+
if (this.entity && typeof this.entity === 'object') {
|
|
990
|
+
const json = this.toJSON(this.responseText, this.isArray);
|
|
991
|
+
return Mapping.encode(json, this.entity, this.circular);
|
|
992
|
+
}
|
|
993
|
+
let res = this.toJSON(this.responseText, this.isArray);
|
|
994
|
+
if (this.circular && Array.isArray(this.circular)) {
|
|
995
|
+
res = JSON10.parse(JSON.stringify(res), this.circular);
|
|
996
|
+
}
|
|
997
|
+
return res;
|
|
998
|
+
}
|
|
999
|
+
}
|
|
1000
|
+
get text() {
|
|
1001
|
+
if (!Helpers$1.isBlob(this.responseText)) {
|
|
1002
|
+
return this.responseText.replace(/^\"/, '').replace(/\"$/, '');
|
|
1003
|
+
}
|
|
1004
|
+
}
|
|
1005
|
+
}
|
|
1006
|
+
Models.HttpBody = HttpBody;
|
|
1007
|
+
class ErrorBody extends BaseBody {
|
|
1008
|
+
constructor(data) {
|
|
1009
|
+
super();
|
|
1010
|
+
this.data = data;
|
|
1011
|
+
}
|
|
1012
|
+
get json() {
|
|
1013
|
+
return this.toJSON(this.data);
|
|
1014
|
+
}
|
|
1015
|
+
get text() {
|
|
1016
|
+
return this.data;
|
|
1017
|
+
}
|
|
1018
|
+
}
|
|
1019
|
+
Models.ErrorBody = ErrorBody;
|
|
1020
|
+
class BaseResponse {
|
|
1021
|
+
constructor(responseText, headers, statusCode, isArray = false) {
|
|
1022
|
+
this.responseText = responseText;
|
|
1023
|
+
this.headers = headers;
|
|
1024
|
+
this.statusCode = statusCode;
|
|
1025
|
+
this.isArray = isArray;
|
|
1026
|
+
}
|
|
1027
|
+
get cookies() {
|
|
1028
|
+
return BaseResponse.cookies;
|
|
1029
|
+
}
|
|
1030
|
+
}
|
|
1031
|
+
BaseResponse.cookies = Cookie.Instance;
|
|
1032
|
+
Models.BaseResponse = BaseResponse;
|
|
1033
|
+
class HttpResponse extends BaseResponse {
|
|
1034
|
+
constructor(sourceRequest, responseText, headers, statusCode, entity, circular, jobid, isArray = false) {
|
|
1035
|
+
super(responseText, headers, statusCode, isArray);
|
|
1036
|
+
this.sourceRequest = sourceRequest;
|
|
1037
|
+
this.responseText = responseText;
|
|
1038
|
+
this.headers = headers;
|
|
1039
|
+
this.statusCode = statusCode;
|
|
1040
|
+
this.entity = entity;
|
|
1041
|
+
this.circular = circular;
|
|
1042
|
+
this.jobid = jobid;
|
|
1043
|
+
this.isArray = isArray;
|
|
1044
|
+
this.init();
|
|
1045
|
+
}
|
|
1046
|
+
init() {
|
|
1047
|
+
if (typeof this.entity === 'string') {
|
|
1048
|
+
let entityJSON = this.headers.getAll(this.entity);
|
|
1049
|
+
if (!!entityJSON) {
|
|
1050
|
+
this.entity = JSON.parse(entityJSON.join());
|
|
1051
|
+
}
|
|
1052
|
+
}
|
|
1053
|
+
if (typeof this.circular === 'string') {
|
|
1054
|
+
let circuralJSON = this.headers.getAll(this.circular);
|
|
1055
|
+
if (!!circuralJSON) {
|
|
1056
|
+
this.circular = JSON.parse(circuralJSON.join());
|
|
1057
|
+
}
|
|
1058
|
+
}
|
|
1059
|
+
this.body = new HttpBody(this.responseText, this.isArray, this.entity, this.circular);
|
|
1060
|
+
}
|
|
1061
|
+
get cache() {
|
|
1062
|
+
if (_.isUndefined(this.rq)) {
|
|
1063
|
+
this.rq = new RequestCache(this);
|
|
1064
|
+
}
|
|
1065
|
+
return new RequestCache(this);
|
|
1066
|
+
}
|
|
1067
|
+
}
|
|
1068
|
+
Models.HttpResponse = HttpResponse;
|
|
1069
|
+
class HttpResponseError extends BaseResponse {
|
|
1070
|
+
constructor(message, responseText, headers, statusCode, jobid) {
|
|
1071
|
+
super(responseText, headers, statusCode);
|
|
1072
|
+
this.message = message;
|
|
1073
|
+
this.jobid = jobid;
|
|
1074
|
+
this.body = new ErrorBody(responseText);
|
|
1075
|
+
}
|
|
1076
|
+
}
|
|
1077
|
+
Models.HttpResponseError = HttpResponseError;
|
|
1078
|
+
/* */
|
|
1079
|
+
/* */
|
|
1080
|
+
/* */
|
|
1081
|
+
/* */
|
|
1082
|
+
/* */
|
|
1083
|
+
/* */
|
|
1084
|
+
})(Models || (Models = {}));
|
|
1085
|
+
;
|
|
1086
1086
|
({}); // @--end-of-file-for-module=ng2-rest lib/models.ts
|
|
1087
1087
|
|
|
1088
|
-
const log$1 = Log.create('[ng2-rest] rest-request', Level.__NOTHING);
|
|
1089
|
-
const jobIDkey = 'jobID';
|
|
1090
|
-
const customObs = 'customObs';
|
|
1091
|
-
const cancelFn = 'cancelFn';
|
|
1092
|
-
const isCanceled = 'isCanceled';
|
|
1093
|
-
class RestRequest {
|
|
1094
|
-
constructor() {
|
|
1095
|
-
this.subjectInuUse = {};
|
|
1096
|
-
this.meta = {};
|
|
1097
|
-
this.replaySubjects = {};
|
|
1098
|
-
}
|
|
1099
|
-
handlerResult(options, sourceRequest) {
|
|
1100
|
-
if (_.isUndefined(options)) {
|
|
1101
|
-
options = {};
|
|
1102
|
-
}
|
|
1103
|
-
const { res, jobid, isArray, method } = options;
|
|
1104
|
-
if (typeof res !== 'object') {
|
|
1105
|
-
throw new Error('No resposnse for request. ');
|
|
1106
|
-
}
|
|
1107
|
-
if (Helpers$1.isBrowser) {
|
|
1108
|
-
res.headers = RestHeaders.from(res.headers);
|
|
1109
|
-
}
|
|
1110
|
-
if (res.error) {
|
|
1111
|
-
this.subjectInuUse[jobid].error(new Models.HttpResponseError(res.error, res.data, res.headers, res.code, jobid));
|
|
1112
|
-
return;
|
|
1113
|
-
}
|
|
1114
|
-
const entity = this.meta[jobid].entity;
|
|
1115
|
-
const circular = this.meta[jobid].circular;
|
|
1116
|
-
const success = Resource['_listenSuccess'];
|
|
1117
|
-
const reqResp = new Models.HttpResponse(sourceRequest, res.data, res.headers, res.code, entity, circular, jobid, isArray);
|
|
1118
|
-
success.next(reqResp);
|
|
1119
|
-
this.subjectInuUse[jobid].next(reqResp);
|
|
1120
|
-
this.meta[jobid] = void 0;
|
|
1121
|
-
this.subjectInuUse[jobid].complete();
|
|
1122
|
-
}
|
|
1123
|
-
checkCache(sourceRequest, jobid) {
|
|
1124
|
-
const existedInCache = RequestCache.findBy(sourceRequest);
|
|
1125
|
-
if (existedInCache) {
|
|
1126
|
-
const success = Resource['_listenSuccess'];
|
|
1127
|
-
success.next(existedInCache.response);
|
|
1128
|
-
this.subjectInuUse[jobid].next(existedInCache);
|
|
1129
|
-
this.subjectInuUse[jobid].complete();
|
|
1130
|
-
return true;
|
|
1131
|
-
}
|
|
1132
|
-
return false;
|
|
1133
|
-
}
|
|
1134
|
-
async req(url, method, headers, body, jobid, isArray = false, mockHttp) {
|
|
1135
|
-
if (this.checkCache({
|
|
1136
|
-
url,
|
|
1137
|
-
body,
|
|
1138
|
-
isArray,
|
|
1139
|
-
method
|
|
1140
|
-
}, jobid)) {
|
|
1141
|
-
return;
|
|
1142
|
-
}
|
|
1143
|
-
const CancelToken = axios.CancelToken;
|
|
1144
|
-
const source = CancelToken.source();
|
|
1145
|
-
this.subjectInuUse[jobid][cancelFn] = source.cancel;
|
|
1146
|
-
var response;
|
|
1147
|
-
if (mockHttp) {
|
|
1148
|
-
if (typeof mockHttp === 'object') {
|
|
1149
|
-
response = {
|
|
1150
|
-
data: mockHttp.data,
|
|
1151
|
-
status: mockHttp.code,
|
|
1152
|
-
headers: mockHttp.headers,
|
|
1153
|
-
statusText: mockHttp.error,
|
|
1154
|
-
config: {}
|
|
1155
|
-
};
|
|
1156
|
-
}
|
|
1157
|
-
else if (typeof mockHttp === 'function') {
|
|
1158
|
-
const r = mockHttp(url, method, headers, body);
|
|
1159
|
-
response = {
|
|
1160
|
-
data: r.data,
|
|
1161
|
-
status: r.code,
|
|
1162
|
-
headers: r.headers,
|
|
1163
|
-
statusText: r.error,
|
|
1164
|
-
config: {}
|
|
1165
|
-
};
|
|
1166
|
-
}
|
|
1167
|
-
}
|
|
1168
|
-
const headersJson = headers.toJSON();
|
|
1169
|
-
const responseType = headersJson.responsetypeaxios ? headersJson.responsetypeaxios : 'text';
|
|
1170
|
-
try {
|
|
1171
|
-
if (!response) {
|
|
1172
|
-
response = await axios({
|
|
1173
|
-
url,
|
|
1174
|
-
method,
|
|
1175
|
-
data: body,
|
|
1176
|
-
responseType,
|
|
1177
|
-
headers: headersJson,
|
|
1178
|
-
cancelToken: source.token,
|
|
1179
|
-
});
|
|
1180
|
-
}
|
|
1181
|
-
if (this.subjectInuUse[jobid][isCanceled]) {
|
|
1182
|
-
return;
|
|
1183
|
-
}
|
|
1184
|
-
this.handlerResult({
|
|
1185
|
-
res: {
|
|
1186
|
-
code: response.status,
|
|
1187
|
-
data: response.data,
|
|
1188
|
-
isArray,
|
|
1189
|
-
jobid,
|
|
1190
|
-
headers: RestHeaders.from(response.headers)
|
|
1191
|
-
},
|
|
1192
|
-
method,
|
|
1193
|
-
jobid,
|
|
1194
|
-
isArray
|
|
1195
|
-
}, {
|
|
1196
|
-
url,
|
|
1197
|
-
body,
|
|
1198
|
-
method,
|
|
1199
|
-
isArray,
|
|
1200
|
-
});
|
|
1201
|
-
}
|
|
1202
|
-
catch (catchedError) {
|
|
1203
|
-
if (this.subjectInuUse[jobid][isCanceled]) {
|
|
1204
|
-
return;
|
|
1205
|
-
}
|
|
1206
|
-
if (typeof catchedError === 'object' && catchedError.response && catchedError.response.data) {
|
|
1207
|
-
const err = catchedError.response.data;
|
|
1208
|
-
const msg = catchedError.response.data.message || '';
|
|
1209
|
-
let stack = (err.stack || '').split('\n');
|
|
1210
|
-
const errObs = Resource['_listenErrors'];
|
|
1211
|
-
errObs.next({
|
|
1212
|
-
msg,
|
|
1213
|
-
stack,
|
|
1214
|
-
data: catchedError.response.data
|
|
1215
|
-
});
|
|
1216
|
-
}
|
|
1217
|
-
const error = (catchedError && catchedError.response) ? `[${catchedError.response.statusText}]: ` : '';
|
|
1218
|
-
this.handlerResult({
|
|
1219
|
-
res: {
|
|
1220
|
-
code: (catchedError && catchedError.response) ? catchedError.response.status : void 0,
|
|
1221
|
-
error: `${error}${catchedError.message}`,
|
|
1222
|
-
data: (catchedError && catchedError.response) ? JSON.stringify(catchedError.response.data) : void 0,
|
|
1223
|
-
isArray,
|
|
1224
|
-
jobid,
|
|
1225
|
-
headers: RestHeaders.from(catchedError && catchedError.response && catchedError.response.headers)
|
|
1226
|
-
},
|
|
1227
|
-
method,
|
|
1228
|
-
jobid,
|
|
1229
|
-
isArray
|
|
1230
|
-
}, {
|
|
1231
|
-
url,
|
|
1232
|
-
body,
|
|
1233
|
-
isArray,
|
|
1234
|
-
method
|
|
1235
|
-
});
|
|
1236
|
-
}
|
|
1237
|
-
}
|
|
1238
|
-
getReplay(method, meta, onlyGetLastReplayForMethod) {
|
|
1239
|
-
let replay;
|
|
1240
|
-
if (_.isUndefined(this.replaySubjects[meta.endpoint])) {
|
|
1241
|
-
this.replaySubjects[meta.endpoint] = {};
|
|
1242
|
-
}
|
|
1243
|
-
if (_.isUndefined(this.replaySubjects[meta.endpoint][meta.path])) {
|
|
1244
|
-
this.replaySubjects[meta.endpoint][meta.path] = {};
|
|
1245
|
-
}
|
|
1246
|
-
if (_.isUndefined(this.replaySubjects[meta.endpoint][meta.path][method])) {
|
|
1247
|
-
this.replaySubjects[meta.endpoint][meta.path][method] = {};
|
|
1248
|
-
}
|
|
1249
|
-
const objectIDToCreateOrLast = (Object.keys(this.replaySubjects[meta.endpoint][meta.path][method]).length) +
|
|
1250
|
-
(onlyGetLastReplayForMethod ? 0 : 1);
|
|
1251
|
-
if (onlyGetLastReplayForMethod && (objectIDToCreateOrLast === 0)) {
|
|
1252
|
-
return replay;
|
|
1253
|
-
}
|
|
1254
|
-
if (_.isUndefined(this.replaySubjects[meta.endpoint][meta.path][method][objectIDToCreateOrLast])) {
|
|
1255
|
-
this.replaySubjects[meta.endpoint][meta.path][method][objectIDToCreateOrLast] = {
|
|
1256
|
-
subject: new Subject(),
|
|
1257
|
-
data: void 0,
|
|
1258
|
-
};
|
|
1259
|
-
}
|
|
1260
|
-
replay = this.replaySubjects[meta.endpoint][meta.path][method][objectIDToCreateOrLast];
|
|
1261
|
-
if (!_.isNumber(replay.id)) {
|
|
1262
|
-
if (RestRequest.jobId === Number.MAX_SAFE_INTEGER) {
|
|
1263
|
-
RestRequest.jobId = 0;
|
|
1264
|
-
}
|
|
1265
|
-
const jobid = RestRequest.jobId++;
|
|
1266
|
-
replay.id = jobid;
|
|
1267
|
-
const subject = replay.subject;
|
|
1268
|
-
subject[jobIDkey] = jobid; // modify internal rxjs subject obj
|
|
1269
|
-
this.meta[jobid] = meta;
|
|
1270
|
-
this.subjectInuUse[jobid] = subject;
|
|
1271
|
-
this.subjectInuUse[jobid][customObs] = new Observable((observer) => {
|
|
1272
|
-
observer.add(() => {
|
|
1273
|
-
if (!this.subjectInuUse[jobid][isCanceled]) {
|
|
1274
|
-
this.subjectInuUse[jobid][isCanceled] = true;
|
|
1275
|
-
if (typeof this.subjectInuUse[jobid][cancelFn] === 'function') {
|
|
1276
|
-
this.subjectInuUse[jobid][cancelFn]('[ng2-rest] on purpose canceled http request');
|
|
1277
|
-
}
|
|
1278
|
-
}
|
|
1279
|
-
else {
|
|
1280
|
-
}
|
|
1281
|
-
});
|
|
1282
|
-
const sub = subject.subscribe({
|
|
1283
|
-
next: a => observer.next(a),
|
|
1284
|
-
error: a => observer.error(a),
|
|
1285
|
-
complete: () => {
|
|
1286
|
-
sub.unsubscribe();
|
|
1287
|
-
observer.complete();
|
|
1288
|
-
},
|
|
1289
|
-
});
|
|
1290
|
-
});
|
|
1291
|
-
}
|
|
1292
|
-
return replay;
|
|
1293
|
-
}
|
|
1294
|
-
generalReq(method, url, body, headers, meta, isArray, mockHttp) {
|
|
1295
|
-
const replay = this.getReplay(method, meta, false);
|
|
1296
|
-
replay.data = { url, body, headers, isArray };
|
|
1297
|
-
((pthis, purl, pmethod, pheaders, pbody, pid, pisArray, pmockHttp) => {
|
|
1298
|
-
setTimeout(() => pthis.req(purl, pmethod, pheaders, pbody, pid, pisArray, pmockHttp));
|
|
1299
|
-
})(this, url, method, headers, body, replay.id, isArray, mockHttp);
|
|
1300
|
-
const resp = firstValueFrom(replay.subject[customObs]);
|
|
1301
|
-
resp.observable = replay.subject[customObs];
|
|
1302
|
-
resp.cache = RequestCache.findBy({
|
|
1303
|
-
body,
|
|
1304
|
-
isArray,
|
|
1305
|
-
method,
|
|
1306
|
-
url
|
|
1307
|
-
});
|
|
1308
|
-
return resp;
|
|
1309
|
-
}
|
|
1310
|
-
get(url, body, headers, meta, isArray, mockHttp) {
|
|
1311
|
-
return this.generalReq('get', url, body, headers, meta, isArray, mockHttp);
|
|
1312
|
-
}
|
|
1313
|
-
head(url, body, headers, meta, isArray, mockHttp) {
|
|
1314
|
-
return this.generalReq('head', url, body, headers, meta, isArray, mockHttp);
|
|
1315
|
-
}
|
|
1316
|
-
delete(url, body, headers, meta, isArray, mockHttp) {
|
|
1317
|
-
return this.generalReq('delete', url, body, headers, meta, isArray, mockHttp);
|
|
1318
|
-
}
|
|
1319
|
-
post(url, body, headers, meta, isArray, mockHttp) {
|
|
1320
|
-
return this.generalReq('post', url, body, headers, meta, isArray, mockHttp);
|
|
1321
|
-
}
|
|
1322
|
-
put(url, body, headers, meta, isArray, mockHttp) {
|
|
1323
|
-
return this.generalReq('put', url, body, headers, meta, isArray, mockHttp);
|
|
1324
|
-
}
|
|
1325
|
-
patch(url, body, headers, meta, isArray, mockHttp) {
|
|
1326
|
-
return this.generalReq('patch', url, body, headers, meta, isArray, mockHttp);
|
|
1327
|
-
}
|
|
1328
|
-
jsonp(url, body, headers, meta, isArray, mockHttp) {
|
|
1329
|
-
const replay = this.getReplay('jsonp', meta, false);
|
|
1330
|
-
const jobid = replay.id;
|
|
1331
|
-
const method = 'jsonp';
|
|
1332
|
-
setTimeout(() => {
|
|
1333
|
-
if (url.endsWith('/'))
|
|
1334
|
-
url = url.slice(0, url.length - 1);
|
|
1335
|
-
let num = Math.round(10000 * Math.random());
|
|
1336
|
-
let callbackMethodName = "cb_" + num;
|
|
1337
|
-
window[callbackMethodName] = (data) => {
|
|
1338
|
-
if (this.checkCache({
|
|
1339
|
-
url,
|
|
1340
|
-
body,
|
|
1341
|
-
isArray,
|
|
1342
|
-
method
|
|
1343
|
-
}, jobid)) {
|
|
1344
|
-
return;
|
|
1345
|
-
}
|
|
1346
|
-
this.handlerResult({
|
|
1347
|
-
res: {
|
|
1348
|
-
data, isArray
|
|
1349
|
-
},
|
|
1350
|
-
method,
|
|
1351
|
-
jobid,
|
|
1352
|
-
isArray
|
|
1353
|
-
}, {
|
|
1354
|
-
url,
|
|
1355
|
-
body,
|
|
1356
|
-
isArray,
|
|
1357
|
-
method,
|
|
1358
|
-
});
|
|
1359
|
-
};
|
|
1360
|
-
let sc = document.createElement('script');
|
|
1361
|
-
sc.src = `${url}?callback=${callbackMethodName}`;
|
|
1362
|
-
document.body.appendChild(sc);
|
|
1363
|
-
document.body.removeChild(sc);
|
|
1364
|
-
});
|
|
1365
|
-
const resp = firstValueFrom(replay.subject[customObs]);
|
|
1366
|
-
resp.observable = replay.subject[customObs];
|
|
1367
|
-
console.log('assiging custom observable');
|
|
1368
|
-
resp.cache = RequestCache.findBy({
|
|
1369
|
-
body,
|
|
1370
|
-
isArray,
|
|
1371
|
-
method,
|
|
1372
|
-
url
|
|
1373
|
-
});
|
|
1374
|
-
return resp;
|
|
1375
|
-
}
|
|
1376
|
-
replay(method, meta) {
|
|
1377
|
-
const replay = this.getReplay(method, meta, true);
|
|
1378
|
-
if (!replay || !replay.data) {
|
|
1379
|
-
console.warn(`Canno replay first ${method} request from ${meta.endpoint}/${meta.path}`);
|
|
1380
|
-
return;
|
|
1381
|
-
}
|
|
1382
|
-
;
|
|
1383
|
-
if (replay && replay.subject && Array.isArray(replay.subject.observers) &&
|
|
1384
|
-
replay.subject.observers.length === 0) {
|
|
1385
|
-
console.warn(`No observators for ${method} request from ${meta.endpoint}/${meta.path}`);
|
|
1386
|
-
return;
|
|
1387
|
-
}
|
|
1388
|
-
const url = replay.data.url;
|
|
1389
|
-
const headers = replay.data.headers;
|
|
1390
|
-
const body = replay.data.body;
|
|
1391
|
-
const isArray = replay.data.isArray;
|
|
1392
|
-
setTimeout(() => this.req(url, method, headers, body, replay.id, isArray));
|
|
1393
|
-
}
|
|
1394
|
-
}
|
|
1395
|
-
RestRequest.jobId = 0;
|
|
1396
|
-
;
|
|
1088
|
+
const log$1 = Log.create('[ng2-rest] rest-request', Level.__NOTHING);
|
|
1089
|
+
const jobIDkey = 'jobID';
|
|
1090
|
+
const customObs = 'customObs';
|
|
1091
|
+
const cancelFn = 'cancelFn';
|
|
1092
|
+
const isCanceled = 'isCanceled';
|
|
1093
|
+
class RestRequest {
|
|
1094
|
+
constructor() {
|
|
1095
|
+
this.subjectInuUse = {};
|
|
1096
|
+
this.meta = {};
|
|
1097
|
+
this.replaySubjects = {};
|
|
1098
|
+
}
|
|
1099
|
+
handlerResult(options, sourceRequest) {
|
|
1100
|
+
if (_.isUndefined(options)) {
|
|
1101
|
+
options = {};
|
|
1102
|
+
}
|
|
1103
|
+
const { res, jobid, isArray, method } = options;
|
|
1104
|
+
if (typeof res !== 'object') {
|
|
1105
|
+
throw new Error('No resposnse for request. ');
|
|
1106
|
+
}
|
|
1107
|
+
if (Helpers$1.isBrowser) {
|
|
1108
|
+
res.headers = RestHeaders.from(res.headers);
|
|
1109
|
+
}
|
|
1110
|
+
if (res.error) {
|
|
1111
|
+
this.subjectInuUse[jobid].error(new Models.HttpResponseError(res.error, res.data, res.headers, res.code, jobid));
|
|
1112
|
+
return;
|
|
1113
|
+
}
|
|
1114
|
+
const entity = this.meta[jobid].entity;
|
|
1115
|
+
const circular = this.meta[jobid].circular;
|
|
1116
|
+
const success = Resource['_listenSuccess'];
|
|
1117
|
+
const reqResp = new Models.HttpResponse(sourceRequest, res.data, res.headers, res.code, entity, circular, jobid, isArray);
|
|
1118
|
+
success.next(reqResp);
|
|
1119
|
+
this.subjectInuUse[jobid].next(reqResp);
|
|
1120
|
+
this.meta[jobid] = void 0;
|
|
1121
|
+
this.subjectInuUse[jobid].complete();
|
|
1122
|
+
}
|
|
1123
|
+
checkCache(sourceRequest, jobid) {
|
|
1124
|
+
const existedInCache = RequestCache.findBy(sourceRequest);
|
|
1125
|
+
if (existedInCache) {
|
|
1126
|
+
const success = Resource['_listenSuccess'];
|
|
1127
|
+
success.next(existedInCache.response);
|
|
1128
|
+
this.subjectInuUse[jobid].next(existedInCache);
|
|
1129
|
+
this.subjectInuUse[jobid].complete();
|
|
1130
|
+
return true;
|
|
1131
|
+
}
|
|
1132
|
+
return false;
|
|
1133
|
+
}
|
|
1134
|
+
async req(url, method, headers, body, jobid, isArray = false, mockHttp) {
|
|
1135
|
+
if (this.checkCache({
|
|
1136
|
+
url,
|
|
1137
|
+
body,
|
|
1138
|
+
isArray,
|
|
1139
|
+
method
|
|
1140
|
+
}, jobid)) {
|
|
1141
|
+
return;
|
|
1142
|
+
}
|
|
1143
|
+
const CancelToken = axios.CancelToken;
|
|
1144
|
+
const source = CancelToken.source();
|
|
1145
|
+
this.subjectInuUse[jobid][cancelFn] = source.cancel;
|
|
1146
|
+
var response;
|
|
1147
|
+
if (mockHttp) {
|
|
1148
|
+
if (typeof mockHttp === 'object') {
|
|
1149
|
+
response = {
|
|
1150
|
+
data: mockHttp.data,
|
|
1151
|
+
status: mockHttp.code,
|
|
1152
|
+
headers: mockHttp.headers,
|
|
1153
|
+
statusText: mockHttp.error,
|
|
1154
|
+
config: {}
|
|
1155
|
+
};
|
|
1156
|
+
}
|
|
1157
|
+
else if (typeof mockHttp === 'function') {
|
|
1158
|
+
const r = mockHttp(url, method, headers, body);
|
|
1159
|
+
response = {
|
|
1160
|
+
data: r.data,
|
|
1161
|
+
status: r.code,
|
|
1162
|
+
headers: r.headers,
|
|
1163
|
+
statusText: r.error,
|
|
1164
|
+
config: {}
|
|
1165
|
+
};
|
|
1166
|
+
}
|
|
1167
|
+
}
|
|
1168
|
+
const headersJson = headers.toJSON();
|
|
1169
|
+
const responseType = headersJson.responsetypeaxios ? headersJson.responsetypeaxios : 'text';
|
|
1170
|
+
try {
|
|
1171
|
+
if (!response) {
|
|
1172
|
+
response = await axios({
|
|
1173
|
+
url,
|
|
1174
|
+
method,
|
|
1175
|
+
data: body,
|
|
1176
|
+
responseType,
|
|
1177
|
+
headers: headersJson,
|
|
1178
|
+
cancelToken: source.token,
|
|
1179
|
+
});
|
|
1180
|
+
}
|
|
1181
|
+
if (this.subjectInuUse[jobid][isCanceled]) {
|
|
1182
|
+
return;
|
|
1183
|
+
}
|
|
1184
|
+
this.handlerResult({
|
|
1185
|
+
res: {
|
|
1186
|
+
code: response.status,
|
|
1187
|
+
data: response.data,
|
|
1188
|
+
isArray,
|
|
1189
|
+
jobid,
|
|
1190
|
+
headers: RestHeaders.from(response.headers)
|
|
1191
|
+
},
|
|
1192
|
+
method,
|
|
1193
|
+
jobid,
|
|
1194
|
+
isArray
|
|
1195
|
+
}, {
|
|
1196
|
+
url,
|
|
1197
|
+
body,
|
|
1198
|
+
method,
|
|
1199
|
+
isArray,
|
|
1200
|
+
});
|
|
1201
|
+
}
|
|
1202
|
+
catch (catchedError) {
|
|
1203
|
+
if (this.subjectInuUse[jobid][isCanceled]) {
|
|
1204
|
+
return;
|
|
1205
|
+
}
|
|
1206
|
+
if (typeof catchedError === 'object' && catchedError.response && catchedError.response.data) {
|
|
1207
|
+
const err = catchedError.response.data;
|
|
1208
|
+
const msg = catchedError.response.data.message || '';
|
|
1209
|
+
let stack = (err.stack || '').split('\n');
|
|
1210
|
+
const errObs = Resource['_listenErrors'];
|
|
1211
|
+
errObs.next({
|
|
1212
|
+
msg,
|
|
1213
|
+
stack,
|
|
1214
|
+
data: catchedError.response.data
|
|
1215
|
+
});
|
|
1216
|
+
}
|
|
1217
|
+
const error = (catchedError && catchedError.response) ? `[${catchedError.response.statusText}]: ` : '';
|
|
1218
|
+
this.handlerResult({
|
|
1219
|
+
res: {
|
|
1220
|
+
code: (catchedError && catchedError.response) ? catchedError.response.status : void 0,
|
|
1221
|
+
error: `${error}${catchedError.message}`,
|
|
1222
|
+
data: (catchedError && catchedError.response) ? JSON.stringify(catchedError.response.data) : void 0,
|
|
1223
|
+
isArray,
|
|
1224
|
+
jobid,
|
|
1225
|
+
headers: RestHeaders.from(catchedError && catchedError.response && catchedError.response.headers)
|
|
1226
|
+
},
|
|
1227
|
+
method,
|
|
1228
|
+
jobid,
|
|
1229
|
+
isArray
|
|
1230
|
+
}, {
|
|
1231
|
+
url,
|
|
1232
|
+
body,
|
|
1233
|
+
isArray,
|
|
1234
|
+
method
|
|
1235
|
+
});
|
|
1236
|
+
}
|
|
1237
|
+
}
|
|
1238
|
+
getReplay(method, meta, onlyGetLastReplayForMethod) {
|
|
1239
|
+
let replay;
|
|
1240
|
+
if (_.isUndefined(this.replaySubjects[meta.endpoint])) {
|
|
1241
|
+
this.replaySubjects[meta.endpoint] = {};
|
|
1242
|
+
}
|
|
1243
|
+
if (_.isUndefined(this.replaySubjects[meta.endpoint][meta.path])) {
|
|
1244
|
+
this.replaySubjects[meta.endpoint][meta.path] = {};
|
|
1245
|
+
}
|
|
1246
|
+
if (_.isUndefined(this.replaySubjects[meta.endpoint][meta.path][method])) {
|
|
1247
|
+
this.replaySubjects[meta.endpoint][meta.path][method] = {};
|
|
1248
|
+
}
|
|
1249
|
+
const objectIDToCreateOrLast = (Object.keys(this.replaySubjects[meta.endpoint][meta.path][method]).length) +
|
|
1250
|
+
(onlyGetLastReplayForMethod ? 0 : 1);
|
|
1251
|
+
if (onlyGetLastReplayForMethod && (objectIDToCreateOrLast === 0)) {
|
|
1252
|
+
return replay;
|
|
1253
|
+
}
|
|
1254
|
+
if (_.isUndefined(this.replaySubjects[meta.endpoint][meta.path][method][objectIDToCreateOrLast])) {
|
|
1255
|
+
this.replaySubjects[meta.endpoint][meta.path][method][objectIDToCreateOrLast] = {
|
|
1256
|
+
subject: new Subject(),
|
|
1257
|
+
data: void 0,
|
|
1258
|
+
};
|
|
1259
|
+
}
|
|
1260
|
+
replay = this.replaySubjects[meta.endpoint][meta.path][method][objectIDToCreateOrLast];
|
|
1261
|
+
if (!_.isNumber(replay.id)) {
|
|
1262
|
+
if (RestRequest.jobId === Number.MAX_SAFE_INTEGER) {
|
|
1263
|
+
RestRequest.jobId = 0;
|
|
1264
|
+
}
|
|
1265
|
+
const jobid = RestRequest.jobId++;
|
|
1266
|
+
replay.id = jobid;
|
|
1267
|
+
const subject = replay.subject;
|
|
1268
|
+
subject[jobIDkey] = jobid; // modify internal rxjs subject obj
|
|
1269
|
+
this.meta[jobid] = meta;
|
|
1270
|
+
this.subjectInuUse[jobid] = subject;
|
|
1271
|
+
this.subjectInuUse[jobid][customObs] = new Observable((observer) => {
|
|
1272
|
+
observer.add(() => {
|
|
1273
|
+
if (!this.subjectInuUse[jobid][isCanceled]) {
|
|
1274
|
+
this.subjectInuUse[jobid][isCanceled] = true;
|
|
1275
|
+
if (typeof this.subjectInuUse[jobid][cancelFn] === 'function') {
|
|
1276
|
+
this.subjectInuUse[jobid][cancelFn]('[ng2-rest] on purpose canceled http request');
|
|
1277
|
+
}
|
|
1278
|
+
}
|
|
1279
|
+
else {
|
|
1280
|
+
}
|
|
1281
|
+
});
|
|
1282
|
+
const sub = subject.subscribe({
|
|
1283
|
+
next: a => observer.next(a),
|
|
1284
|
+
error: a => observer.error(a),
|
|
1285
|
+
complete: () => {
|
|
1286
|
+
sub.unsubscribe();
|
|
1287
|
+
observer.complete();
|
|
1288
|
+
},
|
|
1289
|
+
});
|
|
1290
|
+
});
|
|
1291
|
+
}
|
|
1292
|
+
return replay;
|
|
1293
|
+
}
|
|
1294
|
+
generalReq(method, url, body, headers, meta, isArray, mockHttp) {
|
|
1295
|
+
const replay = this.getReplay(method, meta, false);
|
|
1296
|
+
replay.data = { url, body, headers, isArray };
|
|
1297
|
+
((pthis, purl, pmethod, pheaders, pbody, pid, pisArray, pmockHttp) => {
|
|
1298
|
+
setTimeout(() => pthis.req(purl, pmethod, pheaders, pbody, pid, pisArray, pmockHttp));
|
|
1299
|
+
})(this, url, method, headers, body, replay.id, isArray, mockHttp);
|
|
1300
|
+
const resp = firstValueFrom(replay.subject[customObs]);
|
|
1301
|
+
resp.observable = replay.subject[customObs];
|
|
1302
|
+
resp.cache = RequestCache.findBy({
|
|
1303
|
+
body,
|
|
1304
|
+
isArray,
|
|
1305
|
+
method,
|
|
1306
|
+
url
|
|
1307
|
+
});
|
|
1308
|
+
return resp;
|
|
1309
|
+
}
|
|
1310
|
+
get(url, body, headers, meta, isArray, mockHttp) {
|
|
1311
|
+
return this.generalReq('get', url, body, headers, meta, isArray, mockHttp);
|
|
1312
|
+
}
|
|
1313
|
+
head(url, body, headers, meta, isArray, mockHttp) {
|
|
1314
|
+
return this.generalReq('head', url, body, headers, meta, isArray, mockHttp);
|
|
1315
|
+
}
|
|
1316
|
+
delete(url, body, headers, meta, isArray, mockHttp) {
|
|
1317
|
+
return this.generalReq('delete', url, body, headers, meta, isArray, mockHttp);
|
|
1318
|
+
}
|
|
1319
|
+
post(url, body, headers, meta, isArray, mockHttp) {
|
|
1320
|
+
return this.generalReq('post', url, body, headers, meta, isArray, mockHttp);
|
|
1321
|
+
}
|
|
1322
|
+
put(url, body, headers, meta, isArray, mockHttp) {
|
|
1323
|
+
return this.generalReq('put', url, body, headers, meta, isArray, mockHttp);
|
|
1324
|
+
}
|
|
1325
|
+
patch(url, body, headers, meta, isArray, mockHttp) {
|
|
1326
|
+
return this.generalReq('patch', url, body, headers, meta, isArray, mockHttp);
|
|
1327
|
+
}
|
|
1328
|
+
jsonp(url, body, headers, meta, isArray, mockHttp) {
|
|
1329
|
+
const replay = this.getReplay('jsonp', meta, false);
|
|
1330
|
+
const jobid = replay.id;
|
|
1331
|
+
const method = 'jsonp';
|
|
1332
|
+
setTimeout(() => {
|
|
1333
|
+
if (url.endsWith('/'))
|
|
1334
|
+
url = url.slice(0, url.length - 1);
|
|
1335
|
+
let num = Math.round(10000 * Math.random());
|
|
1336
|
+
let callbackMethodName = "cb_" + num;
|
|
1337
|
+
window[callbackMethodName] = (data) => {
|
|
1338
|
+
if (this.checkCache({
|
|
1339
|
+
url,
|
|
1340
|
+
body,
|
|
1341
|
+
isArray,
|
|
1342
|
+
method
|
|
1343
|
+
}, jobid)) {
|
|
1344
|
+
return;
|
|
1345
|
+
}
|
|
1346
|
+
this.handlerResult({
|
|
1347
|
+
res: {
|
|
1348
|
+
data, isArray
|
|
1349
|
+
},
|
|
1350
|
+
method,
|
|
1351
|
+
jobid,
|
|
1352
|
+
isArray
|
|
1353
|
+
}, {
|
|
1354
|
+
url,
|
|
1355
|
+
body,
|
|
1356
|
+
isArray,
|
|
1357
|
+
method,
|
|
1358
|
+
});
|
|
1359
|
+
};
|
|
1360
|
+
let sc = document.createElement('script');
|
|
1361
|
+
sc.src = `${url}?callback=${callbackMethodName}`;
|
|
1362
|
+
document.body.appendChild(sc);
|
|
1363
|
+
document.body.removeChild(sc);
|
|
1364
|
+
});
|
|
1365
|
+
const resp = firstValueFrom(replay.subject[customObs]);
|
|
1366
|
+
resp.observable = replay.subject[customObs];
|
|
1367
|
+
console.log('assiging custom observable');
|
|
1368
|
+
resp.cache = RequestCache.findBy({
|
|
1369
|
+
body,
|
|
1370
|
+
isArray,
|
|
1371
|
+
method,
|
|
1372
|
+
url
|
|
1373
|
+
});
|
|
1374
|
+
return resp;
|
|
1375
|
+
}
|
|
1376
|
+
replay(method, meta) {
|
|
1377
|
+
const replay = this.getReplay(method, meta, true);
|
|
1378
|
+
if (!replay || !replay.data) {
|
|
1379
|
+
console.warn(`Canno replay first ${method} request from ${meta.endpoint}/${meta.path}`);
|
|
1380
|
+
return;
|
|
1381
|
+
}
|
|
1382
|
+
;
|
|
1383
|
+
if (replay && replay.subject && Array.isArray(replay.subject.observers) &&
|
|
1384
|
+
replay.subject.observers.length === 0) {
|
|
1385
|
+
console.warn(`No observators for ${method} request from ${meta.endpoint}/${meta.path}`);
|
|
1386
|
+
return;
|
|
1387
|
+
}
|
|
1388
|
+
const url = replay.data.url;
|
|
1389
|
+
const headers = replay.data.headers;
|
|
1390
|
+
const body = replay.data.body;
|
|
1391
|
+
const isArray = replay.data.isArray;
|
|
1392
|
+
setTimeout(() => this.req(url, method, headers, body, replay.id, isArray));
|
|
1393
|
+
}
|
|
1394
|
+
}
|
|
1395
|
+
RestRequest.jobId = 0;
|
|
1396
|
+
;
|
|
1397
1397
|
({}); // @--end-of-file-for-module=ng2-rest lib/rest-request.ts
|
|
1398
1398
|
|
|
1399
|
-
const log = Log.create('resouce-service', Level.__NOTHING);
|
|
1400
|
-
class Resource {
|
|
1401
|
-
constructor() {
|
|
1402
|
-
setTimeout(() => {
|
|
1403
|
-
const zone = this.getZone();
|
|
1404
|
-
if (!RestRequest.zone) {
|
|
1405
|
-
RestRequest.zone = zone;
|
|
1406
|
-
}
|
|
1407
|
-
;
|
|
1408
|
-
});
|
|
1409
|
-
}
|
|
1410
|
-
static get listenErrors() {
|
|
1411
|
-
return this._listenErrors.asObservable();
|
|
1412
|
-
}
|
|
1413
|
-
static get listenSuccessOperations() {
|
|
1414
|
-
return this._listenSuccess.asObservable();
|
|
1415
|
-
}
|
|
1416
|
-
getZone() {
|
|
1417
|
-
const isNode = (typeof window === 'undefined');
|
|
1418
|
-
if (isNode) {
|
|
1419
|
-
return;
|
|
1420
|
-
}
|
|
1421
|
-
;
|
|
1422
|
-
const ng = window['ng'];
|
|
1423
|
-
const getAllAngularRootElements = window['getAllAngularRootElements'];
|
|
1424
|
-
if (!ng || !getAllAngularRootElements) {
|
|
1425
|
-
return;
|
|
1426
|
-
}
|
|
1427
|
-
const probe = ng.probe;
|
|
1428
|
-
const coreTokens = ng.coreTokens;
|
|
1429
|
-
if (!coreTokens || !coreTokens.NgZone) {
|
|
1430
|
-
return;
|
|
1431
|
-
}
|
|
1432
|
-
const zoneClass = coreTokens.NgZone;
|
|
1433
|
-
if (!probe || typeof probe !== 'function' || !getAllAngularRootElements) {
|
|
1434
|
-
return;
|
|
1435
|
-
}
|
|
1436
|
-
const angularElements = getAllAngularRootElements();
|
|
1437
|
-
if (!Array.isArray(angularElements) || angularElements.length === 0) {
|
|
1438
|
-
return;
|
|
1439
|
-
}
|
|
1440
|
-
const rootElement = ng.probe(angularElements[0]);
|
|
1441
|
-
if (!rootElement) {
|
|
1442
|
-
return;
|
|
1443
|
-
}
|
|
1444
|
-
const injector = rootElement.injector;
|
|
1445
|
-
if (!injector || !injector.get || typeof injector.get !== 'function') {
|
|
1446
|
-
return;
|
|
1447
|
-
}
|
|
1448
|
-
const zone = injector.get(zoneClass);
|
|
1449
|
-
return zone;
|
|
1450
|
-
}
|
|
1451
|
-
static initAngularNgZone(zone) {
|
|
1452
|
-
RestRequest.zone = zone;
|
|
1453
|
-
}
|
|
1454
|
-
checkNestedModels(model, allModels) {
|
|
1455
|
-
for (let p in allModels) {
|
|
1456
|
-
if (allModels.hasOwnProperty(p)) {
|
|
1457
|
-
let m = allModels[p];
|
|
1458
|
-
if (isValid(p)) {
|
|
1459
|
-
let urlModels = getModels(p);
|
|
1460
|
-
if (containsModels(model, urlModels)) {
|
|
1461
|
-
model = p;
|
|
1462
|
-
break;
|
|
1463
|
-
}
|
|
1464
|
-
}
|
|
1465
|
-
}
|
|
1466
|
-
}
|
|
1467
|
-
return model;
|
|
1468
|
-
}
|
|
1469
|
-
static getModel(endpoint, model) {
|
|
1470
|
-
model = Resource.prepareModel(model);
|
|
1471
|
-
const e = Resource.endpoints[endpoint];
|
|
1472
|
-
if (!e) {
|
|
1473
|
-
return void 0;
|
|
1474
|
-
}
|
|
1475
|
-
const r = Resource.endpoints[endpoint].models[model];
|
|
1476
|
-
return Resource.endpoints[endpoint].models[model];
|
|
1477
|
-
}
|
|
1478
|
-
static create(e, model, entityMapping, circular, customContentType) {
|
|
1479
|
-
const badRestRegEX = new RegExp('((\/:)[a-z]+)+', 'g');
|
|
1480
|
-
const matchArr = model.match(badRestRegEX) || [];
|
|
1481
|
-
const badModelsNextToEachOther = matchArr.join();
|
|
1482
|
-
const atleas2DoubleDots = ((badModelsNextToEachOther.match(new RegExp(':', 'g')) || []).length >= 2);
|
|
1483
|
-
if (atleas2DoubleDots && model.search(badModelsNextToEachOther) !== -1) {
|
|
1399
|
+
const log = Log.create('resouce-service', Level.__NOTHING);
|
|
1400
|
+
class Resource {
|
|
1401
|
+
constructor() {
|
|
1402
|
+
setTimeout(() => {
|
|
1403
|
+
const zone = this.getZone();
|
|
1404
|
+
if (!RestRequest.zone) {
|
|
1405
|
+
RestRequest.zone = zone;
|
|
1406
|
+
}
|
|
1407
|
+
;
|
|
1408
|
+
});
|
|
1409
|
+
}
|
|
1410
|
+
static get listenErrors() {
|
|
1411
|
+
return this._listenErrors.asObservable();
|
|
1412
|
+
}
|
|
1413
|
+
static get listenSuccessOperations() {
|
|
1414
|
+
return this._listenSuccess.asObservable();
|
|
1415
|
+
}
|
|
1416
|
+
getZone() {
|
|
1417
|
+
const isNode = (typeof window === 'undefined');
|
|
1418
|
+
if (isNode) {
|
|
1419
|
+
return;
|
|
1420
|
+
}
|
|
1421
|
+
;
|
|
1422
|
+
const ng = window['ng'];
|
|
1423
|
+
const getAllAngularRootElements = window['getAllAngularRootElements'];
|
|
1424
|
+
if (!ng || !getAllAngularRootElements) {
|
|
1425
|
+
return;
|
|
1426
|
+
}
|
|
1427
|
+
const probe = ng.probe;
|
|
1428
|
+
const coreTokens = ng.coreTokens;
|
|
1429
|
+
if (!coreTokens || !coreTokens.NgZone) {
|
|
1430
|
+
return;
|
|
1431
|
+
}
|
|
1432
|
+
const zoneClass = coreTokens.NgZone;
|
|
1433
|
+
if (!probe || typeof probe !== 'function' || !getAllAngularRootElements) {
|
|
1434
|
+
return;
|
|
1435
|
+
}
|
|
1436
|
+
const angularElements = getAllAngularRootElements();
|
|
1437
|
+
if (!Array.isArray(angularElements) || angularElements.length === 0) {
|
|
1438
|
+
return;
|
|
1439
|
+
}
|
|
1440
|
+
const rootElement = ng.probe(angularElements[0]);
|
|
1441
|
+
if (!rootElement) {
|
|
1442
|
+
return;
|
|
1443
|
+
}
|
|
1444
|
+
const injector = rootElement.injector;
|
|
1445
|
+
if (!injector || !injector.get || typeof injector.get !== 'function') {
|
|
1446
|
+
return;
|
|
1447
|
+
}
|
|
1448
|
+
const zone = injector.get(zoneClass);
|
|
1449
|
+
return zone;
|
|
1450
|
+
}
|
|
1451
|
+
static initAngularNgZone(zone) {
|
|
1452
|
+
RestRequest.zone = zone;
|
|
1453
|
+
}
|
|
1454
|
+
checkNestedModels(model, allModels) {
|
|
1455
|
+
for (let p in allModels) {
|
|
1456
|
+
if (allModels.hasOwnProperty(p)) {
|
|
1457
|
+
let m = allModels[p];
|
|
1458
|
+
if (isValid(p)) {
|
|
1459
|
+
let urlModels = getModels(p);
|
|
1460
|
+
if (containsModels(model, urlModels)) {
|
|
1461
|
+
model = p;
|
|
1462
|
+
break;
|
|
1463
|
+
}
|
|
1464
|
+
}
|
|
1465
|
+
}
|
|
1466
|
+
}
|
|
1467
|
+
return model;
|
|
1468
|
+
}
|
|
1469
|
+
static getModel(endpoint, model) {
|
|
1470
|
+
model = Resource.prepareModel(model);
|
|
1471
|
+
const e = Resource.endpoints[endpoint];
|
|
1472
|
+
if (!e) {
|
|
1473
|
+
return void 0;
|
|
1474
|
+
}
|
|
1475
|
+
const r = Resource.endpoints[endpoint].models[model];
|
|
1476
|
+
return Resource.endpoints[endpoint].models[model];
|
|
1477
|
+
}
|
|
1478
|
+
static create(e, model, entityMapping, circular, customContentType) {
|
|
1479
|
+
const badRestRegEX = new RegExp('((\/:)[a-z]+)+', 'g');
|
|
1480
|
+
const matchArr = model.match(badRestRegEX) || [];
|
|
1481
|
+
const badModelsNextToEachOther = matchArr.join();
|
|
1482
|
+
const atleas2DoubleDots = ((badModelsNextToEachOther.match(new RegExp(':', 'g')) || []).length >= 2);
|
|
1483
|
+
if (atleas2DoubleDots && model.search(badModelsNextToEachOther) !== -1) {
|
|
1484
1484
|
throw new Error(`
|
|
1485
1485
|
|
|
1486
1486
|
Bad rest model: ${model}
|
|
1487
1487
|
|
|
1488
1488
|
Do not create rest models like this: /book/author/:bookid/:authorid
|
|
1489
1489
|
Instead use nested approach: /book/:bookid/author/:authorid
|
|
1490
|
-
`);
|
|
1491
|
-
}
|
|
1492
|
-
;
|
|
1493
|
-
Resource.map(e, e);
|
|
1494
|
-
Resource.instance.add(e, model ? model : '', entityMapping, circular, customContentType);
|
|
1495
|
-
return {
|
|
1496
|
-
model: (params) => Resource.instance.api(e, interpolateParamsToUrl(params, model)),
|
|
1497
|
-
replay: (method) => {
|
|
1498
|
-
Resource.getModel(e, model).replay(method);
|
|
1499
|
-
},
|
|
1500
|
-
get headers() {
|
|
1501
|
-
return Resource.getModel(e, model).headers;
|
|
1502
|
-
}
|
|
1503
|
-
};
|
|
1504
|
-
}
|
|
1505
|
-
static reset() {
|
|
1506
|
-
Resource.endpoints = {};
|
|
1507
|
-
}
|
|
1508
|
-
static map(endpoint, url) {
|
|
1509
|
-
log.i('url', url);
|
|
1510
|
-
let regex = /(http|https):\/\/(\w+:{0,1}\w*)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%!\-\/]))?/;
|
|
1511
|
-
let e = endpoint;
|
|
1512
|
-
if (!regex.test(url)) {
|
|
1513
|
-
throw `Url address is not correct: ${url}`;
|
|
1514
|
-
}
|
|
1515
|
-
if (url.charAt(url.length - 1) === '/')
|
|
1516
|
-
url = url.slice(0, url.length - 1);
|
|
1517
|
-
log.i('url after', url);
|
|
1518
|
-
if (Resource.endpoints[e] !== void 0) {
|
|
1519
|
-
Helpers$1.log('Cannot use map function at the same API endpoint again ('
|
|
1520
|
-
+ Resource.endpoints[e].url + ')');
|
|
1521
|
-
return false;
|
|
1522
|
-
}
|
|
1523
|
-
Resource.endpoints[e] = {
|
|
1524
|
-
url: url,
|
|
1525
|
-
models: {},
|
|
1526
|
-
entity: null
|
|
1527
|
-
};
|
|
1528
|
-
log.i('enpoints', Resource.endpoints);
|
|
1529
|
-
return true;
|
|
1530
|
-
}
|
|
1531
|
-
static prepareModel(model) {
|
|
1532
|
-
if (model.charAt(model.length - 1) === '/')
|
|
1533
|
-
model = model.slice(0, model.length - 1);
|
|
1534
|
-
if (model.charAt(0) === '/')
|
|
1535
|
-
model = model.slice(1, model.length);
|
|
1536
|
-
return model;
|
|
1537
|
-
}
|
|
1538
|
-
/**
|
|
1539
|
-
* And enipoint to application
|
|
1540
|
-
*
|
|
1541
|
-
* @param {E} endpoint
|
|
1542
|
-
* @param {string} model
|
|
1543
|
-
* @returns {boolean}
|
|
1544
|
-
*/
|
|
1545
|
-
add(endpoint, model, entity, circular, customContentType) {
|
|
1546
|
-
log.i(`I am maping ${model} on ${endpoint}`);
|
|
1547
|
-
model = Resource.prepareModel(model);
|
|
1548
|
-
let e;
|
|
1549
|
-
e = (endpoint).toString();
|
|
1550
|
-
if (Resource.endpoints[e] === void 0) {
|
|
1551
|
-
console.error('Endpoint is not mapped ! Cannot add model ' + model);
|
|
1552
|
-
return;
|
|
1553
|
-
}
|
|
1554
|
-
if (Resource.endpoints[e].models[model] !== void 0) {
|
|
1555
|
-
if (Resource.enableWarnings)
|
|
1556
|
-
console.warn(`Model '${model}' is already defined in endpoint: `
|
|
1557
|
-
+ Resource.endpoints[e].url);
|
|
1558
|
-
return;
|
|
1559
|
-
}
|
|
1560
|
-
Resource.endpoints[e].models[model] =
|
|
1561
|
-
new Rest(Resource.endpoints[e].url
|
|
1562
|
-
+ '/' + model, Resource.request, {
|
|
1563
|
-
endpoint: e,
|
|
1564
|
-
path: model,
|
|
1565
|
-
entity,
|
|
1566
|
-
circular,
|
|
1567
|
-
}, customContentType); // TODO put custom content type in meta ?
|
|
1568
|
-
return;
|
|
1569
|
-
}
|
|
1570
|
-
/**
|
|
1571
|
-
* Access api throught endpoint
|
|
1572
|
-
*
|
|
1573
|
-
* @param {E} endpoint
|
|
1574
|
-
* @param {string} model
|
|
1575
|
-
* @returns {Rest<T, TA>}
|
|
1576
|
-
*/
|
|
1577
|
-
api(endpoint, model) {
|
|
1578
|
-
if (model.charAt(0) === '/')
|
|
1579
|
-
model = model.slice(1, model.length);
|
|
1580
|
-
let e = (endpoint).toString();
|
|
1581
|
-
if (Resource.endpoints[e] === void 0) {
|
|
1582
|
-
throw `Endpoint: ${endpoint} is not mapped ! Cannot add model: ${model}`;
|
|
1583
|
-
}
|
|
1584
|
-
let allModels = Resource.endpoints[e].models;
|
|
1585
|
-
let orgModel = model;
|
|
1586
|
-
model = this.checkNestedModels(model, allModels);
|
|
1587
|
-
if (Resource.endpoints[e].models[model] === void 0) {
|
|
1588
|
-
throw `Model '${model}' is undefined in endpoint: ${Resource.endpoints[e].url} `;
|
|
1589
|
-
}
|
|
1590
|
-
let res = Resource.endpoints[(endpoint).toString()].models[model];
|
|
1591
|
-
if (orgModel !== model) {
|
|
1592
|
-
let baseUrl = Resource.endpoints[(endpoint).toString()].url;
|
|
1593
|
-
res.__rest_endpoint = `${baseUrl}/${orgModel}`;
|
|
1594
|
-
}
|
|
1595
|
-
else {
|
|
1596
|
-
res.__rest_endpoint = void 0;
|
|
1597
|
-
}
|
|
1598
|
-
;
|
|
1599
|
-
return res;
|
|
1600
|
-
}
|
|
1601
|
-
}
|
|
1602
|
-
Resource._listenErrors = new Subject();
|
|
1603
|
-
Resource._listenSuccess = new Subject();
|
|
1604
|
-
Resource.enableWarnings = true;
|
|
1605
|
-
Resource.instance = new Resource();
|
|
1606
|
-
Resource.endpoints = {};
|
|
1607
|
-
Resource.request = new RestRequest();
|
|
1608
|
-
Resource.Cookies = Cookie.Instance;
|
|
1609
|
-
;
|
|
1490
|
+
`);
|
|
1491
|
+
}
|
|
1492
|
+
;
|
|
1493
|
+
Resource.map(e, e);
|
|
1494
|
+
Resource.instance.add(e, model ? model : '', entityMapping, circular, customContentType);
|
|
1495
|
+
return {
|
|
1496
|
+
model: (params) => Resource.instance.api(e, interpolateParamsToUrl(params, model)),
|
|
1497
|
+
replay: (method) => {
|
|
1498
|
+
Resource.getModel(e, model).replay(method);
|
|
1499
|
+
},
|
|
1500
|
+
get headers() {
|
|
1501
|
+
return Resource.getModel(e, model).headers;
|
|
1502
|
+
}
|
|
1503
|
+
};
|
|
1504
|
+
}
|
|
1505
|
+
static reset() {
|
|
1506
|
+
Resource.endpoints = {};
|
|
1507
|
+
}
|
|
1508
|
+
static map(endpoint, url) {
|
|
1509
|
+
log.i('url', url);
|
|
1510
|
+
let regex = /(http|https):\/\/(\w+:{0,1}\w*)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%!\-\/]))?/;
|
|
1511
|
+
let e = endpoint;
|
|
1512
|
+
if (!regex.test(url)) {
|
|
1513
|
+
throw `Url address is not correct: ${url}`;
|
|
1514
|
+
}
|
|
1515
|
+
if (url.charAt(url.length - 1) === '/')
|
|
1516
|
+
url = url.slice(0, url.length - 1);
|
|
1517
|
+
log.i('url after', url);
|
|
1518
|
+
if (Resource.endpoints[e] !== void 0) {
|
|
1519
|
+
Helpers$1.log('Cannot use map function at the same API endpoint again ('
|
|
1520
|
+
+ Resource.endpoints[e].url + ')');
|
|
1521
|
+
return false;
|
|
1522
|
+
}
|
|
1523
|
+
Resource.endpoints[e] = {
|
|
1524
|
+
url: url,
|
|
1525
|
+
models: {},
|
|
1526
|
+
entity: null
|
|
1527
|
+
};
|
|
1528
|
+
log.i('enpoints', Resource.endpoints);
|
|
1529
|
+
return true;
|
|
1530
|
+
}
|
|
1531
|
+
static prepareModel(model) {
|
|
1532
|
+
if (model.charAt(model.length - 1) === '/')
|
|
1533
|
+
model = model.slice(0, model.length - 1);
|
|
1534
|
+
if (model.charAt(0) === '/')
|
|
1535
|
+
model = model.slice(1, model.length);
|
|
1536
|
+
return model;
|
|
1537
|
+
}
|
|
1538
|
+
/**
|
|
1539
|
+
* And enipoint to application
|
|
1540
|
+
*
|
|
1541
|
+
* @param {E} endpoint
|
|
1542
|
+
* @param {string} model
|
|
1543
|
+
* @returns {boolean}
|
|
1544
|
+
*/
|
|
1545
|
+
add(endpoint, model, entity, circular, customContentType) {
|
|
1546
|
+
log.i(`I am maping ${model} on ${endpoint}`);
|
|
1547
|
+
model = Resource.prepareModel(model);
|
|
1548
|
+
let e;
|
|
1549
|
+
e = (endpoint).toString();
|
|
1550
|
+
if (Resource.endpoints[e] === void 0) {
|
|
1551
|
+
console.error('Endpoint is not mapped ! Cannot add model ' + model);
|
|
1552
|
+
return;
|
|
1553
|
+
}
|
|
1554
|
+
if (Resource.endpoints[e].models[model] !== void 0) {
|
|
1555
|
+
if (Resource.enableWarnings)
|
|
1556
|
+
console.warn(`Model '${model}' is already defined in endpoint: `
|
|
1557
|
+
+ Resource.endpoints[e].url);
|
|
1558
|
+
return;
|
|
1559
|
+
}
|
|
1560
|
+
Resource.endpoints[e].models[model] =
|
|
1561
|
+
new Rest(Resource.endpoints[e].url
|
|
1562
|
+
+ '/' + model, Resource.request, {
|
|
1563
|
+
endpoint: e,
|
|
1564
|
+
path: model,
|
|
1565
|
+
entity,
|
|
1566
|
+
circular,
|
|
1567
|
+
}, customContentType); // TODO put custom content type in meta ?
|
|
1568
|
+
return;
|
|
1569
|
+
}
|
|
1570
|
+
/**
|
|
1571
|
+
* Access api throught endpoint
|
|
1572
|
+
*
|
|
1573
|
+
* @param {E} endpoint
|
|
1574
|
+
* @param {string} model
|
|
1575
|
+
* @returns {Rest<T, TA>}
|
|
1576
|
+
*/
|
|
1577
|
+
api(endpoint, model) {
|
|
1578
|
+
if (model.charAt(0) === '/')
|
|
1579
|
+
model = model.slice(1, model.length);
|
|
1580
|
+
let e = (endpoint).toString();
|
|
1581
|
+
if (Resource.endpoints[e] === void 0) {
|
|
1582
|
+
throw `Endpoint: ${endpoint} is not mapped ! Cannot add model: ${model}`;
|
|
1583
|
+
}
|
|
1584
|
+
let allModels = Resource.endpoints[e].models;
|
|
1585
|
+
let orgModel = model;
|
|
1586
|
+
model = this.checkNestedModels(model, allModels);
|
|
1587
|
+
if (Resource.endpoints[e].models[model] === void 0) {
|
|
1588
|
+
throw `Model '${model}' is undefined in endpoint: ${Resource.endpoints[e].url} `;
|
|
1589
|
+
}
|
|
1590
|
+
let res = Resource.endpoints[(endpoint).toString()].models[model];
|
|
1591
|
+
if (orgModel !== model) {
|
|
1592
|
+
let baseUrl = Resource.endpoints[(endpoint).toString()].url;
|
|
1593
|
+
res.__rest_endpoint = `${baseUrl}/${orgModel}`;
|
|
1594
|
+
}
|
|
1595
|
+
else {
|
|
1596
|
+
res.__rest_endpoint = void 0;
|
|
1597
|
+
}
|
|
1598
|
+
;
|
|
1599
|
+
return res;
|
|
1600
|
+
}
|
|
1601
|
+
}
|
|
1602
|
+
Resource._listenErrors = new Subject();
|
|
1603
|
+
Resource._listenSuccess = new Subject();
|
|
1604
|
+
Resource.enableWarnings = true;
|
|
1605
|
+
Resource.instance = new Resource();
|
|
1606
|
+
Resource.endpoints = {};
|
|
1607
|
+
Resource.request = new RestRequest();
|
|
1608
|
+
Resource.Cookies = Cookie.Instance;
|
|
1609
|
+
;
|
|
1610
1610
|
({}); // @--end-of-file-for-module=ng2-rest lib/resource.service.ts
|
|
1611
1611
|
|
|
1612
|
-
/**
|
|
1613
|
-
*
|
|
1614
|
-
* @export
|
|
1615
|
-
* @abstract
|
|
1616
|
-
* @class SimpleResource
|
|
1617
|
-
* @extends {Resource<T, A, TA>}
|
|
1618
|
-
* @template E Endpoint type
|
|
1619
|
-
* @template A Single modle type
|
|
1620
|
-
* @template TA Array Model Type
|
|
1621
|
-
* @template RP rest url parameters type
|
|
1622
|
-
* @template QP query parameter type
|
|
1623
|
-
*/
|
|
1624
|
-
class ExtendedResource {
|
|
1625
|
-
constructor(endpoint, path_model) {
|
|
1626
|
-
this.endpoint = endpoint;
|
|
1627
|
-
this.path_model = path_model;
|
|
1628
|
-
/**
|
|
1629
|
-
* Get model by rest params
|
|
1630
|
-
*/
|
|
1631
|
-
this.model = (restParams) => {
|
|
1632
|
-
return {
|
|
1633
|
-
get: (queryPrams) => {
|
|
1634
|
-
return Observable.create((observer) => {
|
|
1635
|
-
ExtendedResource.handlers.push(this.rest.model(restParams)
|
|
1636
|
-
.get([queryPrams], ExtendedResource.doNotSerializeQueryParams)
|
|
1637
|
-
.observable
|
|
1638
|
-
.subscribe(data => observer.next(data.body.json), err => observer.error(err), () => observer.complete()));
|
|
1639
|
-
});
|
|
1640
|
-
},
|
|
1641
|
-
patch: (item, queryParams) => {
|
|
1642
|
-
return Observable.create((observer) => {
|
|
1643
|
-
ExtendedResource.handlers.push(this.rest.model(restParams)
|
|
1644
|
-
.put(item, [queryParams], ExtendedResource.doNotSerializeQueryParams)
|
|
1645
|
-
.observable
|
|
1646
|
-
.subscribe(data => observer.next(data.body.json), err => observer.error(err), () => observer.complete()));
|
|
1647
|
-
});
|
|
1648
|
-
},
|
|
1649
|
-
head: (queryPrams) => {
|
|
1650
|
-
return Observable.create((observer) => {
|
|
1651
|
-
ExtendedResource.handlers.push(this.rest.model(restParams)
|
|
1652
|
-
.head([queryPrams], ExtendedResource.doNotSerializeQueryParams)
|
|
1653
|
-
.observable
|
|
1654
|
-
.subscribe(data => observer.next(data.body.json), err => observer.error(err), () => observer.complete()));
|
|
1655
|
-
});
|
|
1656
|
-
},
|
|
1657
|
-
query: (queryPrams) => {
|
|
1658
|
-
return Observable.create((observer) => {
|
|
1659
|
-
ExtendedResource.handlers.push(this.rest.model(restParams).
|
|
1660
|
-
array
|
|
1661
|
-
.get([queryPrams], ExtendedResource.doNotSerializeQueryParams)
|
|
1662
|
-
.observable
|
|
1663
|
-
.subscribe(data => observer.next(data.body.json), err => observer.error(err), () => observer.complete()));
|
|
1664
|
-
});
|
|
1665
|
-
},
|
|
1666
|
-
post: (item, queryParams) => {
|
|
1667
|
-
return Observable.create((observer) => {
|
|
1668
|
-
ExtendedResource.handlers.push(this.rest.model(restParams)
|
|
1669
|
-
.post(item, [queryParams], ExtendedResource.doNotSerializeQueryParams)
|
|
1670
|
-
.observable
|
|
1671
|
-
.subscribe(data => observer.next(data.body.json), err => observer.error(err), () => observer.complete()));
|
|
1672
|
-
});
|
|
1673
|
-
},
|
|
1674
|
-
put: (item, queryParams) => {
|
|
1675
|
-
return Observable.create((observer) => {
|
|
1676
|
-
ExtendedResource.handlers.push(this.rest.model(restParams)
|
|
1677
|
-
.put(item, [queryParams], ExtendedResource.doNotSerializeQueryParams)
|
|
1678
|
-
.observable
|
|
1679
|
-
.subscribe(data => observer.next(data.body.json), err => observer.error(err), () => observer.complete()));
|
|
1680
|
-
});
|
|
1681
|
-
},
|
|
1682
|
-
delete: (queryPrams) => {
|
|
1683
|
-
return Observable.create((observer) => {
|
|
1684
|
-
ExtendedResource.handlers.push(this.rest.model(restParams)
|
|
1685
|
-
.delete([queryPrams], ExtendedResource.doNotSerializeQueryParams)
|
|
1686
|
-
.observable
|
|
1687
|
-
.subscribe(data => observer.next(data.body.json), err => observer.error(err), () => observer.complete()));
|
|
1688
|
-
});
|
|
1689
|
-
}
|
|
1690
|
-
};
|
|
1691
|
-
};
|
|
1692
|
-
this.rest = Resource.create(endpoint, path_model);
|
|
1693
|
-
}
|
|
1694
|
-
}
|
|
1695
|
-
ExtendedResource.doNotSerializeQueryParams = false;
|
|
1696
|
-
ExtendedResource.handlers = [];
|
|
1697
|
-
/**
|
|
1698
|
-
*
|
|
1699
|
-
* @export
|
|
1700
|
-
* @class SimpleResource
|
|
1701
|
-
* @template A single model type
|
|
1702
|
-
* @template TA array model type
|
|
1703
|
-
* @template RP rest parameters type
|
|
1704
|
-
* @template QP query parameters type
|
|
1705
|
-
*/
|
|
1706
|
-
class SimpleResource {
|
|
1707
|
-
constructor(endpoint, model) {
|
|
1708
|
-
let rest = new ExtendedResource(endpoint, model);
|
|
1709
|
-
this.model = rest.model;
|
|
1710
|
-
}
|
|
1711
|
-
static set doNotSerializeQueryParams(value) {
|
|
1712
|
-
if (!SimpleResource._isSetQueryParamsSerialization) {
|
|
1713
|
-
SimpleResource._isSetQueryParamsSerialization = true;
|
|
1714
|
-
ExtendedResource.doNotSerializeQueryParams = value;
|
|
1715
|
-
return;
|
|
1716
|
-
}
|
|
1612
|
+
/**
|
|
1613
|
+
*
|
|
1614
|
+
* @export
|
|
1615
|
+
* @abstract
|
|
1616
|
+
* @class SimpleResource
|
|
1617
|
+
* @extends {Resource<T, A, TA>}
|
|
1618
|
+
* @template E Endpoint type
|
|
1619
|
+
* @template A Single modle type
|
|
1620
|
+
* @template TA Array Model Type
|
|
1621
|
+
* @template RP rest url parameters type
|
|
1622
|
+
* @template QP query parameter type
|
|
1623
|
+
*/
|
|
1624
|
+
class ExtendedResource {
|
|
1625
|
+
constructor(endpoint, path_model) {
|
|
1626
|
+
this.endpoint = endpoint;
|
|
1627
|
+
this.path_model = path_model;
|
|
1628
|
+
/**
|
|
1629
|
+
* Get model by rest params
|
|
1630
|
+
*/
|
|
1631
|
+
this.model = (restParams) => {
|
|
1632
|
+
return {
|
|
1633
|
+
get: (queryPrams) => {
|
|
1634
|
+
return Observable.create((observer) => {
|
|
1635
|
+
ExtendedResource.handlers.push(this.rest.model(restParams)
|
|
1636
|
+
.get([queryPrams], ExtendedResource.doNotSerializeQueryParams)
|
|
1637
|
+
.observable
|
|
1638
|
+
.subscribe(data => observer.next(data.body.json), err => observer.error(err), () => observer.complete()));
|
|
1639
|
+
});
|
|
1640
|
+
},
|
|
1641
|
+
patch: (item, queryParams) => {
|
|
1642
|
+
return Observable.create((observer) => {
|
|
1643
|
+
ExtendedResource.handlers.push(this.rest.model(restParams)
|
|
1644
|
+
.put(item, [queryParams], ExtendedResource.doNotSerializeQueryParams)
|
|
1645
|
+
.observable
|
|
1646
|
+
.subscribe(data => observer.next(data.body.json), err => observer.error(err), () => observer.complete()));
|
|
1647
|
+
});
|
|
1648
|
+
},
|
|
1649
|
+
head: (queryPrams) => {
|
|
1650
|
+
return Observable.create((observer) => {
|
|
1651
|
+
ExtendedResource.handlers.push(this.rest.model(restParams)
|
|
1652
|
+
.head([queryPrams], ExtendedResource.doNotSerializeQueryParams)
|
|
1653
|
+
.observable
|
|
1654
|
+
.subscribe(data => observer.next(data.body.json), err => observer.error(err), () => observer.complete()));
|
|
1655
|
+
});
|
|
1656
|
+
},
|
|
1657
|
+
query: (queryPrams) => {
|
|
1658
|
+
return Observable.create((observer) => {
|
|
1659
|
+
ExtendedResource.handlers.push(this.rest.model(restParams).
|
|
1660
|
+
array
|
|
1661
|
+
.get([queryPrams], ExtendedResource.doNotSerializeQueryParams)
|
|
1662
|
+
.observable
|
|
1663
|
+
.subscribe(data => observer.next(data.body.json), err => observer.error(err), () => observer.complete()));
|
|
1664
|
+
});
|
|
1665
|
+
},
|
|
1666
|
+
post: (item, queryParams) => {
|
|
1667
|
+
return Observable.create((observer) => {
|
|
1668
|
+
ExtendedResource.handlers.push(this.rest.model(restParams)
|
|
1669
|
+
.post(item, [queryParams], ExtendedResource.doNotSerializeQueryParams)
|
|
1670
|
+
.observable
|
|
1671
|
+
.subscribe(data => observer.next(data.body.json), err => observer.error(err), () => observer.complete()));
|
|
1672
|
+
});
|
|
1673
|
+
},
|
|
1674
|
+
put: (item, queryParams) => {
|
|
1675
|
+
return Observable.create((observer) => {
|
|
1676
|
+
ExtendedResource.handlers.push(this.rest.model(restParams)
|
|
1677
|
+
.put(item, [queryParams], ExtendedResource.doNotSerializeQueryParams)
|
|
1678
|
+
.observable
|
|
1679
|
+
.subscribe(data => observer.next(data.body.json), err => observer.error(err), () => observer.complete()));
|
|
1680
|
+
});
|
|
1681
|
+
},
|
|
1682
|
+
delete: (queryPrams) => {
|
|
1683
|
+
return Observable.create((observer) => {
|
|
1684
|
+
ExtendedResource.handlers.push(this.rest.model(restParams)
|
|
1685
|
+
.delete([queryPrams], ExtendedResource.doNotSerializeQueryParams)
|
|
1686
|
+
.observable
|
|
1687
|
+
.subscribe(data => observer.next(data.body.json), err => observer.error(err), () => observer.complete()));
|
|
1688
|
+
});
|
|
1689
|
+
}
|
|
1690
|
+
};
|
|
1691
|
+
};
|
|
1692
|
+
this.rest = Resource.create(endpoint, path_model);
|
|
1693
|
+
}
|
|
1694
|
+
}
|
|
1695
|
+
ExtendedResource.doNotSerializeQueryParams = false;
|
|
1696
|
+
ExtendedResource.handlers = [];
|
|
1697
|
+
/**
|
|
1698
|
+
*
|
|
1699
|
+
* @export
|
|
1700
|
+
* @class SimpleResource
|
|
1701
|
+
* @template A single model type
|
|
1702
|
+
* @template TA array model type
|
|
1703
|
+
* @template RP rest parameters type
|
|
1704
|
+
* @template QP query parameters type
|
|
1705
|
+
*/
|
|
1706
|
+
class SimpleResource {
|
|
1707
|
+
constructor(endpoint, model) {
|
|
1708
|
+
let rest = new ExtendedResource(endpoint, model);
|
|
1709
|
+
this.model = rest.model;
|
|
1710
|
+
}
|
|
1711
|
+
static set doNotSerializeQueryParams(value) {
|
|
1712
|
+
if (!SimpleResource._isSetQueryParamsSerialization) {
|
|
1713
|
+
SimpleResource._isSetQueryParamsSerialization = true;
|
|
1714
|
+
ExtendedResource.doNotSerializeQueryParams = value;
|
|
1715
|
+
return;
|
|
1716
|
+
}
|
|
1717
1717
|
console.warn(`Query params serialization already set as
|
|
1718
|
-
${ExtendedResource.doNotSerializeQueryParams},`);
|
|
1719
|
-
}
|
|
1720
|
-
static __destroy() {
|
|
1721
|
-
ExtendedResource.handlers.forEach(h => h.unsubscribe());
|
|
1722
|
-
}
|
|
1723
|
-
}
|
|
1724
|
-
SimpleResource._isSetQueryParamsSerialization = false;
|
|
1725
|
-
;
|
|
1718
|
+
${ExtendedResource.doNotSerializeQueryParams},`);
|
|
1719
|
+
}
|
|
1720
|
+
static __destroy() {
|
|
1721
|
+
ExtendedResource.handlers.forEach(h => h.unsubscribe());
|
|
1722
|
+
}
|
|
1723
|
+
}
|
|
1724
|
+
SimpleResource._isSetQueryParamsSerialization = false;
|
|
1725
|
+
;
|
|
1726
1726
|
({}); // @--end-of-file-for-module=ng2-rest lib/other/simple-resource.ts
|
|
1727
1727
|
|
|
1728
|
-
;
|
|
1728
|
+
;
|
|
1729
1729
|
({}); // @--end-of-file-for-module=ng2-rest lib/index.ts
|
|
1730
1730
|
|
|
1731
|
-
/**
|
|
1732
|
-
* Generated bundle index. Do not edit.
|
|
1731
|
+
/**
|
|
1732
|
+
* Generated bundle index. Do not edit.
|
|
1733
1733
|
*/
|
|
1734
1734
|
|
|
1735
1735
|
export { CONTENT_TYPE, Helpers, Mapping, Models, RequestCache, Resource, Rest, RestHeaders, SimpleResource, interpolateParamsToUrl };
|