Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit 71b1894

Browse files
authored
fix: parseId only converts id to number if the collection ids are numeric. (#138)
In early v0.4.x refactor, accidentally removed test for numeric collection ids which lead to issue #136. This PR fixes that problem, tests it, and strengthens error handling. Breaking change: `genIdDefault` will only generate ids for collections with numeric id; throws error otherwise. Updated `rollup.config.js` so it doesn’t fail. Still weirdly runs an unspecified `cjs` rollup config that I can’t find (which produces numerous warnings) before doing the right thing and running the `mud` config. Also does not work if follow instructions and use the `output` property of `rollup.config.js`; does work when I do it “wrong” and put the options in the root. closes #136
1 parent bb8b85f commit 71b1894

17 files changed

+526
-204
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ 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.4"></a>
12+
## 0.4.4 (2017-09-11)
13+
closes #136
14+
15+
A **breaking change** if you expected `genId` to generate ids for a collection
16+
with non-numeric `item.id`.
17+
1118
<a id="0.4.3"></a>
1219
## 0.4.3 (2017-09-11)
1320
Refactoring for clarity and to correctly reflect intent.

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,4 +427,16 @@ these tests reference the library source files.
427427

428428
The `karma-test-shim.js` add `in-mem` to the list of app folders that SystemJS should resolve.
429429

430+
## Rollup
431+
432+
The gulp "umd" task runs rollup for tree-shaking.
433+
434+
I don't remember it ever working without a lot of warnings.
435+
In v.0.4.x, updated to v.0.49 ... which required updating of the `rollup.config.js`.
436+
437+
Still weirdly runs an unspecified `cjs` rollup config first that I can’t find (which produces numerous warnings) before doing the right thing and running the `umd` config.
438+
439+
Also does not work if follow instructions and use the `output` property of `rollup.config.js`; does work when config it “wrong” and put the options in the root.
440+
441+
Ignoring these issues for now.
430442

backend.service.d.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export declare abstract class BackendService {
1414
protected db: Object;
1515
protected dbReadySubject: BehaviorSubject<boolean>;
1616
private passThruBackend;
17+
protected requestInfoUtils: RequestInfoUtilities;
1718
constructor(inMemDbService: InMemoryDbService, config?: InMemoryBackendConfigArgs);
1819
protected readonly dbReady: Observable<boolean>;
1920
/**
@@ -117,21 +118,22 @@ export declare abstract class BackendService {
117118
}>(collection: T[], id: any): T;
118119
/**
119120
* Generate the next available id for item in this collection
120-
* @param collection - collection of items with `id` key property
121121
* Use method from `inMemDbService` if it exists and returns a value,
122-
* else delegates to genIdDefault
122+
* else delegates to `genIdDefault`.
123+
* @param collection - collection of items with `id` key property
123124
*/
124125
protected genId<T extends {
125126
id: any;
126-
}>(collection: T[]): any;
127+
}>(collection: T[], collectionName: string): any;
127128
/**
128129
* Default generator of the next available id for item in this collection
130+
* This default implementation works only for numeric ids.
129131
* @param collection - collection of items with `id` key property
130-
* This default implementation assumes integer ids; returns `1` otherwise
132+
* @param collectionName - name of the collection
131133
*/
132134
protected genIdDefault<T extends {
133135
id: any;
134-
}>(collection: T[]): any;
136+
}>(collection: T[], collectionName: string): any;
135137
protected get({collection, collectionName, headers, id, query, url}: RequestInfo): ResponseOptions;
136138
/** Get JSON body from the request object */
137139
protected abstract getJsonBody(req: any): any;
@@ -144,6 +146,11 @@ export declare abstract class BackendService {
144146
* through to the "real" backend.
145147
*/
146148
protected getPassThruBackend(): PassThruBackend;
149+
/**
150+
* Get utility methods from this service instance.
151+
* Useful within an HTTP method override
152+
*/
153+
protected getRequestInfoUtils(): RequestInfoUtilities;
147154
/**
148155
* return canonical HTTP method name (lowercase) from the request object
149156
* e.g. (req.method || 'get').toLowerCase();
@@ -153,7 +160,14 @@ export declare abstract class BackendService {
153160
protected abstract getRequestMethod(req: any): string;
154161
protected indexOf(collection: any[], id: number): number;
155162
/** Parse the id as a number. Return original value if not a number. */
156-
protected parseId(id: string): any;
163+
protected parseId(collection: any[], collectionName: string, id: string): any;
164+
/**
165+
* return true if can determine that the collection's `item.id` is a number
166+
* This implementation can't tell if the collection is empty so it assumes NO
167+
* */
168+
protected isCollectionIdNumeric<T extends {
169+
id: any;
170+
}>(collection: T[], collectionName: string): boolean;
157171
/**
158172
* Parses the request URL into a `ParsedRequestUrl` object.
159173
* Parsing depends upon certain values of `config`: `apiBase`, `host`, and `urlRoot`.
@@ -175,7 +189,6 @@ export declare abstract class BackendService {
175189
protected post({collection, collectionName, headers, id, req, resourceUrl, url}: RequestInfo): ResponseOptions;
176190
protected put({collection, collectionName, headers, id, req, url}: RequestInfo): ResponseOptions;
177191
protected removeById(collection: any[], id: number): boolean;
178-
protected readonly requestInfoUtils: RequestInfoUtilities;
179192
/**
180193
* Tell your in-mem "database" to reset.
181194
* returns Observable of the database because resetting it could be async

backend.service.js

Lines changed: 68 additions & 29 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.

backend.service.metadata.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
[{"__symbolic":"module","version":3,"metadata":{"BackendService":{"__symbolic":"class","members":{"__ctor__":[{"__symbolic":"constructor","parameters":[{"__symbolic":"reference","module":"./interfaces","name":"InMemoryDbService"},{"__symbolic":"reference","module":"./interfaces","name":"InMemoryBackendConfigArgs"}]}],"handleRequest":[{"__symbolic":"method"}],"handleRequest_":[{"__symbolic":"method"}],"addDelay":[{"__symbolic":"method"}],"applyQuery":[{"__symbolic":"method"}],"bind":[{"__symbolic":"method"}],"bodify":[{"__symbolic":"method"}],"clone":[{"__symbolic":"method"}],"collectionHandler":[{"__symbolic":"method"}],"commands":[{"__symbolic":"method"}],"createErrorResponseOptions":[{"__symbolic":"method"}],"createHeaders":[{"__symbolic":"method"}],"createPassThruBackend":[{"__symbolic":"method"}],"createQueryMap":[{"__symbolic":"method"}],"createResponse$":[{"__symbolic":"method"}],"createResponse$fromResponseOptions$":[{"__symbolic":"method"}],"createResponseOptions$":[{"__symbolic":"method"}],"delete":[{"__symbolic":"method"}],"findById":[{"__symbolic":"method"}],"genId":[{"__symbolic":"method"}],"genIdDefault":[{"__symbolic":"method"}],"get":[{"__symbolic":"method"}],"getJsonBody":[{"__symbolic":"method"}],"getLocation":[{"__symbolic":"method"}],"getPassThruBackend":[{"__symbolic":"method"}],"getRequestMethod":[{"__symbolic":"method"}],"indexOf":[{"__symbolic":"method"}],"parseId":[{"__symbolic":"method"}],"parseRequestUrl":[{"__symbolic":"method"}],"post":[{"__symbolic":"method"}],"put":[{"__symbolic":"method"}],"removeById":[{"__symbolic":"method"}],"resetDb":[{"__symbolic":"method"}]}}}},{"__symbolic":"module","version":1,"metadata":{"BackendService":{"__symbolic":"class","members":{"__ctor__":[{"__symbolic":"constructor","parameters":[{"__symbolic":"reference","module":"./interfaces","name":"InMemoryDbService"},{"__symbolic":"reference","module":"./interfaces","name":"InMemoryBackendConfigArgs"}]}],"handleRequest":[{"__symbolic":"method"}],"handleRequest_":[{"__symbolic":"method"}],"addDelay":[{"__symbolic":"method"}],"applyQuery":[{"__symbolic":"method"}],"bind":[{"__symbolic":"method"}],"bodify":[{"__symbolic":"method"}],"clone":[{"__symbolic":"method"}],"collectionHandler":[{"__symbolic":"method"}],"commands":[{"__symbolic":"method"}],"createErrorResponseOptions":[{"__symbolic":"method"}],"createHeaders":[{"__symbolic":"method"}],"createPassThruBackend":[{"__symbolic":"method"}],"createQueryMap":[{"__symbolic":"method"}],"createResponse$":[{"__symbolic":"method"}],"createResponse$fromResponseOptions$":[{"__symbolic":"method"}],"createResponseOptions$":[{"__symbolic":"method"}],"delete":[{"__symbolic":"method"}],"findById":[{"__symbolic":"method"}],"genId":[{"__symbolic":"method"}],"genIdDefault":[{"__symbolic":"method"}],"get":[{"__symbolic":"method"}],"getJsonBody":[{"__symbolic":"method"}],"getLocation":[{"__symbolic":"method"}],"getPassThruBackend":[{"__symbolic":"method"}],"getRequestMethod":[{"__symbolic":"method"}],"indexOf":[{"__symbolic":"method"}],"parseId":[{"__symbolic":"method"}],"parseRequestUrl":[{"__symbolic":"method"}],"post":[{"__symbolic":"method"}],"put":[{"__symbolic":"method"}],"removeById":[{"__symbolic":"method"}],"resetDb":[{"__symbolic":"method"}]}}}}]
1+
[{"__symbolic":"module","version":3,"metadata":{"BackendService":{"__symbolic":"class","members":{"__ctor__":[{"__symbolic":"constructor","parameters":[{"__symbolic":"reference","module":"./interfaces","name":"InMemoryDbService"},{"__symbolic":"reference","module":"./interfaces","name":"InMemoryBackendConfigArgs"}]}],"handleRequest":[{"__symbolic":"method"}],"handleRequest_":[{"__symbolic":"method"}],"addDelay":[{"__symbolic":"method"}],"applyQuery":[{"__symbolic":"method"}],"bind":[{"__symbolic":"method"}],"bodify":[{"__symbolic":"method"}],"clone":[{"__symbolic":"method"}],"collectionHandler":[{"__symbolic":"method"}],"commands":[{"__symbolic":"method"}],"createErrorResponseOptions":[{"__symbolic":"method"}],"createHeaders":[{"__symbolic":"method"}],"createPassThruBackend":[{"__symbolic":"method"}],"createQueryMap":[{"__symbolic":"method"}],"createResponse$":[{"__symbolic":"method"}],"createResponse$fromResponseOptions$":[{"__symbolic":"method"}],"createResponseOptions$":[{"__symbolic":"method"}],"delete":[{"__symbolic":"method"}],"findById":[{"__symbolic":"method"}],"genId":[{"__symbolic":"method"}],"genIdDefault":[{"__symbolic":"method"}],"get":[{"__symbolic":"method"}],"getJsonBody":[{"__symbolic":"method"}],"getLocation":[{"__symbolic":"method"}],"getPassThruBackend":[{"__symbolic":"method"}],"getRequestInfoUtils":[{"__symbolic":"method"}],"getRequestMethod":[{"__symbolic":"method"}],"indexOf":[{"__symbolic":"method"}],"parseId":[{"__symbolic":"method"}],"isCollectionIdNumeric":[{"__symbolic":"method"}],"parseRequestUrl":[{"__symbolic":"method"}],"post":[{"__symbolic":"method"}],"put":[{"__symbolic":"method"}],"removeById":[{"__symbolic":"method"}],"resetDb":[{"__symbolic":"method"}]}}}},{"__symbolic":"module","version":1,"metadata":{"BackendService":{"__symbolic":"class","members":{"__ctor__":[{"__symbolic":"constructor","parameters":[{"__symbolic":"reference","module":"./interfaces","name":"InMemoryDbService"},{"__symbolic":"reference","module":"./interfaces","name":"InMemoryBackendConfigArgs"}]}],"handleRequest":[{"__symbolic":"method"}],"handleRequest_":[{"__symbolic":"method"}],"addDelay":[{"__symbolic":"method"}],"applyQuery":[{"__symbolic":"method"}],"bind":[{"__symbolic":"method"}],"bodify":[{"__symbolic":"method"}],"clone":[{"__symbolic":"method"}],"collectionHandler":[{"__symbolic":"method"}],"commands":[{"__symbolic":"method"}],"createErrorResponseOptions":[{"__symbolic":"method"}],"createHeaders":[{"__symbolic":"method"}],"createPassThruBackend":[{"__symbolic":"method"}],"createQueryMap":[{"__symbolic":"method"}],"createResponse$":[{"__symbolic":"method"}],"createResponse$fromResponseOptions$":[{"__symbolic":"method"}],"createResponseOptions$":[{"__symbolic":"method"}],"delete":[{"__symbolic":"method"}],"findById":[{"__symbolic":"method"}],"genId":[{"__symbolic":"method"}],"genIdDefault":[{"__symbolic":"method"}],"get":[{"__symbolic":"method"}],"getJsonBody":[{"__symbolic":"method"}],"getLocation":[{"__symbolic":"method"}],"getPassThruBackend":[{"__symbolic":"method"}],"getRequestInfoUtils":[{"__symbolic":"method"}],"getRequestMethod":[{"__symbolic":"method"}],"indexOf":[{"__symbolic":"method"}],"parseId":[{"__symbolic":"method"}],"isCollectionIdNumeric":[{"__symbolic":"method"}],"parseRequestUrl":[{"__symbolic":"method"}],"post":[{"__symbolic":"method"}],"put":[{"__symbolic":"method"}],"removeById":[{"__symbolic":"method"}],"resetDb":[{"__symbolic":"method"}]}}}}]

0 commit comments

Comments
 (0)