@@ -3,6 +3,8 @@ import { getSecretValue } from '../../common/secret/utils';
33import axios from 'axios' ;
44import { getErrText } from '@fastgpt/global/common/error/utils' ;
55import type { RequireOnlyOne } from '@fastgpt/global/common/type/utils' ;
6+ import type { HttpToolConfigType } from '@fastgpt/global/core/app/type' ;
7+ import { contentTypeMap , ContentTypes } from '@fastgpt/global/core/workflow/constants' ;
68
79export type RunHTTPToolParams = {
810 baseUrl : string ;
@@ -11,48 +13,140 @@ export type RunHTTPToolParams = {
1113 params : Record < string , any > ;
1214 headerSecret ?: StoreSecretValueType ;
1315 customHeaders ?: Record < string , string > ;
16+ staticParams ?: HttpToolConfigType [ 'staticParams' ] ;
17+ staticHeaders ?: HttpToolConfigType [ 'staticHeaders' ] ;
18+ staticBody ?: HttpToolConfigType [ 'staticBody' ] ;
1419} ;
1520
1621export type RunHTTPToolResult = RequireOnlyOne < {
1722 data ?: any ;
1823 errorMsg ?: string ;
1924} > ;
2025
21- export async function runHTTPTool ( {
26+ const buildHttpRequest = ( {
27+ method,
28+ params,
29+ headerSecret,
30+ customHeaders,
31+ staticParams,
32+ staticHeaders,
33+ staticBody
34+ } : Omit < RunHTTPToolParams , 'baseUrl' | 'toolPath' > ) => {
35+ const body = ( ( ) => {
36+ if ( ! staticBody || staticBody . type === ContentTypes . none ) {
37+ return [ 'POST' , 'PUT' , 'PATCH' ] . includes ( method . toUpperCase ( ) ) ? params : undefined ;
38+ }
39+
40+ if ( staticBody . type === ContentTypes . json ) {
41+ const staticContent = staticBody . content ? JSON . parse ( staticBody . content ) : { } ;
42+ return { ...staticContent , ...params } ;
43+ }
44+
45+ if ( staticBody . type === ContentTypes . formData ) {
46+ const formData = new ( require ( 'form-data' ) ) ( ) ;
47+ staticBody . formData ?. forEach ( ( { key, value } ) => {
48+ formData . append ( key , value ) ;
49+ } ) ;
50+ Object . entries ( params ) . forEach ( ( [ key , value ] ) => {
51+ formData . append ( key , value ) ;
52+ } ) ;
53+ return formData ;
54+ }
55+
56+ if ( staticBody . type === ContentTypes . xWwwFormUrlencoded ) {
57+ const urlencoded = new URLSearchParams ( ) ;
58+ staticBody . formData ?. forEach ( ( { key, value } ) => {
59+ urlencoded . append ( key , value ) ;
60+ } ) ;
61+ Object . entries ( params ) . forEach ( ( [ key , value ] ) => {
62+ urlencoded . append ( key , String ( value ) ) ;
63+ } ) ;
64+ return urlencoded . toString ( ) ;
65+ }
66+
67+ if ( staticBody . type === ContentTypes . xml || staticBody . type === ContentTypes . raw ) {
68+ return staticBody . content || '' ;
69+ }
70+
71+ return undefined ;
72+ } ) ( ) ;
73+
74+ const contentType = contentTypeMap [ staticBody ?. type || ContentTypes . none ] ;
75+ const headers = {
76+ ...( contentType && { 'Content-Type' : contentType } ) ,
77+ ...( customHeaders || { } ) ,
78+ ...( headerSecret ? getSecretValue ( { storeSecret : headerSecret } ) : { } ) ,
79+ ...( staticHeaders ?. reduce (
80+ ( acc , { key, value } ) => {
81+ acc [ key ] = value ;
82+ return acc ;
83+ } ,
84+ { } as Record < string , string >
85+ ) || { } )
86+ } ;
87+
88+ const queryParams = ( ( ) => {
89+ const staticParamsObj =
90+ staticParams ?. reduce (
91+ ( acc , { key, value } ) => {
92+ acc [ key ] = value ;
93+ return acc ;
94+ } ,
95+ { } as Record < string , any >
96+ ) || { } ;
97+
98+ const mergedParams =
99+ method . toUpperCase ( ) === 'GET' || staticParams
100+ ? { ...staticParamsObj , ...params }
101+ : staticParamsObj ;
102+
103+ return Object . keys ( mergedParams ) . length > 0 ? mergedParams : undefined ;
104+ } ) ( ) ;
105+
106+ return {
107+ headers,
108+ body,
109+ queryParams
110+ } ;
111+ } ;
112+
113+ export const runHTTPTool = async ( {
22114 baseUrl,
23115 toolPath,
24116 method = 'POST' ,
25117 params,
26118 headerSecret,
27- customHeaders
28- } : RunHTTPToolParams ) : Promise < RunHTTPToolResult > {
119+ customHeaders,
120+ staticParams,
121+ staticHeaders,
122+ staticBody
123+ } : RunHTTPToolParams ) : Promise < RunHTTPToolResult > => {
29124 try {
30- const headers = {
31- 'Content-Type' : 'application/json' ,
32- ...( customHeaders || { } ) ,
33- ...( headerSecret ? getSecretValue ( { storeSecret : headerSecret } ) : { } )
34- } ;
125+ const { headers, body, queryParams } = buildHttpRequest ( {
126+ method,
127+ params,
128+ headerSecret,
129+ customHeaders,
130+ staticParams,
131+ staticHeaders,
132+ staticBody
133+ } ) ;
35134
36135 const { data } = await axios ( {
37136 method : method . toUpperCase ( ) ,
38137 baseURL : baseUrl . startsWith ( 'https://' ) ? baseUrl : `https://${ baseUrl } ` ,
39138 url : toolPath ,
40139 headers,
41- data : params ,
42- params,
140+ data : body ,
141+ params : queryParams ,
43142 timeout : 300000 ,
44143 httpsAgent : new ( require ( 'https' ) . Agent ) ( {
45144 rejectUnauthorized : false
46145 } )
47146 } ) ;
48147
49- return {
50- data
51- } ;
148+ return { data } ;
52149 } catch ( error : any ) {
53- console . log ( error ) ;
54- return {
55- errorMsg : getErrText ( error )
56- } ;
150+ return { errorMsg : getErrText ( error ) } ;
57151 }
58- }
152+ } ;
0 commit comments