@ecomweb/sdk 0.4.0 → 0.6.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/dist/index.d.ts +492 -7
- package/dist/index.js +1357 -0
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1294,6 +1294,11 @@ var itemSchema = z15.discriminatedUnion("type", [
|
|
|
1294
1294
|
}),
|
|
1295
1295
|
z15.object({ ...base, type: z15.literal("image_picker") }),
|
|
1296
1296
|
z15.object({ ...base, type: z15.literal("collection") }),
|
|
1297
|
+
z15.object({ ...base, type: z15.literal("page") }),
|
|
1298
|
+
z15.object({ ...base, type: z15.literal("blog_post") }),
|
|
1299
|
+
z15.object({ ...base, type: z15.literal("blog_category") }),
|
|
1300
|
+
z15.object({ ...base, type: z15.literal("blog_tag") }),
|
|
1301
|
+
z15.object({ ...base, type: z15.literal("product") }),
|
|
1297
1302
|
z15.object({
|
|
1298
1303
|
...base,
|
|
1299
1304
|
type: z15.literal("checkbox"),
|
|
@@ -1463,6 +1468,1259 @@ function isSettingVisible(setting, values) {
|
|
|
1463
1468
|
return value !== rule.not_equals;
|
|
1464
1469
|
}
|
|
1465
1470
|
|
|
1471
|
+
// src/types/metafield-engine.ts
|
|
1472
|
+
var METAFIELD_OWNER_TYPES = [
|
|
1473
|
+
"product",
|
|
1474
|
+
"variant",
|
|
1475
|
+
"collection",
|
|
1476
|
+
"customer",
|
|
1477
|
+
"order",
|
|
1478
|
+
"page",
|
|
1479
|
+
"blog_post",
|
|
1480
|
+
"location",
|
|
1481
|
+
"shop"
|
|
1482
|
+
];
|
|
1483
|
+
var METAFIELD_BASE_TYPES = [
|
|
1484
|
+
"single_line_text",
|
|
1485
|
+
"multi_line_text",
|
|
1486
|
+
"rich_text",
|
|
1487
|
+
"email",
|
|
1488
|
+
"integer",
|
|
1489
|
+
"decimal",
|
|
1490
|
+
"date",
|
|
1491
|
+
"date_time",
|
|
1492
|
+
"url",
|
|
1493
|
+
"link",
|
|
1494
|
+
"weight",
|
|
1495
|
+
"volume",
|
|
1496
|
+
"dimension",
|
|
1497
|
+
"boolean",
|
|
1498
|
+
"color",
|
|
1499
|
+
"rating",
|
|
1500
|
+
"money",
|
|
1501
|
+
"language",
|
|
1502
|
+
"json",
|
|
1503
|
+
"file",
|
|
1504
|
+
"image",
|
|
1505
|
+
"video",
|
|
1506
|
+
"product",
|
|
1507
|
+
"variant",
|
|
1508
|
+
"collection",
|
|
1509
|
+
"page",
|
|
1510
|
+
"blog_post",
|
|
1511
|
+
"customer",
|
|
1512
|
+
"order",
|
|
1513
|
+
"metaobject_reference",
|
|
1514
|
+
"mixed_reference"
|
|
1515
|
+
];
|
|
1516
|
+
var BASE_TYPE_SET = new Set(METAFIELD_BASE_TYPES);
|
|
1517
|
+
function isMetafieldType(value) {
|
|
1518
|
+
if (typeof value !== "string") return false;
|
|
1519
|
+
const base2 = value.startsWith("list.") ? value.slice(5) : value;
|
|
1520
|
+
return BASE_TYPE_SET.has(base2);
|
|
1521
|
+
}
|
|
1522
|
+
function isListType(type) {
|
|
1523
|
+
return type.startsWith("list.");
|
|
1524
|
+
}
|
|
1525
|
+
function baseTypeOf(type) {
|
|
1526
|
+
return isListType(type) ? type.slice(5) : type;
|
|
1527
|
+
}
|
|
1528
|
+
var METAFIELD_TYPE_GROUPS = [
|
|
1529
|
+
{
|
|
1530
|
+
group: "text",
|
|
1531
|
+
types: ["single_line_text", "multi_line_text", "rich_text", "email"]
|
|
1532
|
+
},
|
|
1533
|
+
{ group: "media", types: ["file", "image", "video"] },
|
|
1534
|
+
{
|
|
1535
|
+
group: "reference",
|
|
1536
|
+
types: [
|
|
1537
|
+
"blog_post",
|
|
1538
|
+
"collection",
|
|
1539
|
+
"customer",
|
|
1540
|
+
"metaobject_reference",
|
|
1541
|
+
"order",
|
|
1542
|
+
"page",
|
|
1543
|
+
"product",
|
|
1544
|
+
"variant"
|
|
1545
|
+
]
|
|
1546
|
+
},
|
|
1547
|
+
{ group: "link", types: ["link", "url"] },
|
|
1548
|
+
{ group: "date_time", types: ["date", "date_time"] },
|
|
1549
|
+
{ group: "number", types: ["integer", "decimal", "rating", "money"] },
|
|
1550
|
+
{ group: "measurement", types: ["weight", "volume", "dimension"] },
|
|
1551
|
+
{ group: "other", types: ["boolean", "color", "language"] },
|
|
1552
|
+
{ group: "advanced", types: ["json", "mixed_reference"] }
|
|
1553
|
+
];
|
|
1554
|
+
var RESOURCE_REFERENCE_TYPES = [
|
|
1555
|
+
"product",
|
|
1556
|
+
"variant",
|
|
1557
|
+
"collection",
|
|
1558
|
+
"page",
|
|
1559
|
+
"blog_post",
|
|
1560
|
+
"customer",
|
|
1561
|
+
"order"
|
|
1562
|
+
];
|
|
1563
|
+
var METAOBJECT_REFERENCE_TYPES = [
|
|
1564
|
+
"metaobject_reference",
|
|
1565
|
+
"mixed_reference"
|
|
1566
|
+
];
|
|
1567
|
+
var MEDIA_TYPES = ["file", "image", "video"];
|
|
1568
|
+
var PRIVATE_REFERENCE_TYPES = ["customer", "order"];
|
|
1569
|
+
var METAFIELD_LIMITS = {
|
|
1570
|
+
definitions_per_owner_type: 100,
|
|
1571
|
+
pinned_per_owner_type: 20,
|
|
1572
|
+
metaobject_definitions: 100,
|
|
1573
|
+
fields_per_metaobject: 40,
|
|
1574
|
+
filter_keys: 5,
|
|
1575
|
+
value_bytes: 65536,
|
|
1576
|
+
list_items: 128,
|
|
1577
|
+
choices: 128,
|
|
1578
|
+
allowed_domains: 20,
|
|
1579
|
+
regex_length: 200,
|
|
1580
|
+
regex_value_length: 500,
|
|
1581
|
+
single_line_text_length: 5e3,
|
|
1582
|
+
multi_line_text_length: 65e3,
|
|
1583
|
+
link_text_length: 255,
|
|
1584
|
+
email_length: 254,
|
|
1585
|
+
name_length: 255,
|
|
1586
|
+
description_length: 5e3,
|
|
1587
|
+
mixed_reference_definitions_min: 2,
|
|
1588
|
+
mixed_reference_definitions_max: 10
|
|
1589
|
+
};
|
|
1590
|
+
function issue(path, code, message) {
|
|
1591
|
+
return { path, code, message };
|
|
1592
|
+
}
|
|
1593
|
+
function ok(value) {
|
|
1594
|
+
return { ok: true, value };
|
|
1595
|
+
}
|
|
1596
|
+
function fail(path, code, message) {
|
|
1597
|
+
return { ok: false, errors: [issue(path, code, message)] };
|
|
1598
|
+
}
|
|
1599
|
+
function joinPath(base2, segment) {
|
|
1600
|
+
if (typeof segment === "number") return `${base2}[${segment}]`;
|
|
1601
|
+
return base2 ? `${base2}.${segment}` : segment;
|
|
1602
|
+
}
|
|
1603
|
+
function isPlainObject(value) {
|
|
1604
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
1605
|
+
}
|
|
1606
|
+
function hasOwn(object, key) {
|
|
1607
|
+
return Object.prototype.hasOwnProperty.call(object, key);
|
|
1608
|
+
}
|
|
1609
|
+
function unknownKeys(object, allowed, path) {
|
|
1610
|
+
return Object.keys(object).filter((key) => !allowed.includes(key)).map((key) => issue(joinPath(path, key), "unknown_key", `unknown key "${key}"`));
|
|
1611
|
+
}
|
|
1612
|
+
function codePoints(text) {
|
|
1613
|
+
return [...text].length;
|
|
1614
|
+
}
|
|
1615
|
+
var METAFIELD_KEY_PATTERN = /^[a-z0-9_]{2,64}$/;
|
|
1616
|
+
var METAOBJECT_TYPE_PATTERN = /^[a-z0-9_-]{2,64}$/;
|
|
1617
|
+
var ENTRY_HANDLE_PATTERN = /^[a-z0-9-]{1,255}$/;
|
|
1618
|
+
function asciiFold(text) {
|
|
1619
|
+
return text.normalize("NFD").replace(/[\u0300-\u036f]/g, "").replace(/\u0111/g, "d").replace(/\u0110/g, "D");
|
|
1620
|
+
}
|
|
1621
|
+
function slugify(text, separator, max) {
|
|
1622
|
+
const edge = new RegExp(`^${separator}+|${separator}+$`, "g");
|
|
1623
|
+
return asciiFold(text).toLowerCase().replace(/[^a-z0-9]+/g, separator).replace(edge, "").slice(0, max).replace(edge, "");
|
|
1624
|
+
}
|
|
1625
|
+
function toMetafieldKey(text) {
|
|
1626
|
+
return slugify(text, "_", 64);
|
|
1627
|
+
}
|
|
1628
|
+
function toEntryHandle(text) {
|
|
1629
|
+
const handle = slugify(text, "-", 255);
|
|
1630
|
+
return RESERVED_KEYS.includes(handle) ? "" : handle;
|
|
1631
|
+
}
|
|
1632
|
+
function isValidEntryHandle(handle) {
|
|
1633
|
+
return ENTRY_HANDLE_PATTERN.test(handle) && !RESERVED_KEYS.includes(handle);
|
|
1634
|
+
}
|
|
1635
|
+
function metafieldIdentifier(namespace, key) {
|
|
1636
|
+
return `${namespace}.${key}`;
|
|
1637
|
+
}
|
|
1638
|
+
var RESERVED_KEYS = ["__proto__", "constructor", "prototype"];
|
|
1639
|
+
function parseMetafieldIdentifier(identifier) {
|
|
1640
|
+
const parts = identifier.split(".");
|
|
1641
|
+
if (parts.length !== 2) return null;
|
|
1642
|
+
const [namespace, key] = parts;
|
|
1643
|
+
if (!METAFIELD_KEY_PATTERN.test(namespace) || !METAFIELD_KEY_PATTERN.test(key)) {
|
|
1644
|
+
return null;
|
|
1645
|
+
}
|
|
1646
|
+
if (RESERVED_KEYS.includes(namespace) || RESERVED_KEYS.includes(key)) return null;
|
|
1647
|
+
return { namespace, key };
|
|
1648
|
+
}
|
|
1649
|
+
function matchesPattern(pattern, text) {
|
|
1650
|
+
try {
|
|
1651
|
+
return new RegExp("^(?:" + pattern + ")$", "u").test(text);
|
|
1652
|
+
} catch {
|
|
1653
|
+
return false;
|
|
1654
|
+
}
|
|
1655
|
+
}
|
|
1656
|
+
function textChecker(limit, singleLine) {
|
|
1657
|
+
return (value, v, _ctx, path) => {
|
|
1658
|
+
if (typeof value !== "string") return fail(path, "invalid_type", "must be text");
|
|
1659
|
+
if (singleLine && /[\r\n]/.test(value)) {
|
|
1660
|
+
return fail(path, "invalid_format", "must be a single line");
|
|
1661
|
+
}
|
|
1662
|
+
const length = codePoints(value);
|
|
1663
|
+
const cap = v.regex !== void 0 ? Math.min(limit, METAFIELD_LIMITS.regex_value_length) : limit;
|
|
1664
|
+
const max = Math.min(cap, v.max_length ?? cap);
|
|
1665
|
+
if (length > max) return fail(path, "too_long", `must be at most ${max} characters`);
|
|
1666
|
+
if (v.min_length !== void 0 && length < v.min_length) {
|
|
1667
|
+
return fail(path, "too_short", `must be at least ${v.min_length} characters`);
|
|
1668
|
+
}
|
|
1669
|
+
if (v.regex !== void 0 && !matchesPattern(v.regex, value)) {
|
|
1670
|
+
return fail(path, "pattern_mismatch", "does not match the required pattern");
|
|
1671
|
+
}
|
|
1672
|
+
if (v.choices !== void 0 && !v.choices.includes(value)) {
|
|
1673
|
+
return fail(path, "not_a_choice", `must be one of: ${v.choices.join(", ")}`);
|
|
1674
|
+
}
|
|
1675
|
+
return ok(value);
|
|
1676
|
+
};
|
|
1677
|
+
}
|
|
1678
|
+
var EMAIL = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
1679
|
+
var checkEmail = (value, _v, _ctx, path) => {
|
|
1680
|
+
if (typeof value !== "string") return fail(path, "invalid_type", "must be text");
|
|
1681
|
+
const email = value.trim().toLowerCase();
|
|
1682
|
+
if (email.length > METAFIELD_LIMITS.email_length || !EMAIL.test(email)) {
|
|
1683
|
+
return fail(path, "invalid_format", "must be an email address");
|
|
1684
|
+
}
|
|
1685
|
+
return ok(email);
|
|
1686
|
+
};
|
|
1687
|
+
var checkInteger = (value, v, _ctx, path) => {
|
|
1688
|
+
if (typeof value !== "number" || !Number.isSafeInteger(value)) {
|
|
1689
|
+
return fail(path, "invalid_type", "must be a whole number");
|
|
1690
|
+
}
|
|
1691
|
+
if (typeof v.min === "number" && value < v.min) {
|
|
1692
|
+
return fail(path, "too_small", `must be at least ${v.min}`);
|
|
1693
|
+
}
|
|
1694
|
+
if (typeof v.max === "number" && value > v.max) {
|
|
1695
|
+
return fail(path, "too_big", `must be at most ${v.max}`);
|
|
1696
|
+
}
|
|
1697
|
+
return ok(value === 0 ? 0 : value);
|
|
1698
|
+
};
|
|
1699
|
+
var DECIMAL_INPUT = /^[+-]?\d+(\.\d+)?$/;
|
|
1700
|
+
function normalizeDecimal(input) {
|
|
1701
|
+
let text;
|
|
1702
|
+
if (typeof input === "number") {
|
|
1703
|
+
if (!Number.isFinite(input)) return null;
|
|
1704
|
+
text = String(input);
|
|
1705
|
+
} else if (typeof input === "string") {
|
|
1706
|
+
text = input.trim();
|
|
1707
|
+
} else {
|
|
1708
|
+
return null;
|
|
1709
|
+
}
|
|
1710
|
+
if (text.length > 40 || !DECIMAL_INPUT.test(text)) return null;
|
|
1711
|
+
const negative = text.startsWith("-");
|
|
1712
|
+
if (text[0] === "-" || text[0] === "+") text = text.slice(1);
|
|
1713
|
+
const [whole, fraction = ""] = text.split(".");
|
|
1714
|
+
const integer = whole.replace(/^0+(?=\d)/, "");
|
|
1715
|
+
const decimals = fraction.replace(/0+$/, "");
|
|
1716
|
+
const magnitude = decimals ? `${integer}.${decimals}` : integer;
|
|
1717
|
+
return magnitude === "0" ? "0" : `${negative ? "-" : ""}${magnitude}`;
|
|
1718
|
+
}
|
|
1719
|
+
function compareDecimal(a, b) {
|
|
1720
|
+
const aNegative = a.startsWith("-");
|
|
1721
|
+
const bNegative = b.startsWith("-");
|
|
1722
|
+
if (aNegative !== bNegative) return aNegative ? -1 : 1;
|
|
1723
|
+
const sign = aNegative ? -1 : 1;
|
|
1724
|
+
const [aWhole, aFraction = ""] = (aNegative ? a.slice(1) : a).split(".");
|
|
1725
|
+
const [bWhole, bFraction = ""] = (bNegative ? b.slice(1) : b).split(".");
|
|
1726
|
+
let order;
|
|
1727
|
+
if (aWhole.length !== bWhole.length) {
|
|
1728
|
+
order = aWhole.length > bWhole.length ? 1 : -1;
|
|
1729
|
+
} else if (aWhole !== bWhole) {
|
|
1730
|
+
order = aWhole > bWhole ? 1 : -1;
|
|
1731
|
+
} else {
|
|
1732
|
+
const width = Math.max(aFraction.length, bFraction.length);
|
|
1733
|
+
const aPadded = aFraction.padEnd(width, "0");
|
|
1734
|
+
const bPadded = bFraction.padEnd(width, "0");
|
|
1735
|
+
order = aPadded === bPadded ? 0 : aPadded > bPadded ? 1 : -1;
|
|
1736
|
+
}
|
|
1737
|
+
return order === 0 ? 0 : order * sign;
|
|
1738
|
+
}
|
|
1739
|
+
var checkDecimal = (value, v, _ctx, path) => {
|
|
1740
|
+
const decimal = normalizeDecimal(value);
|
|
1741
|
+
if (decimal === null) return fail(path, "invalid_type", "must be a decimal number");
|
|
1742
|
+
const fraction = decimal.split(".")[1] ?? "";
|
|
1743
|
+
if (v.max_precision !== void 0 && fraction.length > v.max_precision) {
|
|
1744
|
+
return fail(path, "precision_exceeded", `must have at most ${v.max_precision} decimal places`);
|
|
1745
|
+
}
|
|
1746
|
+
if (typeof v.min === "string" && compareDecimal(decimal, v.min) < 0) {
|
|
1747
|
+
return fail(path, "too_small", `must be at least ${v.min}`);
|
|
1748
|
+
}
|
|
1749
|
+
if (typeof v.max === "string" && compareDecimal(decimal, v.max) > 0) {
|
|
1750
|
+
return fail(path, "too_big", `must be at most ${v.max}`);
|
|
1751
|
+
}
|
|
1752
|
+
return ok(decimal);
|
|
1753
|
+
};
|
|
1754
|
+
var checkBoolean = (value, _v, _ctx, path) => typeof value === "boolean" ? ok(value) : fail(path, "invalid_type", "must be true or false");
|
|
1755
|
+
var COLOR = /^#[0-9a-fA-F]{6}$/;
|
|
1756
|
+
var checkColor = (value, _v, _ctx, path) => typeof value === "string" && COLOR.test(value) ? ok(value.toLowerCase()) : fail(path, "invalid_format", "must be a hex color such as #1a2b3c");
|
|
1757
|
+
function isCalendarDate(year, month, day) {
|
|
1758
|
+
const date = /* @__PURE__ */ new Date(0);
|
|
1759
|
+
date.setUTCFullYear(year, month - 1, day);
|
|
1760
|
+
return date.getUTCFullYear() === year && date.getUTCMonth() === month - 1 && date.getUTCDate() === day;
|
|
1761
|
+
}
|
|
1762
|
+
var DATE = /^(\d{4})-(\d{2})-(\d{2})$/;
|
|
1763
|
+
function normalizeDate(input) {
|
|
1764
|
+
if (typeof input !== "string") return null;
|
|
1765
|
+
const match = DATE.exec(input);
|
|
1766
|
+
if (!match || !isCalendarDate(+match[1], +match[2], +match[3])) return null;
|
|
1767
|
+
return input;
|
|
1768
|
+
}
|
|
1769
|
+
var DATE_TIME = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2})(?::(\d{2})(?:\.\d{1,3})?)?(Z|[+-](\d{2}):(\d{2}))$/;
|
|
1770
|
+
function normalizeDateTime(input) {
|
|
1771
|
+
if (typeof input !== "string") return null;
|
|
1772
|
+
const match = DATE_TIME.exec(input);
|
|
1773
|
+
if (!match || !isCalendarDate(+match[1], +match[2], +match[3])) return null;
|
|
1774
|
+
if (+match[4] > 23 || +match[5] > 59 || +(match[6] ?? "0") > 59) return null;
|
|
1775
|
+
if (match[8] !== void 0 && (+match[8] > 23 || +match[9] > 59)) return null;
|
|
1776
|
+
const ms = Date.parse(input);
|
|
1777
|
+
if (Number.isNaN(ms)) return null;
|
|
1778
|
+
const iso = new Date(ms).toISOString();
|
|
1779
|
+
return /^\d{4}-/.test(iso) ? `${iso.slice(0, 19)}Z` : null;
|
|
1780
|
+
}
|
|
1781
|
+
function dateChecker(normalize, format) {
|
|
1782
|
+
return (value, v, _ctx, path) => {
|
|
1783
|
+
const normalized = normalize(value);
|
|
1784
|
+
if (normalized === null) return fail(path, "invalid_format", `must be ${format}`);
|
|
1785
|
+
if (typeof v.min === "string" && normalized < v.min) {
|
|
1786
|
+
return fail(path, "too_small", `must be on or after ${v.min}`);
|
|
1787
|
+
}
|
|
1788
|
+
if (typeof v.max === "string" && normalized > v.max) {
|
|
1789
|
+
return fail(path, "too_big", `must be on or before ${v.max}`);
|
|
1790
|
+
}
|
|
1791
|
+
return ok(normalized);
|
|
1792
|
+
};
|
|
1793
|
+
}
|
|
1794
|
+
var MAX_URL_LENGTH = 2048;
|
|
1795
|
+
var UNSAFE_URL_CHARS = /[\u0000-\u001f\u007f-\u009f"<>`\\]/;
|
|
1796
|
+
function parseHttpUrl(value) {
|
|
1797
|
+
if (typeof value !== "string" || value.length > MAX_URL_LENGTH || /\s/.test(value) || UNSAFE_URL_CHARS.test(value)) {
|
|
1798
|
+
return null;
|
|
1799
|
+
}
|
|
1800
|
+
try {
|
|
1801
|
+
const url = new URL(value);
|
|
1802
|
+
return url.protocol === "http:" || url.protocol === "https:" ? url : null;
|
|
1803
|
+
} catch {
|
|
1804
|
+
return null;
|
|
1805
|
+
}
|
|
1806
|
+
}
|
|
1807
|
+
function hostAllowed(url, domains) {
|
|
1808
|
+
if (!domains) return true;
|
|
1809
|
+
const host = url.hostname.toLowerCase();
|
|
1810
|
+
return domains.some((domain) => host === domain || host.endsWith(`.${domain}`));
|
|
1811
|
+
}
|
|
1812
|
+
var checkUrl = (value, v, _ctx, path) => {
|
|
1813
|
+
const url = parseHttpUrl(value);
|
|
1814
|
+
if (!url) return fail(path, "invalid_format", "must be an http(s) URL");
|
|
1815
|
+
if (!hostAllowed(url, v.allowed_domains)) {
|
|
1816
|
+
return fail(path, "domain_not_allowed", `must be on ${(v.allowed_domains ?? []).join(", ")}`);
|
|
1817
|
+
}
|
|
1818
|
+
return ok(value);
|
|
1819
|
+
};
|
|
1820
|
+
var checkLink = (value, v, ctx, path) => {
|
|
1821
|
+
if (!isPlainObject(value)) {
|
|
1822
|
+
return fail(path, "invalid_type", "must be an object with url and text");
|
|
1823
|
+
}
|
|
1824
|
+
const extra = unknownKeys(value, ["url", "text"], path);
|
|
1825
|
+
if (extra.length) return { ok: false, errors: extra };
|
|
1826
|
+
const url = checkUrl(value.url, v, ctx, joinPath(path, "url"));
|
|
1827
|
+
if (!url.ok) return url;
|
|
1828
|
+
const text = value.text ?? "";
|
|
1829
|
+
if (typeof text !== "string") return fail(joinPath(path, "text"), "invalid_type", "must be text");
|
|
1830
|
+
if (codePoints(text) > METAFIELD_LIMITS.link_text_length) {
|
|
1831
|
+
return fail(
|
|
1832
|
+
joinPath(path, "text"),
|
|
1833
|
+
"too_long",
|
|
1834
|
+
`must be at most ${METAFIELD_LIMITS.link_text_length} characters`
|
|
1835
|
+
);
|
|
1836
|
+
}
|
|
1837
|
+
return ok({ url: url.value, text });
|
|
1838
|
+
};
|
|
1839
|
+
var LANGUAGE = /^([a-zA-Z]{2,3})(?:-([a-zA-Z]{4}))?(?:-([a-zA-Z]{2}|\d{3}))?$/;
|
|
1840
|
+
var checkLanguage = (value, _v, _ctx, path) => {
|
|
1841
|
+
const match = typeof value === "string" ? LANGUAGE.exec(value) : null;
|
|
1842
|
+
if (!match) {
|
|
1843
|
+
return fail(path, "invalid_format", "must be a language tag such as vi or en-US");
|
|
1844
|
+
}
|
|
1845
|
+
const script = match[2] ? match[2][0].toUpperCase() + match[2].slice(1).toLowerCase() : void 0;
|
|
1846
|
+
return ok([match[1].toLowerCase(), script, match[3]?.toUpperCase()].filter(Boolean).join("-"));
|
|
1847
|
+
};
|
|
1848
|
+
var checkJson = (value, _v, _ctx, path) => {
|
|
1849
|
+
let text;
|
|
1850
|
+
let nonFinite = false;
|
|
1851
|
+
try {
|
|
1852
|
+
text = JSON.stringify(value, (_key, item) => {
|
|
1853
|
+
if (typeof item === "number" && !Number.isFinite(item)) nonFinite = true;
|
|
1854
|
+
return item;
|
|
1855
|
+
});
|
|
1856
|
+
} catch {
|
|
1857
|
+
text = void 0;
|
|
1858
|
+
}
|
|
1859
|
+
if (nonFinite) return fail(path, "invalid_format", "must not contain NaN or Infinity");
|
|
1860
|
+
return text === void 0 ? fail(path, "invalid_type", "must be JSON") : ok(JSON.parse(text));
|
|
1861
|
+
};
|
|
1862
|
+
var MAX_RICH_ERRORS = 100;
|
|
1863
|
+
var MAILTO = /^mailto:[^\s@?&#,]+@[^\s@?&#,]+$/i;
|
|
1864
|
+
function richChildren(children, path, errors, each) {
|
|
1865
|
+
if (!Array.isArray(children)) {
|
|
1866
|
+
errors.push(issue(path, "invalid_type", "must be a list of nodes"));
|
|
1867
|
+
return [];
|
|
1868
|
+
}
|
|
1869
|
+
const out = [];
|
|
1870
|
+
for (let index = 0; index < children.length; index++) {
|
|
1871
|
+
if (errors.length >= MAX_RICH_ERRORS) break;
|
|
1872
|
+
const node = each(children[index], joinPath(path, index));
|
|
1873
|
+
if (node) out.push(node);
|
|
1874
|
+
}
|
|
1875
|
+
return out;
|
|
1876
|
+
}
|
|
1877
|
+
function richText(node, path, errors) {
|
|
1878
|
+
errors.push(...unknownKeys(node, ["type", "value", "bold", "italic"], path));
|
|
1879
|
+
if (typeof node.value !== "string") {
|
|
1880
|
+
errors.push(issue(joinPath(path, "value"), "invalid_type", "must be text"));
|
|
1881
|
+
return null;
|
|
1882
|
+
}
|
|
1883
|
+
for (const mark of ["bold", "italic"]) {
|
|
1884
|
+
if (node[mark] !== void 0 && typeof node[mark] !== "boolean") {
|
|
1885
|
+
errors.push(issue(joinPath(path, mark), "invalid_type", "must be true or false"));
|
|
1886
|
+
}
|
|
1887
|
+
}
|
|
1888
|
+
const out = { type: "text", value: node.value };
|
|
1889
|
+
if (node.bold === true) out.bold = true;
|
|
1890
|
+
if (node.italic === true) out.italic = true;
|
|
1891
|
+
return out;
|
|
1892
|
+
}
|
|
1893
|
+
function richInline(node, path, errors) {
|
|
1894
|
+
if (!isPlainObject(node)) {
|
|
1895
|
+
errors.push(issue(path, "invalid_node", "must be a text or link node"));
|
|
1896
|
+
return null;
|
|
1897
|
+
}
|
|
1898
|
+
if (node.type === "text") return richText(node, path, errors);
|
|
1899
|
+
if (node.type === "link") {
|
|
1900
|
+
errors.push(...unknownKeys(node, ["type", "url", "title", "children"], path));
|
|
1901
|
+
const url = typeof node.url === "string" ? node.url : "";
|
|
1902
|
+
if (!parseHttpUrl(url) && !(url.length <= MAX_URL_LENGTH && MAILTO.test(url) && !UNSAFE_URL_CHARS.test(url))) {
|
|
1903
|
+
errors.push(issue(joinPath(path, "url"), "unsafe_url", "must be an http(s) or mailto link"));
|
|
1904
|
+
}
|
|
1905
|
+
if (node.title !== void 0 && typeof node.title !== "string") {
|
|
1906
|
+
errors.push(issue(joinPath(path, "title"), "invalid_type", "must be text"));
|
|
1907
|
+
}
|
|
1908
|
+
const children = richChildren(node.children, joinPath(path, "children"), errors, (child, at) => {
|
|
1909
|
+
if (isPlainObject(child) && child.type === "text") return richText(child, at, errors);
|
|
1910
|
+
errors.push(issue(joinPath(at, "type"), "invalid_node", "a link may only contain text"));
|
|
1911
|
+
return null;
|
|
1912
|
+
});
|
|
1913
|
+
const out = { type: "link", url, children };
|
|
1914
|
+
if (typeof node.title === "string") out.title = node.title;
|
|
1915
|
+
return out;
|
|
1916
|
+
}
|
|
1917
|
+
errors.push(
|
|
1918
|
+
issue(joinPath(path, "type"), "invalid_node", `unexpected node type ${JSON.stringify(String(node.type).slice(0, 40))}`)
|
|
1919
|
+
);
|
|
1920
|
+
return null;
|
|
1921
|
+
}
|
|
1922
|
+
function richBlock(node, path, errors) {
|
|
1923
|
+
if (!isPlainObject(node)) {
|
|
1924
|
+
errors.push(issue(path, "invalid_node", "must be a paragraph, heading or list node"));
|
|
1925
|
+
return null;
|
|
1926
|
+
}
|
|
1927
|
+
const inline = (child, at) => richInline(child, at, errors);
|
|
1928
|
+
const children = (at, from) => richChildren(from, joinPath(at, "children"), errors, inline);
|
|
1929
|
+
switch (node.type) {
|
|
1930
|
+
case "paragraph":
|
|
1931
|
+
errors.push(...unknownKeys(node, ["type", "children"], path));
|
|
1932
|
+
return { type: "paragraph", children: children(path, node.children) };
|
|
1933
|
+
case "heading": {
|
|
1934
|
+
errors.push(...unknownKeys(node, ["type", "level", "children"], path));
|
|
1935
|
+
const level = node.level;
|
|
1936
|
+
if (typeof level !== "number" || !Number.isInteger(level) || level < 1 || level > 6) {
|
|
1937
|
+
errors.push(issue(joinPath(path, "level"), "invalid_format", "must be 1 to 6"));
|
|
1938
|
+
}
|
|
1939
|
+
return { type: "heading", level, children: children(path, node.children) };
|
|
1940
|
+
}
|
|
1941
|
+
case "list": {
|
|
1942
|
+
errors.push(...unknownKeys(node, ["type", "listType", "children"], path));
|
|
1943
|
+
if (node.listType !== "ordered" && node.listType !== "unordered") {
|
|
1944
|
+
errors.push(issue(joinPath(path, "listType"), "invalid_format", "must be ordered or unordered"));
|
|
1945
|
+
}
|
|
1946
|
+
const items = richChildren(node.children, joinPath(path, "children"), errors, (child, at) => {
|
|
1947
|
+
if (!isPlainObject(child) || child.type !== "list-item") {
|
|
1948
|
+
errors.push(issue(at, "invalid_node", "must be a list-item node"));
|
|
1949
|
+
return null;
|
|
1950
|
+
}
|
|
1951
|
+
errors.push(...unknownKeys(child, ["type", "children"], at));
|
|
1952
|
+
return { type: "list-item", children: children(at, child.children) };
|
|
1953
|
+
});
|
|
1954
|
+
return { type: "list", listType: node.listType, children: items };
|
|
1955
|
+
}
|
|
1956
|
+
default:
|
|
1957
|
+
errors.push(
|
|
1958
|
+
issue(joinPath(path, "type"), "invalid_node", `unexpected node type ${JSON.stringify(String(node.type).slice(0, 40))}`)
|
|
1959
|
+
);
|
|
1960
|
+
return null;
|
|
1961
|
+
}
|
|
1962
|
+
}
|
|
1963
|
+
var MAX_RICH_TOP_NODES = 2e3;
|
|
1964
|
+
function validateRichText(input, path = "") {
|
|
1965
|
+
if (!isPlainObject(input) || input.type !== "root") {
|
|
1966
|
+
return fail(path, "invalid_node", "must be a rich-text root node");
|
|
1967
|
+
}
|
|
1968
|
+
if (Array.isArray(input.children) && input.children.length > MAX_RICH_TOP_NODES) {
|
|
1969
|
+
return fail(path, "too_large", "has too many nodes");
|
|
1970
|
+
}
|
|
1971
|
+
const errors = unknownKeys(input, ["type", "children"], path);
|
|
1972
|
+
const children = richChildren(
|
|
1973
|
+
input.children,
|
|
1974
|
+
joinPath(path, "children"),
|
|
1975
|
+
errors,
|
|
1976
|
+
(child, at) => richBlock(child, at, errors)
|
|
1977
|
+
);
|
|
1978
|
+
return errors.length ? { ok: false, errors: errors.slice(0, MAX_RICH_ERRORS) } : ok({ type: "root", children });
|
|
1979
|
+
}
|
|
1980
|
+
function richTextLength(root) {
|
|
1981
|
+
let count = 0;
|
|
1982
|
+
const visit = (node) => {
|
|
1983
|
+
if (node.type === "text") count += codePoints(node.value);
|
|
1984
|
+
else node.children.forEach(visit);
|
|
1985
|
+
};
|
|
1986
|
+
for (const block of root.children) {
|
|
1987
|
+
if (block.type === "list") block.children.forEach((item) => item.children.forEach(visit));
|
|
1988
|
+
else block.children.forEach(visit);
|
|
1989
|
+
}
|
|
1990
|
+
return count;
|
|
1991
|
+
}
|
|
1992
|
+
var checkRichText = (value, v, _ctx, path) => {
|
|
1993
|
+
const tree = validateRichText(value, path);
|
|
1994
|
+
if (!tree.ok) return tree;
|
|
1995
|
+
if (v.max_length !== void 0 && richTextLength(tree.value) > v.max_length) {
|
|
1996
|
+
return fail(path, "too_long", `must be at most ${v.max_length} characters`);
|
|
1997
|
+
}
|
|
1998
|
+
return tree;
|
|
1999
|
+
};
|
|
2000
|
+
var MEASUREMENT_UNITS = {
|
|
2001
|
+
weight: { g: 1, kg: 1e3, oz: 28.349523125, lb: 453.59237 },
|
|
2002
|
+
volume: {
|
|
2003
|
+
ml: 1,
|
|
2004
|
+
cl: 10,
|
|
2005
|
+
l: 1e3,
|
|
2006
|
+
m3: 1e6,
|
|
2007
|
+
fl_oz: 29.5735295625,
|
|
2008
|
+
pt: 473.176473,
|
|
2009
|
+
qt: 946.352946,
|
|
2010
|
+
gal: 3785.411784
|
|
2011
|
+
},
|
|
2012
|
+
dimension: { mm: 1, cm: 10, m: 1e3, in: 25.4, ft: 304.8, yd: 914.4 }
|
|
2013
|
+
};
|
|
2014
|
+
function measurementInBaseUnit(measurement, type) {
|
|
2015
|
+
const units = MEASUREMENT_UNITS[type];
|
|
2016
|
+
return hasOwn(units, measurement.unit) ? Number(measurement.value) * units[measurement.unit] : null;
|
|
2017
|
+
}
|
|
2018
|
+
function measurementChecker(type) {
|
|
2019
|
+
const unitList = Object.keys(MEASUREMENT_UNITS[type]).join(", ");
|
|
2020
|
+
return (value, v, _ctx, path) => {
|
|
2021
|
+
if (!isPlainObject(value)) return fail(path, "invalid_type", "must be an object with value and unit");
|
|
2022
|
+
const extra = unknownKeys(value, ["value", "unit"], path);
|
|
2023
|
+
if (extra.length) return { ok: false, errors: extra };
|
|
2024
|
+
const amount = normalizeDecimal(value.value);
|
|
2025
|
+
if (amount === null) return fail(joinPath(path, "value"), "invalid_type", "must be a decimal number");
|
|
2026
|
+
if (typeof value.unit !== "string" || !hasOwn(MEASUREMENT_UNITS[type], value.unit)) {
|
|
2027
|
+
return fail(joinPath(path, "unit"), "invalid_unit", `must be one of: ${unitList}`);
|
|
2028
|
+
}
|
|
2029
|
+
const measurement = { value: amount, unit: value.unit };
|
|
2030
|
+
const base2 = measurementInBaseUnit(measurement, type);
|
|
2031
|
+
if (typeof v.min === "object" && v.min !== null) {
|
|
2032
|
+
const min = measurementInBaseUnit(v.min, type);
|
|
2033
|
+
if (min !== null && base2 < min) {
|
|
2034
|
+
return fail(path, "too_small", `must be at least ${v.min.value} ${v.min.unit}`);
|
|
2035
|
+
}
|
|
2036
|
+
}
|
|
2037
|
+
if (typeof v.max === "object" && v.max !== null) {
|
|
2038
|
+
const max = measurementInBaseUnit(v.max, type);
|
|
2039
|
+
if (max !== null && base2 > max) {
|
|
2040
|
+
return fail(path, "too_big", `must be at most ${v.max.value} ${v.max.unit}`);
|
|
2041
|
+
}
|
|
2042
|
+
}
|
|
2043
|
+
return ok(measurement);
|
|
2044
|
+
};
|
|
2045
|
+
}
|
|
2046
|
+
var checkRating = (value, v, _ctx, path) => {
|
|
2047
|
+
if (!isPlainObject(value)) {
|
|
2048
|
+
return fail(path, "invalid_type", "must be an object with value, scale_min and scale_max");
|
|
2049
|
+
}
|
|
2050
|
+
const extra = unknownKeys(value, ["value", "scale_min", "scale_max"], path);
|
|
2051
|
+
if (extra.length) return { ok: false, errors: extra };
|
|
2052
|
+
const parts = { value: "", scale_min: "", scale_max: "" };
|
|
2053
|
+
for (const key of ["value", "scale_min", "scale_max"]) {
|
|
2054
|
+
const decimal = normalizeDecimal(value[key]);
|
|
2055
|
+
if (decimal === null) return fail(joinPath(path, key), "invalid_type", "must be a decimal number");
|
|
2056
|
+
parts[key] = decimal;
|
|
2057
|
+
}
|
|
2058
|
+
if (v.scale_min !== void 0 && compareDecimal(parts.scale_min, v.scale_min) !== 0 || v.scale_max !== void 0 && compareDecimal(parts.scale_max, v.scale_max) !== 0) {
|
|
2059
|
+
return fail(path, "scale_mismatch", `must use the scale ${v.scale_min} to ${v.scale_max}`);
|
|
2060
|
+
}
|
|
2061
|
+
if (compareDecimal(parts.value, parts.scale_min) < 0) {
|
|
2062
|
+
return fail(joinPath(path, "value"), "too_small", `must be at least ${parts.scale_min}`);
|
|
2063
|
+
}
|
|
2064
|
+
if (compareDecimal(parts.value, parts.scale_max) > 0) {
|
|
2065
|
+
return fail(joinPath(path, "value"), "too_big", `must be at most ${parts.scale_max}`);
|
|
2066
|
+
}
|
|
2067
|
+
return ok(parts);
|
|
2068
|
+
};
|
|
2069
|
+
var checkMoney = (value, _v, ctx, path) => {
|
|
2070
|
+
if (!isPlainObject(value)) return fail(path, "invalid_type", "must be an object with amount and currency_code");
|
|
2071
|
+
const extra = unknownKeys(value, ["amount", "currency_code"], path);
|
|
2072
|
+
if (extra.length) return { ok: false, errors: extra };
|
|
2073
|
+
const amount = normalizeDecimal(value.amount);
|
|
2074
|
+
if (amount === null) return fail(joinPath(path, "amount"), "invalid_type", "must be a decimal number");
|
|
2075
|
+
const currency = typeof value.currency_code === "string" ? value.currency_code.toUpperCase() : "";
|
|
2076
|
+
const expected = ctx.currency_code.toUpperCase();
|
|
2077
|
+
if (currency !== expected) {
|
|
2078
|
+
return fail(joinPath(path, "currency_code"), "currency_mismatch", `must be ${expected}`);
|
|
2079
|
+
}
|
|
2080
|
+
return ok({ amount, currency_code: currency });
|
|
2081
|
+
};
|
|
2082
|
+
var UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
2083
|
+
var checkAsset = (value, _v, _ctx, path) => typeof value === "string" && UUID.test(value) ? ok(value.toLowerCase()) : fail(path, "invalid_format", "must be an asset id");
|
|
2084
|
+
function allowedFileKinds(type, validations) {
|
|
2085
|
+
switch (baseTypeOf(type)) {
|
|
2086
|
+
case "image":
|
|
2087
|
+
return ["image"];
|
|
2088
|
+
case "video":
|
|
2089
|
+
return ["video"];
|
|
2090
|
+
case "file":
|
|
2091
|
+
return validations.file_types ?? null;
|
|
2092
|
+
default:
|
|
2093
|
+
return null;
|
|
2094
|
+
}
|
|
2095
|
+
}
|
|
2096
|
+
var ID = /^[1-9]\d{0,18}$/;
|
|
2097
|
+
var MAX_BIGINT = "9223372036854775807";
|
|
2098
|
+
function normalizeId(input) {
|
|
2099
|
+
if (typeof input === "number") {
|
|
2100
|
+
return Number.isSafeInteger(input) && input > 0 ? String(input) : null;
|
|
2101
|
+
}
|
|
2102
|
+
if (typeof input !== "string" || !ID.test(input)) return null;
|
|
2103
|
+
if (input.length === MAX_BIGINT.length && input > MAX_BIGINT) return null;
|
|
2104
|
+
return input;
|
|
2105
|
+
}
|
|
2106
|
+
var checkId = (value, _v, _ctx, path) => {
|
|
2107
|
+
const id = normalizeId(value);
|
|
2108
|
+
return id === null ? fail(path, "invalid_format", "must be a record id") : ok(id);
|
|
2109
|
+
};
|
|
2110
|
+
var CHECKERS = {
|
|
2111
|
+
single_line_text: textChecker(METAFIELD_LIMITS.single_line_text_length, true),
|
|
2112
|
+
multi_line_text: textChecker(METAFIELD_LIMITS.multi_line_text_length, false),
|
|
2113
|
+
rich_text: checkRichText,
|
|
2114
|
+
email: checkEmail,
|
|
2115
|
+
integer: checkInteger,
|
|
2116
|
+
decimal: checkDecimal,
|
|
2117
|
+
date: dateChecker(normalizeDate, "a date (YYYY-MM-DD)"),
|
|
2118
|
+
date_time: dateChecker(normalizeDateTime, "a date and time (YYYY-MM-DDTHH:mm:ssZ)"),
|
|
2119
|
+
url: checkUrl,
|
|
2120
|
+
link: checkLink,
|
|
2121
|
+
weight: measurementChecker("weight"),
|
|
2122
|
+
volume: measurementChecker("volume"),
|
|
2123
|
+
dimension: measurementChecker("dimension"),
|
|
2124
|
+
boolean: checkBoolean,
|
|
2125
|
+
color: checkColor,
|
|
2126
|
+
rating: checkRating,
|
|
2127
|
+
money: checkMoney,
|
|
2128
|
+
language: checkLanguage,
|
|
2129
|
+
json: checkJson,
|
|
2130
|
+
file: checkAsset,
|
|
2131
|
+
image: checkAsset,
|
|
2132
|
+
video: checkAsset,
|
|
2133
|
+
product: checkId,
|
|
2134
|
+
variant: checkId,
|
|
2135
|
+
collection: checkId,
|
|
2136
|
+
page: checkId,
|
|
2137
|
+
blog_post: checkId,
|
|
2138
|
+
customer: checkId,
|
|
2139
|
+
order: checkId,
|
|
2140
|
+
metaobject_reference: checkId,
|
|
2141
|
+
mixed_reference: checkId
|
|
2142
|
+
};
|
|
2143
|
+
function isBlankValue(value) {
|
|
2144
|
+
return value === null || value === void 0 || value === "" || Array.isArray(value) && value.length === 0;
|
|
2145
|
+
}
|
|
2146
|
+
var TEXT_BASE_TYPES = ["single_line_text", "multi_line_text", "email", "url"];
|
|
2147
|
+
function isBlankScalar(base2, value) {
|
|
2148
|
+
if (isBlankValue(value)) return true;
|
|
2149
|
+
if (typeof value === "string") return TEXT_BASE_TYPES.includes(base2) && value.trim() === "";
|
|
2150
|
+
return base2 === "rich_text" && isPlainObject(value) && value.type === "root" && Array.isArray(value.children) && value.children.length === 0;
|
|
2151
|
+
}
|
|
2152
|
+
function isBlankValueFor(type, value) {
|
|
2153
|
+
if (!isMetafieldType(type)) return isBlankValue(value);
|
|
2154
|
+
if (isListType(type)) return isBlankValue(value);
|
|
2155
|
+
return isBlankScalar(baseTypeOf(type), value);
|
|
2156
|
+
}
|
|
2157
|
+
function checkList(base2, checker, value, v, ctx, path) {
|
|
2158
|
+
if (!Array.isArray(value)) return fail(path, "invalid_type", "must be a list");
|
|
2159
|
+
const max = Math.min(METAFIELD_LIMITS.list_items, v.list_max ?? METAFIELD_LIMITS.list_items);
|
|
2160
|
+
if (value.length > max) return fail(path, "list_too_long", `must have at most ${max} items`);
|
|
2161
|
+
if (v.list_min !== void 0 && value.length < v.list_min) {
|
|
2162
|
+
return fail(path, "list_too_short", `must have at least ${v.list_min} items`);
|
|
2163
|
+
}
|
|
2164
|
+
const items = [];
|
|
2165
|
+
const errors = [];
|
|
2166
|
+
for (let index = 0; index < value.length; index++) {
|
|
2167
|
+
const item = value[index];
|
|
2168
|
+
const at = joinPath(path, index);
|
|
2169
|
+
const result = isBlankScalar(base2, item) ? fail(at, "blank", "is empty") : checker(item, v, ctx, at);
|
|
2170
|
+
if (result.ok) items.push(result.value);
|
|
2171
|
+
else errors.push(...result.errors);
|
|
2172
|
+
}
|
|
2173
|
+
return errors.length ? { ok: false, errors } : ok(items);
|
|
2174
|
+
}
|
|
2175
|
+
function isStorableJson(json) {
|
|
2176
|
+
for (const match of json.matchAll(/\\(?:u([0-9a-f]{4})|[^u])/g)) {
|
|
2177
|
+
const code = match[1] === void 0 ? -1 : parseInt(match[1], 16);
|
|
2178
|
+
if (code === 0 || code >= 55296 && code <= 57343) return false;
|
|
2179
|
+
}
|
|
2180
|
+
return true;
|
|
2181
|
+
}
|
|
2182
|
+
function byteLength(text) {
|
|
2183
|
+
return new TextEncoder().encode(text).length;
|
|
2184
|
+
}
|
|
2185
|
+
function validateMetafieldValue(type, validations, value, ctx, path = "") {
|
|
2186
|
+
if (!isMetafieldType(type)) return fail(path, "invalid_type", "is not a metafield type");
|
|
2187
|
+
if (!isPlainObject(validations)) validations = {};
|
|
2188
|
+
if (isBlankValueFor(type, value)) return fail(path, "blank", "is empty; send null to clear it");
|
|
2189
|
+
if (isListType(type) && Array.isArray(value) && value.length > 4 * METAFIELD_LIMITS.list_items) {
|
|
2190
|
+
return fail(path, "list_too_long", `must have at most ${METAFIELD_LIMITS.list_items} items`);
|
|
2191
|
+
}
|
|
2192
|
+
const base2 = baseTypeOf(type);
|
|
2193
|
+
const checker = hasOwn(CHECKERS, base2) ? CHECKERS[base2] : void 0;
|
|
2194
|
+
if (!checker) return fail(path, "invalid_type", `type ${String(type).slice(0, 40)} is not supported`);
|
|
2195
|
+
const result = isListType(type) ? checkList(base2, checker, value, validations, ctx, path) : checker(value, validations, ctx, path);
|
|
2196
|
+
if (!result.ok) return result;
|
|
2197
|
+
const json = JSON.stringify(result.value);
|
|
2198
|
+
if (!isStorableJson(json)) {
|
|
2199
|
+
return fail(path, "invalid_format", "contains characters that cannot be stored");
|
|
2200
|
+
}
|
|
2201
|
+
if (byteLength(json) > METAFIELD_LIMITS.value_bytes) {
|
|
2202
|
+
return fail(path, "too_large", `must be at most ${METAFIELD_LIMITS.value_bytes} bytes`);
|
|
2203
|
+
}
|
|
2204
|
+
return result;
|
|
2205
|
+
}
|
|
2206
|
+
var VALIDATION_KEYS = {
|
|
2207
|
+
single_line_text: ["min_length", "max_length", "regex", "choices"],
|
|
2208
|
+
multi_line_text: ["min_length", "max_length"],
|
|
2209
|
+
rich_text: ["max_length"],
|
|
2210
|
+
email: [],
|
|
2211
|
+
integer: ["min", "max"],
|
|
2212
|
+
decimal: ["min", "max", "max_precision"],
|
|
2213
|
+
date: ["min", "max"],
|
|
2214
|
+
date_time: ["min", "max"],
|
|
2215
|
+
url: ["allowed_domains"],
|
|
2216
|
+
link: ["allowed_domains"],
|
|
2217
|
+
weight: ["min", "max"],
|
|
2218
|
+
volume: ["min", "max"],
|
|
2219
|
+
dimension: ["min", "max"],
|
|
2220
|
+
boolean: [],
|
|
2221
|
+
color: [],
|
|
2222
|
+
rating: ["scale_min", "scale_max"],
|
|
2223
|
+
money: [],
|
|
2224
|
+
language: [],
|
|
2225
|
+
json: [],
|
|
2226
|
+
file: ["file_types"],
|
|
2227
|
+
image: [],
|
|
2228
|
+
video: [],
|
|
2229
|
+
product: [],
|
|
2230
|
+
variant: [],
|
|
2231
|
+
collection: [],
|
|
2232
|
+
page: [],
|
|
2233
|
+
blog_post: [],
|
|
2234
|
+
customer: [],
|
|
2235
|
+
order: [],
|
|
2236
|
+
metaobject_reference: ["metaobject_definition_id"],
|
|
2237
|
+
mixed_reference: ["metaobject_definition_ids"]
|
|
2238
|
+
};
|
|
2239
|
+
function isSafeRegex(pattern) {
|
|
2240
|
+
if (/\\[1-9]|\\k</.test(pattern)) return false;
|
|
2241
|
+
if (/\(\?<?[=!]/.test(pattern)) return false;
|
|
2242
|
+
let quantifiers = 0;
|
|
2243
|
+
let bars = 0;
|
|
2244
|
+
let afterQuantifier = false;
|
|
2245
|
+
for (let i = 0; i < pattern.length; i++) {
|
|
2246
|
+
const char = pattern[i];
|
|
2247
|
+
const wasQuantifier = afterQuantifier;
|
|
2248
|
+
afterQuantifier = false;
|
|
2249
|
+
if (char === "\\") {
|
|
2250
|
+
const escaped = pattern[i + 1];
|
|
2251
|
+
i++;
|
|
2252
|
+
if ((escaped === "u" || escaped === "p" || escaped === "P") && pattern[i + 1] === "{") {
|
|
2253
|
+
const close = pattern.indexOf("}", i + 1);
|
|
2254
|
+
i = close === -1 ? pattern.length : close;
|
|
2255
|
+
}
|
|
2256
|
+
continue;
|
|
2257
|
+
}
|
|
2258
|
+
if (char === "[") {
|
|
2259
|
+
i++;
|
|
2260
|
+
while (i < pattern.length && pattern[i] !== "]") {
|
|
2261
|
+
if (pattern[i] === "\\") i++;
|
|
2262
|
+
i++;
|
|
2263
|
+
}
|
|
2264
|
+
continue;
|
|
2265
|
+
}
|
|
2266
|
+
if (char === "(") {
|
|
2267
|
+
if (pattern[i + 1] === "?") i++;
|
|
2268
|
+
continue;
|
|
2269
|
+
}
|
|
2270
|
+
if (char === ")") {
|
|
2271
|
+
const next = pattern[i + 1];
|
|
2272
|
+
if (next === "*" || next === "+") return false;
|
|
2273
|
+
if (next === "{" && /^\{\d+(,\d*)?\}/.test(pattern.slice(i + 1))) return false;
|
|
2274
|
+
continue;
|
|
2275
|
+
}
|
|
2276
|
+
if (char === "|") {
|
|
2277
|
+
bars++;
|
|
2278
|
+
} else if (char === "*" || char === "+" || char === "?") {
|
|
2279
|
+
if (!(char === "?" && wasQuantifier)) {
|
|
2280
|
+
quantifiers++;
|
|
2281
|
+
afterQuantifier = true;
|
|
2282
|
+
}
|
|
2283
|
+
} else if (char === "{") {
|
|
2284
|
+
const match = /^\{\d+(,\d*)?\}/.exec(pattern.slice(i));
|
|
2285
|
+
if (match) {
|
|
2286
|
+
if (match[1] !== void 0) quantifiers++;
|
|
2287
|
+
afterQuantifier = true;
|
|
2288
|
+
i += match[0].length - 1;
|
|
2289
|
+
}
|
|
2290
|
+
}
|
|
2291
|
+
}
|
|
2292
|
+
return quantifiers <= 2 && bars <= 4;
|
|
2293
|
+
}
|
|
2294
|
+
var HOSTNAME = /^(?=.{1,253}$)(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z]{2,63}$/;
|
|
2295
|
+
var FILE_KINDS = ["image", "video", "document"];
|
|
2296
|
+
function normalizeBound(base2, input) {
|
|
2297
|
+
switch (base2) {
|
|
2298
|
+
case "integer":
|
|
2299
|
+
return typeof input === "number" && Number.isSafeInteger(input) ? input : null;
|
|
2300
|
+
case "decimal":
|
|
2301
|
+
return normalizeDecimal(input);
|
|
2302
|
+
case "date":
|
|
2303
|
+
return normalizeDate(input);
|
|
2304
|
+
case "date_time":
|
|
2305
|
+
return normalizeDateTime(input);
|
|
2306
|
+
case "weight":
|
|
2307
|
+
case "volume":
|
|
2308
|
+
case "dimension": {
|
|
2309
|
+
if (!isPlainObject(input) || Object.keys(input).some((k) => k !== "value" && k !== "unit")) return null;
|
|
2310
|
+
const amount = normalizeDecimal(input.value);
|
|
2311
|
+
if (amount === null || typeof input.unit !== "string" || !hasOwn(MEASUREMENT_UNITS[base2], input.unit)) {
|
|
2312
|
+
return null;
|
|
2313
|
+
}
|
|
2314
|
+
return { value: amount, unit: input.unit };
|
|
2315
|
+
}
|
|
2316
|
+
default:
|
|
2317
|
+
return null;
|
|
2318
|
+
}
|
|
2319
|
+
}
|
|
2320
|
+
function compareBound(base2, a, b) {
|
|
2321
|
+
if (typeof a === "number" && typeof b === "number") return a - b;
|
|
2322
|
+
if (typeof a === "object" && typeof b === "object") {
|
|
2323
|
+
const type = base2;
|
|
2324
|
+
return (measurementInBaseUnit(a, type) ?? 0) - (measurementInBaseUnit(b, type) ?? 0);
|
|
2325
|
+
}
|
|
2326
|
+
if (base2 === "decimal") return compareDecimal(a, b);
|
|
2327
|
+
return a < b ? -1 : a > b ? 1 : 0;
|
|
2328
|
+
}
|
|
2329
|
+
function isWholeNumber(value, min, max) {
|
|
2330
|
+
return typeof value === "number" && Number.isInteger(value) && value >= min && value <= max;
|
|
2331
|
+
}
|
|
2332
|
+
function validateValidations(type, input, path = "validations") {
|
|
2333
|
+
if (!isMetafieldType(type)) return fail(path, "invalid_type", "is not a metafield type");
|
|
2334
|
+
const raw = input === void 0 || input === null ? {} : input;
|
|
2335
|
+
if (!isPlainObject(raw)) return fail(path, "invalid_type", "must be an object");
|
|
2336
|
+
const base2 = baseTypeOf(type);
|
|
2337
|
+
const allowed = [
|
|
2338
|
+
...VALIDATION_KEYS[base2],
|
|
2339
|
+
...isListType(type) ? ["list_min", "list_max"] : []
|
|
2340
|
+
];
|
|
2341
|
+
const errors = unknownKeys(raw, allowed, path);
|
|
2342
|
+
const out = {};
|
|
2343
|
+
const has = (key) => allowed.includes(key) && raw[key] !== void 0;
|
|
2344
|
+
const bad = (key, message, code = "invalid_validation") => errors.push(issue(joinPath(path, key), code, message));
|
|
2345
|
+
for (const key of ["min_length", "max_length"]) {
|
|
2346
|
+
if (!has(key)) continue;
|
|
2347
|
+
const cap = base2 === "single_line_text" ? METAFIELD_LIMITS.single_line_text_length : METAFIELD_LIMITS.multi_line_text_length;
|
|
2348
|
+
if (isWholeNumber(raw[key], 0, cap)) out[key] = raw[key];
|
|
2349
|
+
else bad(key, "must be a whole number of 0 or more");
|
|
2350
|
+
}
|
|
2351
|
+
if (has("regex")) {
|
|
2352
|
+
const pattern = raw.regex;
|
|
2353
|
+
if (typeof pattern !== "string" || pattern.length === 0 || pattern.length > METAFIELD_LIMITS.regex_length) {
|
|
2354
|
+
bad("regex", `must be 1 to ${METAFIELD_LIMITS.regex_length} characters`);
|
|
2355
|
+
} else if (!isSafeRegex(pattern)) {
|
|
2356
|
+
bad("regex", "is too complex: no backreferences or lookaround, no repetition after a group, at most 2 optional or repeated parts and 4 alternatives", "unsafe_regex");
|
|
2357
|
+
} else {
|
|
2358
|
+
try {
|
|
2359
|
+
new RegExp(pattern, "u");
|
|
2360
|
+
out.regex = pattern;
|
|
2361
|
+
} catch {
|
|
2362
|
+
bad("regex", "is not a valid pattern");
|
|
2363
|
+
}
|
|
2364
|
+
}
|
|
2365
|
+
}
|
|
2366
|
+
if (has("regex")) {
|
|
2367
|
+
const limit = METAFIELD_LIMITS.regex_value_length;
|
|
2368
|
+
if (out.max_length !== void 0 && out.max_length > limit) {
|
|
2369
|
+
bad("max_length", `must be at most ${limit} when a pattern applies`);
|
|
2370
|
+
} else if (out.regex !== void 0 && out.max_length === void 0) {
|
|
2371
|
+
out.max_length = limit;
|
|
2372
|
+
}
|
|
2373
|
+
}
|
|
2374
|
+
if (out.min_length !== void 0 && out.max_length !== void 0 && out.min_length > out.max_length) {
|
|
2375
|
+
bad("min_length", "must not exceed max_length");
|
|
2376
|
+
}
|
|
2377
|
+
if (has("choices")) {
|
|
2378
|
+
const choices = raw.choices;
|
|
2379
|
+
if (!Array.isArray(choices) || choices.length === 0 || choices.length > METAFIELD_LIMITS.choices) {
|
|
2380
|
+
bad("choices", `must list 1 to ${METAFIELD_LIMITS.choices} values`);
|
|
2381
|
+
} else if (choices.some(
|
|
2382
|
+
(c) => typeof c !== "string" || c === "" || /[\r\n]/.test(c) || codePoints(c) > METAFIELD_LIMITS.single_line_text_length
|
|
2383
|
+
)) {
|
|
2384
|
+
bad("choices", "must be non-empty single-line text");
|
|
2385
|
+
} else if (new Set(choices).size !== choices.length) {
|
|
2386
|
+
bad("choices", "must not repeat a value");
|
|
2387
|
+
} else if (choices.some((c) => {
|
|
2388
|
+
const length = codePoints(c);
|
|
2389
|
+
if (length > METAFIELD_LIMITS.regex_value_length && out.regex !== void 0) return true;
|
|
2390
|
+
if (out.max_length !== void 0 && length > out.max_length) return true;
|
|
2391
|
+
if (out.min_length !== void 0 && length < out.min_length) return true;
|
|
2392
|
+
return out.regex !== void 0 && !matchesPattern(out.regex, c);
|
|
2393
|
+
})) {
|
|
2394
|
+
bad("choices", "includes a value the pattern or length rules reject");
|
|
2395
|
+
} else {
|
|
2396
|
+
out.choices = [...choices];
|
|
2397
|
+
}
|
|
2398
|
+
}
|
|
2399
|
+
for (const key of ["min", "max"]) {
|
|
2400
|
+
if (!has(key)) continue;
|
|
2401
|
+
const bound = normalizeBound(base2, raw[key]);
|
|
2402
|
+
if (bound === null) bad(key, `is not a valid ${base2} bound`);
|
|
2403
|
+
else out[key] = bound;
|
|
2404
|
+
}
|
|
2405
|
+
if (out.min !== void 0 && out.max !== void 0 && compareBound(base2, out.min, out.max) > 0) {
|
|
2406
|
+
bad("min", "must not exceed max");
|
|
2407
|
+
}
|
|
2408
|
+
if (has("max_precision")) {
|
|
2409
|
+
if (isWholeNumber(raw.max_precision, 0, 9)) out.max_precision = raw.max_precision;
|
|
2410
|
+
else bad("max_precision", "must be a whole number from 0 to 9");
|
|
2411
|
+
}
|
|
2412
|
+
if (has("allowed_domains")) {
|
|
2413
|
+
const domains = raw.allowed_domains;
|
|
2414
|
+
const lowered = Array.isArray(domains) ? domains.map((d) => typeof d === "string" ? d.toLowerCase() : "") : [];
|
|
2415
|
+
if (!Array.isArray(domains) || domains.length === 0 || domains.length > METAFIELD_LIMITS.allowed_domains || lowered.some((d) => !HOSTNAME.test(d))) {
|
|
2416
|
+
bad("allowed_domains", `must list 1 to ${METAFIELD_LIMITS.allowed_domains} domains such as youtube.com`);
|
|
2417
|
+
} else {
|
|
2418
|
+
out.allowed_domains = [...new Set(lowered)];
|
|
2419
|
+
}
|
|
2420
|
+
}
|
|
2421
|
+
if (base2 === "rating") {
|
|
2422
|
+
for (const key of ["scale_min", "scale_max"]) {
|
|
2423
|
+
if (raw[key] === void 0) {
|
|
2424
|
+
bad(key, "is required for a rating", "required");
|
|
2425
|
+
continue;
|
|
2426
|
+
}
|
|
2427
|
+
const decimal = normalizeDecimal(raw[key]);
|
|
2428
|
+
if (decimal === null) bad(key, "must be a decimal number");
|
|
2429
|
+
else out[key] = decimal;
|
|
2430
|
+
}
|
|
2431
|
+
if (out.scale_min !== void 0 && out.scale_max !== void 0 && compareDecimal(out.scale_min, out.scale_max) >= 0) {
|
|
2432
|
+
bad("scale_min", "must be below scale_max");
|
|
2433
|
+
}
|
|
2434
|
+
}
|
|
2435
|
+
if (has("file_types")) {
|
|
2436
|
+
const kinds = raw.file_types;
|
|
2437
|
+
if (!Array.isArray(kinds) || kinds.length === 0 || kinds.some((k) => !FILE_KINDS.includes(k)) || new Set(kinds).size !== kinds.length) {
|
|
2438
|
+
bad("file_types", "must list image, video and/or document");
|
|
2439
|
+
} else {
|
|
2440
|
+
out.file_types = [...kinds];
|
|
2441
|
+
}
|
|
2442
|
+
}
|
|
2443
|
+
if (base2 === "metaobject_reference") {
|
|
2444
|
+
const id = normalizeId(raw.metaobject_definition_id);
|
|
2445
|
+
if (raw.metaobject_definition_id === void 0) {
|
|
2446
|
+
bad("metaobject_definition_id", "is required", "required");
|
|
2447
|
+
} else if (id === null) {
|
|
2448
|
+
bad("metaobject_definition_id", "must be a metaobject definition id");
|
|
2449
|
+
} else {
|
|
2450
|
+
out.metaobject_definition_id = id;
|
|
2451
|
+
}
|
|
2452
|
+
}
|
|
2453
|
+
if (base2 === "mixed_reference") {
|
|
2454
|
+
const ids = Array.isArray(raw.metaobject_definition_ids) ? raw.metaobject_definition_ids.map(normalizeId) : null;
|
|
2455
|
+
if (raw.metaobject_definition_ids === void 0) {
|
|
2456
|
+
bad("metaobject_definition_ids", "is required", "required");
|
|
2457
|
+
} else if (!ids || ids.length < METAFIELD_LIMITS.mixed_reference_definitions_min || ids.length > METAFIELD_LIMITS.mixed_reference_definitions_max || ids.some((id) => id === null) || new Set(ids).size !== ids.length) {
|
|
2458
|
+
bad(
|
|
2459
|
+
"metaobject_definition_ids",
|
|
2460
|
+
`must list ${METAFIELD_LIMITS.mixed_reference_definitions_min} to ${METAFIELD_LIMITS.mixed_reference_definitions_max} different definition ids`
|
|
2461
|
+
);
|
|
2462
|
+
} else {
|
|
2463
|
+
out.metaobject_definition_ids = ids;
|
|
2464
|
+
}
|
|
2465
|
+
}
|
|
2466
|
+
for (const key of ["list_min", "list_max"]) {
|
|
2467
|
+
if (!has(key)) continue;
|
|
2468
|
+
if (isWholeNumber(raw[key], 0, METAFIELD_LIMITS.list_items)) out[key] = raw[key];
|
|
2469
|
+
else bad(key, `must be a whole number from 0 to ${METAFIELD_LIMITS.list_items}`);
|
|
2470
|
+
}
|
|
2471
|
+
if (out.list_min !== void 0 && out.list_max !== void 0 && out.list_min > out.list_max) {
|
|
2472
|
+
bad("list_min", "must not exceed list_max");
|
|
2473
|
+
}
|
|
2474
|
+
return errors.length ? { ok: false, errors } : ok(out);
|
|
2475
|
+
}
|
|
2476
|
+
function storefrontAccessAllowed(owner) {
|
|
2477
|
+
return owner !== "customer" && owner !== "order" && owner !== "location";
|
|
2478
|
+
}
|
|
2479
|
+
function allowedCustomerAccess(owner) {
|
|
2480
|
+
if (owner === "customer") return ["none", "read", "read_write"];
|
|
2481
|
+
if (owner === "order") return ["none", "read"];
|
|
2482
|
+
return ["none"];
|
|
2483
|
+
}
|
|
2484
|
+
var METAFIELD_DEFINITION_KEYS = [
|
|
2485
|
+
"owner_type",
|
|
2486
|
+
"namespace",
|
|
2487
|
+
"key",
|
|
2488
|
+
"name",
|
|
2489
|
+
"description",
|
|
2490
|
+
"type",
|
|
2491
|
+
"validations",
|
|
2492
|
+
"storefront_access",
|
|
2493
|
+
"customer_access"
|
|
2494
|
+
];
|
|
2495
|
+
function readName(value, path, errors) {
|
|
2496
|
+
const name = typeof value === "string" ? value.trim() : "";
|
|
2497
|
+
if (!name || codePoints(name) > METAFIELD_LIMITS.name_length) {
|
|
2498
|
+
errors.push(issue(path, "invalid_format", `must be 1 to ${METAFIELD_LIMITS.name_length} characters`));
|
|
2499
|
+
}
|
|
2500
|
+
return name;
|
|
2501
|
+
}
|
|
2502
|
+
function readDescription(value, path, errors) {
|
|
2503
|
+
if (value === void 0 || value === null) return null;
|
|
2504
|
+
if (typeof value !== "string" || codePoints(value) > METAFIELD_LIMITS.description_length) {
|
|
2505
|
+
errors.push(issue(path, "invalid_format", `must be text of at most ${METAFIELD_LIMITS.description_length} characters`));
|
|
2506
|
+
return null;
|
|
2507
|
+
}
|
|
2508
|
+
return value.trim() || null;
|
|
2509
|
+
}
|
|
2510
|
+
function readKey(value, path, errors, pattern = METAFIELD_KEY_PATTERN) {
|
|
2511
|
+
if (typeof value !== "string" || !pattern.test(value)) {
|
|
2512
|
+
errors.push(issue(path, "invalid_format", "must be 2 to 64 lowercase letters, digits or underscores"));
|
|
2513
|
+
return "";
|
|
2514
|
+
}
|
|
2515
|
+
if (RESERVED_KEYS.includes(value)) {
|
|
2516
|
+
errors.push(issue(path, "invalid_format", "is a reserved name"));
|
|
2517
|
+
return "";
|
|
2518
|
+
}
|
|
2519
|
+
return value;
|
|
2520
|
+
}
|
|
2521
|
+
function readBoolean(value, fallback, path, errors) {
|
|
2522
|
+
if (value === void 0) return fallback;
|
|
2523
|
+
if (typeof value !== "boolean") {
|
|
2524
|
+
errors.push(issue(path, "invalid_type", "must be true or false"));
|
|
2525
|
+
return fallback;
|
|
2526
|
+
}
|
|
2527
|
+
return value;
|
|
2528
|
+
}
|
|
2529
|
+
function readTypeAndValidations(input, path, errors) {
|
|
2530
|
+
if (!isMetafieldType(input.type)) {
|
|
2531
|
+
errors.push(issue(joinPath(path, "type"), "invalid_format", "is not a metafield type"));
|
|
2532
|
+
return null;
|
|
2533
|
+
}
|
|
2534
|
+
const validations = validateValidations(input.type, input.validations, joinPath(path, "validations"));
|
|
2535
|
+
if (!validations.ok) {
|
|
2536
|
+
errors.push(...validations.errors);
|
|
2537
|
+
return null;
|
|
2538
|
+
}
|
|
2539
|
+
return { type: input.type, validations: validations.value };
|
|
2540
|
+
}
|
|
2541
|
+
function validateMetafieldDefinition(input) {
|
|
2542
|
+
if (!isPlainObject(input)) return fail("", "invalid_type", "must be an object");
|
|
2543
|
+
const errors = unknownKeys(input, METAFIELD_DEFINITION_KEYS, "");
|
|
2544
|
+
const owner = input.owner_type;
|
|
2545
|
+
if (!METAFIELD_OWNER_TYPES.includes(owner)) {
|
|
2546
|
+
errors.push(issue("owner_type", "invalid_format", `must be one of: ${METAFIELD_OWNER_TYPES.join(", ")}`));
|
|
2547
|
+
}
|
|
2548
|
+
const namespace = readKey(input.namespace ?? "custom", "namespace", errors);
|
|
2549
|
+
const key = readKey(input.key, "key", errors);
|
|
2550
|
+
const name = readName(input.name, "name", errors);
|
|
2551
|
+
const description = readDescription(input.description, "description", errors);
|
|
2552
|
+
const typed = readTypeAndValidations(input, "", errors);
|
|
2553
|
+
const storefront = readBoolean(input.storefront_access, false, "storefront_access", errors);
|
|
2554
|
+
if (storefront && METAFIELD_OWNER_TYPES.includes(owner) && !storefrontAccessAllowed(owner)) {
|
|
2555
|
+
errors.push(issue("storefront_access", "not_allowed", `is not available for ${owner} metafields`));
|
|
2556
|
+
}
|
|
2557
|
+
const customer = input.customer_access ?? "none";
|
|
2558
|
+
if (!allowedCustomerAccess(owner).includes(customer)) {
|
|
2559
|
+
errors.push(issue("customer_access", "not_allowed", `must be one of: ${allowedCustomerAccess(owner).join(", ")}`));
|
|
2560
|
+
}
|
|
2561
|
+
if (errors.length || !typed) return { ok: false, errors };
|
|
2562
|
+
const out = {
|
|
2563
|
+
owner_type: owner,
|
|
2564
|
+
namespace,
|
|
2565
|
+
key,
|
|
2566
|
+
name,
|
|
2567
|
+
description,
|
|
2568
|
+
type: typed.type,
|
|
2569
|
+
validations: typed.validations,
|
|
2570
|
+
storefront_access: storefront,
|
|
2571
|
+
customer_access: customer
|
|
2572
|
+
};
|
|
2573
|
+
return storable(out);
|
|
2574
|
+
}
|
|
2575
|
+
function storable(out) {
|
|
2576
|
+
if (!isStorableJson(JSON.stringify(out))) {
|
|
2577
|
+
return fail("", "invalid_format", "contains characters that cannot be stored");
|
|
2578
|
+
}
|
|
2579
|
+
return ok(out);
|
|
2580
|
+
}
|
|
2581
|
+
var FILTERABLE_TYPES = ["single_line_text", "boolean", "integer", "date"];
|
|
2582
|
+
var METAOBJECT_DEFINITION_KEYS = [
|
|
2583
|
+
"type",
|
|
2584
|
+
"name",
|
|
2585
|
+
"description",
|
|
2586
|
+
"fields",
|
|
2587
|
+
"display_name_key",
|
|
2588
|
+
"filter_keys",
|
|
2589
|
+
"has_status",
|
|
2590
|
+
"storefront_access",
|
|
2591
|
+
"customer_access"
|
|
2592
|
+
];
|
|
2593
|
+
var FIELD_KEYS = ["key", "name", "description", "type", "required", "validations"];
|
|
2594
|
+
function readField(input, path, errors) {
|
|
2595
|
+
if (!isPlainObject(input)) {
|
|
2596
|
+
errors.push(issue(path, "invalid_type", "must be an object"));
|
|
2597
|
+
return null;
|
|
2598
|
+
}
|
|
2599
|
+
errors.push(...unknownKeys(input, FIELD_KEYS, path));
|
|
2600
|
+
const key = readKey(input.key, joinPath(path, "key"), errors);
|
|
2601
|
+
const name = readName(input.name, joinPath(path, "name"), errors);
|
|
2602
|
+
const description = readDescription(input.description, joinPath(path, "description"), errors);
|
|
2603
|
+
const required = readBoolean(input.required, false, joinPath(path, "required"), errors);
|
|
2604
|
+
const typed = readTypeAndValidations(input, path, errors);
|
|
2605
|
+
return typed ? { key, name, description, type: typed.type, required, validations: typed.validations } : null;
|
|
2606
|
+
}
|
|
2607
|
+
function validateMetaobjectDefinition(input) {
|
|
2608
|
+
if (!isPlainObject(input)) return fail("", "invalid_type", "must be an object");
|
|
2609
|
+
const errors = unknownKeys(input, METAOBJECT_DEFINITION_KEYS, "");
|
|
2610
|
+
const type = typeof input.type === "string" && METAOBJECT_TYPE_PATTERN.test(input.type) ? input.type : "";
|
|
2611
|
+
if (!type) {
|
|
2612
|
+
errors.push(issue("type", "invalid_format", "must be 2 to 64 lowercase letters, digits, underscores or hyphens"));
|
|
2613
|
+
} else if (RESERVED_KEYS.includes(type)) {
|
|
2614
|
+
errors.push(issue("type", "invalid_format", "is a reserved name"));
|
|
2615
|
+
}
|
|
2616
|
+
const name = readName(input.name, "name", errors);
|
|
2617
|
+
const description = readDescription(input.description, "description", errors);
|
|
2618
|
+
const fields = [];
|
|
2619
|
+
if (!Array.isArray(input.fields)) {
|
|
2620
|
+
errors.push(issue("fields", "invalid_type", "must be a list of fields"));
|
|
2621
|
+
} else if (input.fields.length === 0) {
|
|
2622
|
+
errors.push(issue("fields", "too_short", "must have at least 1 field"));
|
|
2623
|
+
} else if (input.fields.length > METAFIELD_LIMITS.fields_per_metaobject) {
|
|
2624
|
+
errors.push(issue("fields", "too_long", `must have at most ${METAFIELD_LIMITS.fields_per_metaobject} fields`));
|
|
2625
|
+
} else {
|
|
2626
|
+
const seen = /* @__PURE__ */ new Set();
|
|
2627
|
+
input.fields.forEach((raw, index) => {
|
|
2628
|
+
const path = joinPath("fields", index);
|
|
2629
|
+
const field = readField(raw, path, errors);
|
|
2630
|
+
if (!field) return;
|
|
2631
|
+
if (seen.has(field.key)) errors.push(issue(joinPath(path, "key"), "duplicate", `"${field.key}" is used twice`));
|
|
2632
|
+
seen.add(field.key);
|
|
2633
|
+
fields.push(field);
|
|
2634
|
+
});
|
|
2635
|
+
}
|
|
2636
|
+
const byKey = new Map(fields.map((field) => [field.key, field]));
|
|
2637
|
+
const isSingleLine = (key) => byKey.get(key)?.type === "single_line_text";
|
|
2638
|
+
let displayNameKey;
|
|
2639
|
+
if (input.display_name_key === void 0) {
|
|
2640
|
+
displayNameKey = fields.find((field) => field.type === "single_line_text")?.key ?? null;
|
|
2641
|
+
} else if (input.display_name_key === null) {
|
|
2642
|
+
displayNameKey = null;
|
|
2643
|
+
} else if (typeof input.display_name_key === "string" && isSingleLine(input.display_name_key)) {
|
|
2644
|
+
displayNameKey = input.display_name_key;
|
|
2645
|
+
} else {
|
|
2646
|
+
displayNameKey = null;
|
|
2647
|
+
errors.push(issue("display_name_key", "invalid_format", "must name a single line text field"));
|
|
2648
|
+
}
|
|
2649
|
+
const filterKeys = [];
|
|
2650
|
+
const rawFilters = input.filter_keys ?? [];
|
|
2651
|
+
if (!Array.isArray(rawFilters)) {
|
|
2652
|
+
errors.push(issue("filter_keys", "invalid_type", "must be a list of field keys"));
|
|
2653
|
+
} else if (rawFilters.length > METAFIELD_LIMITS.filter_keys) {
|
|
2654
|
+
errors.push(issue("filter_keys", "too_long", `must have at most ${METAFIELD_LIMITS.filter_keys} keys`));
|
|
2655
|
+
} else {
|
|
2656
|
+
rawFilters.forEach((key, index) => {
|
|
2657
|
+
const path = joinPath("filter_keys", index);
|
|
2658
|
+
const field = typeof key === "string" ? byKey.get(key) : void 0;
|
|
2659
|
+
if (!field) errors.push(issue(path, "invalid_format", "must name a field of this definition"));
|
|
2660
|
+
else if (!FILTERABLE_TYPES.includes(field.type)) {
|
|
2661
|
+
errors.push(issue(path, "not_allowed", "only single line text, true or false, integer and date fields can be filters"));
|
|
2662
|
+
} else if (filterKeys.includes(field.key)) errors.push(issue(path, "duplicate", `"${field.key}" is listed twice`));
|
|
2663
|
+
else filterKeys.push(field.key);
|
|
2664
|
+
});
|
|
2665
|
+
}
|
|
2666
|
+
const hasStatus = readBoolean(input.has_status, true, "has_status", errors);
|
|
2667
|
+
const storefront = readBoolean(input.storefront_access, false, "storefront_access", errors);
|
|
2668
|
+
const customer = input.customer_access ?? "none";
|
|
2669
|
+
if (customer !== "none" && customer !== "read") {
|
|
2670
|
+
errors.push(issue("customer_access", "not_allowed", "must be none or read"));
|
|
2671
|
+
}
|
|
2672
|
+
if (errors.length) return { ok: false, errors };
|
|
2673
|
+
return storable({
|
|
2674
|
+
type,
|
|
2675
|
+
name,
|
|
2676
|
+
description,
|
|
2677
|
+
fields,
|
|
2678
|
+
display_name_key: displayNameKey,
|
|
2679
|
+
filter_keys: filterKeys,
|
|
2680
|
+
has_status: hasStatus,
|
|
2681
|
+
storefront_access: storefront,
|
|
2682
|
+
customer_access: customer
|
|
2683
|
+
});
|
|
2684
|
+
}
|
|
2685
|
+
function validateMetaobjectEntry(fields, input, ctx) {
|
|
2686
|
+
if (!Array.isArray(fields)) return fail("", "invalid_type", "fields must be a list");
|
|
2687
|
+
if (!isPlainObject(input)) return fail("", "invalid_type", "must be an object of field values");
|
|
2688
|
+
const errors = unknownKeys(input, fields.map((field) => field.key), "");
|
|
2689
|
+
const out = {};
|
|
2690
|
+
for (const field of fields) {
|
|
2691
|
+
const raw = hasOwn(input, field.key) ? input[field.key] : void 0;
|
|
2692
|
+
if (isBlankValueFor(field.type, raw)) {
|
|
2693
|
+
if (field.required) errors.push(issue(field.key, "required", "is required"));
|
|
2694
|
+
continue;
|
|
2695
|
+
}
|
|
2696
|
+
const result = validateMetafieldValue(field.type, field.validations, raw, ctx, field.key);
|
|
2697
|
+
if (result.ok) {
|
|
2698
|
+
Object.defineProperty(out, field.key, { value: result.value, enumerable: true, writable: true, configurable: true });
|
|
2699
|
+
} else errors.push(...result.errors);
|
|
2700
|
+
}
|
|
2701
|
+
if (!errors.length && byteLength(JSON.stringify(out)) > METAFIELD_LIMITS.value_bytes) {
|
|
2702
|
+
return fail("", "too_large", `must be at most ${METAFIELD_LIMITS.value_bytes} bytes`);
|
|
2703
|
+
}
|
|
2704
|
+
return errors.length ? { ok: false, errors } : ok(out);
|
|
2705
|
+
}
|
|
2706
|
+
function entryDisplayName(definition, fields, handle) {
|
|
2707
|
+
const value = definition.display_name_key ? fields[definition.display_name_key] : void 0;
|
|
2708
|
+
return typeof value === "string" && value.trim() ? [...value.trim()].slice(0, METAFIELD_LIMITS.name_length).join("") : handle;
|
|
2709
|
+
}
|
|
2710
|
+
function checkMetaobjectFieldChanges(before, after) {
|
|
2711
|
+
const prior = new Map(before.map((field) => [field.key, field]));
|
|
2712
|
+
const errors = after.flatMap((field, index) => {
|
|
2713
|
+
const old = prior.get(field.key);
|
|
2714
|
+
return old && old.type !== field.type ? [issue(joinPath(joinPath("fields", index), "type"), "immutable", `cannot change the type of "${field.key}"`)] : [];
|
|
2715
|
+
});
|
|
2716
|
+
if (errors.length) return { ok: false, errors };
|
|
2717
|
+
const kept = new Set(after.map((field) => field.key));
|
|
2718
|
+
return ok({
|
|
2719
|
+
removed: before.filter((field) => !kept.has(field.key)).map((field) => field.key),
|
|
2720
|
+
added: after.filter((field) => !prior.has(field.key)).map((field) => field.key)
|
|
2721
|
+
});
|
|
2722
|
+
}
|
|
2723
|
+
|
|
1466
2724
|
// src/modules/storage/image-transform.ts
|
|
1467
2725
|
var OBJECT_PREFIX = "/storage/v1/object/public/";
|
|
1468
2726
|
var RENDER_PREFIX = "/storage/v1/render/image/public/";
|
|
@@ -1872,6 +3130,42 @@ var ThemesApi = class {
|
|
|
1872
3130
|
}
|
|
1873
3131
|
};
|
|
1874
3132
|
|
|
3133
|
+
// src/modules/metaobjects/api.ts
|
|
3134
|
+
var EMPTY = { items: [], next_cursor: null };
|
|
3135
|
+
var MetaobjectsApi = class {
|
|
3136
|
+
constructor(publicHttp, authHttp) {
|
|
3137
|
+
this.publicHttp = publicHttp;
|
|
3138
|
+
this.authHttp = authHttp;
|
|
3139
|
+
}
|
|
3140
|
+
/**
|
|
3141
|
+
* Active entries of a metaobject type. A type that is unknown or hidden
|
|
3142
|
+
* from this reader lists as empty: the API answers 404 for both, the same
|
|
3143
|
+
* as a missing type, so it never reveals which types exist.
|
|
3144
|
+
*/
|
|
3145
|
+
async list(storeRef, type, params = {}) {
|
|
3146
|
+
const query = new URLSearchParams();
|
|
3147
|
+
for (const [key, value] of Object.entries(params.filter ?? {})) {
|
|
3148
|
+
query.append(`filter[${key}]`, String(value));
|
|
3149
|
+
}
|
|
3150
|
+
if (params.cursor) query.set("cursor", params.cursor);
|
|
3151
|
+
if (params.limit !== void 0) query.set("limit", String(params.limit));
|
|
3152
|
+
const search = query.toString();
|
|
3153
|
+
const http = params.as_customer ? this.authHttp : this.publicHttp;
|
|
3154
|
+
const res = await http.get(
|
|
3155
|
+
`/public/stores/${storeRef}/metaobjects/${encodeURIComponent(type)}${search ? `?${search}` : ""}`
|
|
3156
|
+
);
|
|
3157
|
+
return unwrapOrNull(res, [404]) ?? EMPTY;
|
|
3158
|
+
}
|
|
3159
|
+
/** One active entry by handle; null when it, or its type, is missing or hidden. */
|
|
3160
|
+
async get(storeRef, type, handle, options = {}) {
|
|
3161
|
+
const http = options.as_customer ? this.authHttp : this.publicHttp;
|
|
3162
|
+
const res = await http.get(
|
|
3163
|
+
`/public/stores/${storeRef}/metaobjects/${encodeURIComponent(type)}/${encodeURIComponent(handle)}`
|
|
3164
|
+
);
|
|
3165
|
+
return unwrapOrNull(res, [404]);
|
|
3166
|
+
}
|
|
3167
|
+
};
|
|
3168
|
+
|
|
1875
3169
|
// src/modules/search/api.ts
|
|
1876
3170
|
var SearchApi = class {
|
|
1877
3171
|
constructor(http) {
|
|
@@ -2063,6 +3357,24 @@ var CustomersApi = class {
|
|
|
2063
3357
|
payload
|
|
2064
3358
|
);
|
|
2065
3359
|
}
|
|
3360
|
+
/** The signed-in customer's metafields with customer access (spec §6.6). */
|
|
3361
|
+
async getMetafields(storeRef) {
|
|
3362
|
+
const res = await this.http.get(
|
|
3363
|
+
`/tenant/stores/${storeRef}/customers/metafields`
|
|
3364
|
+
);
|
|
3365
|
+
return unwrap(res);
|
|
3366
|
+
}
|
|
3367
|
+
/**
|
|
3368
|
+
* Writes read_write metafields; `value: null` clears one. Throws
|
|
3369
|
+
* METAFIELD_VALUE_INVALID (details.errors[] by path) or METAFIELD_NOT_WRITABLE.
|
|
3370
|
+
*/
|
|
3371
|
+
async updateMetafields(storeRef, values) {
|
|
3372
|
+
const res = await this.http.put(
|
|
3373
|
+
`/tenant/stores/${storeRef}/customers/metafields`,
|
|
3374
|
+
{ values }
|
|
3375
|
+
);
|
|
3376
|
+
return unwrap(res);
|
|
3377
|
+
}
|
|
2066
3378
|
};
|
|
2067
3379
|
|
|
2068
3380
|
// src/modules/addresses/api.ts
|
|
@@ -2518,6 +3830,7 @@ function createEcomwebSdk(options) {
|
|
|
2518
3830
|
blog: new BlogApi(publicHttp, authHttp),
|
|
2519
3831
|
stores: new StoresApi(publicHttp),
|
|
2520
3832
|
themes: new ThemesApi(publicHttp),
|
|
3833
|
+
metaobjects: new MetaobjectsApi(publicHttp, authHttp),
|
|
2521
3834
|
search: new SearchApi(publicHttp),
|
|
2522
3835
|
banners: new BannersApi(publicHttp),
|
|
2523
3836
|
shipping: new ShippingApi(publicHttp),
|
|
@@ -2556,6 +3869,8 @@ export {
|
|
|
2556
3869
|
DiscountMethodSchema,
|
|
2557
3870
|
EMAIL_COLOR_PATTERN,
|
|
2558
3871
|
EMAIL_PALETTE_DEFAULTS,
|
|
3872
|
+
ENTRY_HANDLE_PATTERN,
|
|
3873
|
+
FILTERABLE_TYPES,
|
|
2559
3874
|
INVENTORY_ADJUSTMENT_REASONS,
|
|
2560
3875
|
INVENTORY_ERROR_CODES,
|
|
2561
3876
|
INVENTORY_POLICIES,
|
|
@@ -2568,15 +3883,26 @@ export {
|
|
|
2568
3883
|
LEVEL_WARD,
|
|
2569
3884
|
MAX_RULES_PER_SET,
|
|
2570
3885
|
MAX_RULE_VALUE_ENTRIES,
|
|
3886
|
+
MEASUREMENT_UNITS,
|
|
3887
|
+
MEDIA_TYPES,
|
|
2571
3888
|
MERCHANT_ADJUSTMENT_REASONS,
|
|
3889
|
+
METAFIELD_BASE_TYPES,
|
|
3890
|
+
METAFIELD_KEY_PATTERN,
|
|
3891
|
+
METAFIELD_LIMITS,
|
|
3892
|
+
METAFIELD_OWNER_TYPES,
|
|
3893
|
+
METAFIELD_TYPE_GROUPS,
|
|
3894
|
+
METAOBJECT_REFERENCE_TYPES,
|
|
3895
|
+
METAOBJECT_TYPE_PATTERN,
|
|
2572
3896
|
MemoryTokenStorage,
|
|
2573
3897
|
MenusApi,
|
|
3898
|
+
MetaobjectsApi,
|
|
2574
3899
|
OBJECT_PREFIX,
|
|
2575
3900
|
ORDER_ACTOR_TYPES,
|
|
2576
3901
|
ORDER_EVENT_TYPES,
|
|
2577
3902
|
ORDER_SOURCES,
|
|
2578
3903
|
OrdersApi,
|
|
2579
3904
|
PAGINATION_DEFAULTS,
|
|
3905
|
+
PRIVATE_REFERENCE_TYPES,
|
|
2580
3906
|
PRODUCT_STATUS_LABELS,
|
|
2581
3907
|
PROMOTION_MODE_COLORS,
|
|
2582
3908
|
PROMOTION_MODE_LABELS,
|
|
@@ -2601,6 +3927,7 @@ export {
|
|
|
2601
3927
|
PromotionWithDiscountSchema,
|
|
2602
3928
|
PromotionsApi,
|
|
2603
3929
|
RENDER_PREFIX,
|
|
3930
|
+
RESOURCE_REFERENCE_TYPES,
|
|
2604
3931
|
RULE_FIELDS,
|
|
2605
3932
|
RedirectsApi,
|
|
2606
3933
|
ReviewsApi,
|
|
@@ -2614,6 +3941,9 @@ export {
|
|
|
2614
3941
|
ThemesApi,
|
|
2615
3942
|
UNAVAILABLE_STATES,
|
|
2616
3943
|
UpdatePromotionSchema,
|
|
3944
|
+
VALIDATION_KEYS,
|
|
3945
|
+
allowedCustomerAccess,
|
|
3946
|
+
allowedFileKinds,
|
|
2617
3947
|
allowedValuesFor,
|
|
2618
3948
|
appliedPromotionSchema,
|
|
2619
3949
|
assetBaseUrl,
|
|
@@ -2623,6 +3953,7 @@ export {
|
|
|
2623
3953
|
baseDeleteOptionsSchema,
|
|
2624
3954
|
baseDeleteResultSchema,
|
|
2625
3955
|
baseQueryParamsSchema,
|
|
3956
|
+
baseTypeOf,
|
|
2626
3957
|
blogCategorySchema,
|
|
2627
3958
|
blogCommentSchema,
|
|
2628
3959
|
blogCommentWithRepliesSchema,
|
|
@@ -2637,7 +3968,9 @@ export {
|
|
|
2637
3968
|
categorySchema,
|
|
2638
3969
|
categoryTreeSchema,
|
|
2639
3970
|
categoryWithChildrenSchema,
|
|
3971
|
+
checkMetaobjectFieldChanges,
|
|
2640
3972
|
colorSchema,
|
|
3973
|
+
compareDecimal,
|
|
2641
3974
|
contentPageSchema,
|
|
2642
3975
|
createAuthAwareHttpClient,
|
|
2643
3976
|
createBannerSchema,
|
|
@@ -2671,22 +4004,35 @@ export {
|
|
|
2671
4004
|
embeddedScriptPlacementSchema,
|
|
2672
4005
|
embeddedScriptSchema,
|
|
2673
4006
|
ensureSuccess,
|
|
4007
|
+
entryDisplayName,
|
|
2674
4008
|
fromObjectPublicUrlToRender,
|
|
2675
4009
|
fulfillmentStatusSchema,
|
|
2676
4010
|
idSchema,
|
|
2677
4011
|
integrationStatusSchema,
|
|
2678
4012
|
inventoryStatusSchema,
|
|
4013
|
+
isBlankValue,
|
|
4014
|
+
isBlankValueFor,
|
|
2679
4015
|
isEditableStatus,
|
|
4016
|
+
isListType,
|
|
4017
|
+
isMetafieldType,
|
|
2680
4018
|
isPublishableStatus,
|
|
2681
4019
|
isRuleField,
|
|
4020
|
+
isSafeRegex,
|
|
2682
4021
|
isSettingVisible,
|
|
2683
4022
|
isUnpublishedStatus,
|
|
4023
|
+
isValidEntryHandle,
|
|
2684
4024
|
jsonSchema,
|
|
2685
4025
|
legacyOrderStatusSchema,
|
|
2686
4026
|
legacyProductStatusSchema,
|
|
2687
4027
|
legalLinksSchema,
|
|
4028
|
+
measurementInBaseUnit,
|
|
2688
4029
|
messageOutboxStatusSchema,
|
|
4030
|
+
metafieldIdentifier,
|
|
2689
4031
|
moneySchema,
|
|
4032
|
+
normalizeDate,
|
|
4033
|
+
normalizeDateTime,
|
|
4034
|
+
normalizeDecimal,
|
|
4035
|
+
normalizeId,
|
|
2690
4036
|
onHand,
|
|
2691
4037
|
operatorsForField,
|
|
2692
4038
|
optionalTimestampSchema,
|
|
@@ -2698,6 +4044,7 @@ export {
|
|
|
2698
4044
|
paginatedResponseSchema,
|
|
2699
4045
|
paginationMetaSchema,
|
|
2700
4046
|
paginationSchema,
|
|
4047
|
+
parseMetafieldIdentifier,
|
|
2701
4048
|
parseStorageUrl,
|
|
2702
4049
|
parseThemeSchema,
|
|
2703
4050
|
paymentMethodSchema,
|
|
@@ -2723,6 +4070,7 @@ export {
|
|
|
2723
4070
|
resolveThemeSettings,
|
|
2724
4071
|
reviewStatusSchema,
|
|
2725
4072
|
rewriteSupabaseUrl,
|
|
4073
|
+
richTextLength,
|
|
2726
4074
|
safeParseBaseQueryParams,
|
|
2727
4075
|
safeParsePaginationMeta,
|
|
2728
4076
|
shippingStatusSchema,
|
|
@@ -2734,11 +4082,14 @@ export {
|
|
|
2734
4082
|
storeSettingsSchema,
|
|
2735
4083
|
storeStatusSchema,
|
|
2736
4084
|
storeWithRelationsSchema,
|
|
4085
|
+
storefrontAccessAllowed,
|
|
2737
4086
|
strictUrlSchema,
|
|
2738
4087
|
themeDefaults,
|
|
2739
4088
|
themeSchemaSchema,
|
|
2740
4089
|
themeSettings,
|
|
2741
4090
|
timestampSchema,
|
|
4091
|
+
toEntryHandle,
|
|
4092
|
+
toMetafieldKey,
|
|
2742
4093
|
toObjectUrl,
|
|
2743
4094
|
toRenderUrl,
|
|
2744
4095
|
tocItemSchema,
|
|
@@ -2769,8 +4120,14 @@ export {
|
|
|
2769
4120
|
updateVariantOptionValueSchema,
|
|
2770
4121
|
urlSchema,
|
|
2771
4122
|
uuidSchema,
|
|
4123
|
+
validateMetafieldDefinition,
|
|
4124
|
+
validateMetafieldValue,
|
|
4125
|
+
validateMetaobjectDefinition,
|
|
4126
|
+
validateMetaobjectEntry,
|
|
2772
4127
|
validatePaginationMeta,
|
|
4128
|
+
validateRichText,
|
|
2773
4129
|
validateThemeValues,
|
|
4130
|
+
validateValidations,
|
|
2774
4131
|
valueKindFor,
|
|
2775
4132
|
variantOptionSchema,
|
|
2776
4133
|
variantOptionValueSchema,
|