Skip to content

Commit bde3e87

Browse files
committed
refactor: define routes and auto proxying routes
1 parent 7b8a622 commit bde3e87

File tree

11 files changed

+255
-205
lines changed

11 files changed

+255
-205
lines changed

core/src/api/index.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* App Route APIs
3+
* @description Enum of all the routes exposed by the app
4+
*/
5+
export enum AppRoute {
6+
setNativeThemeLight = 'setNativeThemeLight',
7+
setNativeThemeDark = 'setNativeThemeDark',
8+
setNativeThemeSystem = 'setNativeThemeSystem',
9+
appDataPath = 'appDataPath',
10+
appVersion = 'appVersion',
11+
openExternalUrl = 'openExternalUrl',
12+
relaunch = 'relaunch',
13+
openAppDirectory = 'openAppDirectory',
14+
openFileExplore = 'openFileExplorer',
15+
getResourcePath = 'getResourcePath',
16+
}
17+
18+
export enum AppEvent {
19+
onAppUpdateDownloadUpdate = 'onAppUpdateDownloadUpdate',
20+
onAppUpdateDownloadError = 'onAppUpdateDownloadError',
21+
onAppUpdateDownloadSuccess = 'onAppUpdateDownloadSuccess',
22+
}
23+
24+
export enum DownloadRoute {
25+
downloadFile = 'downloadFile',
26+
pauseDownload = 'pauseDownload',
27+
resumeDownload = 'resumeDownload',
28+
abortDownload = 'abortDownload',
29+
}
30+
31+
export enum DownloadEvent {
32+
onFileDownloadUpdate = 'onFileDownloadUpdate',
33+
onFileDownloadError = 'onFileDownloadError',
34+
onFileDownloadSuccess = 'onFileDownloadSuccess',
35+
}
36+
37+
export enum ExtensionRoute {
38+
installExtension = 'installExtension',
39+
uninstallExtension = 'uninstallExtension',
40+
getActiveExtensions = 'getActiveExtensions',
41+
updateExtension = 'updateExtension',
42+
invokeExtensionFunc = 'invokeExtensionFunc',
43+
baseExtensions = 'baseExtensions',
44+
extensionPath = 'extensionPath',
45+
}
46+
export enum FileSystemRoute {
47+
deleteFile = 'deleteFile',
48+
isDirectory = 'isDirectory',
49+
getUserSpace = 'getUserSpace',
50+
readFile = 'readFile',
51+
writeFile = 'writeFile',
52+
listFiles = 'listFiles',
53+
appendFile = 'appendFile',
54+
readLineByLine = 'readLineByLine',
55+
mkdir = 'mkdir',
56+
rmdir = 'rmdir',
57+
copyFile = 'copyFile',
58+
getResourcePath = 'getResourcePath',
59+
exists = 'exists',
60+
}

core/src/index.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,39 @@
22
* Export all types.
33
* @module
44
*/
5-
export * from "./types/index";
5+
export * from './types/index'
6+
7+
/**
8+
* Export all routes
9+
*/
10+
export * from './api'
611

712
/**
813
* Export Core module
914
* @module
1015
*/
11-
export * from "./core";
16+
export * from './core'
1217

1318
/**
1419
* Export Event module.
1520
* @module
1621
*/
17-
export * from "./events";
22+
export * from './events'
1823

1924
/**
2025
* Export Filesystem module.
2126
* @module
2227
*/
23-
export * from "./fs";
28+
export * from './fs'
2429

2530
/**
2631
* Export Extension module.
2732
* @module
2833
*/
29-
export * from "./extension";
34+
export * from './extension'
3035

3136
/**
3237
* Export all base extensions.
3338
* @module
3439
*/
35-
export * from "./extensions/index";
40+
export * from './extensions/index'

