perf: fetch url (#4687)
This commit is contained in:
@@ -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);
|
||||
@@ -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);
|
||||
Reference in New Issue
Block a user