Skip to content

Commit 5f7f83a

Browse files
authored
feature/apiloader-ca-upload (#4533)
feat: add CA(.pem, .crt) upload in API Loader
1 parent d134b66 commit 5f7f83a

File tree

1 file changed

+56
-14
lines changed

1 file changed

+56
-14
lines changed

packages/components/nodes/documentloaders/API/APILoader.ts

Lines changed: 56 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
import axios, { AxiosRequestConfig } from 'axios'
2-
import { omit } from 'lodash'
31
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'
54
import { BaseDocumentLoader } from 'langchain/document_loaders/base'
5+
import { TextSplitter } from 'langchain/text_splitter'
6+
import { omit } from 'lodash'
7+
import { getFileFromStorage } from '../../../src'
68
import { ICommonObject, IDocument, INode, INodeData, INodeOutputsValue, INodeParams } from '../../../src/Interface'
79
import { handleEscapeCharacters } from '../../../src/utils'
810

@@ -21,7 +23,7 @@ class API_DocumentLoaders implements INode {
2123
constructor() {
2224
this.label = 'API Loader'
2325
this.name = 'apiLoader'
24-
this.version = 2.0
26+
this.version = 2.1
2527
this.type = 'Document'
2628
this.icon = 'api.svg'
2729
this.category = 'Document Loaders'
@@ -61,6 +63,15 @@ class API_DocumentLoaders implements INode {
6163
additionalParams: true,
6264
optional: true
6365
},
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+
},
6475
{
6576
label: 'Body',
6677
name: 'body',
@@ -105,8 +116,10 @@ class API_DocumentLoaders implements INode {
105116
}
106117
]
107118
}
108-
async init(nodeData: INodeData): Promise<any> {
119+
120+
async init(nodeData: INodeData, _: string, options: ICommonObject): Promise<any> {
109121
const headers = nodeData.inputs?.headers as string
122+
const caFileBase64 = nodeData.inputs?.caFile as string
110123
const url = nodeData.inputs?.url as string
111124
const body = nodeData.inputs?.body as string
112125
const method = nodeData.inputs?.method as string
@@ -120,22 +133,37 @@ class API_DocumentLoaders implements INode {
120133
omitMetadataKeys = _omitMetadataKeys.split(',').map((key) => key.trim())
121134
}
122135

123-
const options: ApiLoaderParams = {
136+
const apiLoaderParam: ApiLoaderParams = {
124137
url,
125138
method
126139
}
127140

128141
if (headers) {
129142
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')
131159
}
132160

133161
if (body) {
134162
const parsedBody = typeof body === 'object' ? body : JSON.parse(body)
135-
options.body = parsedBody
163+
apiLoaderParam.body = parsedBody
136164
}
137165

138-
const loader = new ApiLoader(options)
166+
const loader = new ApiLoader(apiLoaderParam)
139167

140168
let docs: IDocument[] = []
141169

@@ -195,6 +223,7 @@ interface ApiLoaderParams {
195223
method: string
196224
headers?: ICommonObject
197225
body?: ICommonObject
226+
ca?: string
198227
}
199228

200229
class ApiLoader extends BaseDocumentLoader {
@@ -206,28 +235,36 @@ class ApiLoader extends BaseDocumentLoader {
206235

207236
public readonly method: string
208237

209-
constructor({ url, headers, body, method }: ApiLoaderParams) {
238+
public readonly ca?: string
239+
240+
constructor({ url, headers, body, method, ca }: ApiLoaderParams) {
210241
super()
211242
this.url = url
212243
this.headers = headers
213244
this.body = body
214245
this.method = method
246+
this.ca = ca
215247
}
216248

217249
public async load(): Promise<IDocument[]> {
218250
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)
220252
} else {
221-
return this.executeGetRequest(this.url, this.headers)
253+
return this.executeGetRequest(this.url, this.headers, this.ca)
222254
}
223255
}
224256

225-
protected async executeGetRequest(url: string, headers?: ICommonObject): Promise<IDocument[]> {
257+
protected async executeGetRequest(url: string, headers?: ICommonObject, ca?: string): Promise<IDocument[]> {
226258
try {
227259
const config: AxiosRequestConfig = {}
228260
if (headers) {
229261
config.headers = headers
230262
}
263+
if (ca) {
264+
config.httpsAgent = new https.Agent({
265+
ca: ca
266+
})
267+
}
231268
const response = await axios.get(url, config)
232269
const responseJsonString = JSON.stringify(response.data, null, 2)
233270
const doc = new Document({
@@ -242,12 +279,17 @@ class ApiLoader extends BaseDocumentLoader {
242279
}
243280
}
244281

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[]> {
246283
try {
247284
const config: AxiosRequestConfig = {}
248285
if (headers) {
249286
config.headers = headers
250287
}
288+
if (ca) {
289+
config.httpsAgent = new https.Agent({
290+
ca: ca
291+
})
292+
}
251293
const response = await axios.post(url, body ?? {}, config)
252294
const responseJsonString = JSON.stringify(response.data, null, 2)
253295
const doc = new Document({

0 commit comments

Comments
 (0)