UNPKG

n8n-nodes-cloudconvert

Version:

A Node to send file conversion jobs to cloudconvert.com

66 lines 2.56 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.downloadExports = exports.cloudConvertApiRequest = void 0; const jsonpath_plus_1 = require("jsonpath-plus"); const n8n_workflow_1 = require("n8n-workflow"); async function cloudConvertApiRequest(option = {}) { var _a, _b; const credentials = (await this.getCredentials('cloudConvertCredentialsApi')); const baseDomain = credentials.sandbox ? 'sandbox.cloudconvert.com' : 'cloudconvert.com'; const baseURL = option.sync ? `https://sync.api.${baseDomain}` : `https://api.${baseDomain}`; const options = { baseURL, url: '', headers: { Authorization: 'Bearer ' + credentials.key, }, json: true, }; if (Object.keys(option)) { Object.assign(options, option); } try { let response = null; let keepLooking = true; while (keepLooking) { const batch = await this.helpers.httpRequest(options); response = response && response.length ? response.concat(batch.data) : batch.data || batch; if (batch.links && batch.links.next) { options.url = batch.links.next; } else { keepLooking = false; } } return response; } catch (error) { if ((_b = (_a = error.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.message) { error.message += ` - ${error.response.data.code}: ${error.response.data.message}`; } throw new n8n_workflow_1.NodeApiError(this.getNode(), error); } } exports.cloudConvertApiRequest = cloudConvertApiRequest; async function downloadExports(job, binaryItem) { const exportTasks = (0, jsonpath_plus_1.JSONPath)({ path: '$.tasks[?(@.operation==="export/url")]', json: job, }); for (const exportTask of exportTasks) { for (const outputFile of (0, jsonpath_plus_1.JSONPath)({ path: '$.result.files[*]', json: exportTask, })) { let index = 0; const fileContent = await this.helpers.httpRequest({ url: outputFile.url, encoding: 'arraybuffer', }); binaryItem.binary[`${exportTask.name}_${index++}`] = await this.helpers.prepareBinaryData(fileContent, outputFile.filename); } } return binaryItem; } exports.downloadExports = downloadExports; //# sourceMappingURL=GenericFunctions.js.map