8000 :refactor: replace with everywhere · cHullaert/in-memory-web-api@6809d66 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6809d66

Browse files
committed
:refactor: replace with everywhere
1 parent a2f99cd commit 6809d66

13 files changed

+114
-91
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,17 @@ because this is a development tool, not a production product.
88
We do try to tell you about such changes in this `CHANGELOG.md`
99
and we fix bugs as fast as we can.
1010

11+
<a id="0.4.3"></a>
12+
## 0.4.3 (2017-09-11)
13+
Refactoring for clarity and to correctly reflect intent.
14+
A **breaking change** only if your customizations depend directly and explicitly on `RequestInfo` or the `get`, `delete`, `post`, or `put` methods.
15+
16+
- replace all `switchMap` with `concatMap` because, in all previous uses of `switchMap`,
17+
I really meant to wait for the source observable to complete _before_ beginning the inner observable whereas `switchMap` starts the inner observable right away.
18+
19+
- restored `collection` to the `RequestInfo` interface and set it in `handleRequest_`
20+
- `get`, `delete`, `post`, and `put` methods get the `collection` from `requestInfo`; simplifies their signatures to one parameter.
21+
1122
<a id="0.4.2"></a>
1223
## 0.4.2 (2017-09-08)
1324
- Postpones the in-memory database initialization (via `resetDb`) until the first HTTP request.

