feat(http-request): convert non-text data to base64
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
import type { AxiosRequestConfig } from 'axios';
|
||||||
import defineAction from '../../../../helpers/define-action';
|
import defineAction from '../../../../helpers/define-action';
|
||||||
|
|
||||||
type TMethod = 'GET' | 'POST' | 'PATCH' | 'PUT' | 'DELETE';
|
type TMethod = 'GET' | 'POST' | 'PATCH' | 'PUT' | 'DELETE';
|
||||||
@@ -9,6 +10,11 @@ type THeaderEntry = {
|
|||||||
|
|
||||||
type THeaderEntries = THeaderEntry[];
|
type THeaderEntries = THeaderEntry[];
|
||||||
|
|
||||||
|
function isPossiblyNotTextBased(contentType: string) {
|
||||||
|
return contentType.startsWith('application/json')
|
||||||
|
|| contentType.startsWith('text/');
|
||||||
|
}
|
||||||
|
|
||||||
export default defineAction({
|
export default defineAction({
|
||||||
name: 'Custom Request',
|
name: 'Custom Request',
|
||||||
key: 'customRequest',
|
key: 'customRequest',
|
||||||
@@ -93,17 +99,24 @@ export default defineAction({
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const response = await $.http.request({
|
const contentType = metadataResponse.headers['content-type'];
|
||||||
|
const requestData: AxiosRequestConfig = {
|
||||||
url,
|
url,
|
||||||
method,
|
method,
|
||||||
data,
|
data,
|
||||||
headers: headersObject,
|
headers: headersObject,
|
||||||
});
|
};
|
||||||
|
|
||||||
|
if (!isPossiblyNotTextBased(contentType)) {
|
||||||
|
requestData.responseType = 'arraybuffer';
|
||||||
|
}
|
||||||
|
|
||||||
|
const response = await $.http.request(requestData);
|
||||||
|
|
||||||
let responseData = response.data;
|
let responseData = response.data;
|
||||||
|
|
||||||
if (typeof response.data === 'string') {
|
if (!isPossiblyNotTextBased(contentType)) {
|
||||||
responseData = response.data.replaceAll('\u0000', '');
|
responseData = Buffer.from(responseData as string).toString('base64');
|
||||||
}
|
}
|
||||||
|
|
||||||
$.setActionItem({ raw: { data: responseData } });
|
$.setActionItem({ raw: { data: responseData } });
|
||||||
|
Reference in New Issue
Block a user