Woo: construir base de API REST de forma idempotente

El operador puede cargar el base_url con o sin /wp-json/wc/v3. El código
anterior siempre concatenaba el sufijo, lo que producía URLs duplicadas
tipo https://store/wp-json/wc/v3/wp-json/wc/v3/products → 404 (rest_no_route).

Nuevo helper buildWooApiBase detecta si la URL ya incluye /wp-json/ y
respeta lo que vino. Centraliza la lógica en un solo lugar consumido por
los tres clientes Woo (wooSnapshot, wooOrders, woo.js).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Lucas Tettamanti
2026-05-18 00:54:44 -03:00
parent bae85033e0
commit c9a7c7e942
5 changed files with 44 additions and 5 deletions

View File

@@ -2,6 +2,7 @@ import { getDecryptedTenantEcommerceConfig } from "../2-identity/db/repo.js";
import { debug as dbg } from "../shared/debug.js";
import { getSnapshotPriceByWooId, getSnapshotItemsByIds } from "../shared/wooSnapshot.js";
import { normalizeUnitToken } from "../shared/sellUnitPolicy.js";
import { buildWooApiBase } from "../shared/wooApiBase.js";
import * as ordersRepo from "./ordersRepo.js";
// --- Simple in-memory lock to serialize work per key ---
@@ -90,8 +91,7 @@ async function getWooClient({ tenantId }) {
(() => {
throw new Error("consumer_secret not set");
})();
const apiVersion = cfg.api_version || "wc/v3";
const base = `${cfg.base_url.replace(/\/+$/, "")}/wp-json/${apiVersion}`;
const base = buildWooApiBase(cfg.base_url, cfg.api_version);
const auth = Buffer.from(`${consumerKey}:${consumerSecret}`).toString("base64");
return {
base,