Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"main": "dist/bundle.js",
"scripts": {
"build": "cross-env NODE_ENV=production webpack",
"build-dev": "cross-env NODE_ENV=development webpack",
"clean": "cross-env rimraf dist coverage lib",
"coverage": "cross-env npm run build && jest --coverage",
"lint": "cross-env eslint src --ext .js,.ts",
Expand Down Expand Up @@ -83,4 +84,4 @@
"webpack-cli": "4.9.1",
"webpack-node-externals": "3.0.0"
}
}
}
11 changes: 10 additions & 1 deletion src/LoadingStoreValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,16 @@ class LoadingStoreValue implements Resource {

// Proxy to all other unknown properties: return a function that yields another LoadingStoreValue
const loadProperty = loadResource.then(resource => resource[prop])
const result = templateParams => new LoadingStoreValue(loadProperty.then(property => property(templateParams)._meta.load))

const result = templateParams => new LoadingStoreValue(loadProperty.then(property => {
try {
return property(templateParams)._meta.load
} catch (e) {
throw new Error(`Property '${prop.toString()}' on resource ${absoluteSelf} was used like a relation, but no relation with this name was returned by the API (actual return value: ${JSON.stringify(property)})`)
}
}
))

result.toString = () => ''
return result
}
Expand Down
36 changes: 36 additions & 0 deletions tests/store.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1467,6 +1467,42 @@ describe('API store', () => {
expect(vm.api.get('/camps/1').emptyArray).toEqual([])
done()
})

it('throws error when accessing non-existing property like a relation', async done => {
// given
axiosMock.onGet('http://localhost/').reply(200, root.serverResponse)

// when
let loadingObject = null
loadingObject = vm.api.get().nonexistingProperty()

// then (loading)
expect(loadingObject).toBeInstanceOf(LoadingStoreValue)
expect(loadingObject.toJSON()).toEqual('{}')

// then (loaded)
await expect(loadingObject._meta.load).rejects.toThrow("Property 'nonexistingProperty' on resource http://localhost was used like a relation, but no relation with this name was returned by the API (actual return value: undefined)")

done()
})

it('throws error when accessing primitive property like a relation', async done => {
// given
axiosMock.onGet('http://localhost/').reply(200, root.serverResponse)

// when
let loadingObject = null
loadingObject = vm.api.get().the()

// then (loading)
expect(loadingObject).toBeInstanceOf(LoadingStoreValue)
expect(loadingObject.toJSON()).toEqual('{}')

// then (loaded)
await expect(loadingObject._meta.load).rejects.toThrow("Property 'the' on resource http://localhost was used like a relation, but no relation with this name was returned by the API (actual return value: \"root\")")

done()
})
})
})
})
8 changes: 4 additions & 4 deletions webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var path = require('path')
var webpack = require('webpack')
var nodeExternals = require('webpack-node-externals')
const path = require('path')
const webpack = require('webpack')
const nodeExternals = require('webpack-node-externals')

module.exports = {
devtool: 'source-map',
Expand All @@ -16,7 +16,7 @@ module.exports = {
plugins: [
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify('production')
NODE_ENV: JSON.stringify(process.env.NODE_ENV)
}
})
],
Expand Down