UNPKG

n8n-nodes-supadata

Version:

Tool for extracting content from YouTube videos and web pages

65 lines 2.58 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.supadataApiRequest = supadataApiRequest; exports.supadataApiRequestAllItems = supadataApiRequestAllItems; exports.supadataApiPollExtractJob = supadataApiPollExtractJob; const n8n_workflow_1 = require("n8n-workflow"); async function supadataApiRequest(method, endpoint, body = {}, query = {}) { const options = { method, headers: { 'user-agent': 'n8n', 'Content-Type': 'application/json', Accept: 'application/json', }, qs: query, url: `https://api.supadata.ai/v1${endpoint}`, json: true, }; if (Object.keys(body).length > 0) { options.body = body; } try { return await this.helpers.httpRequestWithAuthentication.call(this, 'supadataApi', options); } catch (error) { throw new n8n_workflow_1.NodeApiError(this.getNode(), error); } } async function supadataApiRequestAllItems(propertyName, method, endpoint, body = {}, query = {}) { const returnData = []; let responseData; do { responseData = await supadataApiRequest.call(this, method, endpoint, body, query); if (responseData[propertyName]) { returnData.push(...responseData[propertyName]); } query = {}; } while (responseData.next); return returnData; } async function supadataApiPollExtractJob(jobId, pollIntervalSeconds = 5, maxWaitTimeSeconds = 300) { var _a; const startTime = Date.now(); const maxWaitMs = maxWaitTimeSeconds * 1000; const pollIntervalMs = pollIntervalSeconds * 1000; while (Date.now() - startTime < maxWaitMs) { const response = await supadataApiRequest.call(this, 'GET', `/extract/${jobId}`); if (response.status === 'completed') { return response; } if (response.status === 'failed') { const errorMessage = ((_a = response.error) === null || _a === void 0 ? void 0 : _a.message) || 'Extract job failed'; throw new n8n_workflow_1.NodeApiError(this.getNode(), { message: errorMessage, description: `Extract job ${jobId} failed`, }); } await (0, n8n_workflow_1.sleep)(pollIntervalMs); } throw new n8n_workflow_1.NodeApiError(this.getNode(), { message: `Extract job timed out after ${maxWaitTimeSeconds} seconds`, description: `Job ${jobId} did not complete within the configured wait time`, }); } //# sourceMappingURL=GenericFunctions.js.map