refactor: split preload api by domain
This commit is contained in:
15
src/preload/api/auth.ts
Normal file
15
src/preload/api/auth.ts
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
import type { LoginRequest } from '../../main/types/auth-ipc.types'
|
||||||
|
import type { UserInfo } from '../../main/types/user.types'
|
||||||
|
import { IPC_CHANNELS } from '../../shared/ipc-channels'
|
||||||
|
import { invokeIpc } from '../lib/ipc'
|
||||||
|
|
||||||
|
export const authApi = {
|
||||||
|
getComputerName: () => invokeIpc(IPC_CHANNELS.AUTH_GET_COMPUTER_NAME),
|
||||||
|
silentLogin: () => invokeIpc(IPC_CHANNELS.AUTH_SILENT_LOGIN),
|
||||||
|
login: (request: LoginRequest) => invokeIpc(IPC_CHANNELS.AUTH_LOGIN, request),
|
||||||
|
logout: () => invokeIpc(IPC_CHANNELS.AUTH_LOGOUT),
|
||||||
|
getCurrentUser: () => invokeIpc(IPC_CHANNELS.AUTH_GET_CURRENT_USER),
|
||||||
|
getAllUsers: () => invokeIpc(IPC_CHANNELS.AUTH_GET_ALL_USERS),
|
||||||
|
switchUser: (userInfo: UserInfo) => invokeIpc(IPC_CHANNELS.AUTH_SWITCH_USER, userInfo),
|
||||||
|
isAdmin: () => invokeIpc(IPC_CHANNELS.AUTH_IS_ADMIN)
|
||||||
|
} as const
|
||||||
19
src/preload/api/cleaner.ts
Normal file
19
src/preload/api/cleaner.ts
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
import type {
|
||||||
|
CleanerInput,
|
||||||
|
CleanerProgress,
|
||||||
|
ExportResultItem
|
||||||
|
} from '../../main/types/cleaner.types'
|
||||||
|
import { IPC_CHANNELS } from '../../shared/ipc-channels'
|
||||||
|
import { invokeIpc, ipcRenderer } from '../lib/ipc'
|
||||||
|
|
||||||
|
export const cleanerApi = {
|
||||||
|
runCleaner: (input: CleanerInput) => invokeIpc(IPC_CHANNELS.CLEANER_RUN, input),
|
||||||
|
exportResults: (items: ExportResultItem[]) =>
|
||||||
|
invokeIpc(IPC_CHANNELS.CLEANER_EXPORT_RESULTS, items),
|
||||||
|
onProgress: (callback: (data: CleanerProgress) => void) => {
|
||||||
|
const subscription = (_event: Electron.IpcRendererEvent, data: CleanerProgress) =>
|
||||||
|
callback(data)
|
||||||
|
ipcRenderer.on(IPC_CHANNELS.CLEANER_PROGRESS, subscription)
|
||||||
|
return () => ipcRenderer.removeListener(IPC_CHANNELS.CLEANER_PROGRESS, subscription)
|
||||||
|
}
|
||||||
|
} as const
|
||||||
17
src/preload/api/database.ts
Normal file
17
src/preload/api/database.ts
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
import type { MySqlConfig, SqlServerConfig } from '../../main/types/ipc-api.types'
|
||||||
|
import { IPC_CHANNELS } from '../../shared/ipc-channels'
|
||||||
|
import { invokeIpc } from '../lib/ipc'
|
||||||
|
|
||||||
|
export const databaseApi = {
|
||||||
|
connectMySql: (config: MySqlConfig) => invokeIpc(IPC_CHANNELS.DATABASE_MYSQL_CONNECT, config),
|
||||||
|
disconnectMySql: () => invokeIpc(IPC_CHANNELS.DATABASE_MYSQL_DISCONNECT),
|
||||||
|
isMySqlConnected: () => invokeIpc(IPC_CHANNELS.DATABASE_MYSQL_IS_CONNECTED),
|
||||||
|
queryMySql: (sql: string, params?: unknown[]) =>
|
||||||
|
invokeIpc(IPC_CHANNELS.DATABASE_MYSQL_QUERY, sql, params),
|
||||||
|
connectSqlServer: (config: SqlServerConfig) =>
|
||||||
|
invokeIpc(IPC_CHANNELS.DATABASE_SQLSERVER_CONNECT, config),
|
||||||
|
disconnectSqlServer: () => invokeIpc(IPC_CHANNELS.DATABASE_SQLSERVER_DISCONNECT),
|
||||||
|
isSqlServerConnected: () => invokeIpc(IPC_CHANNELS.DATABASE_SQLSERVER_IS_CONNECTED),
|
||||||
|
querySqlServer: (sql: string, params?: Record<string, unknown>) =>
|
||||||
|
invokeIpc(IPC_CHANNELS.DATABASE_SQLSERVER_QUERY, sql, params)
|
||||||
|
} as const
|
||||||
21
src/preload/api/extractor.ts
Normal file
21
src/preload/api/extractor.ts
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
import type { ExtractorInput, ExtractionProgress } from '../../main/types/extractor.types'
|
||||||
|
import { IPC_CHANNELS } from '../../shared/ipc-channels'
|
||||||
|
import { invokeIpc, ipcRenderer } from '../lib/ipc'
|
||||||
|
|
||||||
|
export const extractorApi = {
|
||||||
|
runExtractor: (input: ExtractorInput) => invokeIpc(IPC_CHANNELS.EXTRACTOR_RUN, input),
|
||||||
|
onProgress: (callback: (data: ExtractionProgress) => void) => {
|
||||||
|
const subscription = (_event: Electron.IpcRendererEvent, data: ExtractionProgress) =>
|
||||||
|
callback(data)
|
||||||
|
ipcRenderer.on(IPC_CHANNELS.EXTRACTOR_PROGRESS, subscription)
|
||||||
|
return () => ipcRenderer.removeListener(IPC_CHANNELS.EXTRACTOR_PROGRESS, subscription)
|
||||||
|
},
|
||||||
|
onLog: (callback: (data: { level: string; message: string }) => void) => {
|
||||||
|
const subscription = (
|
||||||
|
_event: Electron.IpcRendererEvent,
|
||||||
|
data: { level: string; message: string }
|
||||||
|
) => callback(data)
|
||||||
|
ipcRenderer.on(IPC_CHANNELS.EXTRACTOR_LOG, subscription)
|
||||||
|
return () => ipcRenderer.removeListener(IPC_CHANNELS.EXTRACTOR_LOG, subscription)
|
||||||
|
}
|
||||||
|
} as const
|
||||||
11
src/preload/api/file.ts
Normal file
11
src/preload/api/file.ts
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
import { IPC_CHANNELS } from '../../shared/ipc-channels'
|
||||||
|
import { invokeIpc } from '../lib/ipc'
|
||||||
|
|
||||||
|
export const fileApi = {
|
||||||
|
readFile: (filePath: string) => invokeIpc(IPC_CHANNELS.FILE_READ, filePath),
|
||||||
|
writeFile: (filePath: string, content: string) =>
|
||||||
|
invokeIpc(IPC_CHANNELS.FILE_WRITE, filePath, content),
|
||||||
|
fileExists: (filePath: string) => invokeIpc(IPC_CHANNELS.FILE_EXISTS, filePath),
|
||||||
|
listFiles: (dirPath: string) => invokeIpc(IPC_CHANNELS.FILE_LIST, dirPath),
|
||||||
|
openPath: (filePath: string) => invokeIpc(IPC_CHANNELS.FILE_OPEN_PATH, filePath)
|
||||||
|
} as const
|
||||||
39
src/preload/api/index.ts
Normal file
39
src/preload/api/index.ts
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
import { processApi } from './process'
|
||||||
|
import { fileApi } from './file'
|
||||||
|
import { extractorApi } from './extractor'
|
||||||
|
import { cleanerApi } from './cleaner'
|
||||||
|
import { resolverApi } from './resolver'
|
||||||
|
import { authApi } from './auth'
|
||||||
|
import { databaseApi } from './database'
|
||||||
|
import { validationApi } from './validation'
|
||||||
|
import {
|
||||||
|
configApi,
|
||||||
|
materialTypeApi,
|
||||||
|
materialsApi,
|
||||||
|
reportApi,
|
||||||
|
settingsApi,
|
||||||
|
updateApi,
|
||||||
|
userErpConfigApi
|
||||||
|
} from './materials'
|
||||||
|
import { loggerApi } from './logger'
|
||||||
|
|
||||||
|
export const api = {
|
||||||
|
process: processApi,
|
||||||
|
file: fileApi,
|
||||||
|
extractor: extractorApi,
|
||||||
|
cleaner: cleanerApi,
|
||||||
|
resolver: resolverApi,
|
||||||
|
auth: authApi,
|
||||||
|
database: databaseApi,
|
||||||
|
validation: validationApi,
|
||||||
|
materials: materialsApi,
|
||||||
|
settings: settingsApi,
|
||||||
|
materialType: materialTypeApi,
|
||||||
|
userErpConfig: userErpConfigApi,
|
||||||
|
config: configApi,
|
||||||
|
logger: loggerApi,
|
||||||
|
report: reportApi,
|
||||||
|
update: updateApi
|
||||||
|
} as const
|
||||||
|
|
||||||
|
export type ElectronApi = typeof api
|
||||||
14
src/preload/api/logger.ts
Normal file
14
src/preload/api/logger.ts
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
import type { LogLevel } from '../../shared/ipc-channels'
|
||||||
|
import { IPC_CHANNELS } from '../../shared/ipc-channels'
|
||||||
|
import { ipcRenderer } from '../lib/ipc'
|
||||||
|
|
||||||
|
export const loggerApi = {
|
||||||
|
log: (level: LogLevel, message: string, context?: Record<string, unknown>): void => {
|
||||||
|
ipcRenderer.send(IPC_CHANNELS.LOGGER_FORWARD, {
|
||||||
|
level,
|
||||||
|
message,
|
||||||
|
context,
|
||||||
|
timestamp: Date.now()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
} as const
|
||||||
91
src/preload/api/materials.ts
Normal file
91
src/preload/api/materials.ts
Normal file
@@ -0,0 +1,91 @@
|
|||||||
|
import type {
|
||||||
|
MaterialTypeBatchRequest,
|
||||||
|
MaterialTypeRecord
|
||||||
|
} from '../../main/types/validation.types'
|
||||||
|
import { IPC_CHANNELS } from '../../shared/ipc-channels'
|
||||||
|
import type { CleanerConfig } from '../../main/types/config.schema'
|
||||||
|
import type {
|
||||||
|
DownloadReleaseRequest,
|
||||||
|
UpdateDialogCatalog,
|
||||||
|
UpdateStatus
|
||||||
|
} from '../../main/types/update.types'
|
||||||
|
import { invokeIpc, ipcRenderer } from '../lib/ipc'
|
||||||
|
|
||||||
|
type ErpSettingsPayload = {
|
||||||
|
erp?: {
|
||||||
|
username?: string
|
||||||
|
password?: string
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const materialsApi = {
|
||||||
|
upsertBatch: (materials: { materialCode: string; managerName: string }[]) =>
|
||||||
|
invokeIpc(IPC_CHANNELS.MATERIALS_UPSERT_BATCH, { materials }),
|
||||||
|
delete: (materialCodes: string[]) => invokeIpc(IPC_CHANNELS.MATERIALS_DELETE, { materialCodes }),
|
||||||
|
getManagers: () => invokeIpc(IPC_CHANNELS.MATERIALS_GET_MANAGERS),
|
||||||
|
getByManager: (managerName: string) =>
|
||||||
|
invokeIpc(IPC_CHANNELS.MATERIALS_GET_BY_MANAGER, managerName),
|
||||||
|
getAll: () => invokeIpc(IPC_CHANNELS.MATERIALS_GET_ALL),
|
||||||
|
getStatistics: () => invokeIpc(IPC_CHANNELS.MATERIALS_GET_STATISTICS),
|
||||||
|
updateManager: (materialCode: string, managerName: string) =>
|
||||||
|
invokeIpc(IPC_CHANNELS.MATERIALS_UPDATE_MANAGER, { materialCode, managerName })
|
||||||
|
} as const
|
||||||
|
|
||||||
|
export const settingsApi = {
|
||||||
|
getUserType: () => invokeIpc(IPC_CHANNELS.SETTINGS_GET_USER_TYPE),
|
||||||
|
getSettings: () => invokeIpc(IPC_CHANNELS.SETTINGS_GET_SETTINGS),
|
||||||
|
saveSettings: (settings: ErpSettingsPayload) =>
|
||||||
|
invokeIpc(IPC_CHANNELS.SETTINGS_SAVE_SETTINGS, settings),
|
||||||
|
resetDefaults: () => invokeIpc(IPC_CHANNELS.SETTINGS_RESET_DEFAULTS),
|
||||||
|
testDbConnection: () => invokeIpc(IPC_CHANNELS.SETTINGS_TEST_DB_CONNECTION)
|
||||||
|
} as const
|
||||||
|
|
||||||
|
export const materialTypeApi = {
|
||||||
|
getAll: () => invokeIpc<MaterialTypeRecord[]>(IPC_CHANNELS.MATERIAL_TYPE_GET_ALL),
|
||||||
|
getByManager: (managerName: string) =>
|
||||||
|
invokeIpc<MaterialTypeRecord[]>(IPC_CHANNELS.MATERIAL_TYPE_GET_BY_MANAGER, managerName),
|
||||||
|
getManagers: () => invokeIpc<string[]>(IPC_CHANNELS.MATERIAL_TYPE_GET_MANAGERS),
|
||||||
|
upsert: (materialName: string, managerName: string) =>
|
||||||
|
invokeIpc(IPC_CHANNELS.MATERIAL_TYPE_UPSERT, { materialName, managerName }),
|
||||||
|
delete: (materialName: string, managerName: string) =>
|
||||||
|
invokeIpc(IPC_CHANNELS.MATERIAL_TYPE_DELETE, { materialName, managerName }),
|
||||||
|
upsertBatch: (request: MaterialTypeBatchRequest) =>
|
||||||
|
invokeIpc(IPC_CHANNELS.MATERIAL_TYPE_UPSERT_BATCH, request)
|
||||||
|
} as const
|
||||||
|
|
||||||
|
export const userErpConfigApi = {
|
||||||
|
getCurrent: () => invokeIpc(IPC_CHANNELS.USER_ERP_CONFIG_GET_CURRENT),
|
||||||
|
update: (config: { url: string; username: string; password: string }) =>
|
||||||
|
invokeIpc(IPC_CHANNELS.USER_ERP_CONFIG_UPDATE, config),
|
||||||
|
testConnection: (config: { url: string; username: string; password: string }) =>
|
||||||
|
invokeIpc(IPC_CHANNELS.USER_ERP_CONFIG_TEST_CONNECTION, config),
|
||||||
|
getAll: () => invokeIpc(IPC_CHANNELS.USER_ERP_CONFIG_GET_ALL)
|
||||||
|
} as const
|
||||||
|
|
||||||
|
export const configApi = {
|
||||||
|
getCleaner: () => invokeIpc<CleanerConfig>(IPC_CHANNELS.CONFIG_GET_CLEANER),
|
||||||
|
updateCleaner: (updates: Partial<CleanerConfig>) =>
|
||||||
|
invokeIpc<CleanerConfig>(IPC_CHANNELS.CONFIG_UPDATE_CLEANER, updates)
|
||||||
|
} as const
|
||||||
|
|
||||||
|
export const reportApi = {
|
||||||
|
listAll: () => invokeIpc(IPC_CHANNELS.REPORT_LIST_ALL),
|
||||||
|
listByUser: (username: string) => invokeIpc(IPC_CHANNELS.REPORT_LIST_BY_USER, username),
|
||||||
|
download: (key: string) => invokeIpc(IPC_CHANNELS.REPORT_DOWNLOAD, key)
|
||||||
|
} as const
|
||||||
|
|
||||||
|
export const updateApi = {
|
||||||
|
getStatus: () => invokeIpc<UpdateStatus>(IPC_CHANNELS.UPDATE_GET_STATUS),
|
||||||
|
checkNow: () => invokeIpc<UpdateStatus>(IPC_CHANNELS.UPDATE_CHECK_NOW),
|
||||||
|
getCatalog: () => invokeIpc<UpdateDialogCatalog>(IPC_CHANNELS.UPDATE_GET_CATALOG),
|
||||||
|
getChangelog: (release: DownloadReleaseRequest) =>
|
||||||
|
invokeIpc<string>(IPC_CHANNELS.UPDATE_GET_CHANGELOG, release),
|
||||||
|
downloadRelease: (release: DownloadReleaseRequest) =>
|
||||||
|
invokeIpc<UpdateStatus>(IPC_CHANNELS.UPDATE_DOWNLOAD_RELEASE, release),
|
||||||
|
installDownloaded: () => invokeIpc<void>(IPC_CHANNELS.UPDATE_INSTALL_DOWNLOADED),
|
||||||
|
onStatusChanged: (callback: (data: UpdateStatus) => void) => {
|
||||||
|
const subscription = (_event: Electron.IpcRendererEvent, data: UpdateStatus) => callback(data)
|
||||||
|
ipcRenderer.on(IPC_CHANNELS.UPDATE_STATUS_CHANGED, subscription)
|
||||||
|
return () => ipcRenderer.removeListener(IPC_CHANNELS.UPDATE_STATUS_CHANGED, subscription)
|
||||||
|
}
|
||||||
|
} as const
|
||||||
7
src/preload/api/process.ts
Normal file
7
src/preload/api/process.ts
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
export const processApi = {
|
||||||
|
versions: {
|
||||||
|
electron: process.versions.electron,
|
||||||
|
chrome: process.versions.chrome,
|
||||||
|
node: process.versions.node
|
||||||
|
}
|
||||||
|
} as const
|
||||||
8
src/preload/api/resolver.ts
Normal file
8
src/preload/api/resolver.ts
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
import type { ResolverInput } from '../../main/types/resolver-ipc.types'
|
||||||
|
import { IPC_CHANNELS } from '../../shared/ipc-channels'
|
||||||
|
import { invokeIpc } from '../lib/ipc'
|
||||||
|
|
||||||
|
export const resolverApi = {
|
||||||
|
resolve: (input: ResolverInput) => invokeIpc(IPC_CHANNELS.RESOLVER_RESOLVE, input),
|
||||||
|
validateFormat: (inputs: string[]) => invokeIpc(IPC_CHANNELS.RESOLVER_VALIDATE_FORMAT, inputs)
|
||||||
|
} as const
|
||||||
12
src/preload/api/validation.ts
Normal file
12
src/preload/api/validation.ts
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
import type { ValidationRequest } from '../../main/types/validation.types'
|
||||||
|
import { IPC_CHANNELS } from '../../shared/ipc-channels'
|
||||||
|
import { invokeIpc } from '../lib/ipc'
|
||||||
|
|
||||||
|
export const validationApi = {
|
||||||
|
validate: (request: ValidationRequest) => invokeIpc(IPC_CHANNELS.VALIDATION_VALIDATE, request),
|
||||||
|
setSharedProductionIds: (productionIds: string[]) =>
|
||||||
|
invokeIpc(IPC_CHANNELS.VALIDATION_SET_SHARED_PRODUCTION_IDS, productionIds),
|
||||||
|
getSharedProductionIds: () => invokeIpc(IPC_CHANNELS.VALIDATION_GET_SHARED_PRODUCTION_IDS),
|
||||||
|
clearSharedProductionIds: () => invokeIpc(IPC_CHANNELS.VALIDATION_CLEAR_SHARED_PRODUCTION_IDS),
|
||||||
|
getCleanerData: () => invokeIpc(IPC_CHANNELS.VALIDATION_GET_CLEANER_DATA)
|
||||||
|
} as const
|
||||||
@@ -1,245 +1,5 @@
|
|||||||
import { contextBridge, ipcRenderer } from 'electron'
|
import { contextBridge } from 'electron'
|
||||||
import type { MySqlConfig, SqlServerConfig } from '../main/types/ipc-api.types'
|
import { api } from './api'
|
||||||
import type { ExtractorInput, ExtractionProgress } from '../main/types/extractor.types'
|
|
||||||
import type { CleanerInput, CleanerProgress, ExportResultItem } from '../main/types/cleaner.types'
|
|
||||||
import type { ResolverInput } from '../main/types/resolver-ipc.types'
|
|
||||||
import type { LoginRequest } from '../main/types/auth-ipc.types'
|
|
||||||
import type { UserInfo } from '../main/types/user.types'
|
|
||||||
import type {
|
|
||||||
ValidationRequest,
|
|
||||||
MaterialTypeRecord,
|
|
||||||
MaterialTypeBatchRequest
|
|
||||||
} from '../main/types/validation.types'
|
|
||||||
import type { IpcResult } from '../main/types/ipc.types'
|
|
||||||
import { IPC_CHANNELS, type LogLevel } from '../shared/ipc-channels'
|
|
||||||
import type { CleanerConfig } from '../main/types/config.schema'
|
|
||||||
import type { DownloadReleaseRequest, UpdateStatus } from '../main/types/update.types'
|
|
||||||
import type { UpdateDialogCatalog } from '../main/types/update.types'
|
|
||||||
|
|
||||||
type ErpSettingsPayload = {
|
|
||||||
erp?: {
|
|
||||||
username?: string
|
|
||||||
password?: string
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const processApi = {
|
|
||||||
versions: {
|
|
||||||
electron: process.versions.electron,
|
|
||||||
chrome: process.versions.chrome,
|
|
||||||
node: process.versions.node
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function invokeIpc<T = unknown>(channel: string, ...args: unknown[]): Promise<IpcResult<T>> {
|
|
||||||
return ipcRenderer.invoke(channel, ...args).then((result: unknown) => {
|
|
||||||
if (result && typeof result === 'object' && 'success' in (result as Record<string, unknown>)) {
|
|
||||||
const typed = result as Record<string, unknown>
|
|
||||||
const hasIpcShape = 'data' in typed || 'code' in typed || 'error' in typed
|
|
||||||
|
|
||||||
if (hasIpcShape) {
|
|
||||||
return typed as unknown as IpcResult<T>
|
|
||||||
}
|
|
||||||
|
|
||||||
if (typed.success === true) {
|
|
||||||
return { success: true, data: result as T }
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
success: false,
|
|
||||||
error: typeof typed.error === 'string' ? typed.error : 'IPC operation failed'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return { success: true, data: result as T }
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
const api = {
|
|
||||||
process: processApi,
|
|
||||||
|
|
||||||
file: {
|
|
||||||
readFile: (filePath: string) => invokeIpc(IPC_CHANNELS.FILE_READ, filePath),
|
|
||||||
writeFile: (filePath: string, content: string) =>
|
|
||||||
invokeIpc(IPC_CHANNELS.FILE_WRITE, filePath, content),
|
|
||||||
fileExists: (filePath: string) => invokeIpc(IPC_CHANNELS.FILE_EXISTS, filePath),
|
|
||||||
listFiles: (dirPath: string) => invokeIpc(IPC_CHANNELS.FILE_LIST, dirPath),
|
|
||||||
openPath: (filePath: string) => invokeIpc(IPC_CHANNELS.FILE_OPEN_PATH, filePath)
|
|
||||||
},
|
|
||||||
|
|
||||||
extractor: {
|
|
||||||
runExtractor: (input: ExtractorInput): Promise<IpcResult> =>
|
|
||||||
invokeIpc(IPC_CHANNELS.EXTRACTOR_RUN, input),
|
|
||||||
onProgress: (callback: (data: ExtractionProgress) => void) => {
|
|
||||||
const subscription = (_event: Electron.IpcRendererEvent, data: ExtractionProgress) =>
|
|
||||||
callback(data)
|
|
||||||
ipcRenderer.on(IPC_CHANNELS.EXTRACTOR_PROGRESS, subscription)
|
|
||||||
return () => ipcRenderer.removeListener(IPC_CHANNELS.EXTRACTOR_PROGRESS, subscription)
|
|
||||||
},
|
|
||||||
onLog: (callback: (data: { level: string; message: string }) => void) => {
|
|
||||||
const subscription = (
|
|
||||||
_event: Electron.IpcRendererEvent,
|
|
||||||
data: { level: string; message: string }
|
|
||||||
) => callback(data)
|
|
||||||
ipcRenderer.on(IPC_CHANNELS.EXTRACTOR_LOG, subscription)
|
|
||||||
return () => ipcRenderer.removeListener(IPC_CHANNELS.EXTRACTOR_LOG, subscription)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
cleaner: {
|
|
||||||
runCleaner: (input: CleanerInput): Promise<IpcResult> =>
|
|
||||||
invokeIpc(IPC_CHANNELS.CLEANER_RUN, input),
|
|
||||||
exportResults: (items: ExportResultItem[]): Promise<IpcResult> =>
|
|
||||||
invokeIpc(IPC_CHANNELS.CLEANER_EXPORT_RESULTS, items),
|
|
||||||
onProgress: (callback: (data: CleanerProgress) => void) => {
|
|
||||||
const subscription = (_event: Electron.IpcRendererEvent, data: CleanerProgress) =>
|
|
||||||
callback(data)
|
|
||||||
ipcRenderer.on(IPC_CHANNELS.CLEANER_PROGRESS, subscription)
|
|
||||||
return () => ipcRenderer.removeListener(IPC_CHANNELS.CLEANER_PROGRESS, subscription)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
resolver: {
|
|
||||||
resolve: (input: ResolverInput): Promise<IpcResult> =>
|
|
||||||
invokeIpc(IPC_CHANNELS.RESOLVER_RESOLVE, input),
|
|
||||||
validateFormat: (inputs: string[]): Promise<IpcResult> =>
|
|
||||||
invokeIpc(IPC_CHANNELS.RESOLVER_VALIDATE_FORMAT, inputs)
|
|
||||||
},
|
|
||||||
|
|
||||||
auth: {
|
|
||||||
getComputerName: (): Promise<IpcResult> => invokeIpc(IPC_CHANNELS.AUTH_GET_COMPUTER_NAME),
|
|
||||||
silentLogin: (): Promise<IpcResult> => invokeIpc(IPC_CHANNELS.AUTH_SILENT_LOGIN),
|
|
||||||
login: (request: LoginRequest): Promise<IpcResult> =>
|
|
||||||
invokeIpc(IPC_CHANNELS.AUTH_LOGIN, request),
|
|
||||||
logout: (): Promise<IpcResult> => invokeIpc(IPC_CHANNELS.AUTH_LOGOUT),
|
|
||||||
getCurrentUser: (): Promise<IpcResult> => invokeIpc(IPC_CHANNELS.AUTH_GET_CURRENT_USER),
|
|
||||||
getAllUsers: (): Promise<IpcResult> => invokeIpc(IPC_CHANNELS.AUTH_GET_ALL_USERS),
|
|
||||||
switchUser: (userInfo: UserInfo): Promise<IpcResult> =>
|
|
||||||
invokeIpc(IPC_CHANNELS.AUTH_SWITCH_USER, userInfo),
|
|
||||||
isAdmin: (): Promise<IpcResult> => invokeIpc(IPC_CHANNELS.AUTH_IS_ADMIN)
|
|
||||||
},
|
|
||||||
|
|
||||||
database: {
|
|
||||||
connectMySql: (config: MySqlConfig): Promise<IpcResult> =>
|
|
||||||
invokeIpc(IPC_CHANNELS.DATABASE_MYSQL_CONNECT, config),
|
|
||||||
disconnectMySql: (): Promise<IpcResult> => invokeIpc(IPC_CHANNELS.DATABASE_MYSQL_DISCONNECT),
|
|
||||||
isMySqlConnected: (): Promise<IpcResult> => invokeIpc(IPC_CHANNELS.DATABASE_MYSQL_IS_CONNECTED),
|
|
||||||
queryMySql: (sql: string, params?: any[]): Promise<IpcResult> =>
|
|
||||||
invokeIpc(IPC_CHANNELS.DATABASE_MYSQL_QUERY, sql, params),
|
|
||||||
connectSqlServer: (config: SqlServerConfig): Promise<IpcResult> =>
|
|
||||||
invokeIpc(IPC_CHANNELS.DATABASE_SQLSERVER_CONNECT, config),
|
|
||||||
disconnectSqlServer: (): Promise<IpcResult> =>
|
|
||||||
invokeIpc(IPC_CHANNELS.DATABASE_SQLSERVER_DISCONNECT),
|
|
||||||
isSqlServerConnected: (): Promise<IpcResult> =>
|
|
||||||
invokeIpc(IPC_CHANNELS.DATABASE_SQLSERVER_IS_CONNECTED),
|
|
||||||
querySqlServer: (sql: string, params?: Record<string, unknown>): Promise<IpcResult> =>
|
|
||||||
invokeIpc(IPC_CHANNELS.DATABASE_SQLSERVER_QUERY, sql, params)
|
|
||||||
},
|
|
||||||
|
|
||||||
validation: {
|
|
||||||
validate: (request: ValidationRequest): Promise<IpcResult> =>
|
|
||||||
invokeIpc(IPC_CHANNELS.VALIDATION_VALIDATE, request),
|
|
||||||
setSharedProductionIds: (productionIds: string[]): Promise<IpcResult> =>
|
|
||||||
invokeIpc(IPC_CHANNELS.VALIDATION_SET_SHARED_PRODUCTION_IDS, productionIds),
|
|
||||||
getSharedProductionIds: (): Promise<IpcResult> =>
|
|
||||||
invokeIpc(IPC_CHANNELS.VALIDATION_GET_SHARED_PRODUCTION_IDS),
|
|
||||||
clearSharedProductionIds: (): Promise<IpcResult> =>
|
|
||||||
invokeIpc(IPC_CHANNELS.VALIDATION_CLEAR_SHARED_PRODUCTION_IDS),
|
|
||||||
getCleanerData: (): Promise<IpcResult> => invokeIpc(IPC_CHANNELS.VALIDATION_GET_CLEANER_DATA)
|
|
||||||
},
|
|
||||||
|
|
||||||
materials: {
|
|
||||||
upsertBatch: (materials: { materialCode: string; managerName: string }[]): Promise<IpcResult> =>
|
|
||||||
invokeIpc(IPC_CHANNELS.MATERIALS_UPSERT_BATCH, { materials }),
|
|
||||||
delete: (materialCodes: string[]): Promise<IpcResult> =>
|
|
||||||
invokeIpc(IPC_CHANNELS.MATERIALS_DELETE, { materialCodes }),
|
|
||||||
getManagers: (): Promise<IpcResult> => invokeIpc(IPC_CHANNELS.MATERIALS_GET_MANAGERS),
|
|
||||||
getByManager: (managerName: string): Promise<IpcResult> =>
|
|
||||||
invokeIpc(IPC_CHANNELS.MATERIALS_GET_BY_MANAGER, managerName),
|
|
||||||
getAll: (): Promise<IpcResult> => invokeIpc(IPC_CHANNELS.MATERIALS_GET_ALL),
|
|
||||||
getStatistics: (): Promise<IpcResult> => invokeIpc(IPC_CHANNELS.MATERIALS_GET_STATISTICS),
|
|
||||||
updateManager: (materialCode: string, managerName: string): Promise<IpcResult> =>
|
|
||||||
invokeIpc(IPC_CHANNELS.MATERIALS_UPDATE_MANAGER, { materialCode, managerName })
|
|
||||||
},
|
|
||||||
|
|
||||||
settings: {
|
|
||||||
getUserType: (): Promise<IpcResult> => invokeIpc(IPC_CHANNELS.SETTINGS_GET_USER_TYPE),
|
|
||||||
getSettings: (): Promise<IpcResult> => invokeIpc(IPC_CHANNELS.SETTINGS_GET_SETTINGS),
|
|
||||||
saveSettings: (settings: ErpSettingsPayload): Promise<IpcResult> =>
|
|
||||||
invokeIpc(IPC_CHANNELS.SETTINGS_SAVE_SETTINGS, settings),
|
|
||||||
resetDefaults: (): Promise<IpcResult> => invokeIpc(IPC_CHANNELS.SETTINGS_RESET_DEFAULTS),
|
|
||||||
testDbConnection: (): Promise<IpcResult> => invokeIpc(IPC_CHANNELS.SETTINGS_TEST_DB_CONNECTION)
|
|
||||||
},
|
|
||||||
|
|
||||||
materialType: {
|
|
||||||
getAll: (): Promise<IpcResult<MaterialTypeRecord[]>> =>
|
|
||||||
invokeIpc(IPC_CHANNELS.MATERIAL_TYPE_GET_ALL),
|
|
||||||
getByManager: (managerName: string): Promise<IpcResult<MaterialTypeRecord[]>> =>
|
|
||||||
invokeIpc(IPC_CHANNELS.MATERIAL_TYPE_GET_BY_MANAGER, managerName),
|
|
||||||
getManagers: (): Promise<IpcResult<string[]>> =>
|
|
||||||
invokeIpc(IPC_CHANNELS.MATERIAL_TYPE_GET_MANAGERS),
|
|
||||||
upsert: (materialName: string, managerName: string): Promise<IpcResult> =>
|
|
||||||
invokeIpc(IPC_CHANNELS.MATERIAL_TYPE_UPSERT, { materialName, managerName }),
|
|
||||||
delete: (materialName: string, managerName: string): Promise<IpcResult> =>
|
|
||||||
invokeIpc(IPC_CHANNELS.MATERIAL_TYPE_DELETE, { materialName, managerName }),
|
|
||||||
upsertBatch: (request: MaterialTypeBatchRequest): Promise<IpcResult> =>
|
|
||||||
invokeIpc(IPC_CHANNELS.MATERIAL_TYPE_UPSERT_BATCH, request)
|
|
||||||
},
|
|
||||||
|
|
||||||
userErpConfig: {
|
|
||||||
getCurrent: (): Promise<IpcResult> => invokeIpc(IPC_CHANNELS.USER_ERP_CONFIG_GET_CURRENT),
|
|
||||||
update: (config: { url: string; username: string; password: string }): Promise<IpcResult> =>
|
|
||||||
invokeIpc(IPC_CHANNELS.USER_ERP_CONFIG_UPDATE, config),
|
|
||||||
testConnection: (config: {
|
|
||||||
url: string
|
|
||||||
username: string
|
|
||||||
password: string
|
|
||||||
}): Promise<IpcResult> => invokeIpc(IPC_CHANNELS.USER_ERP_CONFIG_TEST_CONNECTION, config),
|
|
||||||
getAll: (): Promise<IpcResult> => invokeIpc(IPC_CHANNELS.USER_ERP_CONFIG_GET_ALL)
|
|
||||||
},
|
|
||||||
|
|
||||||
config: {
|
|
||||||
getCleaner: (): Promise<IpcResult<CleanerConfig>> => invokeIpc(IPC_CHANNELS.CONFIG_GET_CLEANER),
|
|
||||||
updateCleaner: (updates: Partial<CleanerConfig>): Promise<IpcResult<CleanerConfig>> =>
|
|
||||||
invokeIpc(IPC_CHANNELS.CONFIG_UPDATE_CLEANER, updates)
|
|
||||||
},
|
|
||||||
|
|
||||||
logger: {
|
|
||||||
log: (level: LogLevel, message: string, context?: Record<string, unknown>): void => {
|
|
||||||
ipcRenderer.send(IPC_CHANNELS.LOGGER_FORWARD, {
|
|
||||||
level,
|
|
||||||
message,
|
|
||||||
context,
|
|
||||||
timestamp: Date.now()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
report: {
|
|
||||||
listAll: (): Promise<IpcResult> => invokeIpc(IPC_CHANNELS.REPORT_LIST_ALL),
|
|
||||||
listByUser: (username: string): Promise<IpcResult> =>
|
|
||||||
invokeIpc(IPC_CHANNELS.REPORT_LIST_BY_USER, username),
|
|
||||||
download: (key: string): Promise<IpcResult> => invokeIpc(IPC_CHANNELS.REPORT_DOWNLOAD, key)
|
|
||||||
},
|
|
||||||
|
|
||||||
update: {
|
|
||||||
getStatus: (): Promise<IpcResult<UpdateStatus>> => invokeIpc(IPC_CHANNELS.UPDATE_GET_STATUS),
|
|
||||||
checkNow: (): Promise<IpcResult<UpdateStatus>> => invokeIpc(IPC_CHANNELS.UPDATE_CHECK_NOW),
|
|
||||||
getCatalog: (): Promise<IpcResult<UpdateDialogCatalog>> =>
|
|
||||||
invokeIpc(IPC_CHANNELS.UPDATE_GET_CATALOG),
|
|
||||||
getChangelog: (release: DownloadReleaseRequest): Promise<IpcResult<string>> =>
|
|
||||||
invokeIpc(IPC_CHANNELS.UPDATE_GET_CHANGELOG, release),
|
|
||||||
downloadRelease: (release: DownloadReleaseRequest): Promise<IpcResult<UpdateStatus>> =>
|
|
||||||
invokeIpc(IPC_CHANNELS.UPDATE_DOWNLOAD_RELEASE, release),
|
|
||||||
installDownloaded: (): Promise<IpcResult<void>> =>
|
|
||||||
invokeIpc(IPC_CHANNELS.UPDATE_INSTALL_DOWNLOADED),
|
|
||||||
onStatusChanged: (callback: (data: UpdateStatus) => void) => {
|
|
||||||
const subscription = (_event: Electron.IpcRendererEvent, data: UpdateStatus) => callback(data)
|
|
||||||
ipcRenderer.on(IPC_CHANNELS.UPDATE_STATUS_CHANGED, subscription)
|
|
||||||
return () => ipcRenderer.removeListener(IPC_CHANNELS.UPDATE_STATUS_CHANGED, subscription)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} as const
|
|
||||||
|
|
||||||
if (process.contextIsolated) {
|
if (process.contextIsolated) {
|
||||||
try {
|
try {
|
||||||
|
|||||||
28
src/preload/lib/ipc.ts
Normal file
28
src/preload/lib/ipc.ts
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
import { ipcRenderer } from 'electron'
|
||||||
|
import type { IpcResult } from '../../main/types/ipc.types'
|
||||||
|
|
||||||
|
export function invokeIpc<T = unknown>(channel: string, ...args: unknown[]): Promise<IpcResult<T>> {
|
||||||
|
return ipcRenderer.invoke(channel, ...args).then((result: unknown) => {
|
||||||
|
if (result && typeof result === 'object' && 'success' in (result as Record<string, unknown>)) {
|
||||||
|
const typed = result as Record<string, unknown>
|
||||||
|
const hasIpcShape = 'data' in typed || 'code' in typed || 'error' in typed
|
||||||
|
|
||||||
|
if (hasIpcShape) {
|
||||||
|
return typed as unknown as IpcResult<T>
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typed.success === true) {
|
||||||
|
return { success: true, data: result as T }
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
error: typeof typed.error === 'string' ? typed.error : 'IPC operation failed'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return { success: true, data: result as T }
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
export { ipcRenderer }
|
||||||
Reference in New Issue
Block a user