mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 10:02:05 +00:00
⚡ Run requests in HTTP Request Node in parallel
This commit is contained in:
@@ -620,8 +620,8 @@ export class HttpRequest implements INodeType {
|
||||
},
|
||||
};
|
||||
|
||||
let response: any; // tslint:disable-line:no-any
|
||||
const returnItems: INodeExecutionData[] = [];
|
||||
const requestPromises = [];
|
||||
for (let itemIndex = 0; itemIndex < items.length; itemIndex++) {
|
||||
const options = this.getNodeParameter('options', itemIndex, {}) as IDataObject;
|
||||
const url = this.getNodeParameter('url', itemIndex) as string;
|
||||
@@ -814,27 +814,49 @@ export class HttpRequest implements INodeType {
|
||||
} else {
|
||||
requestOptions.json = true;
|
||||
}
|
||||
try {
|
||||
// Now that the options are all set make the actual http request
|
||||
if (oAuth1Api !== undefined) {
|
||||
//@ts-ignore
|
||||
response = await this.helpers.requestOAuth1.call(this, 'oAuth1Api', requestOptions);
|
||||
}
|
||||
else if (oAuth2Api !== undefined) {
|
||||
//@ts-ignore
|
||||
response = await this.helpers.requestOAuth2.call(this, 'oAuth2Api', requestOptions, { tokenType: 'Bearer' });
|
||||
|
||||
// Now that the options are all set make the actual http request
|
||||
if (oAuth1Api !== undefined) {
|
||||
requestPromises.push(this.helpers.requestOAuth1.call(this, 'oAuth1Api', requestOptions));
|
||||
} else if (oAuth2Api !== undefined) {
|
||||
requestPromises.push(this.helpers.requestOAuth2.call(this, 'oAuth2Api', requestOptions, { tokenType: 'Bearer' }));
|
||||
} else {
|
||||
requestPromises.push(this.helpers.request(requestOptions));
|
||||
}
|
||||
}
|
||||
|
||||
// @ts-ignore
|
||||
const promisesResponses = await Promise.allSettled(requestPromises);
|
||||
|
||||
let response: any; // tslint:disable-line:no-any
|
||||
for (let itemIndex = 0; itemIndex < items.length; itemIndex++) {
|
||||
// @ts-ignore
|
||||
response = promisesResponses.shift();
|
||||
|
||||
if (response!.status !== 'fulfilled') {
|
||||
if (this.continueOnFail() !== true) {
|
||||
// throw error;
|
||||
throw new Error(response!.reason);
|
||||
} else {
|
||||
response = await this.helpers.request(requestOptions);
|
||||
}
|
||||
} catch (error) {
|
||||
if (this.continueOnFail() === true) {
|
||||
returnItems.push({ json: { error } });
|
||||
// Return the actual reason as error
|
||||
returnItems.push(
|
||||
{
|
||||
json: {
|
||||
error: response.reason,
|
||||
},
|
||||
}
|
||||
);
|
||||
continue;
|
||||
}
|
||||
|
||||
throw error;
|
||||
}
|
||||
|
||||
response = response.value;
|
||||
|
||||
const options = this.getNodeParameter('options', itemIndex, {}) as IDataObject;
|
||||
const url = this.getNodeParameter('url', itemIndex) as string;
|
||||
|
||||
const fullResponse = !!options.fullResponse as boolean;
|
||||
|
||||
if (responseFormat === 'file') {
|
||||
const dataPropertyName = this.getNodeParameter('dataPropertyName', 0) as string;
|
||||
|
||||
@@ -853,23 +875,22 @@ export class HttpRequest implements INodeType {
|
||||
|
||||
const fileName = (url).split('/').pop();
|
||||
|
||||
|
||||
if (fullResponse === true) {
|
||||
const returnItem: IDataObject = {};
|
||||
for (const property of fullReponseProperties) {
|
||||
if (property === 'body') {
|
||||
continue;
|
||||
}
|
||||
returnItem[property] = response[property];
|
||||
returnItem[property] = response![property];
|
||||
}
|
||||
|
||||
newItem.json = returnItem;
|
||||
|
||||
newItem.binary![dataPropertyName] = await this.helpers.prepareBinaryData(response.body, fileName);
|
||||
newItem.binary![dataPropertyName] = await this.helpers.prepareBinaryData(response!.body, fileName);
|
||||
} else {
|
||||
newItem.json = items[itemIndex].json;
|
||||
|
||||
newItem.binary![dataPropertyName] = await this.helpers.prepareBinaryData(response, fileName);
|
||||
newItem.binary![dataPropertyName] = await this.helpers.prepareBinaryData(response!, fileName);
|
||||
}
|
||||
|
||||
items[itemIndex] = newItem;
|
||||
@@ -880,11 +901,11 @@ export class HttpRequest implements INodeType {
|
||||
const returnItem: IDataObject = {};
|
||||
for (const property of fullReponseProperties) {
|
||||
if (property === 'body') {
|
||||
returnItem[dataPropertyName] = response[property];
|
||||
returnItem[dataPropertyName] = response![property];
|
||||
continue;
|
||||
}
|
||||
|
||||
returnItem[property] = response[property];
|
||||
returnItem[property] = response![property];
|
||||
}
|
||||
returnItems.push({ json: returnItem });
|
||||
} else {
|
||||
@@ -899,7 +920,7 @@ export class HttpRequest implements INodeType {
|
||||
if (fullResponse === true) {
|
||||
const returnItem: IDataObject = {};
|
||||
for (const property of fullReponseProperties) {
|
||||
returnItem[property] = response[property];
|
||||
returnItem[property] = response![property];
|
||||
}
|
||||
|
||||
if (responseFormat === 'json' && typeof returnItem.body === 'string') {
|
||||
|
||||
Reference in New Issue
Block a user