Skip to content

Commit

Permalink
refactor: declare types for APIs, avoid making dynamic calls to any f…
Browse files Browse the repository at this point in the history
…unctions from the web
  • Loading branch information
louis-jan committed Dec 9, 2023
1 parent bde3e87 commit 4aedae4
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 9 deletions.
33 changes: 33 additions & 0 deletions core/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,36 @@ export enum FileSystemRoute {
getResourcePath = 'getResourcePath',
exists = 'exists',
}

export type ApiFunction = (...args: any[]) => any

export type AppRouteFunctions = {
[K in AppRoute]: ApiFunction
}

export type AppEventFunctions = {
[K in AppEvent]: ApiFunction
}

export type DownloadRouteFunctions = {
[K in DownloadRoute]: ApiFunction
}

export type DownloadEventFunctions = {
[K in DownloadEvent]: ApiFunction
}

export type ExtensionRouteFunctions = {
[K in ExtensionRoute]: ApiFunction
}

export type FileSystemRouteFunctions = {
[K in FileSystemRoute]: ApiFunction
}

export type APIRoutes = AppRouteFunctions &
AppEventFunctions &
DownloadRouteFunctions &
DownloadEventFunctions &
ExtensionRouteFunctions &
FileSystemRouteFunctions
2 changes: 1 addition & 1 deletion web/containers/Providers/EventHandler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export default function EventHandler({ children }: { children: ReactNode }) {
}

useEffect(() => {
if (window.core.events) {
if (window.core?.events) {
events.on(EventName.OnMessageResponse, handleNewMessageResponse)
events.on(EventName.OnMessageUpdate, handleMessageResponseUpdate)
events.on(EventName.OnModelReady, handleModelReady)
Expand Down
2 changes: 1 addition & 1 deletion web/containers/Providers/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const Providers = (props: PropsWithChildren) => {
useEffect(() => {
if (setupCore) {
// Electron
if (window && window.core.api) {
if (window && window.core?.api) {
setupExtensions()
} else {
// Host
Expand Down
6 changes: 3 additions & 3 deletions web/extension/ExtensionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export class ExtensionManager {
* @returns An array of extensions.
*/
async getActive(): Promise<Extension[]> {
const res = await window.core.api?.getActiveExtensions()
const res = await window.core?.api?.getActiveExtensions()
if (!res || !Array.isArray(res)) return []

const extensions: Extension[] = res.map(
Expand Down Expand Up @@ -119,7 +119,7 @@ export class ExtensionManager {
if (typeof window === 'undefined') {
return
}
const res = await window.core.api?.installExtension(extensions)
const res = await window.core?.api?.installExtension(extensions)
if (res.cancelled) return false
return res.map(async (ext: any) => {
const extension = new Extension(ext.name, ext.url, ext.active)
Expand All @@ -138,7 +138,7 @@ export class ExtensionManager {
if (typeof window === 'undefined') {
return
}
return window.core.api?.uninstallExtension(extensions, reload)
return window.core?.api?.uninstallExtension(extensions, reload)
}
}

Expand Down
2 changes: 1 addition & 1 deletion web/hooks/useGetAppVersion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export function useGetAppVersion() {
}, [])

const getAppVersion = () => {
window.core.api?.appVersion().then((version: string | undefined) => {
window.core?.api?.appVersion().then((version: string | undefined) => {
setVersion(version ?? '')
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ const ExtensionCatalog = () => {
// Send the filename of the to be installed extension
// to the main process for installation
const installed = await extensionManager.install([extensionFile])
if (installed) window.core.api?.relaunch()
if (installed) window.core?.api?.relaunch()
}

/**
Expand All @@ -80,7 +80,7 @@ const ExtensionCatalog = () => {
// Send the filename of the to be uninstalled extension
// to the main process for removal
const res = await extensionManager.uninstall([name])
if (res) window.core.api?.relaunch()
if (res) window.core?.api?.relaunch()
}

/**
Expand Down
8 changes: 7 additions & 1 deletion web/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import { APIRoutes } from '@janhq/core'

/* eslint-disable @typescript-eslint/no-explicit-any */
export {}

declare global {
declare const PLUGIN_CATALOG: string
declare const VERSION: string
interface Core {
api: APIRoutes
events: EventEmitter
}
interface Window {
core?: any | undefined
core?: Core | undefined
electronAPI?: any | undefined
}
}

0 comments on commit 4aedae4

Please sign in to comment.