UNPKG

@ai-growth/n8n-nodes-wordpress

Version:

n8n node for WordPress integration with AI GROWTH - SEO WP plugin

272 lines (271 loc) 13.5 kB
"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.FeaturedImageService = void 0; var ErrorUtils_1 = require("../utils/ErrorUtils"); var MediaUploadService_1 = require("./MediaUploadService"); /** * Serviço para gerenciar imagens destacadas e metadados de imagens */ var FeaturedImageService = /** @class */ (function () { /** * Construtor do serviço * @param client Cliente WordPress */ function FeaturedImageService(client) { this.client = client; this.mediaService = new MediaUploadService_1.MediaUploadService(client); } /** * Define a imagem destacada para um conteúdo a partir de uma URL de imagem * @param contentId ID do conteúdo (post, página, etc.) * @param contentType Tipo de conteúdo * @param imageUrl URL da imagem * @param options Opções adicionais * @returns Conteúdo atualizado */ FeaturedImageService.prototype.setFeaturedImageFromUrl = function (contentId, contentType, imageUrl, options) { if (options === void 0) { options = {}; } return __awaiter(this, void 0, void 0, function () { var image, error_1; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 4, , 5]); // Verificar se o conteúdo existe return [4 /*yield*/, this.checkContentExists(contentId, contentType)]; case 1: // Verificar se o conteúdo existe _a.sent(); return [4 /*yield*/, this.mediaService.uploadFromUrl(imageUrl, __assign({ metadata: options.metadata }, options.uploadOptions))]; case 2: image = _a.sent(); return [4 /*yield*/, this.setFeaturedImage(contentId, contentType, image.id)]; case 3: // Atualizar imagem destacada return [2 /*return*/, _a.sent()]; case 4: error_1 = _a.sent(); if (error_1 instanceof ErrorUtils_1.WordPressError) { throw error_1; } throw new ErrorUtils_1.WordPressError("Erro ao definir imagem destacada a partir da URL: ".concat(imageUrl), ErrorUtils_1.WordPressErrorType.UNKNOWN); case 5: return [2 /*return*/]; } }); }); }; /** * Define a imagem destacada para um conteúdo a partir de um ID de mídia existente * @param contentId ID do conteúdo (post, página, etc.) * @param contentType Tipo de conteúdo * @param mediaId ID da mídia na biblioteca do WordPress * @returns Conteúdo atualizado */ FeaturedImageService.prototype.setFeaturedImage = function (contentId, contentType, mediaId) { return __awaiter(this, void 0, void 0, function () { var endpoint, response, error_2; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 4, , 5]); // Verificar se o conteúdo existe return [4 /*yield*/, this.checkContentExists(contentId, contentType)]; case 1: // Verificar se o conteúdo existe _a.sent(); // Verificar se a mídia existe return [4 /*yield*/, this.mediaService.getMedia(mediaId)]; case 2: // Verificar se a mídia existe _a.sent(); endpoint = this.getEndpointForContentType(contentType); return [4 /*yield*/, this.client.put("".concat(endpoint, "/").concat(contentId), { featured_media: mediaId })]; case 3: response = _a.sent(); return [2 /*return*/, response]; case 4: error_2 = _a.sent(); if (error_2 instanceof ErrorUtils_1.WordPressError) { throw error_2; } throw new ErrorUtils_1.WordPressError("Erro ao definir imagem destacada para ".concat(contentType, " ID ").concat(contentId), ErrorUtils_1.WordPressErrorType.UNKNOWN); case 5: return [2 /*return*/]; } }); }); }; /** * Remove a imagem destacada de um conteúdo * @param contentId ID do conteúdo (post, página, etc.) * @param contentType Tipo de conteúdo * @returns Conteúdo atualizado */ FeaturedImageService.prototype.removeFeaturedImage = function (contentId, contentType) { return __awaiter(this, void 0, void 0, function () { var endpoint, response, error_3; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 3, , 4]); // Verificar se o conteúdo existe return [4 /*yield*/, this.checkContentExists(contentId, contentType)]; case 1: // Verificar se o conteúdo existe _a.sent(); endpoint = this.getEndpointForContentType(contentType); return [4 /*yield*/, this.client.put("".concat(endpoint, "/").concat(contentId), { featured_media: 0 })]; case 2: response = _a.sent(); return [2 /*return*/, response]; case 3: error_3 = _a.sent(); if (error_3 instanceof ErrorUtils_1.WordPressError) { throw error_3; } throw new ErrorUtils_1.WordPressError("Erro ao remover imagem destacada de ".concat(contentType, " ID ").concat(contentId), ErrorUtils_1.WordPressErrorType.UNKNOWN); case 4: return [2 /*return*/]; } }); }); }; /** * Obtém a imagem destacada de um conteúdo * @param contentId ID do conteúdo (post, página, etc.) * @param contentType Tipo de conteúdo * @returns Imagem destacada ou null se não houver */ FeaturedImageService.prototype.getFeaturedImage = function (contentId, contentType) { return __awaiter(this, void 0, void 0, function () { var content, error_4; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 3, , 4]); return [4 /*yield*/, this.checkContentExists(contentId, contentType)]; case 1: content = _a.sent(); // Verificar se o conteúdo tem imagem destacada if (!content.featured_media) { return [2 /*return*/, null]; } return [4 /*yield*/, this.mediaService.getMedia(content.featured_media)]; case 2: // Obter informações da imagem return [2 /*return*/, _a.sent()]; case 3: error_4 = _a.sent(); if (error_4 instanceof ErrorUtils_1.WordPressError) { throw error_4; } throw new ErrorUtils_1.WordPressError("Erro ao obter imagem destacada de ".concat(contentType, " ID ").concat(contentId), ErrorUtils_1.WordPressErrorType.UNKNOWN); case 4: return [2 /*return*/]; } }); }); }; /** * Verifica se um conteúdo existe * @param contentId ID do conteúdo * @param contentType Tipo de conteúdo * @returns Conteúdo se existir */ FeaturedImageService.prototype.checkContentExists = function (contentId, contentType) { return __awaiter(this, void 0, void 0, function () { var endpoint, error_5; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, , 3]); endpoint = this.getEndpointForContentType(contentType); return [4 /*yield*/, this.client.get("".concat(endpoint, "/").concat(contentId))]; case 1: return [2 /*return*/, _a.sent()]; case 2: error_5 = _a.sent(); if (error_5 instanceof ErrorUtils_1.WordPressError && error_5.type === ErrorUtils_1.WordPressErrorType.NOT_FOUND) { throw new ErrorUtils_1.WordPressError("".concat(this.capitalizeContentType(contentType), " com ID ").concat(contentId, " n\u00E3o encontrado"), ErrorUtils_1.WordPressErrorType.NOT_FOUND); } throw error_5; case 3: return [2 /*return*/]; } }); }); }; /** * Obtém o endpoint da API para o tipo de conteúdo * @param contentType Tipo de conteúdo * @returns Endpoint da API */ FeaturedImageService.prototype.getEndpointForContentType = function (contentType) { switch (contentType) { case 'post': return 'wp/v2/posts'; case 'page': return 'wp/v2/pages'; case 'product': return 'wp/v2/products'; default: throw new ErrorUtils_1.WordPressError("Tipo de conte\u00FAdo n\u00E3o suportado: ".concat(contentType), ErrorUtils_1.WordPressErrorType.VALIDATION_FAILED); } }; /** * Capitaliza a primeira letra do tipo de conteúdo * @param contentType Tipo de conteúdo * @returns Tipo de conteúdo com primeira letra maiúscula */ FeaturedImageService.prototype.capitalizeContentType = function (contentType) { return contentType.charAt(0).toUpperCase() + contentType.slice(1); }; return FeaturedImageService; }()); exports.FeaturedImageService = FeaturedImageService;