n8n-nodes-putio
Version:
n8n node for Put.io API integration
86 lines • 3.64 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateCredentials = exports.putioApiRequest = void 0;
const n8n_workflow_1 = require("n8n-workflow");
function putioApiRequest(method, endpoint, body = {}, query = {}, formData, option = {}) {
var _a, _b, _c, _d;
return __awaiter(this, void 0, void 0, function* () {
const options = {
method,
url: `https://api.put.io/v2${endpoint}`,
qs: query,
body,
json: true,
returnFullResponse: true,
};
if (formData) {
options.body = formData;
options.json = false;
options.headers = {
'Content-Type': 'multipart/form-data',
};
}
if (Object.keys(option).length !== 0) {
Object.assign(options, option);
}
try {
const response = yield this.helpers.requestWithAuthentication.call(this, 'putioApi', options);
if (endpoint.includes('/download')) {
if ((_a = response.headers) === null || _a === void 0 ? void 0 : _a.location) {
return response.headers.location;
}
const urlMatch = (_b = response.body) === null || _b === void 0 ? void 0 : _b.toString().match(/href="([^"]+)"/);
if (urlMatch) {
return urlMatch[1].replace(/&/g, '&');
}
}
return response.body;
}
catch (error) {
if (endpoint.includes('/download') && error.statusCode === 302) {
if ((_d = (_c = error.response) === null || _c === void 0 ? void 0 : _c.headers) === null || _d === void 0 ? void 0 : _d.location) {
return error.response.headers.location;
}
const urlMatch = error.message.match(/href="([^"]+)"/);
if (urlMatch) {
return urlMatch[1].replace(/&/g, '&');
}
}
throw new n8n_workflow_1.NodeApiError(this.getNode(), error);
}
});
}
exports.putioApiRequest = putioApiRequest;
function validateCredentials(decryptedCredentials) {
return __awaiter(this, void 0, void 0, function* () {
try {
const options = {
method: 'GET',
url: 'https://api.put.io/v2/account/info',
json: true,
};
yield this.helpers.requestWithAuthentication.call(this, 'putioApi', options);
return {
status: 'OK',
message: 'Connection successful!',
};
}
catch (error) {
return {
status: 'Error',
message: 'The credentials are not valid!',
};
}
});
}
exports.validateCredentials = validateCredentials;
//# sourceMappingURL=GenericFunctions.js.map