-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
412 lines (412 loc) · 16.4 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
var http_1 = require("@angular/http");
var core_1 = require("@angular/core");
require("rxjs/add/operator/catch");
require("rxjs/add/operator/map");
require("rxjs/add/operator/toPromise");
exports.RESOURCES_PROVIDER_NAME = "RESOURCES_PROVIDER_NAME";
exports.DEFAULT_RESOURCE_NAME = "DEFAULT_RESOURCE_NAME";
exports.BASE_RESOURCE_NAME = "BASE_RESOURCE_NAME";
var ResourceService = (function () {
function ResourceService(_http, res) {
this._http = _http;
this.res = res;
this.isRelative = false;
this.withCredentials = false;
this.propertyMapping = new Map();
this.listAction = {
url: "/",
method: http_1.RequestMethod.Get
};
this.getAction = {
url: "/:id",
method: http_1.RequestMethod.Get
};
this.insertAction = {
url: "/",
method: http_1.RequestMethod.Post
};
this.updateAction = {
url: "/:id",
method: http_1.RequestMethod.Put
};
this.deleteAction = {
url: "/:id",
method: http_1.RequestMethod.Delete
};
this.name = res.name;
this.basePath = res.basePath == null ? res.name : res.basePath;
if (res.headers) {
this.headers = res.headers;
}
if (res.isRelative != null) {
this.isRelative = res.isRelative;
}
if (res.withCredentials != null) {
this.withCredentials = res.withCredentials;
}
if (res.propertyMapping) {
this.propertyMapping = res.propertyMapping;
}
this.requestInterceptor = res.requestInterceptor;
this.resultInterceptor = res.resultInterceptor;
Object.assign(this.listAction, res.listAction);
Object.assign(this.getAction, res.getAction);
Object.assign(this.insertAction, res.insertAction);
Object.assign(this.updateAction, res.updateAction);
Object.assign(this.deleteAction, res.deleteAction);
}
ResourceService.prototype.list = function (overrides) {
return this._executeRequest(this.listAction, null, overrides);
};
ResourceService.prototype.get = function (data, overrides) {
if (!this._isObjectOrNumber(data)) {
throw new Error("Data must be Object or Integer");
}
var obj = this._isNumber(data) ? { id: data } : data;
return this._executeRequest(this.getAction, obj, overrides);
};
ResourceService.prototype.delete = function (data, overrides) {
if (!this._isObjectOrNumber(data)) {
throw new Error("Data must be Object or Integer");
}
var obj = this._isNumber(data) ? { id: data } : data;
return this._executeRequest(this.deleteAction, obj, overrides);
};
ResourceService.prototype.insert = function (data, overrides) {
if (!this._isObject(data)) {
throw new Error("Data must be Object");
}
return this._executeRequest(this.insertAction, data, overrides);
};
ResourceService.prototype.update = function (data, overrides) {
if (!this._isObject(data)) {
throw new Error("Data must be Object");
}
return this._executeRequest(this.updateAction, data, overrides);
};
ResourceService.prototype.save = function (data, overrides) {
if (this._isUpdateAction(data)) {
return this.update(data, overrides);
}
else {
return this.insert(data, overrides);
}
};
ResourceService.prototype.request = function (request, obj) {
return this._executeRequest(request, obj);
};
ResourceService.prototype._executeRequest = function (request, obj, overrides) {
var _request = Object.assign({}, request, overrides);
if (_request.method == null) {
throw Error("Request parameter `method` is required.");
}
_request.url = this._preparePath(_request, obj);
ResourceService.mergeHeaders(this, _request);
if (obj && [http_1.RequestMethod.Patch, http_1.RequestMethod.Post, http_1.RequestMethod.Put, "PATCH", "POST", "PUT"].indexOf(request.method) >= 0) {
_request.body = obj;
}
if (this.requestInterceptor) {
_request = this.requestInterceptor(_request);
}
if (_request.requestInterceptor) {
_request = _request.requestInterceptor(_request);
}
if (_request.withCredentials == null && this.withCredentials) {
_request.withCredentials = this.withCredentials;
}
var o = this._http.request("", _request);
var i = _request.resultInterceptor ? _request.resultInterceptor : this.resultInterceptor;
return new ResourceResult(o, i);
};
ResourceService.prototype._preparePath = function (request, obj) {
var _this = this;
var path = this.basePath;
var url = request.url;
path.replace("\\", "/");
url.replace("\\", "/");
if (this.isRelative) {
if (path.startsWith("/")) {
path = path.substring(1, path.length);
}
}
else {
if (!this.basePath.startsWith("/") && !this.basePath.startsWith("http")) {
path = "/" + path;
}
}
path += (!path.endsWith("/") && !url.startsWith("/") ? "/" : "") + url;
path.replace("//", "/");
var parts = path.split("/");
parts.forEach(function (part) {
if (part.startsWith(":")) {
var value = _this._findValueByMap(obj, part);
if (value) {
path = path.replace(part, value.toString());
}
else {
throw new Error("Cannot resolve parameter '" + part + "' in request '" + _this.name + "'");
}
}
});
return path;
};
ResourceService.prototype._findValueByMap = function (obj, part) {
var result;
if (obj) {
var key = part.substring(1, part.length);
var newKey = this.propertyMapping[key];
result = newKey ? obj[newKey] : obj[key];
}
return result;
};
ResourceService.prototype._isUpdateAction = function (data) {
var isUpdate = true;
var parts = this.updateAction.url.split("/");
for (var i = 0; i < parts.length; i++) {
var part = parts[i];
if (part.startsWith(":")) {
var value = this._findValueByMap(data, part);
if (!value || value.toString().length == 0 || value.toString() == "0") {
isUpdate = false;
break;
}
}
}
return isUpdate;
};
ResourceService.mergeHeaders = function (from, to) {
if (from.headers) {
to.headers = to.headers || new http_1.Headers();
from.headers.forEach(function (values, name) {
for (var i = 0; i < values.length; i++) {
to.headers.append(name, values[i]);
}
});
}
};
ResourceService.prototype._isObject = function (data) {
return typeof data === 'object';
};
ResourceService.prototype._isNumber = function (data) {
return typeof data === 'number';
};
ResourceService.prototype._isObjectOrNumber = function (data) {
return this._isObject(data) || this._isNumber(data);
};
return ResourceService;
}());
exports.ResourceService = ResourceService;
var ResourceFactory = ResourceFactory_1 = (function () {
function ResourceFactory(http, appResources) {
this.http = http;
this.resources = new Map();
this.createAll(appResources);
}
ResourceFactory.prototype.create = function (res) {
var _res = this.defaultConfig != null ? Object.assign({}, this.defaultConfig, res) : res;
var service = new ResourceService(this.http, _res);
this._mergeBasePath(service);
this.resources[res.name] = service;
};
ResourceFactory.prototype.createAll = function (resources) {
var _this = this;
var mergeConfigIndex = resources.findIndex(function (value) {
return value.name == exports.BASE_RESOURCE_NAME;
});
if (mergeConfigIndex > -1) {
this.baseConfig = resources[mergeConfigIndex];
var _baseConfig = Object.assign({}, this.baseConfig);
_baseConfig.basePath = _baseConfig.basePath == null ? "" : _baseConfig.basePath;
resources.splice(mergeConfigIndex, 1);
this.resources[exports.BASE_RESOURCE_NAME] = new ResourceService(this.http, _baseConfig);
}
var defaultConfigIndex = resources.findIndex(function (value) {
return value.name == exports.DEFAULT_RESOURCE_NAME;
});
if (defaultConfigIndex > -1) {
this.defaultConfig = resources[defaultConfigIndex];
resources.splice(defaultConfigIndex, 1);
ResourceFactory_1._mergeConfig(this.baseConfig, this.defaultConfig);
var _defaultConfig = Object.assign({}, this.defaultConfig);
_defaultConfig.basePath = _defaultConfig.basePath == null ? "" : _defaultConfig.basePath;
var service = new ResourceService(this.http, _defaultConfig);
this._mergeBasePath(service);
this.resources[exports.DEFAULT_RESOURCE_NAME] = service;
}
resources.forEach(function (res) {
_this.create(res);
});
};
ResourceFactory.prototype.get = function (name) {
if (this.resources[name]) {
return this.resources[name];
}
else {
throw new Error("Resource with name '" + name + "' not found!");
}
};
ResourceFactory.prototype._mergeBasePath = function (service) {
if (this.baseConfig.basePath) {
this.baseConfig.basePath.replace("\\", "/");
var separator = !service.basePath.startsWith("/") && !this.baseConfig.basePath.endsWith("/") ? "/" : "";
service.basePath = this.baseConfig.basePath + separator + service.basePath;
}
};
ResourceFactory._mergeConfig = function (from, to) {
if (from) {
ResourceFactory_1._replaceParam(from, to, "isRelative");
ResourceFactory_1._replaceParam(from, to, "withCredentials");
ResourceFactory_1._mergeAction(from.listAction, to.listAction);
ResourceFactory_1._mergeAction(from.getAction, to.getAction);
ResourceFactory_1._mergeAction(from.insertAction, to.insertAction);
ResourceFactory_1._mergeAction(from.updateAction, to.updateAction);
ResourceFactory_1._mergeAction(from.deleteAction, to.deleteAction);
ResourceService.mergeHeaders(from, to);
ResourceFactory_1._copyRequestInterceptor(from, to);
ResourceFactory_1._copyResultInterceptor(from, to);
if (from.propertyMapping) {
if (to.propertyMapping == null) {
to.propertyMapping = from.propertyMapping;
}
else {
to.propertyMapping = Object.assign({}, from.propertyMapping, to.propertyMapping);
}
}
}
};
ResourceFactory._mergeAction = function (from, to) {
if (from) {
ResourceFactory_1._replaceParam(from, to, "url");
ResourceFactory_1._replaceParam(from, to, "method");
ResourceFactory_1._replaceParam(from, to, "withCredentials");
ResourceFactory_1._replaceParam(from, to, "responseType");
//cannot properly merge `body, as type is `any`
ResourceFactory_1._replaceParam(from, to, "body");
ResourceService.mergeHeaders(from, to);
ResourceFactory_1._copyRequestInterceptor(from, to);
ResourceFactory_1._copyResultInterceptor(from, to);
if (from.params != null) {
if (to.params == null) {
to.params = from.params;
}
else {
if (to.params instanceof http_1.URLSearchParams && from.params instanceof http_1.URLSearchParams) {
to.params.appendAll(from.params);
}
else if (to.params instanceof Map && from.params instanceof Map) {
to.params = Object.assign({}, from.params, to.params);
}
}
}
}
};
ResourceFactory._replaceParam = function (from, to, param) {
to[param] = to[param] == null && from[param] != null ? from[param] : to[param];
};
ResourceFactory._copyRequestInterceptor = function (from, to) {
if (from.requestInterceptor != null) {
if (to.requestInterceptor == null) {
to.requestInterceptor = from.requestInterceptor;
}
else {
var copy_1 = to.requestInterceptor;
to.requestInterceptor = function (request) {
request = from.requestInterceptor(request);
return copy_1(request);
};
}
}
};
ResourceFactory._copyResultInterceptor = function (from, to) {
if (from.resultInterceptor != null) {
if (to.resultInterceptor == null) {
to.resultInterceptor = from.resultInterceptor;
}
else {
var copy_2 = to.resultInterceptor;
to.resultInterceptor = function (o) {
return copy_2(from.resultInterceptor(o));
};
}
}
};
return ResourceFactory;
}());
ResourceFactory = ResourceFactory_1 = __decorate([
core_1.Injectable(),
__param(1, core_1.Inject(exports.RESOURCES_PROVIDER_NAME)),
__metadata("design:paramtypes", [http_1.Http, Array])
], ResourceFactory);
exports.ResourceFactory = ResourceFactory;
var ResourceResult = (function () {
function ResourceResult($o, interceptor) {
this._$o = $o.share();
this._interceptor = interceptor;
}
Object.defineProperty(ResourceResult.prototype, "$o", {
get: function () {
return this._$o;
},
enumerable: true,
configurable: true
});
Object.defineProperty(ResourceResult.prototype, "$od", {
get: function () {
return this._$o.map(function (r) { return r.json(); });
},
enumerable: true,
configurable: true
});
Object.defineProperty(ResourceResult.prototype, "$p", {
get: function () {
return this._$o.toPromise();
},
enumerable: true,
configurable: true
});
Object.defineProperty(ResourceResult.prototype, "$pd", {
get: function () {
return this.$od.toPromise();
},
enumerable: true,
configurable: true
});
Object.defineProperty(ResourceResult.prototype, "$oi", {
get: function () {
return this._interceptor ? this._interceptor(this._$o) : this._$o;
},
enumerable: true,
configurable: true
});
Object.defineProperty(ResourceResult.prototype, "$pi", {
get: function () {
return this.$oi.toPromise();
},
enumerable: true,
configurable: true
});
return ResourceResult;
}());
exports.ResourceResult = ResourceResult;
function provideResources(config) {
return [
{ provide: exports.RESOURCES_PROVIDER_NAME, useValue: config },
ResourceFactory
];
}
exports.provideResources = provideResources;
var ResourceFactory_1;
//# sourceMappingURL=index.js.map