Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions nodes/ApifyContentCrawler/resources/genericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ type IApiRequestOptions = IRequestOptions & { uri?: string };

/**
* Make an API request to Apify
*
*/
export async function apiRequest(
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions,
Expand All @@ -24,16 +23,22 @@ export async function apiRequest(
const query = qs || {};
const endpoint = `https://api.apify.com${uri}`;

const headers: Record<string, string> = {
'x-apify-integration-platform': 'n8n',
'x-apify-integration-app-id': 'website-content-crawler-app',
};

if (isUsedAsAiTool(this.getNode().type)) {
headers['x-apify-integration-ai-tool'] = 'true';
}

const options: IRequestOptions = {
json: true,
...rest,
method,
qs: query,
url: endpoint,
headers: {
'x-apify-integration-platform': 'n8n',
'x-apify-integration-app-id': 'website-content-crawler-app',
},
headers,
};

if (method === 'GET') {
Expand All @@ -42,6 +47,7 @@ export async function apiRequest(

try {
const authenticationMethod = this.getNodeParameter('authentication', 0) as string;

try {
await this.getCredentials(authenticationMethod);
} catch {
Expand All @@ -53,10 +59,7 @@ export async function apiRequest(

return await this.helpers.requestWithAuthentication.call(this, authenticationMethod, options);
} catch (error) {
/**
* using `error instanceof NodeApiError` results in `false`
* because it's thrown by a different instance of n8n-workflow
*/
// Re-throw structured error for n8n
if (error instanceof NodeApiError) {
throw error;
}
Expand Down Expand Up @@ -136,6 +139,7 @@ export async function pollRunStatus(
method: 'GET',
uri: `/v2/actor-runs/${runId}`,
});

const status = pollResult?.data?.status;
lastRunData = pollResult?.data;
if (['SUCCEEDED', 'FAILED', 'TIMED-OUT', 'ABORTED'].includes(status)) {
Expand Down