Skip to content

feat(): add vue2 support using vue-demi dependency #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
526 changes: 352 additions & 174 deletions package-lock.json

Large diffs are not rendered by default.

25 changes: 18 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
"url": "https://github.com/baloise/vue-keycloak.git"
},
"scripts": {
"test": "jest",
"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",
"test:watch": "jest --watchAll",
"build": "npm run build:clean && npm run build:compile && npm run build:bundle",
"build:clean": "rimraf dist && rimraf dist-transpiled",
"build:compile": "tsc -p .",
"build:bundle": "rollup --config rollup.config.js",
"build:compile": "vue-demi-switch 3 vue3 && tsc -p .",
"build:bundle": "vue-demi-switch 3 vue3 && rollup --config rollup.config.js",
"lint": "eslint src --ext .ts,vue",
"format": "npm run prettier:write",
"prettier:write": "prettier --write \"./src\"",
Expand All @@ -37,10 +37,19 @@
"composition-api"
],
"license": "Apache-2.0",
"dependencies": {
"vue-demi": "^0.13"
},
"peerDependencies": {
"@vue/composition-api": "^1.3.0",
"jwt-decode": "^3.1.2",
"keycloak-js": "^12.0.4",
"vue": "^3.0.8"
"vue": "^2 || ^3"
},
"peerDependenciesMeta": {
"@vue/composition-api": {
"optional": true
}
},
"devDependencies": {
"@semantic-release/changelog": "^5.0.1",
Expand All @@ -50,6 +59,7 @@
"@types/jest": "^26.0.22",
"@typescript-eslint/eslint-plugin": "^4.19.0",
"@typescript-eslint/parser": "^4.19.0",
"@vue/composition-api": "^1.3.0",
"@vue/eslint-config-prettier": "^6.0.0",
"@vue/eslint-config-typescript": "^7.0.0",
"eslint": "^7.23.0",
Expand All @@ -64,7 +74,8 @@
"semantic-release": "^17.4.2",
"ts-jest": "^26.5.4",
"typescript": "^4.2.3",
"vue": "^3.0.8"
},
"dependencies": {}
"vue27": "npm:vue@^2.7",
"vue2": "npm:vue@<2.7",
"vue3": "npm:vue@^3"
}
}
6 changes: 3 additions & 3 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ export default {
dir: 'dist/',
format: 'commonjs',
preferConst: true,
sourcemap: true,
},
sourcemap: true
}
],
external: ['keycloak-js', 'jwt-decode', 'vue'],
external: ['keycloak-js', 'jwt-decode', 'vue-demi', 'vue']
}
2 changes: 1 addition & 1 deletion src/composable.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { KeycloakInstance } from 'keycloak-js'
import { toRefs, Ref } from 'vue'
import { toRefs, Ref } from 'vue-demi'
import { getKeycloak } from './keycloak'
import { KeycloakState, state } from './state'
import { isNil } from './utils'
Expand Down
14 changes: 13 additions & 1 deletion src/plugin.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { isVue2, isVue3, Vue2 } from 'vue-demi'
import { KeycloakConfig } from 'keycloak-js'
import { vueKeycloak } from './plugin'
import { createKeycloak, initKeycloak } from './keycloak'
import { defaultInitConfig } from './const'

const testVue3 = isVue3 ? test : test.skip
const testVue2 = isVue2 ? test : test.skip

jest.mock('./keycloak', () => {
return {
initKeycloak: jest.fn(),
Expand Down Expand Up @@ -64,14 +68,22 @@ describe('vueKeycloak', () => {
}
})

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

expect(appMock.config.globalProperties.$keycloak).toBeDefined()
expect(createKeycloak as jest.Mock).toBeCalled()
expect(initKeycloak as jest.Mock).toBeCalled()
})

testVue2('should set $keycloak in prototype', async () => {
await vueKeycloak.install(appMock, { config: keycloakConfig })

expect(Vue2.prototype.$keycloak).toBeDefined()
expect(createKeycloak as jest.Mock).toBeCalled()
expect(initKeycloak as jest.Mock).toBeCalled()
})

test('should call init with the default config', async () => {
await vueKeycloak.install(appMock, { config: keycloakConfig })

Expand Down
8 changes: 6 additions & 2 deletions src/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Plugin } from 'vue'
import { Plugin, isVue3, Vue2 } from 'vue-demi'
import Keycloak from 'keycloak-js'
import { defaultInitConfig } from './const'
import { createKeycloak, initKeycloak } from './keycloak'
Expand Down Expand Up @@ -36,7 +36,11 @@ export const vueKeycloak: Plugin = {
: defaultInitConfig

const _keycloak = createKeycloak(keycloakConfig)
app.config.globalProperties.$keycloak = _keycloak
if (isVue3) {
app.config.globalProperties.$keycloak = _keycloak
} else {
Vue2.prototype.$keycloak = _keycloak
}

await initKeycloak(keycloakInitOptions)
},
Expand Down
2 changes: 1 addition & 1 deletion src/state.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('state', () => {
expect(state.token).toBe('')
expect(state.username).toBe('')
expect(state.roles).toStrictEqual([])
expect(state.resourceRoles).toStrictEqual({});
expect(state.resourceRoles).toStrictEqual({})
})

test('should update the state', () => {
Expand Down
8 changes: 4 additions & 4 deletions src/state.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { reactive } from 'vue'
import { reactive } from 'vue-demi'
import jwtDecode from 'jwt-decode'

export interface KeycloakState<T = unknown> {
Expand Down Expand Up @@ -37,9 +37,9 @@ export const setToken = (token: string): void => {
state.decodedToken = content
state.roles = content.realm_access.roles
state.username = content.preferred_username
state.resourceRoles = content.resource_access ? Object.fromEntries(
Object.entries(content.resource_access).map(([key, value]) => [key, value.roles]),
) : {};
state.resourceRoles = content.resource_access
? Object.fromEntries(Object.entries(content.resource_access).map(([key, value]) => [key, value.roles]))
: {}
}

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