Skip to content

Commit

Permalink
Resolves #250 Added a means to disable errors for missing resource de…
Browse files Browse the repository at this point in the history
…finitions.
  • Loading branch information
ibustosatlegalzoom authored and auvipy committed Jul 29, 2023
1 parent 759a3a3 commit 4e5ef27
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
11 changes: 11 additions & 0 deletions README.md
Expand Up @@ -95,6 +95,17 @@ Devour takes an object as the initializer. The following options are available:

**trailingSlash**: An optional object to use trailing slashes on resource and/or collection urls (defaults to false), `new JsonApi({apiUrl: 'http://your-api-here.com', trailingSlash: {resource: false, collection: true})`

### Errors

Devour may throw an error when a model definition is not yet defined in Devour's model directory. To disable such errors, use the `disableErrorsForMissingResourceDefinitions` flag like so:

```js
const jsonApi = new JsonApi({
apiUrl: 'http://your-api-here.com',
disableErrorsForMissingResourceDefinitions: true
})
```

### Relationships

Devour comes stock with an easy way of defining relationships which can be included when hitting your API.
Expand Down
14 changes: 11 additions & 3 deletions src/index.js
Expand Up @@ -69,7 +69,8 @@ class JsonApi {
resetBuilderOnCall: true,
auth: {},
bearer: null,
trailingSlash: { collection: false, resource: false }
trailingSlash: { collection: false, resource: false },
disableErrorsForMissingResourceDefinitions: false
}

const deprecatedConstructors = (args) => {
Expand All @@ -93,6 +94,7 @@ class JsonApi {
this.auth = options.auth
this.apiUrl = options.apiUrl
this.bearer = options.bearer
this.disableErrorsForMissingResourceDefinitions = options.disableErrorsForMissingResourceDefinitions
this.models = {}
this.deserialize = deserialize
this.serialize = serialize
Expand Down Expand Up @@ -417,9 +419,15 @@ class JsonApi {

modelFor (modelName) {
if (!this.models[modelName]) {
throw new Error(`API resource definition for model "${modelName}" not found. Available models: ${Object.keys(this.models)}`)
if (!this.disableErrorsForMissingResourceDefinitions) {
throw new Error(`API resource definition for model "${modelName}" not found. Available models: ${Object.keys(this.models)}`)
}
Logger.error(`API resource definition for model "${modelName}" not found. Available models: ${Object.keys(this.models)}`)
return {
attributes: {},
options: {}
}
}

return this.models[modelName]
}

Expand Down
8 changes: 8 additions & 0 deletions test/api/api-test.js
Expand Up @@ -911,6 +911,14 @@ describe('JsonApi', () => {
it.skip('should throw an error while attempting to access undefined model', function (done) {
expect(function () { jsonApi.findAll('derp').then(done).catch(done) }).to.throwException(/API resource definition for model/)
})

it('should not throw any errors accessing undefined models while the disableErrorsForMissingResourceDefinitions flag is enabled.', function () {
jsonApi.disableErrorsForMissingResourceDefinitions = true
expect(jsonApi.modelFor('derp')).to.be.an('object').and.to.eql({
attributes: {},
options: {}
})
})
})

describe('Complex API calls', () => {
Expand Down

0 comments on commit 4e5ef27

Please sign in to comment.