Skip to content

Commit

Permalink
Make clearOnUpdate work with full URLs
Browse files Browse the repository at this point in the history
- Safer string handling in getTypeID
- clearRecords and addRecords are equivalent so don't duplicate the work.
  • Loading branch information
mrichar1 committed Jun 28, 2023
1 parent 00e2886 commit 354d8e6
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 9 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jsonapi-vuex",
"version": "5.12.0",
"version": "5.13.0",
"description": "Access restructured JSONAPI data from a Vuex Store.",
"author": "Matthew Richardson <[email protected]>",
"scripts": {
Expand Down Expand Up @@ -37,7 +37,7 @@
"babel-polyfill": "^6.26.0",
"chai": "^4.3.7",
"chai-as-promised": "^7.1.1",
"chromedriver": "^110.0.0",
"chromedriver": "^113.0.0",
"concurrently": "^7.6.0",
"core-js": "^3.29.1",
"eslint": "^8.36.0",
Expand Down
9 changes: 6 additions & 3 deletions src/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,18 @@ const actions = (api, conf) => {
merge(apiConf, config)
return api(apiConf).then((results) => {
let resData = utils.jsonapiToNorm(results.data.data)
context.commit('addRecords', resData)
let [type, id] = utils.getTypeId(data)
if (!id && conf.clearOnUpdate) {
let record = resData
if (Object.keys(resData).length === 0) {
if (Object.keys(resData).length === 0 && type) {
// No records - assume type == endpoint
record = { _jv: { type: type } }
}
context.commit('clearRecords', record)
if (record) {
context.commit('clearRecords', record)
}
} else {
context.commit('addRecords', resData)
}
utils.processIncludedRecords(context, results)
resData = utils.checkAndFollowRelationships(context.state, context.getters, resData)
Expand Down
7 changes: 7 additions & 0 deletions src/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,13 @@ const Utils = class {
getTypeId(data, encode = true) {
let type, id, rel
if (typeof data === 'string') {
try {
// If the data param is a full URL, we can't extract type/id/rel from it
new URL(data)
return []
} catch {
// Continue if not a full URL
}
;[type, id, rel] = data.replace(/^\//, '').split('/')
} else {
;({ type, id } = data[this.jvtag])
Expand Down
3 changes: 3 additions & 0 deletions tests/unit/jsonapi-vuex.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,9 @@ describe('jsonapi-vuex tests', function () {
it('should get type only from string', function () {
expect(utils.getTypeId('widget')).to.deep.equal(['widget'])
})
it('should not get type & id from a full URL', function () {
expect(utils.getTypeId('https://www.example.com/api/widget')).to.deep.equal([])
})
it('should get type, id & relname from string', function () {
expect(utils.getTypeId('widget/1/relname')).to.deep.equal(['widget', '1', 'relname'])
})
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2849,10 +2849,10 @@ chrome-trace-event@^1.0.2:
resolved "https://registry.yarnpkg.com/chrome-trace-event/-/chrome-trace-event-1.0.3.tgz#1015eced4741e15d06664a957dbbf50d041e26ac"
integrity sha512-p3KULyQg4S7NIHixdwbGX+nFHkoBiA4YQmyWtjb8XngSKV124nJmRysgAeujbUVb15vh+RvFUfCPqU7rXk+hZg==

chromedriver@^110.0.0:
version "110.0.0"
resolved "https://registry.yarnpkg.com/chromedriver/-/chromedriver-110.0.0.tgz#d00a1a2976592d933faa8e9839e97692922834a4"
integrity sha512-Le6q8xrA/3fAt+g8qiN0YjsYxINIhQMC6wj9X3W5L77uN4NspEzklDrqYNwBcEVn7PcAEJ73nLlS7mTyZRspHA==
chromedriver@^113.0.0:
version "113.0.0"
resolved "https://registry.yarnpkg.com/chromedriver/-/chromedriver-113.0.0.tgz#d4855f156ee51cea4282e04aadd29fa154e44dbb"
integrity sha512-UnQlt2kPicYXVNHPzy9HfcWvEbKJjjKAEaatdcnP/lCIRwuSoZFVLH0HVDAGdbraXp3dNVhfE2Qx7gw8TnHnPw==
dependencies:
"@testim/chrome-version" "^1.1.3"
axios "^1.2.1"
Expand Down

0 comments on commit 354d8e6

Please sign in to comment.