@hiberus-magento/magento-rest 0.1.0 → 0.3.0
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.
- package/README.md +135 -17
- package/THIRD-PARTY-NOTICES.txt +1 -0
- package/dist/cli.js +277 -36
- package/dist/mcp.js +1754 -157
- package/package.json +6 -3
- package/server.json +56 -0
package/dist/cli.js
CHANGED
|
@@ -7,7 +7,7 @@ var __export = (target, all) => {
|
|
|
7
7
|
};
|
|
8
8
|
|
|
9
9
|
// src/cli.ts
|
|
10
|
-
import { createHash } from "crypto";
|
|
10
|
+
import { createHash as createHash2 } from "crypto";
|
|
11
11
|
|
|
12
12
|
// ../mcp-core/src/bootstrap/stdout-guard.ts
|
|
13
13
|
import { Writable } from "stream";
|
|
@@ -26,6 +26,10 @@ function isEntryPoint(importMetaUrl, entryPoint = process.argv[1]) {
|
|
|
26
26
|
}
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
+
// ../mcp-core/src/http/http-endpoint.ts
|
|
30
|
+
import { createHash, timingSafeEqual } from "crypto";
|
|
31
|
+
var DEFAULT_MAX_BODY_BYTES = 1024 * 1024;
|
|
32
|
+
|
|
29
33
|
// ../mcp-core/src/credentials/token-provider.ts
|
|
30
34
|
function formatRemedyMessage(prefix, remedy) {
|
|
31
35
|
return `${prefix} for domain "${remedy.domain}".
|
|
@@ -9124,8 +9128,8 @@ function ko_default() {
|
|
|
9124
9128
|
}
|
|
9125
9129
|
|
|
9126
9130
|
// ../../node_modules/.pnpm/zod@4.5.4/node_modules/zod/v4/locales/lt.js
|
|
9127
|
-
var capitalizeFirstCharacter = (
|
|
9128
|
-
return
|
|
9131
|
+
var capitalizeFirstCharacter = (text4) => {
|
|
9132
|
+
return text4.charAt(0).toUpperCase() + text4.slice(1);
|
|
9129
9133
|
};
|
|
9130
9134
|
function getUnitTypeFromNumber(number4) {
|
|
9131
9135
|
const abs = Math.abs(number4);
|
|
@@ -19048,11 +19052,15 @@ function readTokenFromStdin() {
|
|
|
19048
19052
|
// src/client/errors.ts
|
|
19049
19053
|
var MAX_MESSAGE_CHARS = 240;
|
|
19050
19054
|
var RETRYABLE_STATUSES = /* @__PURE__ */ new Set([429, 502, 503, 504]);
|
|
19051
|
-
var MagentoRestError = class extends Error {
|
|
19055
|
+
var MagentoRestError = class _MagentoRestError extends Error {
|
|
19052
19056
|
kind;
|
|
19053
19057
|
status;
|
|
19054
19058
|
retryable;
|
|
19055
19059
|
remedy;
|
|
19060
|
+
resources;
|
|
19061
|
+
retryAfterMs;
|
|
19062
|
+
timedOut;
|
|
19063
|
+
answer;
|
|
19056
19064
|
constructor(init) {
|
|
19057
19065
|
super(init.message, init.cause !== void 0 ? { cause: init.cause } : void 0);
|
|
19058
19066
|
this.name = "MagentoRestError";
|
|
@@ -19060,6 +19068,25 @@ var MagentoRestError = class extends Error {
|
|
|
19060
19068
|
this.status = init.status;
|
|
19061
19069
|
this.retryable = init.retryable ?? false;
|
|
19062
19070
|
this.remedy = init.remedy;
|
|
19071
|
+
this.resources = init.resources;
|
|
19072
|
+
this.retryAfterMs = init.retryAfterMs;
|
|
19073
|
+
this.timedOut = init.timedOut;
|
|
19074
|
+
this.answer = init.answer;
|
|
19075
|
+
}
|
|
19076
|
+
/** The same error, carrying where and when Magento gave it. */
|
|
19077
|
+
withAnswer(answer) {
|
|
19078
|
+
return new _MagentoRestError({
|
|
19079
|
+
kind: this.kind,
|
|
19080
|
+
message: this.message,
|
|
19081
|
+
retryable: this.retryable,
|
|
19082
|
+
...this.status !== void 0 ? { status: this.status } : {},
|
|
19083
|
+
...this.remedy !== void 0 ? { remedy: this.remedy } : {},
|
|
19084
|
+
...this.resources !== void 0 ? { resources: this.resources } : {},
|
|
19085
|
+
...this.retryAfterMs !== void 0 ? { retryAfterMs: this.retryAfterMs } : {},
|
|
19086
|
+
...this.timedOut !== void 0 ? { timedOut: this.timedOut } : {},
|
|
19087
|
+
...this.cause !== void 0 ? { cause: this.cause } : {},
|
|
19088
|
+
answer
|
|
19089
|
+
});
|
|
19063
19090
|
}
|
|
19064
19091
|
};
|
|
19065
19092
|
function kindForStatus(status2) {
|
|
@@ -19110,7 +19137,7 @@ function extractAclResources(status2, bodyText) {
|
|
|
19110
19137
|
}
|
|
19111
19138
|
return void 0;
|
|
19112
19139
|
}
|
|
19113
|
-
function classifyFailure(status2, bodyText) {
|
|
19140
|
+
function classifyFailure(status2, bodyText, retryAfterMs2) {
|
|
19114
19141
|
const kind = kindForStatus(status2);
|
|
19115
19142
|
const template = extractTemplate(bodyText);
|
|
19116
19143
|
const resources = extractAclResources(status2, bodyText);
|
|
@@ -19121,7 +19148,9 @@ function classifyFailure(status2, bodyText) {
|
|
|
19121
19148
|
message,
|
|
19122
19149
|
status: status2,
|
|
19123
19150
|
retryable: RETRYABLE_STATUSES.has(status2),
|
|
19124
|
-
...remedy !== void 0 ? { remedy } : {}
|
|
19151
|
+
...remedy !== void 0 ? { remedy } : {},
|
|
19152
|
+
...resources !== void 0 ? { resources } : {},
|
|
19153
|
+
...retryAfterMs2 !== void 0 ? { retryAfterMs: retryAfterMs2 } : {}
|
|
19125
19154
|
});
|
|
19126
19155
|
}
|
|
19127
19156
|
|
|
@@ -19209,7 +19238,7 @@ function normalizeBaseUrl(baseUrl) {
|
|
|
19209
19238
|
}
|
|
19210
19239
|
return baseUrl.replace(/\/+$/, "");
|
|
19211
19240
|
}
|
|
19212
|
-
var SCOPED_BASE = /^(.*\/rest)(?:\/[A-Za-z0-9_]+)?\/V1$/;
|
|
19241
|
+
var SCOPED_BASE = /^(.*\/rest)(?:\/([A-Za-z0-9_]+))?\/V1$/;
|
|
19213
19242
|
var STORE_CODE = /^[A-Za-z][A-Za-z0-9_]{0,31}$/;
|
|
19214
19243
|
function scopedBase(restRoot, scope) {
|
|
19215
19244
|
if (!STORE_CODE.test(scope)) {
|
|
@@ -19237,7 +19266,9 @@ function retryAfterMs(response) {
|
|
|
19237
19266
|
}
|
|
19238
19267
|
function createRestTransport(deps) {
|
|
19239
19268
|
const baseUrl = normalizeBaseUrl(deps.baseUrl);
|
|
19240
|
-
const
|
|
19269
|
+
const scopedMatch = SCOPED_BASE.exec(baseUrl);
|
|
19270
|
+
const restRoot = scopedMatch?.[1];
|
|
19271
|
+
const baseStoreView = scopedMatch?.[2];
|
|
19241
19272
|
const random = deps.random ?? Math.random;
|
|
19242
19273
|
const timeoutMs = deps.timeoutMs ?? DEFAULT_REST_TIMEOUT_MS;
|
|
19243
19274
|
const rateLimiter = new TokenBucketRateLimiter(REST_PACE, { now: deps.now, sleep: deps.sleep });
|
|
@@ -19250,7 +19281,7 @@ function createRestTransport(deps) {
|
|
|
19250
19281
|
headers["Content-Type"] = "application/json";
|
|
19251
19282
|
}
|
|
19252
19283
|
let response;
|
|
19253
|
-
let
|
|
19284
|
+
let text4;
|
|
19254
19285
|
try {
|
|
19255
19286
|
response = await deps.fetch(`${root}${request.path}${request.query !== void 0 && request.query !== "" ? `?${request.query}` : ""}`, {
|
|
19256
19287
|
method: request.method,
|
|
@@ -19261,7 +19292,7 @@ function createRestTransport(deps) {
|
|
|
19261
19292
|
signal: request.signal !== void 0 ? AbortSignal.any([request.signal, AbortSignal.timeout(timeoutMs)]) : AbortSignal.timeout(timeoutMs),
|
|
19262
19293
|
...hasBody ? { body: JSON.stringify(request.body) } : {}
|
|
19263
19294
|
});
|
|
19264
|
-
|
|
19295
|
+
text4 = await response.text();
|
|
19265
19296
|
} catch (cause) {
|
|
19266
19297
|
if (request.signal?.aborted === true) {
|
|
19267
19298
|
return { failure: cancelled(cause) };
|
|
@@ -19272,25 +19303,37 @@ function createRestTransport(deps) {
|
|
|
19272
19303
|
kind: "transport",
|
|
19273
19304
|
message: timedOut ? `Magento REST request timed out after ${Math.round(timeoutMs / 1e3)} s without a response.` : "Magento REST request failed before a response was received.",
|
|
19274
19305
|
retryable: true,
|
|
19306
|
+
...timedOut ? { timedOut: true } : {},
|
|
19275
19307
|
cause
|
|
19276
19308
|
})
|
|
19277
19309
|
};
|
|
19278
19310
|
}
|
|
19279
19311
|
if (!response.ok) {
|
|
19280
19312
|
const after = retryAfterMs(response);
|
|
19281
|
-
return { failure: classifyFailure(response.status,
|
|
19313
|
+
return { failure: classifyFailure(response.status, text4, after), ...after !== void 0 ? { retryAfterMs: after } : {} };
|
|
19282
19314
|
}
|
|
19283
|
-
if (
|
|
19315
|
+
if (text4 === "") {
|
|
19284
19316
|
return { value: void 0 };
|
|
19285
19317
|
}
|
|
19286
19318
|
try {
|
|
19287
|
-
return { value: JSON.parse(
|
|
19319
|
+
return { value: JSON.parse(text4) };
|
|
19288
19320
|
} catch {
|
|
19289
19321
|
return {
|
|
19290
19322
|
failure: new MagentoRestError({ kind: "unparseable", message: "Magento REST returned a success status with a body that is not JSON.", status: response.status })
|
|
19291
19323
|
};
|
|
19292
19324
|
}
|
|
19293
19325
|
}
|
|
19326
|
+
function answerFor(request, root) {
|
|
19327
|
+
const pathname = new URL(`${root}${request.path}`).pathname;
|
|
19328
|
+
const fromRest = pathname.indexOf("/rest/");
|
|
19329
|
+
const scope = request.scope ?? baseStoreView;
|
|
19330
|
+
const storeView = scope === "all" ? void 0 : scope;
|
|
19331
|
+
return {
|
|
19332
|
+
endpoint: `${request.method} ${fromRest >= 0 ? pathname.slice(fromRest) : pathname}`,
|
|
19333
|
+
readAt: new Date(deps.now()).toISOString(),
|
|
19334
|
+
...storeView !== void 0 ? { storeView } : {}
|
|
19335
|
+
};
|
|
19336
|
+
}
|
|
19294
19337
|
return {
|
|
19295
19338
|
async request(request) {
|
|
19296
19339
|
assertPath(request.path);
|
|
@@ -19302,6 +19345,7 @@ function createRestTransport(deps) {
|
|
|
19302
19345
|
}
|
|
19303
19346
|
const outcome = await attemptOnce(request, root);
|
|
19304
19347
|
if ("value" in outcome) {
|
|
19348
|
+
request.onAnswered?.(answerFor(request, root));
|
|
19305
19349
|
return outcome.value;
|
|
19306
19350
|
}
|
|
19307
19351
|
const decision = decideRetry(
|
|
@@ -19310,7 +19354,7 @@ function createRestTransport(deps) {
|
|
|
19310
19354
|
random
|
|
19311
19355
|
);
|
|
19312
19356
|
if (!decision.retry) {
|
|
19313
|
-
throw outcome.failure;
|
|
19357
|
+
throw outcome.failure.status !== void 0 ? outcome.failure.withAnswer(answerFor(request, root)) : outcome.failure;
|
|
19314
19358
|
}
|
|
19315
19359
|
await deps.sleep(decision.delayMs);
|
|
19316
19360
|
}
|
|
@@ -19387,14 +19431,14 @@ function buildSearchCriteria(input2) {
|
|
|
19387
19431
|
|
|
19388
19432
|
// src/client/paths.ts
|
|
19389
19433
|
function encodeSegment(value) {
|
|
19390
|
-
const
|
|
19391
|
-
if (
|
|
19434
|
+
const text4 = String(value);
|
|
19435
|
+
if (text4 === "") {
|
|
19392
19436
|
throw new Error("restPath: an empty path segment is not allowed");
|
|
19393
19437
|
}
|
|
19394
|
-
if (/^\.+$/.test(
|
|
19395
|
-
return
|
|
19438
|
+
if (/^\.+$/.test(text4)) {
|
|
19439
|
+
return text4.replaceAll(".", "%2E");
|
|
19396
19440
|
}
|
|
19397
|
-
return encodeURIComponent(
|
|
19441
|
+
return encodeURIComponent(text4);
|
|
19398
19442
|
}
|
|
19399
19443
|
function restPath(strings, ...values) {
|
|
19400
19444
|
let out = strings[0] ?? "";
|
|
@@ -19442,15 +19486,15 @@ var NAMES = {
|
|
|
19442
19486
|
};
|
|
19443
19487
|
var MAX_INPUT_LENGTH = 64;
|
|
19444
19488
|
function normalizeCountry(input2) {
|
|
19445
|
-
const
|
|
19446
|
-
if (
|
|
19489
|
+
const text4 = input2.trim().toLowerCase();
|
|
19490
|
+
if (text4.length === 0 || text4.length > MAX_INPUT_LENGTH) {
|
|
19447
19491
|
return void 0;
|
|
19448
19492
|
}
|
|
19449
|
-
const named = NAMES[
|
|
19450
|
-
if (named !== void 0 && Object.hasOwn(NAMES,
|
|
19493
|
+
const named = NAMES[text4];
|
|
19494
|
+
if (named !== void 0 && Object.hasOwn(NAMES, text4)) {
|
|
19451
19495
|
return named;
|
|
19452
19496
|
}
|
|
19453
|
-
return /^[a-z]{2}$/.test(
|
|
19497
|
+
return /^[a-z]{2}$/.test(text4) ? text4.toUpperCase() : void 0;
|
|
19454
19498
|
}
|
|
19455
19499
|
|
|
19456
19500
|
// src/client/date-range.ts
|
|
@@ -19468,8 +19512,8 @@ function formatMagentoTimestamp(date5) {
|
|
|
19468
19512
|
function formatDay(date5) {
|
|
19469
19513
|
return date5.toISOString().slice(0, 10);
|
|
19470
19514
|
}
|
|
19471
|
-
function parseDay(
|
|
19472
|
-
const match = DAY_PATTERN.exec(
|
|
19515
|
+
function parseDay(text4) {
|
|
19516
|
+
const match = DAY_PATTERN.exec(text4);
|
|
19473
19517
|
if (match === null) {
|
|
19474
19518
|
return void 0;
|
|
19475
19519
|
}
|
|
@@ -19894,7 +19938,7 @@ function analyticsTools({ readTool }) {
|
|
|
19894
19938
|
}),
|
|
19895
19939
|
readTool({
|
|
19896
19940
|
name: "magentorest_get_customer_ordered_products_by_email",
|
|
19897
|
-
description: "Use this to list every order of one customer, found by exact email, with each order's lines (SKU, name, price, quantity). Returns the customer's id, name and email, never their addresses. For a product's own detail use magentorest_get_product_by_sku. Reads at most max_pages pages of 100 orders and says when the result is partial.",
|
|
19941
|
+
description: "Use this to list every order of one customer, found by exact email, with each order's lines (SKU, name, price, quantity). Returns the customer's id, name and email only when the server runs with MAGENTO_REST_CUSTOMER_DATA=true, never their addresses. For a product's own detail use magentorest_get_product_by_sku. Reads at most max_pages pages of 100 orders and says when the result is partial.",
|
|
19898
19942
|
input: external_exports.object({ email: external_exports.email().max(254).describe("The customer's email address (exact)."), max_pages: maxPages }).strict(),
|
|
19899
19943
|
shape: [
|
|
19900
19944
|
["customer", ["id", "email", "firstname", "lastname"]],
|
|
@@ -20062,7 +20106,7 @@ function cartTools({ readTool, shapes }) {
|
|
|
20062
20106
|
return [
|
|
20063
20107
|
readTool({
|
|
20064
20108
|
name: "magentorest_get_cart_by_id",
|
|
20065
|
-
description: "Use this to read one cart by ID: activity, item count and quantity, currency
|
|
20109
|
+
description: "Use this to read one cart by ID: activity, item count and quantity, currency and the cart lines; the customer's identity (id, name, email, group) only when the server runs with MAGENTO_REST_CUSTOMER_DATA=true. Addresses are not returned.",
|
|
20066
20110
|
input: external_exports.object({ cart_id: cartId }).strict(),
|
|
20067
20111
|
shape: shapes.cart,
|
|
20068
20112
|
run: ({ cart_id }, client) => client.get(restPath`/carts/${cart_id}`)
|
|
@@ -20107,7 +20151,7 @@ function cartTools({ readTool, shapes }) {
|
|
|
20107
20151
|
}),
|
|
20108
20152
|
readTool({
|
|
20109
20153
|
name: "magentorest_get_abandoned_carts",
|
|
20110
|
-
description: "Use this to find abandoned carts: active, non-empty carts not updated for at least a number of days, most recently updated first. Each entry names the owner (or Guest)
|
|
20154
|
+
description: "Use this to find abandoned carts: active, non-empty carts not updated for at least a number of days, most recently updated first. Each entry gives the item count and the dates, and names the owner (or Guest) only when the server runs with MAGENTO_REST_CUSTOMER_DATA=true. Returns a bounded page, the total count and the cutoff used.",
|
|
20111
20155
|
input: external_exports.object({
|
|
20112
20156
|
days_old: external_exports.number().int().min(0).max(3650).optional().describe("Minimum days since the cart was last updated (default 7)."),
|
|
20113
20157
|
page_size: pageSize,
|
|
@@ -20451,7 +20495,7 @@ function customerTools({ readTool, writeTool, shapes }) {
|
|
|
20451
20495
|
return [
|
|
20452
20496
|
readTool({
|
|
20453
20497
|
name: "magentorest_get_customer_by_id",
|
|
20454
|
-
description: "Use this to read one customer by numeric ID:
|
|
20498
|
+
description: "Use this to read one customer by numeric ID: group, website, store and the default address IDs, plus name and email only when the server runs with MAGENTO_REST_CUSTOMER_DATA=true. Date of birth, tax number and the address book are not returned.",
|
|
20455
20499
|
input: external_exports.object({ customer_id: entityId("customer") }).strict(),
|
|
20456
20500
|
shape: shapes.customer,
|
|
20457
20501
|
run: ({ customer_id }, client) => client.get(restPath`/customers/${customer_id}`)
|
|
@@ -20500,7 +20544,7 @@ function customerTools({ readTool, writeTool, shapes }) {
|
|
|
20500
20544
|
}),
|
|
20501
20545
|
readTool({
|
|
20502
20546
|
name: "magentorest_get_customer_addresses",
|
|
20503
|
-
description: "Use this to list a customer's addresses by location: country, region
|
|
20547
|
+
description: "Use this to list a customer's addresses by location: country, region and which is the default billing or shipping address, plus city and postcode only when the server runs with MAGENTO_REST_CUSTOMER_DATA=true. Street, telephone and the addressee are not returned.",
|
|
20504
20548
|
input: external_exports.object({ customer_id: entityId("customer") }).strict(),
|
|
20505
20549
|
shape: shapes.customer_address,
|
|
20506
20550
|
run: async ({ customer_id }, client) => {
|
|
@@ -20572,11 +20616,208 @@ function customerTools({ readTool, writeTool, shapes }) {
|
|
|
20572
20616
|
];
|
|
20573
20617
|
}
|
|
20574
20618
|
|
|
20619
|
+
// src/tools/output-schema.ts
|
|
20620
|
+
var primitive = external_exports.union([external_exports.string(), external_exports.number(), external_exports.boolean(), external_exports.null()]);
|
|
20621
|
+
var provenanceSchema = external_exports.object({
|
|
20622
|
+
/** The first request: method and path from `/rest` on, with no host, query string or token. */
|
|
20623
|
+
endpoint: external_exports.string(),
|
|
20624
|
+
/** Each distinct endpoint, in the order first used, when the tool used more than one (a paged walk counts once). */
|
|
20625
|
+
endpoints: external_exports.array(external_exports.string()).optional(),
|
|
20626
|
+
api_version: external_exports.literal("V1"),
|
|
20627
|
+
/** ISO 8601 UTC, when Magento answered the last request. */
|
|
20628
|
+
read_at: external_exports.string(),
|
|
20629
|
+
/** The store view the requests addressed, when the URL named one. */
|
|
20630
|
+
store_view: external_exports.string().optional()
|
|
20631
|
+
});
|
|
20632
|
+
|
|
20633
|
+
// src/tools/rest-tool.ts
|
|
20634
|
+
function provenanceOf(answers) {
|
|
20635
|
+
const first = answers[0];
|
|
20636
|
+
const last = answers.at(-1);
|
|
20637
|
+
if (first === void 0 || last === void 0) {
|
|
20638
|
+
throw new Error("The read produced no Magento answer to cite, so its result cannot carry provenance.");
|
|
20639
|
+
}
|
|
20640
|
+
const endpoints = [...new Set(answers.map((answer) => answer.endpoint))];
|
|
20641
|
+
return {
|
|
20642
|
+
endpoint: first.endpoint,
|
|
20643
|
+
...endpoints.length > 1 ? { endpoints } : {},
|
|
20644
|
+
api_version: "V1",
|
|
20645
|
+
read_at: last.readAt,
|
|
20646
|
+
...first.storeView !== void 0 ? { store_view: first.storeView } : {}
|
|
20647
|
+
};
|
|
20648
|
+
}
|
|
20649
|
+
|
|
20575
20650
|
// src/tools/database-tools.ts
|
|
20576
20651
|
var SQL_PARAM = external_exports.union([external_exports.string().max(2e3), external_exports.number(), external_exports.boolean(), external_exports.null()]);
|
|
20577
20652
|
var params = external_exports.array(SQL_PARAM).max(50).optional().describe("Values for the ? placeholders in the statement, in order. Always pass values here instead of writing them into the SQL.");
|
|
20578
20653
|
var tableName = external_exports.string().regex(/^[A-Za-z0-9_]{1,64}$/, "letters, digits and underscore only (max 64)");
|
|
20579
20654
|
|
|
20655
|
+
// src/tools/tool-error.ts
|
|
20656
|
+
var ToolArgumentError = class extends Error {
|
|
20657
|
+
code;
|
|
20658
|
+
argument;
|
|
20659
|
+
constructor(code, argument, message) {
|
|
20660
|
+
super(message);
|
|
20661
|
+
this.name = "ToolArgumentError";
|
|
20662
|
+
this.code = code;
|
|
20663
|
+
this.argument = argument;
|
|
20664
|
+
}
|
|
20665
|
+
};
|
|
20666
|
+
|
|
20667
|
+
// src/tools/product-content-tools.ts
|
|
20668
|
+
var PRODUCT_CONTENT_READ_TOOL = "magentorest_get_product_content";
|
|
20669
|
+
var PRODUCT_CONTENT_WRITE_TOOL = "magentorest_update_product_content";
|
|
20670
|
+
var CONTENT_FIELDS = ["name", "description", "short_description", "meta_title", "meta_description"];
|
|
20671
|
+
var MAX_CHARS = { name: 255, meta_title: 255, meta_description: 255 };
|
|
20672
|
+
var MAX_TEXT_BYTES = 65535;
|
|
20673
|
+
var MAX_LABEL_CHARS = 255;
|
|
20674
|
+
var STORE_VIEW = /^[A-Za-z][A-Za-z0-9_]{0,31}$/;
|
|
20675
|
+
var GLOBAL_SCOPES = /* @__PURE__ */ new Set(["all", "admin"]);
|
|
20676
|
+
var UNKNOWN_ROUTE = "Request does not match any route";
|
|
20677
|
+
var contentSchema = external_exports.object(Object.fromEntries(CONTENT_FIELDS.map((field2) => [field2, external_exports.string().nullable()])));
|
|
20678
|
+
var imageSchema = external_exports.object({ id: external_exports.string(), file: external_exports.string().nullable(), label: external_exports.string().nullable() });
|
|
20679
|
+
var productContentSchema = external_exports.object({
|
|
20680
|
+
found: external_exports.boolean(),
|
|
20681
|
+
sku: external_exports.string(),
|
|
20682
|
+
store_view: external_exports.string(),
|
|
20683
|
+
content: contentSchema.optional(),
|
|
20684
|
+
images: external_exports.array(imageSchema).optional(),
|
|
20685
|
+
provenance: provenanceSchema
|
|
20686
|
+
});
|
|
20687
|
+
var isRecord3 = (value) => value !== null && typeof value === "object" && !Array.isArray(value);
|
|
20688
|
+
var text3 = (value) => typeof value === "string" && value !== "" ? value : null;
|
|
20689
|
+
function parseProduct(body) {
|
|
20690
|
+
const record2 = isRecord3(body) ? body : {};
|
|
20691
|
+
const custom2 = /* @__PURE__ */ new Map();
|
|
20692
|
+
for (const attribute of Array.isArray(record2["custom_attributes"]) ? record2["custom_attributes"] : []) {
|
|
20693
|
+
if (isRecord3(attribute) && typeof attribute["attribute_code"] === "string") custom2.set(attribute["attribute_code"], attribute["value"]);
|
|
20694
|
+
}
|
|
20695
|
+
const content2 = Object.fromEntries(CONTENT_FIELDS.map((field2) => [field2, text3(field2 === "name" ? record2["name"] : custom2.get(field2))]));
|
|
20696
|
+
const media = [];
|
|
20697
|
+
for (const entry of Array.isArray(record2["media_gallery_entries"]) ? record2["media_gallery_entries"] : []) {
|
|
20698
|
+
if (isRecord3(entry) && typeof entry["id"] === "number") media.push({ id: entry["id"], label: text3(entry["label"]), file: text3(entry["file"]), raw: entry });
|
|
20699
|
+
}
|
|
20700
|
+
return { content: content2, media };
|
|
20701
|
+
}
|
|
20702
|
+
function shaped(skuValue, storeView, product, provenance) {
|
|
20703
|
+
return {
|
|
20704
|
+
found: true,
|
|
20705
|
+
sku: skuValue,
|
|
20706
|
+
store_view: storeView,
|
|
20707
|
+
content: product.content,
|
|
20708
|
+
images: product.media.map((entry) => ({ id: String(entry.id), file: entry.file, label: entry.label })),
|
|
20709
|
+
provenance
|
|
20710
|
+
};
|
|
20711
|
+
}
|
|
20712
|
+
function checkedStoreView(value, forWrite) {
|
|
20713
|
+
if (value === void 0 || value === "") {
|
|
20714
|
+
throw new ToolArgumentError("invalid_argument", "store_view", "store_view is required: the code of the store view to read or write (for example default).");
|
|
20715
|
+
}
|
|
20716
|
+
if (!STORE_VIEW.test(value)) {
|
|
20717
|
+
throw new ToolArgumentError("invalid_argument", "store_view", "store_view must be a store view code: letters, digits and underscores.");
|
|
20718
|
+
}
|
|
20719
|
+
if (forWrite && GLOBAL_SCOPES.has(value.toLowerCase())) {
|
|
20720
|
+
throw new ToolArgumentError("invalid_argument", "store_view", "Content is written to one store view only: all and admin are the global scope, which would change every language and market.");
|
|
20721
|
+
}
|
|
20722
|
+
return value;
|
|
20723
|
+
}
|
|
20724
|
+
var isUnknownStoreView = (error61) => error61 instanceof MagentoRestError && error61.kind === "not-found" && error61.message.includes(UNKNOWN_ROUTE);
|
|
20725
|
+
var isMissingProduct = (error61) => error61 instanceof MagentoRestError && error61.kind === "not-found" && !error61.message.includes(UNKNOWN_ROUTE);
|
|
20726
|
+
async function readProduct(client, skuValue) {
|
|
20727
|
+
try {
|
|
20728
|
+
return parseProduct(await client.get(restPath`/products/${skuValue}`));
|
|
20729
|
+
} catch (error61) {
|
|
20730
|
+
if (isUnknownStoreView(error61)) throw new ToolArgumentError("invalid_argument", "store_view", "Magento has no store view with that code.");
|
|
20731
|
+
throw error61;
|
|
20732
|
+
}
|
|
20733
|
+
}
|
|
20734
|
+
var storeViewInput = external_exports.string().optional().describe("The store view code (for example default). Required; checked by the tool.");
|
|
20735
|
+
var contentInput = external_exports.object(Object.fromEntries(CONTENT_FIELDS.map((field2) => [field2, external_exports.string().optional()]))).strict().optional().describe("The content fields to set, each optional: name, description, short_description, meta_title, meta_description.");
|
|
20736
|
+
var imagesInput = external_exports.array(external_exports.object({ id: external_exports.string().describe('The media entry id, as a string (for example "12").'), label: external_exports.string().describe("The new label (alt text).") }).strict()).max(100).optional().describe("Image labels to set, by media entry id.");
|
|
20737
|
+
function checkedChange(content2, images) {
|
|
20738
|
+
for (const field2 of CONTENT_FIELDS) {
|
|
20739
|
+
const value = content2?.[field2];
|
|
20740
|
+
if (value === void 0) continue;
|
|
20741
|
+
const argument = `content.${field2}`;
|
|
20742
|
+
if (value === "") throw new ToolArgumentError("invalid_argument", argument, `${field2} cannot be empty: clearing a value is not supported.`);
|
|
20743
|
+
const maxChars = MAX_CHARS[field2];
|
|
20744
|
+
if (maxChars !== void 0 && [...value].length > maxChars) throw new ToolArgumentError("invalid_argument", argument, `${field2} is at most ${maxChars} characters.`);
|
|
20745
|
+
if (maxChars === void 0 && Buffer.byteLength(value, "utf8") > MAX_TEXT_BYTES) throw new ToolArgumentError("invalid_argument", argument, `${field2} is at most ${MAX_TEXT_BYTES} bytes.`);
|
|
20746
|
+
}
|
|
20747
|
+
const seen = /* @__PURE__ */ new Set();
|
|
20748
|
+
const checkedImages = (images ?? []).map((image, index) => {
|
|
20749
|
+
if (!/^[1-9][0-9]{0,9}$/.test(image.id)) throw new ToolArgumentError("invalid_argument", `images[${index}].id`, 'An image id is the media entry id as a decimal string, for example "12".');
|
|
20750
|
+
if (seen.has(image.id)) throw new ToolArgumentError("invalid_argument", `images[${index}].id`, "Each image id may appear once.");
|
|
20751
|
+
seen.add(image.id);
|
|
20752
|
+
if (image.label === "") throw new ToolArgumentError("invalid_argument", `images[${index}].label`, "A label cannot be empty: Magento stores an empty label as no label.");
|
|
20753
|
+
if ([...image.label].length > MAX_LABEL_CHARS) throw new ToolArgumentError("invalid_argument", `images[${index}].label`, `A label is at most ${MAX_LABEL_CHARS} characters.`);
|
|
20754
|
+
return { id: Number(image.id), label: image.label };
|
|
20755
|
+
});
|
|
20756
|
+
return { content: content2 ?? {}, images: checkedImages };
|
|
20757
|
+
}
|
|
20758
|
+
function productContentTools({ shapedTool }) {
|
|
20759
|
+
return [
|
|
20760
|
+
shapedTool({
|
|
20761
|
+
name: PRODUCT_CONTENT_READ_TOOL,
|
|
20762
|
+
description: "Use this to read a product's content as one store view shows it: name, description, short description, meta title and meta description, and each image's id, file and label. Each value is the store view's own or the one it inherits, null when there is none. An unknown SKU returns found: false.",
|
|
20763
|
+
input: external_exports.object({ sku, store_view: storeViewInput }).strict(),
|
|
20764
|
+
output: productContentSchema,
|
|
20765
|
+
run: async ({ sku: skuValue, store_view }, track) => {
|
|
20766
|
+
const storeView = checkedStoreView(store_view, false);
|
|
20767
|
+
const read = track();
|
|
20768
|
+
try {
|
|
20769
|
+
const product = await readProduct(read.client.with({ scope: storeView }), skuValue);
|
|
20770
|
+
return shaped(skuValue, storeView, product, provenanceOf(read.answers));
|
|
20771
|
+
} catch (error61) {
|
|
20772
|
+
if (isMissingProduct(error61) && error61 instanceof MagentoRestError && error61.answer !== void 0) {
|
|
20773
|
+
return { found: false, sku: skuValue, store_view: storeView, provenance: provenanceOf([error61.answer]) };
|
|
20774
|
+
}
|
|
20775
|
+
throw error61;
|
|
20776
|
+
}
|
|
20777
|
+
}
|
|
20778
|
+
}),
|
|
20779
|
+
shapedTool({
|
|
20780
|
+
name: PRODUCT_CONTENT_WRITE_TOOL,
|
|
20781
|
+
description: "Use this to change a product's content in ONE store view: any of name, description, short description, meta title and meta description, and image labels by id. Only the values that differ from the current ones are sent; the result is the content read back. A store view is required (all and admin are refused). Requires Magento 2.4.7 or later. Side effect: Magento then writes a store-view value for every image of the product, so those images stop inheriting later changes made to the default values in this store view.",
|
|
20782
|
+
effect: "update",
|
|
20783
|
+
input: external_exports.object({ sku, store_view: storeViewInput, content: contentInput, images: imagesInput }).strict(),
|
|
20784
|
+
output: productContentSchema,
|
|
20785
|
+
run: async ({ sku: skuValue, store_view, content: content2, images }, track) => {
|
|
20786
|
+
const storeView = checkedStoreView(store_view, true);
|
|
20787
|
+
const change = checkedChange(content2, images);
|
|
20788
|
+
const before = track();
|
|
20789
|
+
let current;
|
|
20790
|
+
try {
|
|
20791
|
+
current = await readProduct(before.client.with({ scope: storeView }), skuValue);
|
|
20792
|
+
} catch (error61) {
|
|
20793
|
+
if (isMissingProduct(error61)) throw new ToolArgumentError("not_found", "sku", "No product with that SKU: nothing was written.");
|
|
20794
|
+
throw error61;
|
|
20795
|
+
}
|
|
20796
|
+
const entries = change.images.map((image, index) => {
|
|
20797
|
+
const found = current.media.find((entry) => entry.id === image.id);
|
|
20798
|
+
if (found === void 0) throw new ToolArgumentError("not_found", `images[${index}].id`, "The product has no image with that id: nothing was written.");
|
|
20799
|
+
return { entry: found, label: image.label };
|
|
20800
|
+
});
|
|
20801
|
+
const scoped = before.client.with({ scope: storeView });
|
|
20802
|
+
const changedFields = CONTENT_FIELDS.filter((field2) => change.content[field2] !== void 0 && change.content[field2] !== current.content[field2]);
|
|
20803
|
+
if (changedFields.length > 0) {
|
|
20804
|
+
const custom2 = changedFields.filter((field2) => field2 !== "name").map((field2) => ({ attribute_code: field2, value: change.content[field2] }));
|
|
20805
|
+
await scoped.put(restPath`/products/${skuValue}`, {
|
|
20806
|
+
product: { sku: skuValue, ...changedFields.includes("name") ? { name: change.content.name } : {}, ...custom2.length > 0 ? { custom_attributes: custom2 } : {} }
|
|
20807
|
+
});
|
|
20808
|
+
}
|
|
20809
|
+
for (const { entry, label: label2 } of entries) {
|
|
20810
|
+
if (label2 === entry.label) continue;
|
|
20811
|
+
await scoped.put(restPath`/products/${skuValue}/media/${entry.id}`, { entry: { ...entry.raw, label: label2 } });
|
|
20812
|
+
}
|
|
20813
|
+
const after = track();
|
|
20814
|
+
const product = await readProduct(after.client.with({ scope: storeView }), skuValue);
|
|
20815
|
+
return shaped(skuValue, storeView, product, provenanceOf(after.answers));
|
|
20816
|
+
}
|
|
20817
|
+
})
|
|
20818
|
+
];
|
|
20819
|
+
}
|
|
20820
|
+
|
|
20580
20821
|
// src/tools/products-tools.ts
|
|
20581
20822
|
var IDENTITY_ATTRIBUTES = /* @__PURE__ */ new Set(["sku", "id", "entity_id", "type_id"]);
|
|
20582
20823
|
var primitiveValue = external_exports.union([external_exports.string(), external_exports.number(), external_exports.boolean()]);
|
|
@@ -20946,11 +21187,11 @@ var LIST_PAGE_SIZE7 = 20;
|
|
|
20946
21187
|
var statusCode = (what) => external_exports.string().regex(/^[a-z0-9_]{1,32}$/, "lowercase letters, digits and underscores only").describe(what);
|
|
20947
21188
|
var incrementId = external_exports.string().regex(/^[A-Za-z0-9-]{1,32}$/, "letters, digits and hyphens only (max 32)").describe("The increment ID (order number) shown to the customer.");
|
|
20948
21189
|
var orderId = entityId("order (entity ID, not the order number)");
|
|
20949
|
-
function
|
|
21190
|
+
function isRecord4(value) {
|
|
20950
21191
|
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
20951
21192
|
}
|
|
20952
21193
|
function arrayField(record2, key) {
|
|
20953
|
-
if (
|
|
21194
|
+
if (isRecord4(record2)) {
|
|
20954
21195
|
const value = record2[key];
|
|
20955
21196
|
return Array.isArray(value) ? value : [];
|
|
20956
21197
|
}
|
|
@@ -20960,7 +21201,7 @@ function orderTools({ readTool, writeTool, shapes }) {
|
|
|
20960
21201
|
return [
|
|
20961
21202
|
readTool({
|
|
20962
21203
|
name: "magentorest_get_order_by_id",
|
|
20963
|
-
description: "Use this to read one order by entity ID: status, state, totals, currency
|
|
21204
|
+
description: "Use this to read one order by entity ID: status, state, totals, currency and the order lines; who placed it (customer ID, name, email) only when the server runs with MAGENTO_REST_CUSTOMER_DATA=true. Billing and shipping addresses and payment details are not returned.",
|
|
20964
21205
|
input: external_exports.object({ order_id: orderId }).strict(),
|
|
20965
21206
|
shape: shapes.order,
|
|
20966
21207
|
run: ({ order_id }, client) => client.get(restPath`/orders/${order_id}`)
|
|
@@ -21375,7 +21616,7 @@ function taxTools({ readTool, shapes }) {
|
|
|
21375
21616
|
|
|
21376
21617
|
// src/tools/index.ts
|
|
21377
21618
|
var GROUPS = {
|
|
21378
|
-
products: productTools,
|
|
21619
|
+
products: (factory) => [...productTools(factory), ...productContentTools(factory)],
|
|
21379
21620
|
store: storeTools,
|
|
21380
21621
|
categories: categoryTools,
|
|
21381
21622
|
customers: customerTools,
|
|
@@ -21451,7 +21692,7 @@ async function authStatus(deps) {
|
|
|
21451
21692
|
} catch (error61) {
|
|
21452
21693
|
return { exitCode: 1, stderr: redact(error61.message) };
|
|
21453
21694
|
}
|
|
21454
|
-
const fingerprint =
|
|
21695
|
+
const fingerprint = createHash2("sha256").update(token).digest("hex").slice(0, 8);
|
|
21455
21696
|
const lines = [`key fingerprint: ${fingerprint}`];
|
|
21456
21697
|
try {
|
|
21457
21698
|
await deps.probe();
|