@apify/n8n-nodes-apify
Version:
n8n nodes for Apify
126 lines • 4.99 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.runActor = runActor;
const n8n_workflow_1 = require("n8n-workflow");
const genericFunctions_1 = require("../../genericFunctions");
async function runActor(i) {
var _a;
const actorId = this.getNodeParameter('actorId', i, undefined, {
extractValue: true,
});
const timeout = this.getNodeParameter('timeout', i);
const memory = this.getNodeParameter('memory', i);
const buildParam = this.getNodeParameter('build', i);
const waitForFinish = this.getNodeParameter('waitForFinish', i);
const rawStringifiedInput = this.getNodeParameter('customBody', i, '{}');
let userInput;
try {
userInput = rawStringifiedInput ? JSON.parse(rawStringifiedInput) : {};
}
catch (err) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Could not parse custom body: ${rawStringifiedInput}`);
}
if (!actorId) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Actor ID is required');
}
const actor = await genericFunctions_1.apiRequest.call(this, {
method: 'GET',
uri: `/v2/acts/${actorId}`,
});
if (!actor || !actor.data) {
throw new n8n_workflow_1.NodeApiError(this.getNode(), {
message: `Actor ${actorId} not found`,
});
}
const actorData = actor.data;
let build;
if (buildParam) {
build = await getBuildByTag.call(this, actorId, buildParam, actorData);
}
else {
build = await getDefaultBuild.call(this, actorId);
}
const defaultInput = getDefaultInputsFromBuild(build);
const mergedInput = { ...defaultInput, ...userInput };
const qs = {};
if (timeout != null)
qs.timeout = timeout;
if (memory != null)
qs.memory = memory;
if (build === null || build === void 0 ? void 0 : build.buildNumber)
qs.build = build.buildNumber;
qs.waitForFinish = 0;
const run = await runActorApi.call(this, actorId, mergedInput, qs);
if (!((_a = run === null || run === void 0 ? void 0 : run.data) === null || _a === void 0 ? void 0 : _a.id)) {
throw new n8n_workflow_1.NodeApiError(this.getNode(), {
message: `Run ID not found after running the actor`,
});
}
if (!waitForFinish) {
return {
json: { ...run.data },
};
}
const runId = run.data.id;
const lastRunData = await genericFunctions_1.pollRunStatus.call(this, runId);
return {
json: { ...lastRunData },
};
}
async function getBuildByTag(actorId, buildTag, actorData) {
var _a, _b;
const buildByTag = actorData.taggedBuilds && actorData.taggedBuilds[buildTag];
if (!(buildByTag === null || buildByTag === void 0 ? void 0 : buildByTag.buildId)) {
throw new n8n_workflow_1.NodeApiError(this.getNode(), {
message: `Build tag '${buildTag}' does not exist for actor ${(_b = (_a = actorData.title) !== null && _a !== void 0 ? _a : actorData.name) !== null && _b !== void 0 ? _b : actorId}`,
});
}
const buildResp = await genericFunctions_1.apiRequest.call(this, {
method: 'GET',
uri: `/v2/actor-builds/${buildByTag.buildId}`,
});
if (!buildResp || !buildResp.data) {
throw new n8n_workflow_1.NodeApiError(this.getNode(), {
message: `Build with ID '${buildByTag.buildId}' not found for actor ${actorId}`,
});
}
return buildResp.data;
}
async function getDefaultBuild(actorId) {
const defaultBuildResp = await genericFunctions_1.apiRequest.call(this, {
method: 'GET',
uri: `/v2/acts/${actorId}/builds/default`,
});
if (!defaultBuildResp || !defaultBuildResp.data) {
throw new n8n_workflow_1.NodeApiError(this.getNode(), {
message: `Could not fetch default build for actor ${actorId}`,
});
}
return defaultBuildResp.data;
}
function getDefaultInputsFromBuild(build) {
var _a, _b;
const buildInputProperties = (_b = (_a = build === null || build === void 0 ? void 0 : build.actorDefinition) === null || _a === void 0 ? void 0 : _a.input) === null || _b === void 0 ? void 0 : _b.properties;
const defaultInput = {};
if (buildInputProperties && typeof buildInputProperties === 'object') {
for (const [key, property] of Object.entries(buildInputProperties)) {
if (property &&
typeof property === 'object' &&
'prefill' in property &&
property.prefill !== undefined &&
property.prefill !== null) {
defaultInput[key] = property.prefill;
}
}
}
return defaultInput;
}
async function runActorApi(actorId, mergedInput, qs) {
return await genericFunctions_1.apiRequest.call(this, {
method: 'POST',
uri: `/v2/acts/${actorId}/runs`,
body: mergedInput,
qs,
});
}
//# sourceMappingURL=execute.js.map