@velaro/cli 1.4.161 → 1.4.162

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Helpers for `velaro magento customers|orders|products`.
3
+ *
4
+ * The admin API (MagentoController) needs `configId` and `query` on every /Magento/*\/Search route.
5
+ * The CLI used to send only `q` and `limit`, so the server saw configId=0 and answered
6
+ * 404 {"error":"Config not found."} even when a store was connected.
7
+ */
8
+
9
+ /** Pick which connected store to search: the one asked for with --config-id, or the only active one. */
10
+ export function pickConfigId(configs, requestedId) {
11
+ const list = Array.isArray(configs) ? configs : [];
12
+ if (requestedId !== undefined && requestedId !== null) {
13
+ const hit = list.find(c => c.id === requestedId);
14
+ if (!hit) throw new Error(`No connected Magento store has id ${requestedId}. Run \`velaro magento list\`.`);
15
+ return hit.id;
16
+ }
17
+ const active = list.filter(c => c.enabled !== false && c.isActive !== false);
18
+ if (active.length === 0) {
19
+ throw new Error('No active Magento store connected. Set one up in the admin UI under Integrations, Magento.');
20
+ }
21
+ if (active.length > 1) {
22
+ const ids = active.map(c => `${c.id} (${c.displayName ?? c.storeName ?? c.storeUrl})`).join(', ');
23
+ throw new Error(`More than one Magento store is connected. Pass --config-id <id>. Stores: ${ids}`);
24
+ }
25
+ return active[0].id;
26
+ }
27
+
28
+ /** URL for GET /Magento/{Customers|Orders|Products}/Search, using the parameter names the server binds. */
29
+ export function buildSearchPath(kind, configId, query) {
30
+ const route = { customers: 'Customers', orders: 'Orders', products: 'Products' }[kind];
31
+ if (!route) throw new Error(`Unknown Magento search: ${kind}`);
32
+ return `/Magento/${route}/Search?configId=${encodeURIComponent(configId)}&query=${encodeURIComponent(query)}`;
33
+ }
@@ -0,0 +1,49 @@
1
+ import test from 'node:test';
2
+ import assert from 'node:assert/strict';
3
+ import { pickConfigId, buildSearchPath } from './magento-search.js';
4
+
5
+ const store = (id, extra = {}) => ({ id, displayName: `Store ${id}`, enabled: true, isActive: true, ...extra });
6
+
7
+ test('buildSearchPath uses the parameter names the server binds (configId + query)', () => {
8
+ assert.equal(buildSearchPath('products', 2, 'hat'), '/Magento/Products/Search?configId=2&query=hat');
9
+ assert.equal(buildSearchPath('customers', 2, 'a+b@x.com'), '/Magento/Customers/Search?configId=2&query=a%2Bb%40x.com');
10
+ assert.equal(buildSearchPath('orders', 7, '000000030'), '/Magento/Orders/Search?configId=7&query=000000030');
11
+ assert.throws(() => buildSearchPath('carts', 1, 'x'), /Unknown Magento search/);
12
+ });
13
+
14
+ test('buildSearchPath never sends the old q/limit-only form that returned "Config not found"', () => {
15
+ assert.doesNotMatch(buildSearchPath('products', 2, 'shirt'), /[?&]q=/);
16
+ });
17
+
18
+ test('pickConfigId: the only active store is used without --config-id', () => {
19
+ assert.equal(pickConfigId([store(2)], undefined), 2);
20
+ });
21
+
22
+ test('pickConfigId: inactive and disabled stores are ignored', () => {
23
+ assert.equal(pickConfigId([store(1, { isActive: false }), store(2, { enabled: false }), store(3)], undefined), 3);
24
+ });
25
+
26
+ test('pickConfigId: an explicit --config-id wins and must exist', () => {
27
+ assert.equal(pickConfigId([store(1), store(2)], 2), 2);
28
+ assert.throws(() => pickConfigId([store(1)], 9), /No connected Magento store has id 9/);
29
+ });
30
+
31
+ test('pickConfigId: several active stores need --config-id and the error lists them', () => {
32
+ assert.throws(() => pickConfigId([store(1), store(2)], undefined), /--config-id.*1 \(Store 1\), 2 \(Store 2\)/);
33
+ });
34
+
35
+ test('pickConfigId: no stores gives a plain setup message', () => {
36
+ assert.throws(() => pickConfigId([], undefined), /No active Magento store connected/);
37
+ assert.throws(() => pickConfigId(null, undefined), /No active Magento store connected/);
38
+ });
39
+
40
+ test('every Magento search call in the CLI and the MCP server goes through the configId form', async () => {
41
+ const fs = await import('node:fs');
42
+ const path = await import('node:path');
43
+ const { fileURLToPath } = await import('node:url');
44
+ const root = path.join(path.dirname(fileURLToPath(import.meta.url)), '..', '..', '..');
45
+ for (const file of ['cli/lib/commands/magento.js', 'mcp/server.js']) {
46
+ const src = fs.readFileSync(path.join(root, file), 'utf8');
47
+ assert.doesNotMatch(src, /\/Magento\/(Customers|Orders|Products)\/Search\?q=/, `${file} still calls a Magento search without configId`);
48
+ }
49
+ });
@@ -12,6 +12,13 @@
12
12
  */
