diff --git a/CHANGELOG.md b/CHANGELOG.md index 6e22665..b6935b9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,30 @@ # Changelog +## `2.1.7` + +:tophat: + + - Added `mustGet` and `mustFind` as whinny versions of `get` and `find` + +## `2.1.6` + +:nail_care: + + - Added flow types in the build process + - Updated dependencies + +## `2.1.5` + +:nail_care: + + - Improved error handling (kudos to @p3drosola) + +## `2.1.4` + +:bug: Bugfix: + + - Stop publishing `babelrc` since it breaks React native + ## `2.1.3` :bug: Bugfix: diff --git a/package.json b/package.json index 60b9b63..ce1dd90 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mobx-rest", - "version": "2.1.6", + "version": "2.1.7", "description": "REST conventions for mobx.", "repository": { "type": "git", @@ -31,7 +31,7 @@ }, "devDependencies": { "babel-cli": "^6.24.1", - "babel-core": "^6.24.1", + "babel-core": "^6.25.0", "babel-eslint": "^7.2.3", "babel-jest": "^20.0.3", "babel-plugin-transform-async-to-generator": "^6.24.1", diff --git a/src/Collection.js b/src/Collection.js index fff6de4..4bdb0bb 100644 --- a/src/Collection.js +++ b/src/Collection.js @@ -95,6 +95,17 @@ export default class Collection { return this.models.find(item => item.id === id) } + /** + * The whinny version of the `get` method + */ + mustGet (id: Id): T { + const model = this.get(id) + + if (!model) throw Error(`Invariant: Model must be found with id: ${id}`) + + return model + } + /** * Get resources matching criteria */ @@ -113,6 +124,20 @@ export default class Collection { }) } + /** + * The whinny version of `find` + */ + mustFind (query: { [key: string]: mixed }): T { + const model = this.find(query) + + if (!model) { + const conditions = JSON.stringify(query) + throw Error(`Invariant: Model must be found with: ${conditions}`) + } + + return model + } + /** * Adds a collection of models. * Returns the added models.