@ai-growth/n8n-nodes-wordpress
Version:
n8n node for WordPress integration with AI GROWTH - SEO WP plugin
491 lines (490 loc) • 24.3 kB
JavaScript
"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
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());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (g && (g = 0, op[0] && (_ = 0)), _) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
exports.__esModule = true;
exports.WordPressClient = void 0;
var axios_1 = require("axios");
var form_data_1 = require("form-data");
var Validator_1 = require("./Validator");
var ErrorUtils_1 = require("./ErrorUtils");
var Logger_1 = require("./Logger");
var AuthHeaderManager_1 = require("./AuthHeaderManager");
/**
* Cliente HTTP para a API REST do WordPress
* Encapsula o Axios e fornece métodos para interagir com a API
*/
var WordPressClient = /** @class */ (function () {
/**
* Construtor do cliente WordPress
* @param credentials Credenciais para acesso à API
* @param options Opções de configuração
*/
function WordPressClient(credentials, options) {
if (options === void 0) { options = {}; }
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
// Validar e sanitizar URL
var validationResult = Validator_1.Validator.validateCredentials(credentials);
if (!validationResult.valid) {
throw new ErrorUtils_1.WordPressError("Invalid WordPress credentials: ".concat(validationResult.error), ErrorUtils_1.WordPressErrorType.VALIDATION);
}
this.credentials = __assign(__assign({}, credentials), { url: Validator_1.Validator.sanitizeUrl(credentials.url) });
this.baseUrl = this.credentials.url;
// Definir opções padrão
this.options = {
timeout: (_a = options.timeout) !== null && _a !== void 0 ? _a : 30000,
maxRetries: (_b = options.maxRetries) !== null && _b !== void 0 ? _b : 3,
retryDelay: (_c = options.retryDelay) !== null && _c !== void 0 ? _c : 1000,
debug: (_d = options.debug) !== null && _d !== void 0 ? _d : false,
apiVersion: (_e = options.apiVersion) !== null && _e !== void 0 ? _e : 'v2',
logOptions: (_f = options.logOptions) !== null && _f !== void 0 ? _f : {}
};
// Configurar o logger
this.logger = new Logger_1.Logger(__assign(__assign({}, this.options.logOptions), { prefix: (_h = (_g = this.options.logOptions) === null || _g === void 0 ? void 0 : _g.prefix) !== null && _h !== void 0 ? _h : '[WordPress API]', level: this.options.debug
? Logger_1.LogLevel.DEBUG
: ((_k = (_j = this.options.logOptions) === null || _j === void 0 ? void 0 : _j.level) !== null && _k !== void 0 ? _k : Logger_1.LogLevel.ERROR) }));
// Configurar o gerenciador de autenticação
this.authManager = new AuthHeaderManager_1.AuthHeaderManager(this.credentials);
// Criar instância do Axios
this.client = this.createAxiosInstance();
// Configurar interceptadores
this.setupInterceptors();
// Logar inicialização
this.logger.info("WordPress client initialized for ".concat(this.baseUrl, "/wp-json/wp/").concat(this.options.apiVersion));
}
/**
* Cria a instância do Axios com as configurações necessárias
* @returns Instância configurada do Axios
*/
WordPressClient.prototype.createAxiosInstance = function () {
// Obter cabeçalhos básicos de autenticação
var headers = this.authManager.getBasicHeaders();
// Criar e retornar a instância
return axios_1["default"].create({
baseURL: "".concat(this.baseUrl, "/wp-json/wp/").concat(this.options.apiVersion),
headers: headers,
timeout: this.options.timeout
});
};
/**
* Configura interceptadores para request/response
*/
WordPressClient.prototype.setupInterceptors = function () {
var _this = this;
// Interceptador de requisição
this.client.interceptors.request.use(function (config) {
var _a;
var method = ((_a = config.method) === null || _a === void 0 ? void 0 : _a.toUpperCase()) || 'UNKNOWN';
var url = config.url || 'UNKNOWN';
_this.logger.request(method, url, config.params, config.data);
return config;
}, function (error) {
_this.logger.error('Request configuration error', error);
return Promise.reject(error);
});
// Interceptador de resposta
this.client.interceptors.response.use(function (response) {
var _a;
var method = ((_a = response.config.method) === null || _a === void 0 ? void 0 : _a.toUpperCase()) || 'UNKNOWN';
var url = response.config.url || 'UNKNOWN';
_this.logger.response(method, url, response.status, response.data);
return response;
}, function (error) {
var _a, _b, _c;
// Logar o erro
var method = ((_b = (_a = error.config) === null || _a === void 0 ? void 0 : _a.method) === null || _b === void 0 ? void 0 : _b.toUpperCase()) || 'UNKNOWN';
var url = ((_c = error.config) === null || _c === void 0 ? void 0 : _c.url) || 'UNKNOWN';
_this.logger.httpError(method, url, error);
// Converter para WordPressError
var wpError = ErrorUtils_1.ErrorUtils.fromError(error);
return Promise.reject(wpError);
});
};
/**
* Executa uma requisição com suporte a retry
* @param requestFn Função que executa a requisição
* @returns Resultado da requisição
*/
WordPressClient.prototype.executeWithRetry = function (requestFn) {
return __awaiter(this, void 0, void 0, function () {
var lastError, attempt, _loop_1, this_1, state_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
lastError = null;
attempt = 0;
_loop_1 = function () {
var response, error_1, wpError, delayTime_1;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_b.trys.push([0, 2, , 4]);
attempt++;
return [4 /*yield*/, requestFn()];
case 1:
response = _b.sent();
return [2 /*return*/, { value: response.data }];
case 2:
error_1 = _b.sent();
wpError = error_1 instanceof ErrorUtils_1.WordPressError
? error_1
: ErrorUtils_1.ErrorUtils.fromError(error_1);
lastError = wpError;
// Verificar se o erro é retentável
if (!wpError.isRetryable()) {
this_1.logger.warn("Error not retryable: ".concat(wpError.message));
return [2 /*return*/, "break"];
}
// Última tentativa, não esperar
if (attempt >= this_1.options.maxRetries) {
this_1.logger.warn("Maximum retry attempts (".concat(this_1.options.maxRetries, ") reached"));
return [2 /*return*/, "break"];
}
delayTime_1 = this_1.calculateBackoff(attempt);
// Esperar antes da próxima tentativa
this_1.logger.warn("Attempt ".concat(attempt, " failed: ").concat(wpError.message, ". Retrying in ").concat(delayTime_1, "ms..."));
return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, delayTime_1); })];
case 3:
_b.sent();
return [3 /*break*/, 4];
case 4: return [2 /*return*/];
}
});
};
this_1 = this;
_a.label = 1;
case 1:
if (!(attempt < this.options.maxRetries)) return [3 /*break*/, 3];
return [5 /*yield**/, _loop_1()];
case 2:
state_1 = _a.sent();
if (typeof state_1 === "object")
return [2 /*return*/, state_1.value];
if (state_1 === "break")
return [3 /*break*/, 3];
return [3 /*break*/, 1];
case 3:
// Todas as tentativas falharam
throw lastError || new ErrorUtils_1.WordPressError('Request failed after maximum retries', ErrorUtils_1.WordPressErrorType.UNKNOWN);
}
});
});
};
/**
* Calcula o tempo de espera para o backoff exponencial
* @param attempt Número da tentativa atual (começando em 1)
* @returns Tempo de espera em ms
*/
WordPressClient.prototype.calculateBackoff = function (attempt) {
// Backoff exponencial com jitter para evitar thundering herd
var baseDelay = this.options.retryDelay;
var exponentialDelay = baseDelay * Math.pow(2, attempt - 1);
var jitter = Math.random() * baseDelay;
return Math.min(exponentialDelay + jitter, 30000); // Máximo de 30 segundos
};
/**
* Realiza uma requisição GET
* @param endpoint Endpoint da API (sem barra inicial)
* @param params Parâmetros da query string
* @returns Dados da resposta
*/
WordPressClient.prototype.get = function (endpoint, params) {
return __awaiter(this, void 0, void 0, function () {
var formattedEndpoint, error_2;
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
formattedEndpoint = this.formatEndpoint(endpoint);
this.logger.info("GET ".concat(formattedEndpoint), params);
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
return [4 /*yield*/, this.executeWithRetry(function () {
return _this.client.get(formattedEndpoint, { params: params });
})];
case 2: return [2 /*return*/, _a.sent()];
case 3:
error_2 = _a.sent();
this.logger.error("GET ".concat(formattedEndpoint, " failed"), error_2);
throw error_2;
case 4: return [2 /*return*/];
}
});
});
};
/**
* Realiza uma requisição POST
* @param endpoint Endpoint da API (sem barra inicial)
* @param data Dados a serem enviados
* @returns Dados da resposta
*/
WordPressClient.prototype.post = function (endpoint, data) {
return __awaiter(this, void 0, void 0, function () {
var formattedEndpoint, error_3;
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
formattedEndpoint = this.formatEndpoint(endpoint);
this.logger.info("POST ".concat(formattedEndpoint), { data: data });
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
return [4 /*yield*/, this.executeWithRetry(function () {
return _this.client.post(formattedEndpoint, data);
})];
case 2: return [2 /*return*/, _a.sent()];
case 3:
error_3 = _a.sent();
this.logger.error("POST ".concat(formattedEndpoint, " failed"), error_3);
throw error_3;
case 4: return [2 /*return*/];
}
});
});
};
/**
* Realiza uma requisição POST com opções adicionais
* @param endpoint Endpoint da API (sem barra inicial)
* @param data Dados a serem enviados
* @param options Opções adicionais da requisição
* @returns Dados da resposta
*/
WordPressClient.prototype.postWithOptions = function (endpoint, data, options) {
return __awaiter(this, void 0, void 0, function () {
var formattedEndpoint, error_4;
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
formattedEndpoint = this.formatEndpoint(endpoint);
this.logger.info("POST ".concat(formattedEndpoint, " with options"), { data: data, options: options });
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
return [4 /*yield*/, this.executeWithRetry(function () {
return _this.client.post(formattedEndpoint, data, options);
})];
case 2: return [2 /*return*/, _a.sent()];
case 3:
error_4 = _a.sent();
this.logger.error("POST ".concat(formattedEndpoint, " with options failed"), error_4);
throw error_4;
case 4: return [2 /*return*/];
}
});
});
};
/**
* Realiza uma requisição PUT
* @param endpoint Endpoint da API (sem barra inicial)
* @param data Dados a serem enviados
* @returns Dados da resposta
*/
WordPressClient.prototype.put = function (endpoint, data) {
return __awaiter(this, void 0, void 0, function () {
var formattedEndpoint, error_5;
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
formattedEndpoint = this.formatEndpoint(endpoint);
this.logger.info("PUT ".concat(formattedEndpoint), { data: data });
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
return [4 /*yield*/, this.executeWithRetry(function () {
return _this.client.put(formattedEndpoint, data);
})];
case 2: return [2 /*return*/, _a.sent()];
case 3:
error_5 = _a.sent();
this.logger.error("PUT ".concat(formattedEndpoint, " failed"), error_5);
throw error_5;
case 4: return [2 /*return*/];
}
});
});
};
/**
* Realiza uma requisição DELETE
* @param endpoint Endpoint da API (sem barra inicial)
* @returns Dados da resposta
*/
WordPressClient.prototype["delete"] = function (endpoint) {
return __awaiter(this, void 0, void 0, function () {
var formattedEndpoint, error_6;
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
formattedEndpoint = this.formatEndpoint(endpoint);
this.logger.info("DELETE ".concat(formattedEndpoint));
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
return [4 /*yield*/, this.executeWithRetry(function () {
return _this.client["delete"](formattedEndpoint);
})];
case 2: return [2 /*return*/, _a.sent()];
case 3:
error_6 = _a.sent();
this.logger.error("DELETE ".concat(formattedEndpoint, " failed"), error_6);
throw error_6;
case 4: return [2 /*return*/];
}
});
});
};
/**
* Faz upload de um arquivo de mídia para o WordPress
* @param fileData Buffer com os dados do arquivo
* @param fileName Nome do arquivo
* @param mimeType Tipo MIME do arquivo
* @returns Informações do arquivo enviado
*/
WordPressClient.prototype.uploadMedia = function (fileData, fileName, mimeType) {
return __awaiter(this, void 0, void 0, function () {
var formData_1, headers_1, error_7;
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
this.logger.info("UPLOAD media: ".concat(fileName, " (").concat(mimeType, "), size: ").concat(fileData.length, " bytes"));
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
formData_1 = new form_data_1["default"]();
formData_1.append('file', fileData, {
filename: fileName,
contentType: mimeType
});
headers_1 = this.authManager.getMediaUploadHeaders(formData_1.getHeaders());
return [4 /*yield*/, this.executeWithRetry(function () {
return axios_1["default"].post("".concat(_this.baseUrl, "/wp-json/wp/").concat(_this.options.apiVersion, "/media"), formData_1, { headers: headers_1, timeout: _this.options.timeout });
})];
case 2: return [2 /*return*/, _a.sent()];
case 3:
error_7 = _a.sent();
this.logger.error("UPLOAD media ".concat(fileName, " failed"), error_7);
throw error_7;
case 4: return [2 /*return*/];
}
});
});
};
/**
* Formata o endpoint removendo barras iniciais e duplicadas
* @param endpoint Endpoint a ser formatado
* @returns Endpoint formatado
*/
WordPressClient.prototype.formatEndpoint = function (endpoint) {
return endpoint.startsWith('/') ? endpoint.substring(1) : endpoint;
};
/**
* Obter URL base da API
* @returns URL base
*/
WordPressClient.prototype.getBaseUrl = function () {
return this.baseUrl;
};
/**
* Obter versão da API
* @returns Versão da API
*/
WordPressClient.prototype.getApiVersion = function () {
return this.options.apiVersion;
};
/**
* Definir nível de log
* @param level Nível de log
*/
WordPressClient.prototype.setLogLevel = function (level) {
this.logger.setLevel(level);
};
/**
* Atualiza as credenciais do cliente
* @param credentials Novas credenciais
*/
WordPressClient.prototype.updateCredentials = function (credentials) {
// Validar novas credenciais
var validationResult = Validator_1.Validator.validateCredentials(credentials);
if (!validationResult.valid) {
throw new ErrorUtils_1.WordPressError("Invalid WordPress credentials: ".concat(validationResult.error), ErrorUtils_1.WordPressErrorType.VALIDATION);
}
// Atualizar credenciais e URL
this.credentials = __assign(__assign({}, credentials), { url: Validator_1.Validator.sanitizeUrl(credentials.url) });
this.baseUrl = this.credentials.url;
// Atualizar gerenciador de autenticação
this.authManager.updateCredentials(this.credentials);
// Recriar cliente
this.client = this.createAxiosInstance();
// Logar atualização
this.logger.info("Credentials updated for ".concat(this.baseUrl));
};
/**
* Define um token de nonce para requisições que necessitam
* @param nonce Token de nonce
*/
WordPressClient.prototype.setNonce = function (nonce) {
this.authManager.setNonce(nonce);
this.logger.debug('Nonce token set');
};
/**
* Obtém um cabeçalho da última resposta recebida
* @param header Nome do cabeçalho
* @returns Valor do cabeçalho ou null se não existir
*/
WordPressClient.prototype.getLastResponseHeader = function (header) {
// Por simplicidade, vamos retornar null.
// Em uma implementação real, seria necessário armazenar os headers da última resposta
return '0';
};
return WordPressClient;
}());
exports.WordPressClient = WordPressClient;