13
13
 
14
14
  import { request, messagingRequest } from '../api.js';
15
+ import { pickConfigId, buildSearchPath } from './magento-search.js';
16
+
17
+ // Which connected store a search runs against: --config-id, or the only active store.
18
+ async function resolveConfigId(argv) {
19
+ const configs = await request('GET', '/Magento/Configs');
20
+ return pickConfigId(configs, argv['config-id'] ?? argv.configId);
21
+ }
15
22
 
16
23
  const GRN = '\x1b[32m';
17
24
  const RED = '\x1b[31m';
@@ -73,16 +80,6 @@ ${RED}✗ Connection failed: ${e.message}${RST}
73
80
  }
74
81
  }
75
82
 
76
- // The search endpoints need the Magento config id (configId) and the search text (query).
77
- // Resolve the active store from /Magento/Configs; --config-id overrides it.
78
- async function resolveConfigId(argv) {
79
- if (argv.configId) return argv.configId;
80
- const stores = await request('GET', '/Magento/Configs');
81
- const active = (Array.isArray(stores) ? stores : []).find((s) => s.isActive && s.enabled !== false);
82
- if (!active) throw new Error('No active Magento store connected for this site.');
83
- return active.id;
84
- }
85
-
86
83
  // ── customers ─────────────────────────────────────────────────────────────────
87
84
 
88
85
  async function customersHandler(argv) {
@@ -92,7 +89,7 @@ async function customersHandler(argv) {
92
89
  return;
93
90
  }
94
91
  const configId = await resolveConfigId(argv);
95
- const found = await request('GET', `/Magento/Customers/Search?configId=${configId}&query=${encodeURIComponent(argv.email)}`);
92
+ const found = await request('GET', buildSearchPath('customers', configId, argv.email));
96
93
  const r = Array.isArray(found) ? found[0] : found;
97
94
  if (!r || !r.id) {
98
95
  console.log(`\nNo customer found for ${argv.email}.\n`);
@@ -117,7 +114,7 @@ async function ordersHandler(argv) {
117
114
  const q = argv.id ?? argv.email;
118
115
  const limit = Math.min(50, argv.limit ?? 10);
119
116
  const configId = await resolveConfigId(argv);
120
- const orders = await request('GET', `/Magento/Orders/Search?configId=${configId}&query=${encodeURIComponent(q)}&limit=${limit}`);
117
+ const orders = await request('GET', buildSearchPath('orders', configId, q) + '&limit=' + limit);
121
118
  if (!Array.isArray(orders) || orders.length === 0) {
122
119
  console.log(`\nNo orders found for "${q}".\n`);
123
120
  return;
@@ -143,7 +140,7 @@ async function productsHandler(argv) {
143
140
  }
144
141
  const limit = Math.min(50, argv.limit ?? 10);
145
142
  const configId = await resolveConfigId(argv);
146
- const products = await request('GET', `/Magento/Products/Search?configId=${configId}&query=${encodeURIComponent(argv.query)}&limit=${limit}`);
143
+ const products = await request('GET', buildSearchPath('products', configId, argv.query) + '&limit=' + limit);
147
144
  if (!Array.isArray(products) || products.length === 0) {
148
145
  console.log(`\nNo products found for "${argv.query}".\n`);
149
146
  return;
@@ -270,7 +267,8 @@ export const magentoCommand = {
270
267
  command: 'customers',
271
268
  describe: 'Search Magento customers by email',
272
269
  builder: yy => yy
273
- .option('email', { describe: 'Customer email address', type: 'string' }),
270
+ .option('email', { describe: 'Customer email address', type: 'string' })
271
+ .option('config-id', { describe: 'Connected store id (see `velaro magento list`); optional when only one store is connected', type: 'number' }),
274
272
  handler: customersHandler,
275
273
  })
276
274
  .command({
@@ -280,6 +278,7 @@ export const magentoCommand = {
280
278
  .option('email', { describe: 'Customer email address', type: 'string' })
281
279
  .option('id', { describe: 'Order increment ID (e.g. 000001234)', type: 'string' })
282
280
  .option('limit', { describe: 'Max results (default: 10)', type: 'number', default: 10 })
281
+ .option('config-id', { describe: 'Connected store id (see `velaro magento list`); optional when only one store is connected', type: 'number' })
283
282
  .option('verbose', { describe: 'Show order line items', type: 'boolean', default: false }),
284
283
  handler: ordersHandler,
285
284
  })
@@ -288,6 +287,7 @@ export const magentoCommand = {
288
287
  describe: 'Search the Magento product catalog',
289
288
  builder: yy => yy
290
289
  .option('query', { describe: 'Product name search query', type: 'string' })
290
+ .option('config-id', { describe: 'Connected store id (see `velaro magento list`); optional when only one store is connected', type: 'number' })
291
291
  .option('limit', { describe: 'Max results (default: 10)', type: 'number', default: 10 }),
292
292
  handler: productsHandler,
293
293
  })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@velaro/cli",
3
- "version": "1.4.161",
3
+ "version": "1.4.162",
4
4
  "description": "Velaro Workspace v20 — command-line interface for managing bots, knowledge base ingestion, MCP API keys, search indexes, and ops health.",
5
5
  "type": "module",
6
6
  "bin": {