Skip to content

Commit 4146a8a

Browse files
Merge pull request #302 from manuelmeister/feature/vue2-compatibility-v3
Make v3 compatible with vue2
2 parents 319633e + a4b74ea commit 4146a8a

File tree

5 files changed

+83
-41
lines changed

5 files changed

+83
-41
lines changed

package-lock.json

Lines changed: 40 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@
4747
],
4848
"authors": [
4949
"Carlo Beltrame",
50-
"Urban Suppiger"
50+
"Urban Suppiger",
51+
"Manuel Meister"
5152
],
5253
"license": "MIT",
5354
"bugs": {
@@ -56,7 +57,17 @@
5657
"homepage": "https://github.com/ecamp/hal-json-vuex#readme",
5758
"dependencies": {
5859
"hal-json-normalizer": "^4.2.0",
59-
"url-template": "^3.1.1"
60+
"url-template": "^3.1.1",
61+
"vue-demi": "^0.14.10"
62+
},
63+
"peerDependencies": {
64+
"@vue/composition-api": "^1.0.0-rc.1",
65+
"vue": "^2.0.0 || >=3.0.0"
66+
},
67+
"peerDependenciesMeta": {
68+
"@vue/composition-api": {
69+
"optional": true
70+
}
6071
},
6172
"devDependencies": {
6273
"@typescript-eslint/eslint-plugin": "5.10.1",

src/index.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import ResourceInterface from './interfaces/ResourceInterface'
1313
import StoreData, { Link, SerializablePromise } from './interfaces/StoreData'
1414
import ApiActions from './interfaces/ApiActions'
1515
import { isVirtualResource } from './halHelpers'
16+
import { App, isVue2, isVue3, Vue2 } from 'vue-demi'
1617

1718
/**
1819
* Defines the API store methods available in all Vue components. The methods can be called as follows:
@@ -444,17 +445,19 @@ function HalJsonVuex (store: Store<Record<string, State>>, axios: AxiosInstance,
444445
const halJsonVuex = { ...apiActions, purge, purgeAll, href, Resource, LoadingResource }
445446

446447
// eslint-disable-next-line @typescript-eslint/no-explicit-any
447-
function install (app: any) {
448-
if (app.version && app.version.charAt(0) === '3') {
449-
Object.defineProperties(app.config.globalProperties, {
450-
[opts.apiName]: {
451-
get () {
452-
return halJsonVuex
453-
}
454-
}
448+
function install (app: App | typeof Vue2) {
449+
if (isVue3) {
450+
Object.defineProperty(app.config.globalProperties, opts.apiName, {
451+
get: () => halJsonVuex,
452+
configurable: true
453+
})
454+
} else if (isVue2) {
455+
Object.defineProperty(app.prototype, opts.apiName, {
456+
get: () => halJsonVuex,
457+
configurable: true
455458
})
456459
} else {
457-
throw new Error('Vue2 detected: this version of hal-json-vuex is not compatible with Vue2')
460+
throw new Error('Neither Vue2 or Vue3 detected')
458461
}
459462
}
460463

src/storeModule.ts

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { del, set } from 'vue-demi'
2+
13
import StoreData from './interfaces/StoreData'
24

35
import { MutationTree } from 'vuex/types'
@@ -13,9 +15,7 @@ export const mutations: MutationTree<State> = {
1315
* @param uri URI of the object that is being fetched
1416
*/
1517
addEmpty (state: State, uri: string) : void {
16-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
17-
// @ts-ignore
18-
state[uri] = { _meta: { self: uri, loading: true } }
18+
set(state, uri, { _meta: { self: uri, loading: true } })
1919
},
2020
/**
2121
* Adds entities loaded from the API to the Vuex store.
@@ -24,12 +24,10 @@ export const mutations: MutationTree<State> = {
2424
*/
2525
add (state: State, data: Record<string, unknown>) : void {
2626
Object.keys(data).forEach(uri => {
27-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
28-
// @ts-ignore
29-
state[uri] = data[uri]
27+
set(state, uri, data[uri])
3028

31-
state[uri]._meta.loading = false
32-
state[uri]._meta.reloading = false
29+
set(state[uri]._meta, 'loading', false)
30+
set(state[uri]._meta, 'reloading', false)
3331
})
3432
},
3533
/**
@@ -38,23 +36,23 @@ export const mutations: MutationTree<State> = {
3836
* @param uri URI of the entity that is currently being reloaded
3937
*/
4038
reloading (state: State, uri: string) : void {
41-
if (state[uri]) state[uri]._meta.reloading = true
39+
if (state[uri]) set(state[uri]._meta, 'reloading', true)
4240
},
4341
/**
4442
* Marks a single entity in the Vuex store as normal again, after it has been marked as reloading before.
4543
* @param state Vuex state
4644
* @param uri URI of the entity that is currently being reloaded
4745
*/
4846
reloadingFailed (state: State, uri: string) : void {
49-
if (state[uri]) state[uri]._meta.reloading = false
47+
if (state[uri]) set(state[uri]._meta, 'reloading', false)
5048
},
5149
/**
5250
* Removes a single entity from the Vuex store.
5351
* @param state Vuex state
5452
* @param uri URI of the entity to be removed
5553
*/
5654
purge (state: State, uri: string) : void {
57-
delete state[uri]
55+
del(state, uri)
5856
},
5957
/**
6058
* Removes all entities from the Vuex store.
@@ -63,7 +61,7 @@ export const mutations: MutationTree<State> = {
6361
*/
6462
purgeAll (state: State) : void {
6563
Object.keys(state).forEach(uri => {
66-
delete state[uri]
64+
del(state, uri)
6765
})
6866
},
6967
/**
@@ -72,15 +70,15 @@ export const mutations: MutationTree<State> = {
7270
* @param uri URI of the entity that is currently being deleted
7371
*/
7472
deleting (state: State, uri: string) : void {
75-
if (state[uri]) state[uri]._meta.deleting = true
73+
if (state[uri]) set(state[uri]._meta, 'deleting', true)
7674
},
7775
/**
7876
* Marks a single entity in the Vuex store as normal again, after it has been marked as deleting before.
7977
* @param state Vuex state
8078
* @param uri URI of the entity that failed to be deleted
8179
*/
8280
deletingFailed (state: State, uri: string) : void {
83-
if (state[uri]) state[uri]._meta.deleting = false
81+
if (state[uri]) set(state[uri]._meta, 'deleting', false)
8482
}
8583
}
8684

vite.config.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,19 @@ export default defineConfig({
2121
},
2222
rollupOptions: {
2323
// externalize outputs if we have these modules as dependencies
24-
external: ['hal-json-normalizer', 'url-template'],
24+
external: ['hal-json-normalizer', 'url-template', 'vue-demi'],
2525
output: {
2626
globals: {
2727
'url-template': 'parseTemplate',
28-
'hal-json-normalizer': 'normalize'
28+
'hal-json-normalizer': 'normalize',
29+
'vue-demi': 'VueDemi'
2930
}
3031
}
3132
}
3233
},
34+
optimizeDeps: {
35+
exclude: ['vue-demi']
36+
},
3337
test: {
3438
environment: 'jsdom',
3539
globals: true,

0 commit comments

Comments
 (0)