-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathin-memory-backend.service.js
387 lines (387 loc) · 20.1 KB
/
in-memory-backend.service.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
System.register(['angular2/core', 'angular2/http', 'rxjs/Observable', 'rxjs/add/operator/delay', './http-status-codes'], function(exports_1, context_1) {
"use strict";
var __moduleName = context_1 && context_1.id;
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 core_1, http_1, Observable_1, http_status_codes_1;
var SEED_DATA, InMemoryBackendConfig, isSuccess, InMemoryBackendService;
return {
setters:[
function (core_1_1) {
core_1 = core_1_1;
},
function (http_1_1) {
http_1 = http_1_1;
},
function (Observable_1_1) {
Observable_1 = Observable_1_1;
},
function (_1) {},
function (http_status_codes_1_1) {
http_status_codes_1 = http_status_codes_1_1;
}],
execute: function() {
/**
* Seed data for in-memory database
* Must implement InMemoryDbService.
*/
exports_1("SEED_DATA", SEED_DATA = new core_1.OpaqueToken('seedData'));
/**
* InMemoryBackendService configuration options
* Usage:
* provide(InMemoryBackendConfig, {useValue: {delay:600}}),
*/
InMemoryBackendConfig = (function () {
function InMemoryBackendConfig(config) {
if (config === void 0) { config = {}; }
Object.assign(this, {
defaultResponseOptions: new http_1.BaseResponseOptions(),
delay: 500,
delete404: false
}, config);
}
return InMemoryBackendConfig;
}());
exports_1("InMemoryBackendConfig", InMemoryBackendConfig);
exports_1("isSuccess", isSuccess = function (status) { return (status >= 200 && status < 300); });
/**
* Simulate the behavior of a RESTy web api
* backed by the simple in-memory data store provided by the injected InMemoryDataService service.
* Conforms mostly to behavior described here:
* http://www.restapitutorial.com/lessons/httpmethods.html
*
* ### Usage
*
* Create InMemoryDataService class the implements IInMemoryDataService.
* Register both this service and the seed data as in:
* ```
* // other imports
* import { HTTP_PROVIDERS, XHRBackend } from 'angular2/http';
* import { InMemoryBackendConfig, InMemoryBackendService, SEED_DATA } from '../in-memory-backend/in-memory-backend.service';
* import { InMemoryStoryService } from '../api/in-memory-story.service';
*
* @Component({
* selector: ...,
* templateUrl: ...,
* providers: [
* HTTP_PROVIDERS,
* provide(XHRBackend, { useClass: InMemoryBackendService }),
* provide(SEED_DATA, { useClass: InMemoryStoryService }),
* provide(InMemoryBackendConfig, { useValue: { delay: 600 } }),
* ]
* })
* export class AppComponent { ... }
* ```
*/
InMemoryBackendService = (function () {
function InMemoryBackendService(_seedData, config) {
this._seedData = _seedData;
this._config = new InMemoryBackendConfig();
this._resetDb();
var loc = this._getLocation('./');
this._config.host = loc.host;
this._config.rootPath = loc.pathname;
Object.assign(this._config, config);
}
InMemoryBackendService.prototype.createConnection = function (req) {
var res = this._handleRequest(req);
var response = new Observable_1.Observable(function (responseObserver) {
if (isSuccess(res.status)) {
responseObserver.next(res);
responseObserver.complete();
}
else {
responseObserver.error(res);
}
return function () { }; // unsubscribe function
});
response = response.delay(this._config.delay || 500);
return { response: response };
};
//// protected /////
/**
* Process Request and return an Http Response object
* in the manner of a RESTy web api.
*
* Expect URI pattern in the form :base/:collectionName/:id?
* Examples:
* api/characters
* api/characters/42
* api/characters.json/42 // ignores the ".json"
* commands/resetDb // resets the "database"
*/
InMemoryBackendService.prototype._handleRequest = function (req) {
var _a = this._parseUrl(req.url), base = _a.base, collectionName = _a.collectionName, id = _a.id, resourceUrl = _a.resourceUrl;
var reqInfo = {
req: req,
base: base,
collection: this._db[collectionName],
collectionName: collectionName,
headers: new http_1.Headers({ 'Content-Type': 'application/json' }),
id: this._parseId(id),
resourceUrl: resourceUrl
};
var options;
try {
if ('commands' === reqInfo.base.toLowerCase()) {
options = this._commands(reqInfo);
}
else if (reqInfo.collection) {
switch (req.method) {
case http_1.RequestMethod.Get:
options = this._get(reqInfo);
break;
case http_1.RequestMethod.Post:
options = this._post(reqInfo);
break;
case http_1.RequestMethod.Put:
options = this._put(reqInfo);
break;
case http_1.RequestMethod.Delete:
options = this._delete(reqInfo);
break;
default:
options = this._createErrorResponse(http_status_codes_1.STATUS.METHOD_NOT_ALLOWED, 'Method not allowed');
break;
}
}
else {
options = this._createErrorResponse(http_status_codes_1.STATUS.NOT_FOUND, "Collection '" + collectionName + "' not found");
}
}
catch (error) {
var err = error.message || error;
options = this._createErrorResponse(http_status_codes_1.STATUS.INTERNAL_SERVER_ERROR, "" + err);
}
options = this._setStatusText(options);
if (this._config.defaultResponseOptions) {
options = this._config.defaultResponseOptions.merge(options);
}
return new http_1.Response(options);
};
InMemoryBackendService.prototype._clone = function (data) {
return JSON.parse(JSON.stringify(data));
};
/**
* When the `base`="commands", the `collectionName` is the command
* Example URLs:
* commands/resetdb // Reset the "database" to its original state
* commands/config (GET) // Return this service's config object
* commands/config (!GET) // Update the config (e.g. delay)
*
* Usage:
* http.post('commands/resetdb', null);
* http.get('commands/config');
* http.post('commands/config', '{"delay":1000}');
*/
InMemoryBackendService.prototype._commands = function (reqInfo) {
var command = reqInfo.collectionName.toLowerCase();
var method = reqInfo.req.method;
var options;
switch (command) {
case 'resetdb':
this._resetDb();
options = new http_1.ResponseOptions({ status: http_status_codes_1.STATUS.OK });
break;
case 'config':
if (method === http_1.RequestMethod.Get) {
options = new http_1.ResponseOptions({
body: this._clone(this._config),
status: http_status_codes_1.STATUS.OK
});
}
else {
// Be nice ... any other method is a config update
var body = JSON.parse(reqInfo.req.text() || '{}');
Object.assign(this._config, body);
options = new http_1.ResponseOptions({ status: http_status_codes_1.STATUS.NO_CONTENT });
}
break;
default:
options = this._createErrorResponse(http_status_codes_1.STATUS.INTERNAL_SERVER_ERROR, "Unknown command \"" + command + "\"");
}
return options;
};
InMemoryBackendService.prototype._createErrorResponse = function (status, message) {
return new http_1.ResponseOptions({
body: { 'error': "" + message },
headers: new http_1.Headers({ 'Content-Type': 'application/json' }),
status: status
});
};
InMemoryBackendService.prototype._delete = function (_a) {
var id = _a.id, collection = _a.collection, collectionName = _a.collectionName, headers = _a.headers;
if (!id) {
return this._createErrorResponse(http_status_codes_1.STATUS.NOT_FOUND, "Missing \"" + collectionName + "\" id");
}
var exists = this._removeById(collection, id);
return new http_1.ResponseOptions({
headers: headers,
status: (exists || !this._config.delete404) ? http_status_codes_1.STATUS.NO_CONTENT : http_status_codes_1.STATUS.NOT_FOUND
});
};
InMemoryBackendService.prototype._findById = function (collection, id) {
return collection.find(function (item) { return item.id === id; });
};
InMemoryBackendService.prototype._genId = function (collection) {
// assumes numeric ids
var maxId = 0;
collection.reduce(function (prev, item) {
maxId = Math.max(maxId, typeof item.id === 'number' ? item.id : maxId);
}, null);
return maxId + 1;
};
InMemoryBackendService.prototype._get = function (_a) {
var id = _a.id, collection = _a.collection, collectionName = _a.collectionName, headers = _a.headers;
var data = (id) ? this._findById(collection, id) : collection;
if (!data) {
return this._createErrorResponse(http_status_codes_1.STATUS.NOT_FOUND, "'" + collectionName + "' with id='" + id + "' not found");
}
return new http_1.ResponseOptions({
body: { data: this._clone(data) },
headers: headers,
status: http_status_codes_1.STATUS.OK
});
};
InMemoryBackendService.prototype._getLocation = function (href) {
var l = document.createElement('a');
l.href = href;
return l;
};
;
InMemoryBackendService.prototype._indexOf = function (collection, id) {
return collection.findIndex(function (item) { return item.id === id; });
};
// tries to parse id as integer; returns input id if not an integer.
InMemoryBackendService.prototype._parseId = function (id) {
if (!id) {
return null;
}
var idNum = parseInt(id, 10);
return isNaN(idNum) ? id : idNum;
};
InMemoryBackendService.prototype._parseUrl = function (url) {
try {
var loc = this._getLocation(url);
var drop = this._config.rootPath.length;
var urlRoot = '';
if (loc.host !== this._config.host) {
// url for a server on a different host!
// assume it's collection is actually here too.
drop = 1; // the leading slash
urlRoot = loc.protocol + '//' + loc.host + '/';
}
var path = loc.pathname.substring(drop);
var _a = path.split('/'), base = _a[0], collectionName = _a[1], id = _a[2];
var resourceUrl = urlRoot + base + '/' + collectionName + '/';
collectionName = collectionName.split('.')[0]; // ignore anything after the '.', e.g., '.json'
return { base: base, id: id, collectionName: collectionName, resourceUrl: resourceUrl };
}
catch (err) {
var msg = "unable to parse url '" + url + "'; original error: " + err.message;
throw new Error(msg);
}
};
InMemoryBackendService.prototype._post = function (_a) {
var collection = _a.collection, headers = _a.headers, id = _a.id, req = _a.req, resourceUrl = _a.resourceUrl;
var item = JSON.parse(req.text());
if (!item.id) {
item.id = id || this._genId(collection);
}
// ignore the request id, if any. Alternatively,
// could reject request if id differs from item.id
id = item.id;
var existingIx = this._indexOf(collection, id);
if (existingIx > -1) {
collection[existingIx] = item;
return new http_1.ResponseOptions({
headers: headers,
status: http_status_codes_1.STATUS.NO_CONTENT
});
}
else {
collection.push(item);
headers.set('Location', resourceUrl + '/' + id);
return new http_1.ResponseOptions({
headers: headers,
body: { data: this._clone(item) },
status: http_status_codes_1.STATUS.CREATED
});
}
};
InMemoryBackendService.prototype._put = function (_a) {
var id = _a.id, collection = _a.collection, collectionName = _a.collectionName, headers = _a.headers, req = _a.req;
var item = JSON.parse(req.text());
if (!id) {
return this._createErrorResponse(http_status_codes_1.STATUS.NOT_FOUND, "Missing '" + collectionName + "' id");
}
if (id !== item.id) {
return this._createErrorResponse(http_status_codes_1.STATUS.BAD_REQUEST, "\"" + collectionName + "\" id does not match item.id");
}
var existingIx = this._indexOf(collection, id);
if (existingIx > -1) {
collection[existingIx] = item;
return new http_1.ResponseOptions({
headers: headers,
status: http_status_codes_1.STATUS.NO_CONTENT // successful; no content
});
}
else {
collection.push(item);
return new http_1.ResponseOptions({
body: { data: this._clone(item) },
headers: headers,
status: http_status_codes_1.STATUS.CREATED
});
}
};
InMemoryBackendService.prototype._removeById = function (collection, id) {
var ix = this._indexOf(collection, id);
if (ix > -1) {
collection.splice(ix, 1);
return true;
}
return false;
};
/**
* Reset the "database" to its original state
*/
InMemoryBackendService.prototype._resetDb = function () {
this._db = this._seedData.createDb();
};
InMemoryBackendService.prototype._setStatusText = function (options) {
try {
var statusCode = http_status_codes_1.STATUS_CODE_INFO[options.status];
options['statusText'] = statusCode ? statusCode.text : 'Unknown Status';
return options;
}
catch (err) {
return new http_1.ResponseOptions({
status: http_status_codes_1.STATUS.INTERNAL_SERVER_ERROR,
statusText: 'Invalid Server Operation'
});
}
};
InMemoryBackendService = __decorate([
__param(0, core_1.Inject(SEED_DATA)),
__param(1, core_1.Inject(InMemoryBackendConfig)),
__param(1, core_1.Optional()),
__metadata('design:paramtypes', [Object, Object])
], InMemoryBackendService);
return InMemoryBackendService;
}());
exports_1("InMemoryBackendService", InMemoryBackendService);
}
}
});
//# sourceMappingURL=in-memory-backend.service.js.map