Skip to content

Commit 4f04e8a

Browse files
authored
feat: ai tool header (#23)
* feat: ai tool header * fix: simplification * fix: linting fixes
1 parent ad4a7ed commit 4f04e8a

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

nodes/ApifyContentCrawler/resources/genericFunctions.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ type IApiRequestOptions = IRequestOptions & { uri?: string };
1313

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

26+
const headers: Record<string, string> = {
27+
'x-apify-integration-platform': 'n8n',
28+
'x-apify-integration-app-id': 'website-content-crawler-app',
29+
};
30+
31+
if (isUsedAsAiTool(this.getNode().type)) {
32+
headers['x-apify-integration-ai-tool'] = 'true';
33+
}
34+
2735
const options: IRequestOptions = {
2836
json: true,
2937
...rest,
3038
method,
3139
qs: query,
3240
url: endpoint,
33-
headers: {
34-
'x-apify-integration-platform': 'n8n',
35-
'x-apify-integration-app-id': 'website-content-crawler-app',
36-
},
41+
headers,
3742
};
3843

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

4348
try {
4449
const authenticationMethod = this.getNodeParameter('authentication', 0) as string;
50+
4551
try {
4652
await this.getCredentials(authenticationMethod);
4753
} catch {
@@ -53,10 +59,7 @@ export async function apiRequest(
5359

5460
return await this.helpers.requestWithAuthentication.call(this, authenticationMethod, options);
5561
} catch (error) {
56-
/**
57-
* using `error instanceof NodeApiError` results in `false`
58-
* because it's thrown by a different instance of n8n-workflow
59-
*/
62+
// Re-throw structured error for n8n
6063
if (error instanceof NodeApiError) {
6164
throw error;
6265
}
@@ -136,6 +139,7 @@ export async function pollRunStatus(
136139
method: 'GET',
137140
uri: `/v2/actor-runs/${runId}`,
138141
});
142+
139143
const status = pollResult?.data?.status;
140144
lastRunData = pollResult?.data;
141145
if (['SUCCEEDED', 'FAILED', 'TIMED-OUT', 'ABORTED'].includes(status)) {

0 commit comments

Comments
 (0)