backend.service.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ export declare abstract class BackendService {
106106
* @param resOptionsFactory - creates ResponseOptions when observable is subscribed
107107
*/
108108
protected createResponseOptions$(resOptionsFactory: () => ResponseOptions): Observable<ResponseOptions>;
109-
protected delete(collection: any[], {id, collectionName, headers, url}: RequestInfo): ResponseOptions;
109+
protected delete({collection, collectionName, headers, id, url}: RequestInfo): ResponseOptions;
110110
/**
111111
* Find first instance of item in collection by `item.id`
112112
* @param collection
@@ -132,7 +132,7 @@ export declare abstract class BackendService {
132132
protected genIdDefault<T extends {
133133
id: any;
134134
}>(collection: T[]): any;
135-
protected get(collection: any[], {id, query, collectionName, headers, url}: RequestInfo): ResponseOptions;
135+
protected get({collection, collectionName, headers, id, query, url}: RequestInfo): ResponseOptions;
136136
/** Get JSON body from the request object */
137137
protected abstract getJsonBody(req: any): any;
138138
/**
@@ -172,8 +172,8 @@ export declare abstract class BackendService {
172172
* To replace this default method, assign your alternative to your InMemDbService['parseRequestUrl']
173173
*/
174174
protected parseRequestUrl(url: string): ParsedRequestUrl;
175-
protected post(collection: any[], {id, collectionName, headers, req, resourceUrl, url}: RequestInfo): ResponseOptions;
176-
protected put(collection: any[], {id, collectionName, headers, req, url}: RequestInfo): ResponseOptions;
175+
protected post({collection, collectionName, headers, id, req, resourceUrl, url}: RequestInfo): ResponseOptions;
176+
protected put({collection, collectionName, headers, id, req, url}: RequestInfo): ResponseOptions;
177177
protected removeById(collection: any[], id: number): boolean;
178178
protected readonly requestInfoUtils: RequestInfoUtilities;
179179
/**

backend.service.js

Lines changed: 17 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

backend.service.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bundles/in-memory-web-api.umd.js

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
(function (global, factory) {
2-
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('rxjs/Observable'), require('rxjs/BehaviorSubject'), require('rxjs/observable/of'), require('rxjs/observable/fromPromise'), require('rxjs/util/isPromise'), require('rxjs/operator/delay'), require('rxjs/operator/filter'), require('rxjs/operator/first'), require('rxjs/operator/switchMap'), require('@angular/core'), require('@angular/http'), require('rxjs/operator/map'), require('@angular/common/http')) :
3-
typeof define === 'function' && define.amd ? define(['exports', 'rxjs/Observable', 'rxjs/BehaviorSubject', 'rxjs/observable/of', 'rxjs/observable/fromPromise', 'rxjs/util/isPromise', 'rxjs/operator/delay', 'rxjs/operator/filter', 'rxjs/operator/first', 'rxjs/operator/switchMap', '@angular/core', '@angular/http', 'rxjs/operator/map', '@angular/common/http'], factory) :
4-
(factory((global.ng = global.ng || {}, global.ng.inMemoryWebApi = {}),global.Rx,global.rxjs_BehaviorSubject,global.rxjs_observable_of,global.rxjs_observable_fromPromise,global.rxjs_util_isPromise,global.rxjs_operator_delay,global.rxjs_operator_filter,global.rxjs_operator_first,global.rxjs_operator_switchMap,global.ng.core,global.ng.http,global.rxjs_operator_map,global._angular_common_http));
5-
}(this, (function (exports,rxjs_Observable,rxjs_BehaviorSubject,rxjs_observable_of,rxjs_observable_fromPromise,rxjs_util_isPromise,rxjs_operator_delay,rxjs_operator_filter,rxjs_operator_first,rxjs_operator_switchMap,_angular_core,_angular_http,rxjs_operator_map,_angular_common_http) { 'use strict';
2+
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('rxjs/Observable'), require('rxjs/BehaviorSubject'), require('rxjs/observable/of'), require('rxjs/observable/fromPromise'), require('rxjs/util/isPromise'), require('rxjs/operator/concatMap'), require('rxjs/operator/delay'), require('rxjs/operator/filter'), require('rxjs/operator/first'), require('@angular/core'), require('@angular/http'), require('rxjs/operator/map'), require('@angular/common/http')) :
3+
typeof define === 'function' && define.amd ? define(['exports', 'rxjs/Observable', 'rxjs/BehaviorSubject', 'rxjs/observable/of', 'rxjs/observable/fromPromise', 'rxjs/util/isPromise', 'rxjs/operator/concatMap', 'rxjs/operator/delay', 'rxjs/operator/filter', 'rxjs/operator/first', '@angular/core', '@angular/http', 'rxjs/operator/map', '@angular/common/http'], factory) :
4+
(factory((global.ng = global.ng || {}, global.ng.inMemoryWebApi = {}),global.Rx,global.rxjs_BehaviorSubject,global.rxjs_observable_of,global.rxjs_observable_fromPromise,global.rxjs_util_isPromise,global.rxjs_operator_concatMap,global.rxjs_operator_delay,global.rxjs_operator_filter,global.rxjs_operator_first,global.ng.core,global.ng.http,global.rxjs_operator_map,global._angular_common_http));
5+
}(this, (function (exports,rxjs_Observable,rxjs_BehaviorSubject,rxjs_observable_of,rxjs_observable_fromPromise,rxjs_util_isPromise,rxjs_operator_concatMap,rxjs_operator_delay,rxjs_operator_filter,rxjs_operator_first,_angular_core,_angular_http,rxjs_operator_map,_angular_common_http) { 'use strict';
66

77
var STATUS = {
88
CONTINUE: 100,
@@ -618,7 +618,7 @@ var BackendService = (function () {
618618
BackendService.prototype.handleRequest = function (req) {
619619
var _this = this;
620620
// handle the request when there is an in-memory database
621-
return rxjs_operator_switchMap.switchMap.call(this.dbReady, function () { return _this.handleRequest_(req); });
621+
return rxjs_operator_concatMap.concatMap.call(this.dbReady, function () { return _this.handleRequest_(req); });
622622
};
623623
BackendService.prototype.handleRequest_ = function (req) {
624624
var _this = this;
@@ -629,9 +629,11 @@ var BackendService = (function () {
629629
var parsed = (parser && parser(url, this.requestInfoUtils)) ||
630630
this.parseRequestUrl(url);
631631
var collectionName = parsed.collectionName;
632+
var collection = this.db[collectionName];
632633
var reqInfo = {
633634
req: req,
634635
apiBase: parsed.apiBase,
636+
collection: collection,
635637
collectionName: collectionName,
636638
headers: this.createHeaders({ 'Content-Type': 'application/json' }),
637639
id: this.parseId(parsed.id),
@@ -723,16 +725,16 @@ var BackendService = (function () {
723725
var resOptions;
724726
switch (reqInfo.method) {
725727
case 'get':
726-
resOptions = this.get(collection, reqInfo);
728+
resOptions = this.get(reqInfo);
727729
break;
728730
case 'post':
729-
resOptions = this.post(collection, reqInfo);
731+
resOptions = this.post(reqInfo);
730732
break;
731733
case 'put':
732-
resOptions = this.put(collection, reqInfo);
734+
resOptions = this.put(reqInfo);
733735
break;
734736
case 'delete':
735-
resOptions = this.delete(collection, reqInfo);
737+
resOptions = this.delete(reqInfo);
736738
break;
737739
default:
738740
resOptions = this.createErrorResponseOptions(reqInfo.url, STATUS.METHOD_NOT_ALLOWED, 'Method not allowed');
@@ -767,7 +769,7 @@ var BackendService = (function () {
767769
};
768770
switch (command) {
769771
case 'resetdb':
770-
return rxjs_operator_switchMap.switchMap.call(this.resetDb(reqInfo), function () { return _this.createResponse$(function () { return resOptions; }, false /* no delay */); });
772+
return rxjs_operator_concatMap.concatMap.call(this.resetDb(reqInfo), function () { return _this.createResponse$(function () { return resOptions; }, false /* no delay */); });
771773
case 'config':
772774
if (method === 'get') {
773775
resOptions.body = this.clone(this.config);
@@ -826,8 +828,8 @@ var BackendService = (function () {
826828
return function () { }; // unsubscribe function
827829
});
828830
};
829-
BackendService.prototype.delete = function (collection, _a) {
830-
var id = _a.id, collectionName = _a.collectionName, headers = _a.headers, url = _a.url;
831+
BackendService.prototype.delete = function (_a) {
832+
var collection = _a.collection, collectionName = _a.collectionName, headers = _a.headers, id = _a.id, url = _a.url;
831833
// tslint:disable-next-line:triple-equals
832834
if (id == undefined) {
833835
return this.createErrorResponseOptions(url, STATUS.NOT_FOUND, "Missing \"" + collectionName + "\" id");
@@ -875,8 +877,8 @@ var BackendService = (function () {
875877
}, undefined);
876878
return maxId + 1;
877879
};
878-
BackendService.prototype.get = function (collection, _a) {
879-
var id = _a.id, query = _a.query, collectionName = _a.collectionName, headers = _a.headers, url = _a.url;
880+
BackendService.prototype.get = function (_a) {
881+
var collection = _a.collection, collectionName = _a.collectionName, headers = _a.headers, id = _a.id, query = _a.query, url = _a.url;
880882
var data = collection;
881883
// tslint:disable-next-line:triple-equals
882884
if (id != undefined && id !== '') {
@@ -990,8 +992,8 @@ var BackendService = (function () {
990992
};
991993
// Create entity
992994
// Can update an existing entity too if post409 is false.
993-
BackendService.prototype.post = function (collection, _a) {
994-
var id = _a.id, collectionName = _a.collectionName, headers = _a.headers, req = _a.req, resourceUrl = _a.resourceUrl, url = _a.url;
995+
BackendService.prototype.post = function (_a) {
996+
var collection = _a.collection, collectionName = _a.collectionName, headers = _a.headers, id = _a.id, req = _a.req, resourceUrl = _a.resourceUrl, url = _a.url;
995997
var item = this.getJsonBody(req);
996998
// tslint:disable-next-line:triple-equals
997999
if (item.id == undefined) {
@@ -1022,8 +1024,8 @@ var BackendService = (function () {
10221024
};
10231025
// Update existing entity
10241026
// Can create an entity too if put404 is false.
1025-
BackendService.prototype.put = function (collection, _a) {
1026-
var id = _a.id, collectionName = _a.collectionName, headers = _a.headers, req = _a.req, url = _a.url;
1027+
BackendService.prototype.put = function (_a) {
1028+
var collection = _a.collection, collectionName = _a.collectionName, headers = _a.headers, id = _a.id, req = _a.req, url = _a.url;
10271029
var item = this.getJsonBody(req);
10281030
// tslint:disable-next-line:triple-equals
10291031
if (item.id == undefined) {

interfaces.d.ts

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -98,23 +98,6 @@ export declare abstract class InMemoryBackendConfigArgs {
9898
export declare class InMemoryBackendConfig implements InMemoryBackendConfigArgs {
9999
constructor(config?: InMemoryBackendConfigArgs);
100100
}
101-
/** Interface of information about a Uri */
102-
export interface UriInfo {
103-
source: string;
104-
protocol: string;
105-
authority: string;
106-
userInfo: string;
107-
user: string;
108-
password: string;
109-
host: string;
110-
port: string;
111-
relative: string;
112-
path: string;
113-
directory: string;
114-
file: string;
115-
query: string;
116-
anchor: string;
117-
}
118101
/** Return information (UriInfo) about a URI */
119102
export declare function parseUri(str: string): UriInfo;
120103
/**
@@ -157,6 +140,7 @@ export interface RequestInfo {
157140
req: RequestCore;
158141
apiBase: string;
159142
collectionName: string;
143+
collection: any;
160144
headers: HeadersCore;
161145
method: string;
162146
id: any;
@@ -228,3 +212,20 @@ export interface ResponseOptions {
228212
*/
229213
url?: string;
230214
}
215+
/** Interface of information about a Uri */
216+
export interface UriInfo {
217+
source: string;
218+
protocol: string;
219+
authority: string;
220+
userInfo: string;
221+
user: string;
222+
password: string;
223+
host: string;
224+
port: string;
225+
relative: string;
226+
path: string;
227+
directory: string;
228+
file: string;
229+
query: string;
230+
anchor: string;
231+
}

0 commit comments

Comments
 (0)
0