Skip to content

Commit 354d8e6

Browse files
committed
Make clearOnUpdate work with full URLs
- Safer string handling in getTypeID - clearRecords and addRecords are equivalent so don't duplicate the work.
1 parent 00e2886 commit 354d8e6

File tree

5 files changed

+22
-9
lines changed

5 files changed

+22
-9
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jsonapi-vuex",
3-
"version": "5.12.0",
3+
"version": "5.13.0",
44
"description": "Access restructured JSONAPI data from a Vuex Store.",
55
"author": "Matthew Richardson <[email protected]>",
66
"scripts": {
@@ -37,7 +37,7 @@
3737
"babel-polyfill": "^6.26.0",
3838
"chai": "^4.3.7",
3939
"chai-as-promised": "^7.1.1",
40-
"chromedriver": "^110.0.0",
40+
"chromedriver": "^113.0.0",
4141
"concurrently": "^7.6.0",
4242
"core-js": "^3.29.1",
4343
"eslint": "^8.36.0",

src/actions.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,18 @@ const actions = (api, conf) => {
9393
merge(apiConf, config)
9494
return api(apiConf).then((results) => {
9595
let resData = utils.jsonapiToNorm(results.data.data)
96-
context.commit('addRecords', resData)
9796
let [type, id] = utils.getTypeId(data)
9897
if (!id && conf.clearOnUpdate) {
9998
let record = resData
100-
if (Object.keys(resData).length === 0) {
99+
if (Object.keys(resData).length === 0 && type) {
101100
// No records - assume type == endpoint
102101
record = { _jv: { type: type } }
103102
}
104-
context.commit('clearRecords', record)
103+
if (record) {
104+
context.commit('clearRecords', record)
105+
}
106+
} else {
107+
context.commit('addRecords', resData)
105108
}
106109
utils.processIncludedRecords(context, results)
107110
resData = utils.checkAndFollowRelationships(context.state, context.getters, resData)

src/lib.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,13 @@ const Utils = class {
310310
getTypeId(data, encode = true) {
311311
let type, id, rel
312312
if (typeof data === 'string') {
313+
try {
314+
// If the data param is a full URL, we can't extract type/id/rel from it
315+
new URL(data)
316+
return []
317+
} catch {
318+
// Continue if not a full URL
319+
}
313320
;[type, id, rel] = data.replace(/^\//, '').split('/')
314321
} else {
315322
;({ type, id } = data[this.jvtag])

tests/unit/jsonapi-vuex.spec.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,9 @@ describe('jsonapi-vuex tests', function () {
333333
it('should get type only from string', function () {
334334
expect(utils.getTypeId('widget')).to.deep.equal(['widget'])
335335
})
336+
it('should not get type & id from a full URL', function () {
337+
expect(utils.getTypeId('https://www.example.com/api/widget')).to.deep.equal([])
338+
})
336339
it('should get type, id & relname from string', function () {
337340
expect(utils.getTypeId('widget/1/relname')).to.deep.equal(['widget', '1', 'relname'])
338341
})

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2849,10 +2849,10 @@ chrome-trace-event@^1.0.2:
28492849
resolved "https://registry.yarnpkg.com/chrome-trace-event/-/chrome-trace-event-1.0.3.tgz#1015eced4741e15d06664a957dbbf50d041e26ac"
28502850
integrity sha512-p3KULyQg4S7NIHixdwbGX+nFHkoBiA4YQmyWtjb8XngSKV124nJmRysgAeujbUVb15vh+RvFUfCPqU7rXk+hZg==
28512851

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

0 commit comments

Comments
 (0)