-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: show engine and CLI versions in engine dropdown widget (#202)
* feat: add version fetching for Java and Go Casbin engines * fix: update engine GitHub links to use version-specific tags for Java and Go * feat: extract engine version logic into custom hook useEngineVersions * feat: handle unknown versions and refactor GitHub link generation in useEngineVersions hook * refactor: make getVersion method optional in ICasbinEngine interface and simplify version fetching in useEngineVersions hook * feat: enhance version handling to include both engine and library versions * fix: ensure consistent version formatting in editor component * refactor: simplify and generalize engine version handling in useEngineVersions hook * refactor: simplify version handling and improve type safety * style: update formatting in editor dropdown options for Java and Go versions
- Loading branch information
1 parent
39a8df9
commit c4e29ca
Showing
4 changed files
with
146 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { useState, useEffect } from 'react'; | ||
import { createCasbinEngine } from '../CasbinEngine'; | ||
import { VersionInfo } from './useRemoteEnforcer'; | ||
|
||
type EngineType = 'java' | 'go' | 'node'; | ||
|
||
export interface EngineVersionsReturn { | ||
javaVersion: VersionInfo; | ||
goVersion: VersionInfo; | ||
casbinVersion: string | undefined; | ||
engineGithubLinks: Record<EngineType, string>; | ||
} | ||
|
||
interface EngineConfig { | ||
githubRepo: string; | ||
createEngine: () => ReturnType<typeof createCasbinEngine>; | ||
} | ||
|
||
const ENGINE_CONFIGS: Record<EngineType, EngineConfig> = { | ||
java: { | ||
githubRepo: 'casbin/jcasbin', | ||
createEngine: () => { | ||
return createCasbinEngine('java'); | ||
}, | ||
}, | ||
go: { | ||
githubRepo: 'casbin/casbin', | ||
createEngine: () => { | ||
return createCasbinEngine('go'); | ||
}, | ||
}, | ||
node: { | ||
githubRepo: 'casbin/node-casbin', | ||
createEngine: () => { | ||
return createCasbinEngine('node'); | ||
}, | ||
}, | ||
}; | ||
|
||
export default function useEngineVersions(isEngineLoading: boolean): EngineVersionsReturn { | ||
const [versions, setVersions] = useState<Record<EngineType, VersionInfo>>(() => { | ||
return Object.fromEntries( | ||
Object.keys(ENGINE_CONFIGS).map((key) => { | ||
return [key, { engineVersion: '', libVersion: '' }]; | ||
}), | ||
) as Record<EngineType, VersionInfo>; | ||
}); | ||
|
||
const casbinVersion = process.env.CASBIN_VERSION; | ||
|
||
useEffect(() => { | ||
const fetchVersions = async () => { | ||
if (isEngineLoading) return; | ||
|
||
try { | ||
const versionEntries = await Promise.all( | ||
Object.entries(ENGINE_CONFIGS).map(async ([type, config]) => { | ||
const engine = config.createEngine(); | ||
const version = await engine.getVersion?.(); | ||
return [type, version || { engineVersion: 'unknown', libVersion: 'unknown' }] as const; | ||
}), | ||
); | ||
|
||
setVersions(Object.fromEntries(versionEntries) as Record<EngineType, VersionInfo>); | ||
} catch (error) { | ||
const defaultVersions = Object.fromEntries( | ||
Object.keys(ENGINE_CONFIGS).map((key) => { | ||
return [key, { engineVersion: 'unknown', libVersion: 'unknown' }]; | ||
}), | ||
); | ||
setVersions(defaultVersions as Record<EngineType, VersionInfo>); | ||
} | ||
}; | ||
|
||
fetchVersions(); | ||
}, [isEngineLoading]); | ||
|
||
const getVersionedLink = (repo: string, version?: string | null) => { | ||
return version && version !== 'unknown' | ||
? `https://github.com/${repo}/releases/tag/v${version.startsWith('v') ? version.slice(1) : version}` | ||
: `https://github.com/${repo}/releases/`; | ||
}; | ||
|
||
const engineGithubLinks = Object.fromEntries( | ||
Object.entries(ENGINE_CONFIGS).map(([type, config]) => { | ||
return [type, getVersionedLink(config.githubRepo, type === 'node' ? casbinVersion : versions[type as EngineType]?.libVersion)]; | ||
}), | ||
) as Record<EngineType, string>; | ||
|
||
return { | ||
javaVersion: versions.java, | ||
goVersion: versions.go, | ||
casbinVersion, | ||
engineGithubLinks, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters