Skip to content

Commit 2a97c5b

Browse files
authored
Merge pull request #287 from usu/feat/nuxt3-vite
Feat/nuxt3 vite
2 parents 3ed8cdd + f92ea43 commit 2a97c5b

File tree

14 files changed

+3367
-2433
lines changed

14 files changed

+3367
-2433
lines changed

jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module.exports = {
1+
export default {
22
preset: 'ts-jest',
33
testEnvironment: 'jsdom',
44
transform: {

package-lock.json

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

package.json

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,31 @@
11
{
22
"name": "hal-json-vuex",
3-
"version": "2.0.0-alpha.16",
3+
"version": "3.0.0-alpha.1",
44
"description": "Use a HAL JSON API with Vuex",
5-
"main": "dist/bundle.js",
5+
"main": "./dist/hal-json-vuex.umd.cjs",
6+
"module": "./dist/hal-json-vuex.js",
7+
"typings": "./dist/hal-json-vuex.d.ts",
8+
"exports": {
9+
".": {
10+
"import": "./dist/hal-json-vuex.js",
11+
"require": "./dist/hal-json-vuex.umd.cjs"
12+
}
13+
},
614
"scripts": {
7-
"build": "cross-env NODE_ENV=production webpack",
8-
"build-dev": "cross-env NODE_ENV=development webpack",
15+
"build": "cross-env NODE_ENV=production tsc && vite build",
16+
"build:watch": "cross-env NODE_ENV=production vite build --watch",
17+
"build-dev": "cross-env NODE_ENV=development tsc && vite build",
918
"clean": "cross-env rimraf dist coverage lib",
1019
"coverage": "cross-env npm run build && jest --coverage",
11-
"lint": "cross-env eslint src --ext .js,.ts",
20+
"lint": "cross-env eslint src tests --ext .js,.ts",
1221
"test": "jest",
1322
"test:debug": "node --inspect-brk ./node_modules/.bin/jest -i",
1423
"version": "npm run build"
1524
},
1625
"engines": {
1726
"node": ">=16.0.0 <21.0.0"
1827
},
28+
"type": "module",
1929
"files": [
2030
"/dist",
2131
"/CHANGELOG.md",
@@ -50,12 +60,11 @@
5060
"@babel/plugin-transform-runtime": "7.16.10",
5161
"@babel/preset-env": "7.16.11",
5262
"@babel/preset-typescript": "7.16.7",
53-
"@nuxt/types": "2.15.8",
5463
"@types/jest": "27.5.1",
5564
"@typescript-eslint/eslint-plugin": "5.10.1",
5665
"@typescript-eslint/parser": "5.10.1",
5766
"@vue/eslint-config-standard": "6.1.0",
58-
"@vue/test-utils": "1.3.0",
67+
"@vue/test-utils": "2.4.1",
5968
"axios": "0.25.0",
6069
"axios-mock-adapter": "1.20.0",
6170
"babel-eslint": "10.1.0",
@@ -78,12 +87,9 @@
7887
"ts-jest": "27.1.3",
7988
"ts-loader": "9.2.6",
8089
"typescript": "4.5.5",
81-
"vue": "2.6.14",
82-
"vue-axios": "3.2.5",
83-
"vue-template-compiler": "2.6.14",
84-
"vuex": "3.6.2",
85-
"webpack": "5.67.0",
86-
"webpack-cli": "4.9.2",
87-
"webpack-node-externals": "3.0.0"
90+
"vite": "4.4.4",
91+
"vue": "3.3.6",
92+
"@vue/compiler-sfc": "3.3.6",
93+
"vuex": "4.1.0"
8894
}
8995
}

src/index.ts

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,31 +30,14 @@ function HalJsonVuex (store: Store<Record<string, State>>, axios: AxiosInstance,
3030
const defaultOptions = {
3131
apiName: 'api',
3232
avoidNPlusOneRequests: true,
33-
forceRequestedSelfLink: false,
34-
nuxtInject: undefined
33+
forceRequestedSelfLink: false
3534
}
3635
const opts = { ...defaultOptions, ...options, apiRoot: axios.defaults.baseURL }
3736

3837
store.registerModule(opts.apiName, { state: {}, ...storeModule })
3938

4039
const resourceCreator = new ResourceCreator({ get, reload, post, patch, del, href, isUnknown }, opts)
4140

42-
if (opts.nuxtInject !== null) axios = adaptNuxtAxios(axios)
43-
44-
/**
45-
* Since Nuxt.js uses $get, $post etc., we need to use an adapter in the case of a Nuxt.js app...
46-
* @param $axios
47-
*/
48-
function adaptNuxtAxios ($axios) {
49-
return {
50-
get: $axios.$get,
51-
patch: $axios.$patch,
52-
post: $axios.$post,
53-
delete: $axios.$delete,
54-
...$axios
55-
}
56-
}
57-
5841
/**
5942
* Sends a POST request to the API, in order to create a new entity. Note that this does not
6043
* reload any collections that this new entity might be in, the caller has to do that on their own.
@@ -461,19 +444,17 @@ function HalJsonVuex (store: Store<Record<string, State>>, axios: AxiosInstance,
461444
const halJsonVuex = { ...apiActions, purge, purgeAll, href, Resource, LoadingResource }
462445

463446
// eslint-disable-next-line @typescript-eslint/no-explicit-any
464-
function install (Vue: any) {
465-
if (!opts.nuxtInject) {
466-
// Normal installation in a Vue app
467-
Object.defineProperties(Vue.prototype, {
447+
function install (app: any) {
448+
if (app.version && app.version.charAt(0) === '3') {
449+
Object.defineProperties(app.config.globalProperties, {
468450
[opts.apiName]: {
469451
get () {
470452
return halJsonVuex
471453
}
472454
}
473455
})
474456
} else {
475-
// Support for Nuxt-style inject installation
476-
opts.nuxtInject(opts.apiName, halJsonVuex)
457+
throw new Error('Vue2 detected: this version of hal-json-vuex is not compatible with Vue2')
477458
}
478459
}
479460

src/interfaces/Config.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
import { Inject } from '@nuxt/types/app'
2-
31
interface ExternalConfig {
42
apiName?: string
53
avoidNPlusOneRequests?: boolean
64
forceRequestedSelfLink?: boolean
7-
nuxtInject?: Inject
85
}
96

107
interface InternalConfig extends ExternalConfig {

src/storeModule.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import Vue from 'vue'
21
import StoreData from './interfaces/StoreData'
32

43
import { MutationTree } from 'vuex/types'
@@ -14,7 +13,9 @@ export const mutations: MutationTree<State> = {
1413
* @param uri URI of the object that is being fetched
1514
*/
1615
addEmpty (state: State, uri: string) : void {
17-
Vue.set(state, uri, { _meta: { self: uri, loading: true } })
16+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
17+
// @ts-ignore
18+
state[uri] = { _meta: { self: uri, loading: true } }
1819
},
1920
/**
2021
* Adds entities loaded from the API to the Vuex store.
@@ -23,10 +24,12 @@ export const mutations: MutationTree<State> = {
2324
*/
2425
add (state: State, data: Record<string, unknown>) : void {
2526
Object.keys(data).forEach(uri => {
26-
Vue.set(state, uri, data[uri])
27+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
28+
// @ts-ignore
29+
state[uri] = data[uri]
2730

28-
Vue.set(state[uri]._meta, 'loading', false)
29-
Vue.set(state[uri]._meta, 'reloading', false)
31+
state[uri]._meta.loading = false
32+
state[uri]._meta.reloading = false
3033
})
3134
},
3235
/**
@@ -35,23 +38,23 @@ export const mutations: MutationTree<State> = {
3538
* @param uri URI of the entity that is currently being reloaded
3639
*/
3740
reloading (state: State, uri: string) : void {
38-
if (state[uri]) Vue.set(state[uri]._meta, 'reloading', true)
41+
if (state[uri]) state[uri]._meta.reloading = true
3942
},
4043
/**
4144
* Marks a single entity in the Vuex store as normal again, after it has been marked as reloading before.
4245
* @param state Vuex state
4346
* @param uri URI of the entity that is currently being reloaded
4447
*/
4548
reloadingFailed (state: State, uri: string) : void {
46-
if (state[uri]) Vue.set(state[uri]._meta, 'reloading', false)
49+
if (state[uri]) state[uri]._meta.reloading = false
4750
},
4851
/**
4952
* Removes a single entity from the Vuex store.
5053
* @param state Vuex state
5154
* @param uri URI of the entity to be removed
5255
*/
5356
purge (state: State, uri: string) : void {
54-
Vue.delete(state, uri)
57+
delete state[uri]
5558
},
5659
/**
5760
* Removes all entities from the Vuex store.
@@ -60,7 +63,7 @@ export const mutations: MutationTree<State> = {
6063
*/
6164
purgeAll (state: State) : void {
6265
Object.keys(state).forEach(uri => {
63-
Vue.delete(state, uri)
66+
delete state[uri]
6467
})
6568
},
6669
/**
@@ -69,15 +72,15 @@ export const mutations: MutationTree<State> = {
6972
* @param uri URI of the entity that is currently being deleted
7073
*/
7174
deleting (state: State, uri: string) : void {
72-
if (state[uri]) Vue.set(state[uri]._meta, 'deleting', true)
75+
if (state[uri]) state[uri]._meta.deleting = true
7376
},
7477
/**
7578
* Marks a single entity in the Vuex store as normal again, after it has been marked as deleting before.
7679
* @param state Vuex state
7780
* @param uri URI of the entity that failed to be deleted
7881
*/
7982
deletingFailed (state: State, uri: string) : void {
80-
if (state[uri]) Vue.set(state[uri]._meta, 'deleting', false)
83+
if (state[uri]) state[uri]._meta.deleting = false
8184
}
8285
}
8386

tests/.eslintrc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"env": {
3+
"jest": true
4+
}
5+
}

tests/.eslintrc.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)