Skip to content

Commit d5996da

Browse files
committed
forbid patch/post/del on virtual resources (both in functions and in main/index function)
1 parent e88fadd commit d5996da

File tree

3 files changed

+70
-12
lines changed

3 files changed

+70
-12
lines changed

src/Resource.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,26 +74,14 @@ class Resource implements ResourceInterface {
7474
}
7575

7676
$post (data: unknown): Promise<ResourceInterface | null> {
77-
if (isVirtualResource(this)) {
78-
throw new Error('$post is not implemented for virtual resources')
79-
}
80-
8177
return this.apiActions.post(this._meta.self, data)
8278
}
8379

8480
$patch (data: unknown): Promise<ResourceInterface> {
85-
if (isVirtualResource(this)) {
86-
throw new Error('$patch is not implemented for virtual resources')
87-
}
88-
8981
return this.apiActions.patch(this._meta.self, data)
9082
}
9183

9284
$del (): Promise<string | void> {
93-
if (isVirtualResource(this)) {
94-
throw new Error('$del is not implemented for virtual resources')
95-
}
96-
9785
return this.apiActions.del(this._meta.self)
9886
}
9987

src/index.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@ function HalJsonVuex (store: Store<Record<string, State>>, axios: AxiosInstance,
6969
return Promise.reject(new Error(`Could not perform POST, "${uriOrCollection}" is not an entity or URI`))
7070
}
7171

72+
if (!isUnknown(uri)) {
73+
const entity = get(uri)
74+
if (isVirtualResource(entity)) {
75+
return Promise.reject(new Error('post is not implemented for virtual resources'))
76+
}
77+
}
78+
7279
return axios.post(axios.defaults.baseURL + uri, data).then(({ data, status }) => {
7380
if (status === 204) {
7481
return null
@@ -262,6 +269,13 @@ function HalJsonVuex (store: Store<Record<string, State>>, axios: AxiosInstance,
262269
}
263270
const existsInStore = !isUnknown(uri)
264271

272+
if (existsInStore) {
273+
const entity = get(uri)
274+
if (isVirtualResource(entity)) {
275+
return Promise.reject(new Error('patch is not implemented for virtual resources'))
276+
}
277+
}
278+
265279
if (!existsInStore) {
266280
store.commit('addEmpty', uri)
267281
}
@@ -320,6 +334,14 @@ function HalJsonVuex (store: Store<Record<string, State>>, axios: AxiosInstance,
320334
// Can't delete an unknown URI, do nothing
321335
return Promise.reject(new Error(`Could not perform DELETE, "${uriOrEntity}" is not an entity or URI`))
322336
}
337+
338+
if (!isUnknown(uri)) {
339+
const entity = get(uri)
340+
if (isVirtualResource(entity)) {
341+
return Promise.reject(new Error('del is not implemented for virtual resources'))
342+
}
343+
}
344+
323345
store.commit('deleting', uri)
324346
return axios.delete(axios.defaults.baseURL + uri).then(
325347
() => deleted(uri),

tests/apiOperations.spec.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,4 +648,52 @@ describe('Using dollar methods', () => {
648648
})
649649
done()
650650
})
651+
652+
it('throws error when deleting virtual resource', async done => {
653+
// given
654+
axiosMock.onGet('http://localhost/camps/1').replyOnce(200, embeddedCollection.serverResponse)
655+
const camp = await vm.api.get('/camps/1')._meta.load
656+
await letNetworkRequestFinish()
657+
658+
// when
659+
await expect(camp.periods().$del())
660+
661+
// then
662+
.rejects
663+
.toThrow('del is not implemented for virtual resources')
664+
665+
done()
666+
})
667+
668+
it('throws error when posting on virtual resource', async done => {
669+
// given
670+
axiosMock.onGet('http://localhost/camps/1').replyOnce(200, embeddedCollection.serverResponse)
671+
const camp = await vm.api.get('/camps/1')._meta.load
672+
await letNetworkRequestFinish()
673+
674+
// when
675+
await expect(camp.periods().$post({}))
676+
677+
// then
678+
.rejects
679+
.toThrow('post is not implemented for virtual resources')
680+
681+
done()
682+
})
683+
684+
it('throws error when patching an virtual resource', async done => {
685+
// given
686+
axiosMock.onGet('http://localhost/camps/1').replyOnce(200, embeddedCollection.serverResponse)
687+
const camp = await vm.api.get('/camps/1')._meta.load
688+
await letNetworkRequestFinish()
689+
690+
// when
691+
await expect(camp.periods().$patch([]))
692+
693+
// then
694+
.rejects
695+
.toThrow('patch is not implemented for virtual resources')
696+
697+
done()
698+
})
651699
})

0 commit comments

Comments
 (0)