Skip to content

Commit dd2e0f0

Browse files
committed
refactor: declare types for APIs, avoid making dynamic calls to any functions from the web
1 parent bde3e87 commit dd2e0f0

File tree

6 files changed

+47
-8
lines changed

6 files changed

+47
-8
lines changed

core/src/api/index.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,36 @@ export enum FileSystemRoute {
5858
getResourcePath = 'getResourcePath',
5959
exists = 'exists',
6060
}
61+
62+
export type ApiFunction = (...args: any[]) => any
63+
64+
export type AppRouteFunctions = {
65+
[K in AppRoute]: ApiFunction
66+
}
67+
68+
export type AppEventFunctions = {
69+
[K in AppEvent]: ApiFunction
70+
}
71+
72+
export type DownloadRouteFunctions = {
73+
[K in DownloadRoute]: ApiFunction
74+
}
75+
76+
export type DownloadEventFunctions = {
77+
[K in DownloadEvent]: ApiFunction
78+
}
79+
80+
export type ExtensionRouteFunctions = {
81+
[K in ExtensionRoute]: ApiFunction
82+
}
83+
84+
export type FileSystemRouteFunctions = {
85+
[K in FileSystemRoute]: ApiFunction
86+
}
87+
88+
export type APIRoutes = AppRouteFunctions &
89+
AppEventFunctions &
90+
DownloadRouteFunctions &
91+
DownloadEventFunctions &
92+
ExtensionRouteFunctions &
93+
FileSystemRouteFunctions

web/containers/Providers/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ const Providers = (props: PropsWithChildren) => {
5353
useEffect(() => {
5454
if (setupCore) {
5555
// Electron
56-
if (window && window.core.api) {
56+
if (window && window.core?.api) {
5757
setupExtensions()
5858
} else {
5959
// Host

web/extension/ExtensionManager.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export class ExtensionManager {
5858
* @returns An array of extensions.
5959
*/
6060
async getActive(): Promise<Extension[]> {
61-
const res = await window.core.api?.getActiveExtensions()
61+
const res = await window.core?.api?.getActiveExtensions()
6262
if (!res || !Array.isArray(res)) return []
6363

6464
const extensions: Extension[] = res.map(
@@ -119,7 +119,7 @@ export class ExtensionManager {
119119
if (typeof window === 'undefined') {
120120
return
121121
}
122-
const res = await window.core.api?.installExtension(extensions)
122+
const res = await window.core?.api?.installExtension(extensions)
123123
if (res.cancelled) return false
124124
return res.map(async (ext: any) => {
125125
const extension = new Extension(ext.name, ext.url, ext.active)
@@ -138,7 +138,7 @@ export class ExtensionManager {
138138
if (typeof window === 'undefined') {
139139
return
140140
}
141-
return window.core.api?.uninstallExtension(extensions, reload)
141+
return window.core?.api?.uninstallExtension(extensions, reload)
142142
}
143143
}
144144

web/hooks/useGetAppVersion.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export function useGetAppVersion() {
88
}, [])
99

1010
const getAppVersion = () => {
11-
window.core.api?.appVersion().then((version: string | undefined) => {
11+
window.core?.api?.appVersion().then((version: string | undefined) => {
1212
setVersion(version ?? '')
1313
})
1414
}

web/screens/Settings/CoreExtensions/ExtensionsCatalog/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ const ExtensionCatalog = () => {
6767
// Send the filename of the to be installed extension
6868
// to the main process for installation
6969
const installed = await extensionManager.install([extensionFile])
70-
if (installed) window.core.api?.relaunch()
70+
if (installed) window.core?.api?.relaunch()
7171
}
7272

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

8686
/**

web/types/index.d.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1+
import { APIRoutes } from '@janhq/core'
2+
13
/* eslint-disable @typescript-eslint/no-explicit-any */
24
export {}
35

46
declare global {
57
declare const PLUGIN_CATALOG: string
68
declare const VERSION: string
9+
interface Core {
10+
api: APIRoutes
11+
events: EventEmitter
12+
}
713
interface Window {
8-
core?: any | undefined
14+
core?: Core | undefined
915
electronAPI?: any | undefined
1016
}
1117
}

0 commit comments

Comments
 (0)