Skip to content

Commit 49291bc

Browse files
committed
feat(): add vue2 support using vue-demi dependency
1 parent d7a47a3 commit 49291bc

8 files changed

+398
-193
lines changed

package-lock.json

+352-174
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+18-7
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313
"url": "https://github.com/baloise/vue-keycloak.git"
1414
},
1515
"scripts": {
16-
"test": "jest",
16+
"test": "vue-demi-switch 3 vue3 && jest && vue-demi-switch 2.7 vue27 && jest && vue-demi-switch 2 vue2 && jest && vue-demi-switch 3 vue3",
1717
"test:watch": "jest --watchAll",
1818
"build": "npm run build:clean && npm run build:compile && npm run build:bundle",
1919
"build:clean": "rimraf dist && rimraf dist-transpiled",
20-
"build:compile": "tsc -p .",
21-
"build:bundle": "rollup --config rollup.config.js",
20+
"build:compile": "vue-demi-switch 3 vue3 && tsc -p .",
21+
"build:bundle": "vue-demi-switch 3 vue3 && rollup --config rollup.config.js",
2222
"lint": "eslint src --ext .ts,vue",
2323
"format": "npm run prettier:write",
2424
"prettier:write": "prettier --write \"./src\"",
@@ -37,10 +37,19 @@
3737
"composition-api"
3838
],
3939
"license": "Apache-2.0",
40+
"dependencies": {
41+
"vue-demi": "^0.13"
42+
},
4043
"peerDependencies": {
44+
"@vue/composition-api": "^1.3.0",
4145
"jwt-decode": "^3.1.2",
4246
"keycloak-js": "^12.0.4",
43-
"vue": "^3.0.8"
47+
"vue": "^2 || ^3"
48+
},
49+
"peerDependenciesMeta": {
50+
"@vue/composition-api": {
51+
"optional": true
52+
}
4453
},
4554
"devDependencies": {
4655
"@semantic-release/changelog": "^5.0.1",
@@ -50,6 +59,7 @@
5059
"@types/jest": "^26.0.22",
5160
"@typescript-eslint/eslint-plugin": "^4.19.0",
5261
"@typescript-eslint/parser": "^4.19.0",
62+
"@vue/composition-api": "^1.3.0",
5363
"@vue/eslint-config-prettier": "^6.0.0",
5464
"@vue/eslint-config-typescript": "^7.0.0",
5565
"eslint": "^7.23.0",
@@ -64,7 +74,8 @@
6474
"semantic-release": "^17.4.2",
6575
"ts-jest": "^26.5.4",
6676
"typescript": "^4.2.3",
67-
"vue": "^3.0.8"
68-
},
69-
"dependencies": {}
77+
"vue27": "npm:vue@^2.7",
78+
"vue2": "npm:vue@<2.7",
79+
"vue3": "npm:vue@^3"
80+
}
7081
}

rollup.config.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ export default {
1212
dir: 'dist/',
1313
format: 'commonjs',
1414
preferConst: true,
15-
sourcemap: true,
16-
},
15+
sourcemap: true
16+
}
1717
],
18-
external: ['keycloak-js', 'jwt-decode', 'vue'],
18+
external: ['keycloak-js', 'jwt-decode', 'vue-demi', 'vue']
1919
}

src/composable.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { KeycloakInstance } from 'keycloak-js'
2-
import { toRefs, Ref } from 'vue'
2+
import { toRefs, Ref } from 'vue-demi'
33
import { getKeycloak } from './keycloak'
44
import { KeycloakState, state } from './state'
55
import { isNil } from './utils'

src/plugin.test.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
import { isVue2, isVue3, Vue2 } from 'vue-demi'
12
import { KeycloakConfig } from 'keycloak-js'
23
import { vueKeycloak } from './plugin'
34
import { createKeycloak, initKeycloak } from './keycloak'
45
import { defaultInitConfig } from './const'
56

7+
const testVue3 = isVue3 ? test : test.skip
8+
const testVue2 = isVue2 ? test : test.skip
9+
610
jest.mock('./keycloak', () => {
711
return {
812
initKeycloak: jest.fn(),
@@ -64,14 +68,22 @@ describe('vueKeycloak', () => {
6468
}
6569
})
6670

67-
test('should set globalProperties', async () => {
71+
testVue3('should set $keycloak in globalProperties', async () => {
6872
await vueKeycloak.install(appMock, { config: keycloakConfig })
6973

7074
expect(appMock.config.globalProperties.$keycloak).toBeDefined()
7175
expect(createKeycloak as jest.Mock).toBeCalled()
7276
expect(initKeycloak as jest.Mock).toBeCalled()
7377
})
7478

79+
testVue2('should set $keycloak in prototype', async () => {
80+
await vueKeycloak.install(appMock, { config: keycloakConfig })
81+
82+
expect(Vue2.prototype.$keycloak).toBeDefined()
83+
expect(createKeycloak as jest.Mock).toBeCalled()
84+
expect(initKeycloak as jest.Mock).toBeCalled()
85+
})
86+
7587
test('should call init with the default config', async () => {
7688
await vueKeycloak.install(appMock, { config: keycloakConfig })
7789

src/plugin.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Plugin } from 'vue'
1+
import { Plugin, isVue3, Vue2 } from 'vue-demi'
22
import Keycloak from 'keycloak-js'
33
import { defaultInitConfig } from './const'
44
import { createKeycloak, initKeycloak } from './keycloak'
@@ -36,7 +36,11 @@ export const vueKeycloak: Plugin = {
3636
: defaultInitConfig
3737

3838
const _keycloak = createKeycloak(keycloakConfig)
39-
app.config.globalProperties.$keycloak = _keycloak
39+
if (isVue3) {
40+
app.config.globalProperties.$keycloak = _keycloak
41+
} else {
42+
Vue2.prototype.$keycloak = _keycloak
43+
}
4044

4145
await initKeycloak(keycloakInitOptions)
4246
},

src/state.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ describe('state', () => {
1111
expect(state.token).toBe('')
1212
expect(state.username).toBe('')
1313
expect(state.roles).toStrictEqual([])
14-
expect(state.resourceRoles).toStrictEqual({});
14+
expect(state.resourceRoles).toStrictEqual({})
1515
})
1616

1717
test('should update the state', () => {

src/state.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { reactive } from 'vue'
1+
import { reactive } from 'vue-demi'
22
import jwtDecode from 'jwt-decode'
33

44
export interface KeycloakState<T = unknown> {
@@ -37,9 +37,9 @@ export const setToken = (token: string): void => {
3737
state.decodedToken = content
3838
state.roles = content.realm_access.roles
3939
state.username = content.preferred_username
40-
state.resourceRoles = content.resource_access ? Object.fromEntries(
41-
Object.entries(content.resource_access).map(([key, value]) => [key, value.roles]),
42-
) : {};
40+
state.resourceRoles = content.resource_access
41+
? Object.fromEntries(Object.entries(content.resource_access).map(([key, value]) => [key, value.roles]))
42+
: {}
4343
}
4444

4545
export const hasFailed = (value: boolean): void => {

0 commit comments

Comments
 (0)