1
- import axios , { AxiosRequestConfig } from 'axios'
2
- import { omit } from 'lodash'
3
1
import { Document } from '@langchain/core/documents'
4
- import { TextSplitter } from 'langchain/text_splitter'
2
+ import axios , { AxiosRequestConfig } from 'axios'
3
+ import * as https from 'https'
5
4
import { BaseDocumentLoader } from 'langchain/document_loaders/base'
5
+ import { TextSplitter } from 'langchain/text_splitter'
6
+ import { omit } from 'lodash'
7
+ import { getFileFromStorage } from '../../../src'
6
8
import { ICommonObject , IDocument , INode , INodeData , INodeOutputsValue , INodeParams } from '../../../src/Interface'
7
9
import { handleEscapeCharacters } from '../../../src/utils'
8
10
@@ -21,7 +23,7 @@ class API_DocumentLoaders implements INode {
21
23
constructor ( ) {
22
24
this . label = 'API Loader'
23
25
this . name = 'apiLoader'
24
- this . version = 2.0
26
+ this . version = 2.1
25
27
this . type = 'Document'
26
28
this . icon = 'api.svg'
27
29
this . category = 'Document Loaders'
@@ -61,6 +63,15 @@ class API_DocumentLoaders implements INode {
61
63
additionalParams : true ,
62
64
optional : true
63
65
} ,
66
+ {
67
+ label : 'SSL Certificate' ,
68
+ description : 'Please upload a SSL certificate file in either .pem or .crt' ,
69
+ name : 'caFile' ,
70
+ type : 'file' ,
71
+ fileType : '.pem, .crt' ,
72
+ additionalParams : true ,
73
+ optional : true
74
+ } ,
64
75
{
65
76
label : 'Body' ,
66
77
name : 'body' ,
@@ -105,8 +116,10 @@ class API_DocumentLoaders implements INode {
105
116
}
106
117
]
107
118
}
108
- async init ( nodeData : INodeData ) : Promise < any > {
119
+
120
+ async init ( nodeData : INodeData , _ : string , options : ICommonObject ) : Promise < any > {
109
121
const headers = nodeData . inputs ?. headers as string
122
+ const caFileBase64 = nodeData . inputs ?. caFile as string
110
123
const url = nodeData . inputs ?. url as string
111
124
const body = nodeData . inputs ?. body as string
112
125
const method = nodeData . inputs ?. method as string
@@ -120,22 +133,37 @@ class API_DocumentLoaders implements INode {
120
133
omitMetadataKeys = _omitMetadataKeys . split ( ',' ) . map ( ( key ) => key . trim ( ) )
121
134
}
122
135
123
- const options : ApiLoaderParams = {
136
+ const apiLoaderParam : ApiLoaderParams = {
124
137
url,
125
138
method
126
139
}
127
140
128
141
if ( headers ) {
129
142
const parsedHeaders = typeof headers === 'object' ? headers : JSON . parse ( headers )
130
- options . headers = parsedHeaders
143
+ apiLoaderParam . headers = parsedHeaders
144
+ }
145
+
146
+ if ( caFileBase64 . startsWith ( 'FILE-STORAGE::' ) ) {
147
+ let file = caFileBase64 . replace ( 'FILE-STORAGE::' , '' )
148
+ file = file . replace ( '[' , '' )
149
+ file = file . replace ( ']' , '' )
150
+ const orgId = options . orgId
151
+ const chatflowid = options . chatflowid
152
+ const fileData = await getFileFromStorage ( file , orgId , chatflowid )
153
+ apiLoaderParam . ca = fileData . toString ( )
154
+ } else {
155
+ const splitDataURI = caFileBase64 . split ( ',' )
156
+ splitDataURI . pop ( )
157
+ const bf = Buffer . from ( splitDataURI . pop ( ) || '' , 'base64' )
158
+ apiLoaderParam . ca = bf . toString ( 'utf-8' )
131
159
}
132
160
133
161
if ( body ) {
134
162
const parsedBody = typeof body === 'object' ? body : JSON . parse ( body )
135
- options . body = parsedBody
163
+ apiLoaderParam . body = parsedBody
136
164
}
137
165
138
- const loader = new ApiLoader ( options )
166
+ const loader = new ApiLoader ( apiLoaderParam )
139
167
140
168
let docs : IDocument [ ] = [ ]
141
169
@@ -195,6 +223,7 @@ interface ApiLoaderParams {
195
223
method : string
196
224
headers ?: ICommonObject
197
225
body ?: ICommonObject
226
+ ca ?: string
198
227
}
199
228
200
229
class ApiLoader extends BaseDocumentLoader {
@@ -206,28 +235,36 @@ class ApiLoader extends BaseDocumentLoader {
206
235
207
236
public readonly method : string
208
237
209
- constructor ( { url, headers, body, method } : ApiLoaderParams ) {
238
+ public readonly ca ?: string
239
+
240
+ constructor ( { url, headers, body, method, ca } : ApiLoaderParams ) {
210
241
super ( )
211
242
this . url = url
212
243
this . headers = headers
213
244
this . body = body
214
245
this . method = method
246
+ this . ca = ca
215
247
}
216
248
217
249
public async load ( ) : Promise < IDocument [ ] > {
218
250
if ( this . method === 'POST' ) {
219
- return this . executePostRequest ( this . url , this . headers , this . body )
251
+ return this . executePostRequest ( this . url , this . headers , this . body , this . ca )
220
252
} else {
221
- return this . executeGetRequest ( this . url , this . headers )
253
+ return this . executeGetRequest ( this . url , this . headers , this . ca )
222
254
}
223
255
}
224
256
225
- protected async executeGetRequest ( url : string , headers ?: ICommonObject ) : Promise < IDocument [ ] > {
257
+ protected async executeGetRequest ( url : string , headers ?: ICommonObject , ca ?: string ) : Promise < IDocument [ ] > {
226
258
try {
227
259
const config : AxiosRequestConfig = { }
228
260
if ( headers ) {
229
261
config . headers = headers
230
262
}
263
+ if ( ca ) {
264
+ config . httpsAgent = new https . Agent ( {
265
+ ca : ca
266
+ } )
267
+ }
231
268
const response = await axios . get ( url , config )
232
269
const responseJsonString = JSON . stringify ( response . data , null , 2 )
233
270
const doc = new Document ( {
@@ -242,12 +279,17 @@ class ApiLoader extends BaseDocumentLoader {
242
279
}
243
280
}
244
281
245
- protected async executePostRequest ( url : string , headers ?: ICommonObject , body ?: ICommonObject ) : Promise < IDocument [ ] > {
282
+ protected async executePostRequest ( url : string , headers ?: ICommonObject , body ?: ICommonObject , ca ?: string ) : Promise < IDocument [ ] > {
246
283
try {
247
284
const config : AxiosRequestConfig = { }
248
285
if ( headers ) {
249
286
config . headers = headers
250
287
}
288
+ if ( ca ) {
289
+ config . httpsAgent = new https . Agent ( {
290
+ ca : ca
291
+ } )
292
+ }
251
293
const response = await axios . post ( url , body ?? { } , config )
252
294
const responseJsonString = JSON . stringify ( response . data , null , 2 )
253
295
const doc = new Document ( {
0 commit comments