|
| 1 | +import { TextDecoder } from 'util'; |
| 2 | + |
1 | 3 | import axios, { AxiosError } from 'axios'; |
2 | 4 |
|
3 | 5 | import { ImporterParameterError } from '../exceptions/ImporterParameterError'; |
@@ -64,23 +66,19 @@ export class HttpImporter extends Importer { |
64 | 66 | override async doFetch(parameters: Record<string, unknown>): Promise<string> { |
65 | 67 | const uri = parameters.location as string; |
66 | 68 | const encoding = parameters.encoding as string; |
| 69 | + const decoder = new TextDecoder(encoding); |
67 | 70 |
|
68 | | - // The old impl retrieved data as byte array and then converted using encoding: |
69 | | - // Return new String(rawResponse, Charset.forName((String) parameters.get("encoding"))); |
70 | | - // Unfortunately there does not seem to be a universal method .toString(encoding?: string) in javascript |
| 71 | + // Fetch as ArrayBuffer |
71 | 72 | const result = await axios |
72 | | - .get(uri, { responseEncoding: encoding }) |
| 73 | + .get(uri, { responseType: 'arraybuffer' }) |
73 | 74 | .catch((error: AxiosError) => { |
74 | 75 | if (error.response) { |
75 | 76 | console.log(error.response); |
76 | 77 | throw new Error('Could not Fetch from URI:' + uri); |
77 | 78 | } |
78 | 79 | throw new ImporterParameterError('Could not Fetch from URI:' + uri); |
79 | 80 | }); |
80 | | - // Check if data is object/array -> return stringified (because this method returns string) |
81 | | - if (typeof result.data === 'object' || Array.isArray(result.data)) { |
82 | | - return JSON.stringify(result.data); |
83 | | - } |
84 | | - return result.data as string; |
| 81 | + // Convert ArrayBuffer response to string using encoding |
| 82 | + return decoder.decode(result.data); |
85 | 83 | } |
86 | 84 | } |
0 commit comments