| 227 | } |
| 228 | |
| 229 | class ApiLoader extends BaseDocumentLoader { |
| 230 | public readonly url: string |
| 231 | |
| 232 | public readonly headers?: ICommonObject |
| 233 | |
| 234 | public readonly body?: ICommonObject |
| 235 | |
| 236 | public readonly method: string |
| 237 | |
| 238 | public readonly ca?: string |
| 239 | |
| 240 | constructor({ url, headers, body, method, ca }: ApiLoaderParams) { |
| 241 | super() |
| 242 | this.url = url |
| 243 | this.headers = headers |
| 244 | this.body = body |
| 245 | this.method = method |
| 246 | this.ca = ca |
| 247 | } |
| 248 | |
| 249 | public async load(): Promise<IDocument[]> { |
| 250 | if (this.method === 'POST') { |
| 251 | return this.executePostRequest(this.url, this.headers, this.body, this.ca) |
| 252 | } else { |
| 253 | return this.executeGetRequest(this.url, this.headers, this.ca) |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | protected async executeGetRequest(url: string, headers?: ICommonObject, ca?: string): Promise<IDocument[]> { |
| 258 | try { |
| 259 | const config: AxiosRequestConfig = { method: 'GET', url, headers: headers ?? {} } |
| 260 | const agentOptions = ca ? { ca } : undefined |
| 261 | const response = await secureAxiosRequest(config, 5, agentOptions) |
| 262 | const responseJsonString = JSON.stringify(response.data, null, 2) |
| 263 | const doc = new Document({ |
| 264 | pageContent: responseJsonString, |
| 265 | metadata: { |
| 266 | url |
| 267 | } |
| 268 | }) |
| 269 | return [doc] |
| 270 | } catch (error) { |
| 271 | throw new Error(`Failed to fetch ${url}: ${error}`) |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | protected async executePostRequest(url: string, headers?: ICommonObject, body?: ICommonObject, ca?: string): Promise<IDocument[]> { |
| 276 | try { |
| 277 | const config: AxiosRequestConfig = { method: 'POST', url, data: body ?? {}, headers: headers ?? {} } |
| 278 | const agentOptions = ca ? { ca } : undefined |
| 279 | const response = await secureAxiosRequest(config, 5, agentOptions) |
| 280 | const responseJsonString = JSON.stringify(response.data, null, 2) |
| 281 | const doc = new Document({ |
| 282 | pageContent: responseJsonString, |
| 283 | metadata: { |
| 284 | url |
| 285 | } |
| 286 | }) |
nothing calls this directly
no outgoing calls
no test coverage detected