fix: support specific importer when import from curl (#9197)

This commit is contained in:
Bingbing
2025-10-09 18:14:52 +08:00
committed by GitHub
parent 8f25cec593
commit c53e80e106
4 changed files with 34 additions and 12 deletions

View File

@@ -135,7 +135,7 @@ const main: Window['main'] = {
insecureReadFile: options => ipcRenderer.invoke('insecureReadFile', options),
insecureReadFileWithEncoding: options => ipcRenderer.invoke('insecureReadFileWithEncoding', options),
secureReadFile: options => ipcRenderer.invoke('secureReadFile', options),
parseImport: options => ipcRenderer.invoke('parseImport', options),
parseImport: (...args) => ipcRenderer.invoke('parseImport', ...args),
readDir: options => ipcRenderer.invoke('readDir', options),
lintSpec: options => ipcRenderer.invoke('lintSpec', options),
on: (channel, listener) => {

View File

@@ -18,9 +18,19 @@ export interface ConvertResult {
};
}
export const convert = async (importEntry: ImportEntry) => {
const importers = (await import('./importers')).importers;
export const convert = async (
importEntry: ImportEntry,
{
importerId,
}: {
importerId?: string;
} = {},
) => {
let importers = (await import('./importers')).importers;
const errMsgList: string[] = [];
if (importerId) {
importers = importers.filter(i => i.id === importerId);
}
for (const importer of importers) {
let resources;
if (importer.acceptFilePath === true) {

View File

@@ -66,7 +66,7 @@ export interface RendererToMainBridgeAPI {
cancelAuthorizationInDefaultBrowser: typeof cancelAuthorizationInDefaultBrowser;
setMenuBarVisibility: (visible: boolean) => void;
installPlugin: typeof installPlugin;
parseImport: (options: { contentStr: string }) => Promise<{ data: { resources: models.BaseModel[] } }>;
parseImport: typeof convert;
writeFile: (options: { path: string; content: string }) => Promise<string>;
secureReadFile: (options: { path: string }) => Promise<string>;
insecureReadFile: (options: { path: string }) => Promise<string>;
@@ -162,8 +162,8 @@ export function registerMainHandlers() {
return cancelAuthorizationInDefaultBrowser(options);
},
);
ipcMainHandle('parseImport', async (_, options: { contentStr: string }) => {
return convert(options);
ipcMainHandle('parseImport', async (_, ...args: Parameters<typeof convert>) => {
return convert(...args);
});
ipcMainHandle('writeFile', async (_, options: { path: string; content: string }) => {
try {

View File

@@ -21,9 +21,14 @@ export const PasteCurlModal = ({
useEffect(() => {
async function parseCurlToRequest() {
try {
const { data } = await window.main.parseImport({
contentStr: defaultValue || '',
});
const { data } = await window.main.parseImport(
{
contentStr: defaultValue || '',
},
{
importerId: 'curl',
},
);
const { resources } = data;
const importedRequest = resources[0];
setIsValid(true);
@@ -53,12 +58,19 @@ export const PasteCurlModal = ({
defaultValue={defaultValue}
onChange={async value => {
if (!value) {
setIsValid(false);
setReq({});
return;
}
try {
const { data } = await window.main.parseImport({
contentStr: value,
});
const { data } = await window.main.parseImport(
{
contentStr: value,
},
{
importerId: 'curl',
},
);
const { resources } = data;
const importedRequest = resources[0];
setIsValid(true);