perf: fetch url (#4687)

This commit is contained in:
Archer
2025-04-27 23:11:08 +08:00
committed by GitHub
parent 1d2026786e
commit d6fed3d23a
9 changed files with 60 additions and 66 deletions

View File

@@ -1,51 +0,0 @@
import { NextAPI } from '@/service/middleware/entry';
import { ApiRequestProps, ApiResponseType } from '@fastgpt/service/type/next';
import axios from 'axios';
import { authCert } from '@fastgpt/service/support/permission/auth/common';
export type FetchWorkflowBody = {
url: string;
};
export type FetchWorkflowQuery = {
url: string;
};
export type FetchWorkflowResponseType = ApiResponseType<{
data: JSON;
}>;
async function handler(
req: ApiRequestProps<FetchWorkflowBody, FetchWorkflowQuery>,
res: FetchWorkflowResponseType
) {
await authCert({ req, authToken: true });
const url = req.body?.url || req.query?.url;
if (!url) {
return Promise.reject('app:type.error.URLempty');
}
const response = await axios.get(url, {
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
'User-Agent': 'Mozilla/5.0 (compatible; FastGPT/1.0)'
},
timeout: 30000,
validateStatus: (status) => status < 500
});
const contentType = response.headers['content-type'] || '';
if (!response.data || response.data.length === 0) {
return Promise.reject('app:type.error.workflowresponseempty');
}
JSON.parse(JSON.stringify(response.data));
return response.data;
}
export default NextAPI(handler);

View File

@@ -0,0 +1,46 @@
import { NextAPI } from '@/service/middleware/entry';
import { ApiRequestProps } from '@fastgpt/service/type/next';
import axios from 'axios';
import { authCert } from '@fastgpt/service/support/permission/auth/common';
import { isInternalAddress } from '@fastgpt/service/common/system/utils';
import { NextApiResponse } from 'next';
export type FetchWorkflowBody = {
url: string;
};
export type FetchWorkflowQuery = {};
export type FetchWorkflowResponseType = {
data: Record<string, any>;
};
async function handler(
req: ApiRequestProps<FetchWorkflowBody, FetchWorkflowQuery>,
res: NextApiResponse
): Promise<FetchWorkflowResponseType> {
await authCert({ req, authToken: true });
const url = req.body?.url;
if (!url) {
return Promise.reject('Url is empty');
}
if (isInternalAddress(url)) {
return Promise.reject('Url is invalid');
}
const { data } = await axios.get(url, {
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
'User-Agent': 'Mozilla/5.0 (compatible; FastGPT/1.0)'
},
timeout: 30000,
validateStatus: (status) => status < 500
});
return data;
}
export default NextAPI(handler);