electron/handlers/app.ts

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,42 @@
1-
import { app, ipcMain, shell } from 'electron'
1+
import { app, ipcMain, shell, nativeTheme } from 'electron'
22
import { ModuleManager } from './../managers/module'
33
import { join } from 'path'
44
import { ExtensionManager } from './../managers/extension'
55
import { WindowManager } from './../managers/window'
66
import { userSpacePath } from './../utils/path'
7+
import { AppRoute } from '@janhq/core'
8+
import { getResourcePath } from './../utils/path'
79

810
export function handleAppIPCs() {
11+
/**
12+
* Handles the "setNativeThemeLight" IPC message by setting the native theme source to "light".
13+
* This will change the appearance of the app to the light theme.
14+
*/
15+
ipcMain.handle(AppRoute.setNativeThemeLight, () => {
16+
nativeTheme.themeSource = 'light'
17+
})
18+
19+
/**
20+
* Handles the "setNativeThemeDark" IPC message by setting the native theme source to "dark".
21+
* This will change the appearance of the app to the dark theme.
22+
*/
23+
ipcMain.handle(AppRoute.setNativeThemeDark, () => {
24+
nativeTheme.themeSource = 'dark'
25+
})
26+
27+
/**
28+
* Handles the "setNativeThemeSystem" IPC message by setting the native theme source to "system".
29+
* This will change the appearance of the app to match the system's current theme.
30+
*/
31+
ipcMain.handle(AppRoute.setNativeThemeSystem, () => {
32+
nativeTheme.themeSource = 'system'
33+
})
934
/**
1035
* Retrieves the path to the app data directory using the `coreAPI` object.
1136
* If the `coreAPI` object is not available, the function returns `undefined`.
1237
* @returns A Promise that resolves with the path to the app data directory, or `undefined` if the `coreAPI` object is not available.
1338
*/
14-
ipcMain.handle('appDataPath', async (_event) => {
39+
ipcMain.handle(AppRoute.appDataPath, async (_event) => {
1540
return app.getPath('userData')
1641
})
1742

@@ -20,7 +45,7 @@ export function handleAppIPCs() {
2045
* @param _event - The IPC event object.
2146
* @returns The version of the app.
2247
*/
23-
ipcMain.handle('appVersion', async (_event) => {
48+
ipcMain.handle(AppRoute.appVersion, async (_event) => {
2449
return app.getVersion()
2550
})
2651

@@ -29,16 +54,20 @@ export function handleAppIPCs() {
2954
* The `shell.openPath` method is used to open the directory in the user's default file explorer.
3055
* @param _event - The IPC event object.
3156
*/
32-
ipcMain.handle('openAppDirectory', async (_event) => {
57+
ipcMain.handle(AppRoute.openAppDirectory, async (_event) => {
3358
shell.openPath(userSpacePath)
3459
})
3560

61+
ipcMain.handle(AppRoute.getResourcePath, async (_event) => {
62+
return getResourcePath()
63+
})
64+
3665
/**
3766
* Opens a URL in the user's default browser.
3867
* @param _event - The IPC event object.
3968
* @param url - The URL to open.
4069
*/
41-
ipcMain.handle('openExternalUrl', async (_event, url) => {
70+
ipcMain.handle(AppRoute.openExternalUrl, async (_event, url) => {
4271
shell.openExternal(url)
4372
})
4473

@@ -47,7 +76,7 @@ export function handleAppIPCs() {
4776
* @param _event - The IPC event object.
4877
* @param url - The URL to reload.
4978
*/
50-
ipcMain.handle('relaunch', async (_event, url) => {
79+
ipcMain.handle(AppRoute.relaunch, async (_event, url) => {
5180
ModuleManager.instance.clearImportedModules()
5281

5382
if (app.isPackaged) {
@@ -56,9 +85,7 @@ export function handleAppIPCs() {
5685
} else {
5786
for (const modulePath in ModuleManager.instance.requiredModules) {
5887
delete require.cache[
59-
require.resolve(
60-
join(userSpacePath, 'extensions', modulePath)
61-
)
88+
require.resolve(join(userSpacePath, 'extensions', modulePath))
6289
]
6390
}
6491
ExtensionManager.instance.setupExtensions()

electron/handlers/download.ts

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { resolve, join } from 'path'
44
import { WindowManager } from './../managers/window'
55
import request from 'request'
66
import { createWriteStream } from 'fs'
7-
import { getResourcePath } from './../utils/path'
7+
import { DownloadEvent, DownloadRoute } from '@janhq/core'
88
const progress = require('request-progress')
99

1010
export function handleDownloaderIPCs() {
@@ -13,7 +13,7 @@ export function handleDownloaderIPCs() {
1313
* @param _event - The IPC event object.
1414
* @param fileName - The name of the file being downloaded.
1515
*/
16-
ipcMain.handle('pauseDownload', async (_event, fileName) => {
16+
ipcMain.handle(DownloadRoute.pauseDownload, async (_event, fileName) => {
1717
DownloadManager.instance.networkRequests[fileName]?.pause()
1818
})
1919

@@ -22,7 +22,7 @@ export function handleDownloaderIPCs() {
2222
* @param _event - The IPC event object.
2323
* @param fileName - The name of the file being downloaded.
2424
*/
25-
ipcMain.handle('resumeDownload', async (_event, fileName) => {
25+
ipcMain.handle(DownloadRoute.resumeDownload, async (_event, fileName) => {
2626
DownloadManager.instance.networkRequests[fileName]?.resume()
2727
})
2828

@@ -32,31 +32,27 @@ export function handleDownloaderIPCs() {
3232
* @param _event - The IPC event object.
3333
* @param fileName - The name of the file being downloaded.
3434
*/
35-
ipcMain.handle('abortDownload', async (_event, fileName) => {
35+
ipcMain.handle(DownloadRoute.abortDownload, async (_event, fileName) => {
3636
const rq = DownloadManager.instance.networkRequests[fileName]
3737
DownloadManager.instance.networkRequests[fileName] = undefined
3838
rq?.abort()
3939
})
4040

41-
ipcMain.handle('getResourcePath', async (_event) => {
42-
return getResourcePath()
43-
})
44-
4541
/**
4642
* Downloads a file from a given URL.
4743
* @param _event - The IPC event object.
4844
* @param url - The URL to download the file from.
4945
* @param fileName - The name to give the downloaded file.
5046
*/
51-
ipcMain.handle('downloadFile', async (_event, url, fileName) => {
47+
ipcMain.handle(DownloadRoute.downloadFile, async (_event, url, fileName) => {
5248
const userDataPath = join(app.getPath('home'), 'jan')
5349
const destination = resolve(userDataPath, fileName)
5450
const rq = request(url)
5551

5652
progress(rq, {})
5753
.on('progress', function (state: any) {
5854
WindowManager?.instance.currentWindow?.webContents.send(
59-
'onFileDownloadUpdate',
55+
DownloadEvent.onFileDownloadUpdate,
6056
{
6157
...state,
6258
fileName,
@@ -65,7 +61,7 @@ export function handleDownloaderIPCs() {
6561
})
6662
.on('error', function (err: Error) {
6763
WindowManager?.instance.currentWindow?.webContents.send(
68-
'onFileDownloadError',
64+
DownloadEvent.onFileDownloadError,
6965
{
7066
fileName,
7167
err,
@@ -75,15 +71,15 @@ export function handleDownloaderIPCs() {
7571
.on('end', function () {
7672
if (DownloadManager.instance.networkRequests[fileName]) {
7773
WindowManager?.instance.currentWindow?.webContents.send(
78-
'onFileDownloadSuccess',
74+
DownloadEvent.onFileDownloadSuccess,
7975
{
8076
fileName,
8177
}
8278
)
8379
DownloadManager.instance.setRequest(fileName, undefined)
8480
} else {
8581
WindowManager?.instance.currentWindow?.webContents.send(
86-
'onFileDownloadError',
82+
DownloadEvent.onFileDownloadError,
8783
{
8884
fileName,
8985
err: 'Download cancelled',

0 commit comments

Comments
 (0)