From caf900a1caa5046aff9a703ff29d476ee87f838c Mon Sep 17 00:00:00 2001 From: ibustos Date: Wed, 28 Jun 2023 13:33:46 -0500 Subject: [PATCH] Resolves #250 Added a means to disable errors for missing resource definitions. --- README.md | 11 +++++++++++ src/index.js | 14 +++++++++++--- test/api/api-test.js | 8 ++++++++ 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 8ba8aad..592155c 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/src/index.js b/src/index.js index 9e53db7..1950fb2 100644 --- a/src/index.js +++ b/src/index.js @@ -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) => { @@ -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 @@ -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] } diff --git a/test/api/api-test.js b/test/api/api-test.js index b8d14a9..61f4bfa 100644 --- a/test/api/api-test.js +++ b/test/api/api-test.js @@ -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', () => {