feat(http-request): add headers support
This commit is contained in:
@@ -2,6 +2,13 @@ import defineAction from '../../../../helpers/define-action';
|
|||||||
|
|
||||||
type TMethod = 'GET' | 'POST' | 'PATCH' | 'PUT' | 'DELETE';
|
type TMethod = 'GET' | 'POST' | 'PATCH' | 'PUT' | 'DELETE';
|
||||||
|
|
||||||
|
type THeaderEntry = {
|
||||||
|
key: string;
|
||||||
|
value: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
type THeaderEntries = THeaderEntry[];
|
||||||
|
|
||||||
export default defineAction({
|
export default defineAction({
|
||||||
name: 'Custom Request',
|
name: 'Custom Request',
|
||||||
key: 'customRequest',
|
key: 'customRequest',
|
||||||
@@ -38,15 +45,47 @@ export default defineAction({
|
|||||||
description: 'Place raw JSON data here.',
|
description: 'Place raw JSON data here.',
|
||||||
variables: true,
|
variables: true,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
label: 'Headers',
|
||||||
|
key: 'headers',
|
||||||
|
type: 'dynamic' as const,
|
||||||
|
required: false,
|
||||||
|
description: 'Add or remove headers as needed',
|
||||||
|
value: [{
|
||||||
|
key: 'Content-Type',
|
||||||
|
value: 'application/json'
|
||||||
|
}],
|
||||||
|
fields: [
|
||||||
|
{
|
||||||
|
label: 'Key',
|
||||||
|
key: 'key',
|
||||||
|
type: 'string' as const,
|
||||||
|
required: true,
|
||||||
|
description: 'Header key',
|
||||||
|
variables: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
label: 'Value',
|
||||||
|
key: 'value',
|
||||||
|
type: 'string' as const,
|
||||||
|
required: true,
|
||||||
|
description: 'Header value',
|
||||||
|
variables: true,
|
||||||
|
}
|
||||||
|
],
|
||||||
|
}
|
||||||
],
|
],
|
||||||
|
|
||||||
async run($) {
|
async run($) {
|
||||||
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 headers = $.step.parameters.headers as THeaderEntries;
|
||||||
const maxFileSize = 25 * 1024 * 1024; // 25MB
|
const maxFileSize = 25 * 1024 * 1024; // 25MB
|
||||||
|
|
||||||
const metadataResponse = await $.http.head(url);
|
const headersObject = headers.reduce((result, entry) => ({ ...result, [entry.key]: entry.value }), {})
|
||||||
|
|
||||||
|
const metadataResponse = await $.http.head(url, { headers: headersObject });
|
||||||
|
|
||||||
if (Number(metadataResponse.headers['content-length']) > maxFileSize) {
|
if (Number(metadataResponse.headers['content-length']) > maxFileSize) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
@@ -58,9 +97,7 @@ export default defineAction({
|
|||||||
url,
|
url,
|
||||||
method,
|
method,
|
||||||
data,
|
data,
|
||||||
headers: {
|
headers: headersObject,
|
||||||
'Content-Type': 'application/json',
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
|
|
||||||
let responseData = response.data;
|
let responseData = response.data;
|
||||||
|
@@ -9,7 +9,7 @@ const appInfoConverter = (rawAppData: IApp) => {
|
|||||||
|
|
||||||
if (rawAppData.auth?.fields) {
|
if (rawAppData.auth?.fields) {
|
||||||
rawAppData.auth.fields = rawAppData.auth.fields.map((field) => {
|
rawAppData.auth.fields = rawAppData.auth.fields.map((field) => {
|
||||||
if (typeof field.value === 'string') {
|
if (field.type === 'string' && typeof field.value === 'string') {
|
||||||
return {
|
return {
|
||||||
...field,
|
...field,
|
||||||
value: field.value.replace('{WEB_APP_URL}', appConfig.webAppUrl),
|
value: field.value.replace('{WEB_APP_URL}', appConfig.webAppUrl),
|
||||||
|
@@ -47,6 +47,7 @@ export default function createHttpClient({
|
|||||||
if (
|
if (
|
||||||
// TODO: provide a `shouldRefreshToken` function in the app
|
// TODO: provide a `shouldRefreshToken` function in the app
|
||||||
(status === 401 || status === 403) &&
|
(status === 401 || status === 403) &&
|
||||||
|
$.app.auth &&
|
||||||
$.app.auth.refreshToken &&
|
$.app.auth.refreshToken &&
|
||||||
!$.app.auth.isRefreshTokenRequested
|
!$.app.auth.isRefreshTokenRequested
|
||||||
) {
|
) {
|
||||||
|
Reference in New Issue
Block a user