mirror of
https://github.com/usebruno/bruno.git
synced 2026-07-08 22:45:25 +00:00
126 lines
4.2 KiB
JavaScript
126 lines
4.2 KiB
JavaScript
const { ipcMain } = require('electron');
|
|
const { openApiSpecDialog, openApiSpec } = require('../app/apiSpecs');
|
|
const { writeFile } = require('../utils/filesystem');
|
|
const { removeApiSpecUid } = require('../cache/apiSpecUids');
|
|
const { removeApiSpecFromWorkspace } = require('../utils/workspace-config');
|
|
const { getCertsAndProxyConfig } = require('./network/cert-utils');
|
|
const { makeAxiosInstance } = require('./network/axios-instance');
|
|
const { proxySwaggerFetch } = require('./swagger-fetch');
|
|
const path = require('path');
|
|
const fs = require('fs');
|
|
|
|
const registerRendererEventHandlers = (mainWindow, watcher, lastOpenedApiSpecs) => {
|
|
ipcMain.handle('renderer:open-api-spec', (event, workspacePath = null) => {
|
|
if (watcher && mainWindow) {
|
|
openApiSpecDialog(mainWindow, watcher, { workspacePath });
|
|
}
|
|
});
|
|
|
|
ipcMain.handle('renderer:open-api-spec-file', (event, apiSpecPath, workspacePath = null) => {
|
|
if (watcher && mainWindow) {
|
|
openApiSpec(mainWindow, watcher, apiSpecPath, { workspacePath });
|
|
}
|
|
});
|
|
|
|
ipcMain.handle('renderer:save-api-spec', async (event, pathname, content) => {
|
|
try {
|
|
await writeFile(pathname, content);
|
|
Promise.resolve();
|
|
} catch (error) {
|
|
return Promise.reject(error);
|
|
}
|
|
});
|
|
|
|
ipcMain.handle('renderer:create-api-spec', async (event, apiSpecName, apiSpecLocation, content = '', workspacePath = null) => {
|
|
try {
|
|
let pathname = path.join(apiSpecLocation, apiSpecName);
|
|
if (fs.existsSync(pathname)) {
|
|
throw new Error(`path: ${pathname} already exists`);
|
|
}
|
|
await writeFile(pathname, content);
|
|
openApiSpec(mainWindow, watcher, pathname, { workspacePath });
|
|
} catch (error) {
|
|
return Promise.reject(error);
|
|
}
|
|
});
|
|
|
|
ipcMain.handle('renderer:remove-api-spec', async (event, pathname, workspacePath = null) => {
|
|
try {
|
|
if (watcher && mainWindow) {
|
|
watcher.removeWatcher(pathname, mainWindow);
|
|
removeApiSpecUid(pathname);
|
|
|
|
if (workspacePath) {
|
|
const workspaceFilePath = path.join(workspacePath, 'workspace.yml');
|
|
|
|
if (fs.existsSync(workspaceFilePath)) {
|
|
await removeApiSpecFromWorkspace(workspacePath, pathname);
|
|
}
|
|
}
|
|
}
|
|
} catch (error) {
|
|
return Promise.reject(error);
|
|
}
|
|
});
|
|
|
|
ipcMain.handle('renderer:fetch-api-spec', async (event, url) => {
|
|
try {
|
|
// Use a proxy-aware axios instance so that the user's configured proxy
|
|
const { proxyMode, proxyConfig, httpsAgentRequestFields, interpolationOptions }
|
|
= await getCertsAndProxyConfig({
|
|
collectionUid: null,
|
|
collection: { promptVariables: {} },
|
|
request: {},
|
|
envVars: {},
|
|
runtimeVariables: {},
|
|
processEnvVars: {},
|
|
collectionPath: '',
|
|
globalEnvironmentVariables: {}
|
|
});
|
|
|
|
const axiosInstance = makeAxiosInstance({ proxyMode, proxyConfig, httpsAgentRequestFields, interpolationOptions });
|
|
const response = await axiosInstance.get(url, {
|
|
timeout: 30000,
|
|
transformResponse: [(data) => data]
|
|
});
|
|
return response.data;
|
|
} catch (error) {
|
|
return Promise.reject(error);
|
|
}
|
|
});
|
|
|
|
ipcMain.handle('renderer:swagger-fetch', async (event, req) => {
|
|
return proxySwaggerFetch(req);
|
|
});
|
|
|
|
ipcMain.handle('renderer:ensure-apispec-folder', async (event, workspacePath) => {
|
|
try {
|
|
const apiSpecPath = path.join(workspacePath, 'apispec');
|
|
if (!fs.existsSync(apiSpecPath)) {
|
|
fs.mkdirSync(apiSpecPath, { recursive: true });
|
|
}
|
|
return apiSpecPath;
|
|
} catch (error) {
|
|
return Promise.reject(error);
|
|
}
|
|
});
|
|
};
|
|
|
|
const registerMainEventHandlers = (mainWindow, watcher, lastOpenedApiSpecs) => {
|
|
ipcMain.handle('main:open-api-spec', () => {
|
|
if (watcher && mainWindow) {
|
|
openApiSpecDialog(mainWindow, watcher);
|
|
}
|
|
});
|
|
ipcMain.on('main:apispec-opened', (win, pathname, uid, workspacePath = null) => {
|
|
watcher.addWatcher(win, pathname, uid, {}, workspacePath);
|
|
});
|
|
};
|
|
|
|
const registerApiSpecIpc = (mainWindow, watcher, lastOpenedApiSpecs) => {
|
|
registerRendererEventHandlers(mainWindow, watcher, lastOpenedApiSpecs);
|
|
registerMainEventHandlers(mainWindow, watcher, lastOpenedApiSpecs);
|
|
};
|
|
|
|
module.exports = registerApiSpecIpc;
|