feat(http-request): cover file downloads

This commit is contained in:
Faruk AYDIN
2023-02-07 21:22:03 +01:00
committed by Ali BARIN
parent 9cfaa9e603
commit 2be8e14f66

View File

@@ -12,8 +12,7 @@ export default defineAction({
key: 'method', key: 'method',
type: 'dropdown' as const, type: 'dropdown' as const,
required: true, required: true,
description: description: `The HTTP method we'll use to perform the request.`,
`The HTTP method we'll use to perform the request.`,
value: 'GET', value: 'GET',
options: [ options: [
{ label: 'DELETE', value: 'DELETE' }, { label: 'DELETE', value: 'DELETE' },
@@ -21,7 +20,7 @@ export default defineAction({
{ label: 'PATCH', value: 'PATCH' }, { label: 'PATCH', value: 'PATCH' },
{ label: 'POST', value: 'POST' }, { label: 'POST', value: 'POST' },
{ label: 'PUT', value: 'PUT' }, { label: 'PUT', value: 'PUT' },
] ],
}, },
{ {
label: 'URL', label: 'URL',
@@ -35,7 +34,7 @@ export default defineAction({
label: 'Data', label: 'Data',
key: 'data', key: 'data',
type: 'string' as const, type: 'string' as const,
required: true, required: false,
description: 'Place raw JSON data here.', description: 'Place raw JSON data here.',
variables: true, variables: true,
}, },
@@ -45,18 +44,31 @@ export default defineAction({
const method = $.step.parameters.method as TMethod; const method = $.step.parameters.method as TMethod;
const data = $.step.parameters.data as string; const data = $.step.parameters.data as string;
const url = $.step.parameters.url as string; const url = $.step.parameters.url as string;
const maxFileSize = 25 * 1024 * 1024; // 25MB
const response = await $.http.request( const metadataResponse = await $.http.head(url);
{
if (Number(metadataResponse.headers['content-length']) > maxFileSize) {
throw new Error(
`Response is too large. Maximum size is 25MB. Actual size is ${metadataResponse.headers['content-length']}`
);
}
const response = await $.http.request({
url, url,
method, method,
data, data,
headers: { headers: {
'Content-Type': 'application/json' 'Content-Type': 'application/json',
} },
} });
);
let responseData = response.data;
$.setActionItem({ raw: response.data });
if (typeof response.data === 'string') {
responseData = response.data.replaceAll('\u0000', '');
}
$.setActionItem({ raw: { data: responseData } });
}, },
}); });