@harrylabsj/kiwi-merchant-cloud 0.12.28 → 0.12.30
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/app/cloud/bootstrap.js +26 -9
- package/app/cloud/bootstrap.js.map +1 -1
- package/app/cloud/file-listing-publication.d.ts +12 -0
- package/app/cloud/file-listing-publication.js +34 -3
- package/app/cloud/file-listing-publication.js.map +1 -1
- package/app/cloud/product-import-tabular.d.ts +1 -1
- package/app/cloud/product-import-tabular.js +47 -8
- package/app/cloud/product-import-tabular.js.map +1 -1
- package/app/cloud/product-source.d.ts +4 -0
- package/app/cloud/product-source.js +6 -2
- package/app/cloud/product-source.js.map +1 -1
- package/app/cloud/trade-platforms-client.d.ts +8 -2
- package/app/cloud/trade-platforms-client.js +18 -6
- package/app/cloud/trade-platforms-client.js.map +1 -1
- package/app/cloud-sample/products.json +3 -3
- package/app/handoff/marketplace-product-link.d.ts +13 -5
- package/app/handoff/marketplace-product-link.js +146 -10
- package/app/handoff/marketplace-product-link.js.map +1 -1
- package/app/http/merchant-management/api.js +55 -9
- package/app/http/merchant-management/api.js.map +1 -1
- package/app/http/merchant-management/draft-store.d.ts +8 -1
- package/app/http/merchant-management/draft-store.js +36 -6
- package/app/http/merchant-management/draft-store.js.map +1 -1
- package/app/http/merchant-management/page.js +145 -46
- package/app/http/merchant-management/page.js.map +1 -1
- package/app/merchant-gateway/catalog-publications.d.ts +2 -0
- package/app/merchant-gateway/catalog-publications.js +16 -2
- package/app/merchant-gateway/catalog-publications.js.map +1 -1
- package/app/product-publish.js +1 -1
- package/app/product-publish.js.map +1 -1
- package/build-manifest.json +69 -69
- package/deployment-lock.json +8 -8
- package/package.json +1 -1
- package/runtime-package.json +1 -1
- package/shipping-sbom.cdx.json +21 -21
- package/shipping-source.json +11 -11
|
@@ -2,8 +2,10 @@
|
|
|
2
2
|
* Merchant-selected trade platform and its opaque decimal product ID.
|
|
3
3
|
*
|
|
4
4
|
* The allowed platform set is operator-configured (catalog
|
|
5
|
-
* `/v1/marketplace-platforms`)
|
|
6
|
-
* product
|
|
5
|
+
* `/v1/marketplace-platforms`). A merchant may enter either the platform
|
|
6
|
+
* product ID or a full product detail-page URL; both are resolved to the same
|
|
7
|
+
* opaque ID, and the public link is regenerated from the platform's url
|
|
8
|
+
* template so tracking parameters or lookalike hosts never reach the catalog.
|
|
7
9
|
*/
|
|
8
10
|
import { DEFAULT_TRADE_PLATFORM_RULES, buildTradePlatformUrl, resolveTradePlatform, } from "./trade-platform-rules.js";
|
|
9
11
|
export class MarketplaceProductLinkError extends Error {
|
|
@@ -29,22 +31,156 @@ export function normalizeMarketplaceTradePlatform(value, rules = DEFAULT_TRADE_P
|
|
|
29
31
|
function empty(value) {
|
|
30
32
|
return value === undefined || (typeof value === "string" && value.trim() === "");
|
|
31
33
|
}
|
|
32
|
-
|
|
34
|
+
function escapeRegExp(text) {
|
|
35
|
+
return text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
36
|
+
}
|
|
37
|
+
/** Path shape of a template whose `{id}` lives in the path; undefined for query templates. */
|
|
38
|
+
function templatePathPattern(template) {
|
|
39
|
+
const noHash = template.split("#")[0] ?? "";
|
|
40
|
+
const queryIndex = noHash.indexOf("?");
|
|
41
|
+
const pathPart = queryIndex === -1 ? noHash : noHash.slice(0, queryIndex);
|
|
42
|
+
const queryPart = queryIndex === -1 ? "" : noHash.slice(queryIndex + 1);
|
|
43
|
+
if (queryPart.includes("{id}"))
|
|
44
|
+
return undefined;
|
|
45
|
+
const matched = /^https:\/\/[^/]+(\/[^?#]*)$/.exec(pathPart);
|
|
46
|
+
const path = matched?.[1] ?? "/";
|
|
47
|
+
if (!path.includes("{id}"))
|
|
48
|
+
return undefined;
|
|
49
|
+
return new RegExp(`^${path.split("{id}").map(escapeRegExp).join("([^/]+)")}$`);
|
|
50
|
+
}
|
|
51
|
+
/** Static path prefix before `{id}` (e.g. `/ip/` for walmart); `/` when absent. */
|
|
52
|
+
function templatePathPrefix(template) {
|
|
53
|
+
const noHash = template.split("#")[0] ?? "";
|
|
54
|
+
const queryIndex = noHash.indexOf("?");
|
|
55
|
+
const pathPart = queryIndex === -1 ? noHash : noHash.slice(0, queryIndex);
|
|
56
|
+
const matched = /^https:\/\/[^/]+(\/[^?#]*)$/.exec(pathPart);
|
|
57
|
+
const path = matched?.[1] ?? "/";
|
|
58
|
+
const prefix = path.split("{id}")[0] ?? "/";
|
|
59
|
+
return prefix === "" ? "/" : prefix;
|
|
60
|
+
}
|
|
61
|
+
/** Query parameter name whose value is `{id}` in the template (e.g. taobao `id`). */
|
|
62
|
+
function templateQueryParam(template) {
|
|
63
|
+
const queryIndex = template.indexOf("?");
|
|
64
|
+
if (queryIndex === -1)
|
|
65
|
+
return undefined;
|
|
66
|
+
for (const pair of (template.slice(queryIndex + 1).split("#")[0] ?? "").split("&")) {
|
|
67
|
+
const equals = pair.indexOf("=");
|
|
68
|
+
if (equals === -1)
|
|
69
|
+
continue;
|
|
70
|
+
if (pair.slice(equals + 1) === "{id}")
|
|
71
|
+
return decodeURIComponent(pair.slice(0, equals));
|
|
72
|
+
}
|
|
73
|
+
return undefined;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Extract the opaque product ID from a full product detail-page URL.
|
|
77
|
+
*
|
|
78
|
+
* The URL must stay on the platform's trusted domain and use the platform's
|
|
79
|
+
* own path/query shape (from `url_template`); extra tracking parameters and
|
|
80
|
+
* fragments are tolerated but dropped — only the ID is kept.
|
|
81
|
+
*/
|
|
82
|
+
function productIdFromUrl(rule, raw) {
|
|
83
|
+
let url;
|
|
84
|
+
try {
|
|
85
|
+
url = new URL(raw.trim());
|
|
86
|
+
}
|
|
87
|
+
catch {
|
|
88
|
+
throw new MarketplaceProductLinkError("platform_product_url", "商品链接不是合法 URL");
|
|
89
|
+
}
|
|
90
|
+
if (url.protocol !== "https:") {
|
|
91
|
+
throw new MarketplaceProductLinkError("platform_product_url", "商品链接必须使用 https://");
|
|
92
|
+
}
|
|
93
|
+
if (url.username !== "" || url.password !== "" || url.port !== "") {
|
|
94
|
+
throw new MarketplaceProductLinkError("platform_product_url", "商品链接不能包含用户名、密码或端口");
|
|
95
|
+
}
|
|
96
|
+
const host = url.hostname.toLowerCase();
|
|
97
|
+
if (host !== rule.domain && !host.endsWith(`.${rule.domain}`)) {
|
|
98
|
+
throw new MarketplaceProductLinkError("platform_product_url", `商品链接域名必须是 ${rule.domain}(收到 ${host})`);
|
|
99
|
+
}
|
|
100
|
+
const queryParam = templateQueryParam(rule.url_template);
|
|
101
|
+
let id;
|
|
102
|
+
if (queryParam !== undefined) {
|
|
103
|
+
id = url.searchParams.get(queryParam);
|
|
104
|
+
if (id === null) {
|
|
105
|
+
throw new MarketplaceProductLinkError("platform_product_url", `商品链接缺少平台商品ID参数 ${queryParam}`);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
const pattern = templatePathPattern(rule.url_template);
|
|
110
|
+
if (pattern === undefined) {
|
|
111
|
+
throw new MarketplaceProductLinkError("platform_product_url", "平台链接模板无法识别商品ID,请填写平台商品ID");
|
|
112
|
+
}
|
|
113
|
+
const pathname = url.pathname.replace(/\/+$/, "") || "/";
|
|
114
|
+
const matched = pattern.exec(pathname);
|
|
115
|
+
if (matched !== null) {
|
|
116
|
+
id = matched[1] ?? null;
|
|
117
|
+
}
|
|
118
|
+
else {
|
|
119
|
+
// 真实详情页常带可读 slug 或尾斜杠(如 /ip/some-name/123、/123/)。
|
|
120
|
+
// 只放宽到「模板静态前缀之下、最后一个纯数字路径段」,仍限定平台域名。
|
|
121
|
+
const prefix = templatePathPrefix(rule.url_template);
|
|
122
|
+
const segments = pathname.split("/").filter(Boolean);
|
|
123
|
+
const last = segments[segments.length - 1] ?? "";
|
|
124
|
+
const candidate = last.endsWith(".html") ? last.slice(0, -5) : last;
|
|
125
|
+
id =
|
|
126
|
+
pathname.startsWith(prefix) && /^[1-9][0-9]*$/.test(candidate)
|
|
127
|
+
? candidate
|
|
128
|
+
: null;
|
|
129
|
+
if (id === null) {
|
|
130
|
+
throw new MarketplaceProductLinkError("platform_product_url", `商品链接路径不符合平台格式(${rule.url_template})`);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
if (id === null || !/^[1-9][0-9]*$/.test(id)) {
|
|
135
|
+
throw new MarketplaceProductLinkError("platform_product_url", "商品链接中的商品ID必须为ASCII正整数字符串");
|
|
136
|
+
}
|
|
137
|
+
return id;
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Resolve the optional trade-entry fields. Platform is mandatory whenever an ID
|
|
141
|
+
* or a full URL is provided; the platform may be written as id or label, in
|
|
142
|
+
* Chinese or English. At least one of ID/URL is required. When both are given
|
|
143
|
+
* they must point at the same product.
|
|
144
|
+
*/
|
|
33
145
|
export function parseMarketplaceProductFields(value, rules = DEFAULT_TRADE_PLATFORM_RULES) {
|
|
34
146
|
const platformEmpty = empty(value.trade_platform);
|
|
35
147
|
const idEmpty = empty(value.platform_product_id);
|
|
36
|
-
|
|
148
|
+
const urlEmpty = empty(value.platform_product_url);
|
|
149
|
+
if (platformEmpty && idEmpty && urlEmpty)
|
|
37
150
|
return {};
|
|
38
151
|
if (platformEmpty) {
|
|
39
|
-
throw new MarketplaceProductLinkError("trade_platform", "填写平台商品ID
|
|
152
|
+
throw new MarketplaceProductLinkError("trade_platform", "填写平台商品ID或商品链接时必须同时填写交易平台");
|
|
40
153
|
}
|
|
41
|
-
if (idEmpty) {
|
|
42
|
-
throw new MarketplaceProductLinkError("platform_product_id", "填写交易平台时必须同时填写平台商品ID");
|
|
154
|
+
if (idEmpty && urlEmpty) {
|
|
155
|
+
throw new MarketplaceProductLinkError("platform_product_id", "填写交易平台时必须同时填写平台商品ID或商品链接");
|
|
43
156
|
}
|
|
44
157
|
const trade_platform = normalizeMarketplaceTradePlatform(value.trade_platform, rules);
|
|
45
|
-
const
|
|
46
|
-
if (
|
|
47
|
-
|
|
158
|
+
const rule = resolveTradePlatform(trade_platform, rules);
|
|
159
|
+
if (rule === undefined) {
|
|
160
|
+
// normalizeMarketplaceTradePlatform 已保证存在;保留防御性分支。
|
|
161
|
+
throw new MarketplaceProductLinkError("trade_platform", "交易平台不在允许范围内");
|
|
162
|
+
}
|
|
163
|
+
let id;
|
|
164
|
+
if (!idEmpty) {
|
|
165
|
+
const rawId = value.platform_product_id;
|
|
166
|
+
if (typeof rawId !== "string" || !/^[1-9][0-9]*$/.test(rawId)) {
|
|
167
|
+
throw new MarketplaceProductLinkError("platform_product_id", "平台商品ID必须为ASCII正整数字符串;不能填数字类型、科学计数法、URL或公式");
|
|
168
|
+
}
|
|
169
|
+
id = rawId;
|
|
170
|
+
}
|
|
171
|
+
if (!urlEmpty) {
|
|
172
|
+
const rawUrl = value.platform_product_url;
|
|
173
|
+
if (typeof rawUrl !== "string") {
|
|
174
|
+
throw new MarketplaceProductLinkError("platform_product_url", "商品链接必须是文本");
|
|
175
|
+
}
|
|
176
|
+
const fromUrl = productIdFromUrl(rule, rawUrl);
|
|
177
|
+
if (id !== undefined && id !== fromUrl) {
|
|
178
|
+
throw new MarketplaceProductLinkError("platform_product_url", `商品链接与平台商品ID不一致(链接为 ${fromUrl},ID 为 ${id})`);
|
|
179
|
+
}
|
|
180
|
+
id = fromUrl;
|
|
181
|
+
}
|
|
182
|
+
if (id === undefined) {
|
|
183
|
+
throw new MarketplaceProductLinkError("platform_product_id", "填写交易平台时必须同时填写平台商品ID或商品链接");
|
|
48
184
|
}
|
|
49
185
|
return { trade_platform, platform_product_id: id };
|
|
50
186
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"marketplace-product-link.js","sourceRoot":"","sources":["../../src/handoff/marketplace-product-link.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"marketplace-product-link.js","sourceRoot":"","sources":["../../src/handoff/marketplace-product-link.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,OAA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|
|
@@ -230,6 +230,27 @@ export function createMerchantManagementApiHandler(options) {
|
|
|
230
230
|
// ── 只读路由(认证即鉴权入口;业务授权在服务层逐次复核)────────────────
|
|
231
231
|
/** Workbench v1 读取面复用同一应用服务;未实现路由明确失败,不返回假数据。 */
|
|
232
232
|
async function routeWorkbenchV1Get(req, res, url, rest, requestId) {
|
|
233
|
+
if (rest === "/products/publication-status") {
|
|
234
|
+
const auth = requireActor(req);
|
|
235
|
+
authorizeOrThrow(auth.ctx, "products:import");
|
|
236
|
+
if (auth.ctx.role !== "owner")
|
|
237
|
+
throw new ManagementError("forbidden", "only owner can publish products");
|
|
238
|
+
const channel = options.fileListingPublication;
|
|
239
|
+
if (channel === undefined)
|
|
240
|
+
throw new ManagementError("unavailable", "product publication is unavailable");
|
|
241
|
+
try {
|
|
242
|
+
const result = await channel.listPublished();
|
|
243
|
+
writeJson(res, 200, result, { "x-request-id": requestId, "cache-control": "no-store" });
|
|
244
|
+
}
|
|
245
|
+
catch (err) {
|
|
246
|
+
if (err instanceof FileListingPublicationError) {
|
|
247
|
+
// 不反射远端错误或凭据;页面按“状态未知”降级。
|
|
248
|
+
throw new ManagementError("precondition_changed", "目录发布状态暂不可读取,请检查连接状态后刷新。");
|
|
249
|
+
}
|
|
250
|
+
throw err;
|
|
251
|
+
}
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
233
254
|
const listingDraftMatch = /^\/products\/publication-drafts\/(flp_[a-z0-9]+_[0-9a-f]{12})$/.exec(rest);
|
|
234
255
|
if (listingDraftMatch !== null) {
|
|
235
256
|
const auth = requireActor(req);
|
|
@@ -846,6 +867,8 @@ export function createMerchantManagementApiHandler(options) {
|
|
|
846
867
|
const channel = options.fileListingPublication;
|
|
847
868
|
if (channel === undefined)
|
|
848
869
|
throw new ManagementError("unavailable", "product publication is unavailable");
|
|
870
|
+
// 发布链接按当前运营规则生成(无需重启实例)。
|
|
871
|
+
await options.marketplacePlatforms?.();
|
|
849
872
|
try {
|
|
850
873
|
if (publicationCommitMatch !== null) {
|
|
851
874
|
const fields = objectFields(await readJsonBody(req), ["expected_digest", "confirm_publication"]);
|
|
@@ -1962,6 +1985,8 @@ export function createMerchantManagementApiHandler(options) {
|
|
|
1962
1985
|
}
|
|
1963
1986
|
if (rest === "/products") {
|
|
1964
1987
|
const auth = requireActor(req);
|
|
1988
|
+
// 列表里的成交链接要按运营当前规则生成;catalog 不可达时保留上次快照。
|
|
1989
|
+
await options.marketplacePlatforms?.();
|
|
1965
1990
|
writeJson(res, 200, await readService.listProducts(auth.ctx, pageQuery(url)));
|
|
1966
1991
|
return;
|
|
1967
1992
|
}
|
|
@@ -2844,9 +2869,10 @@ export function createMerchantManagementApiHandler(options) {
|
|
|
2844
2869
|
}
|
|
2845
2870
|
// ── 商品导入(BD §10.1:校验预览 → 确认提交;全批成功或全批不变)────────
|
|
2846
2871
|
/**
|
|
2847
|
-
* POST /products/import-drafts ——
|
|
2848
|
-
* 行号)+ 当前表 CAS(base_digest)+
|
|
2849
|
-
*
|
|
2872
|
+
* POST /products/import-drafts —— 严格校验上传表(任何行错误 → 整批拒绝并报
|
|
2873
|
+
* 行号)+ 当前表 CAS(base_digest)+ 增/改/留预览;只存草稿,不动商品表。
|
|
2874
|
+
* 语义是**按 SKU 合并追加**:文件里的 SKU 新增或覆盖同 SKU 的本机记录,
|
|
2875
|
+
* 未出现在文件中的现有商品保持不变(removed 恒为 0)。
|
|
2850
2876
|
*/
|
|
2851
2877
|
async function postProductsImportDraft(req, res) {
|
|
2852
2878
|
const auth = requireActor(req);
|
|
@@ -2860,7 +2886,9 @@ export function createMerchantManagementApiHandler(options) {
|
|
|
2860
2886
|
}
|
|
2861
2887
|
let table;
|
|
2862
2888
|
try {
|
|
2863
|
-
|
|
2889
|
+
// 导入预览/提交都用运营最新规则校验平台与链接,不依赖进程启动快照。
|
|
2890
|
+
const platformRules = await options.marketplacePlatforms?.();
|
|
2891
|
+
table = parseProductTable(fields["table"], "request body", platformRules);
|
|
2864
2892
|
table = await importChannel.prepareTable?.(table) ?? table;
|
|
2865
2893
|
}
|
|
2866
2894
|
catch (err) {
|
|
@@ -2887,6 +2915,13 @@ export function createMerchantManagementApiHandler(options) {
|
|
|
2887
2915
|
throw new ManagementError("precondition_changed", "product table changed since preview");
|
|
2888
2916
|
}
|
|
2889
2917
|
const currentBySku = new Map(base.records.map((record) => [record.sku, record]));
|
|
2918
|
+
const incomingBySku = new Map();
|
|
2919
|
+
for (const record of table.products) {
|
|
2920
|
+
if (incomingBySku.has(record.sku)) {
|
|
2921
|
+
throw new ManagementError("invalid_input", `商品表存在重复 SKU:${record.sku}`);
|
|
2922
|
+
}
|
|
2923
|
+
incomingBySku.set(record.sku, record);
|
|
2924
|
+
}
|
|
2890
2925
|
let added = 0;
|
|
2891
2926
|
let updated = 0;
|
|
2892
2927
|
let unchanged = 0;
|
|
@@ -2899,12 +2934,20 @@ export function createMerchantManagementApiHandler(options) {
|
|
|
2899
2934
|
else
|
|
2900
2935
|
updated += 1;
|
|
2901
2936
|
}
|
|
2902
|
-
|
|
2903
|
-
|
|
2937
|
+
// 按 SKU 合并:现有商品保持原顺序,同 SKU 用上传行覆盖,新 SKU 追加。
|
|
2938
|
+
// 未出现在文件里的现有商品不删除——导入永远不是“整表替换”。
|
|
2939
|
+
const kept = base.records.filter((record) => !incomingBySku.has(record.sku)).length;
|
|
2940
|
+
const mergedProducts = base.records.map((record) => incomingBySku.get(record.sku) ?? record);
|
|
2941
|
+
for (const record of table.products) {
|
|
2942
|
+
if (!currentBySku.has(record.sku))
|
|
2943
|
+
mergedProducts.push(record);
|
|
2944
|
+
}
|
|
2945
|
+
const mergedTable = { ...table, products: mergedProducts };
|
|
2946
|
+
const digest = productTableDigest(mergedTable);
|
|
2904
2947
|
const created = options.drafts.create({
|
|
2905
2948
|
merchantId: auth.ctx.merchantId,
|
|
2906
2949
|
kind: "products_import",
|
|
2907
|
-
payloadJson: JSON.stringify(
|
|
2950
|
+
payloadJson: JSON.stringify(mergedTable),
|
|
2908
2951
|
payloadDigest: digest,
|
|
2909
2952
|
...(base.digest !== "" ? { baseDigest: base.digest } : {}),
|
|
2910
2953
|
});
|
|
@@ -2918,14 +2961,15 @@ export function createMerchantManagementApiHandler(options) {
|
|
|
2918
2961
|
added,
|
|
2919
2962
|
updated,
|
|
2920
2963
|
unchanged,
|
|
2921
|
-
|
|
2964
|
+
kept,
|
|
2965
|
+
removed: 0,
|
|
2922
2966
|
},
|
|
2923
2967
|
});
|
|
2924
2968
|
}
|
|
2925
2969
|
/**
|
|
2926
2970
|
* POST /products/import-parse?format=csv|xlsx —— 把商家上传的 CSV/xlsx 原文
|
|
2927
2971
|
* (WP12)转换为商品表草稿:表头映射报告 + 逐行问题清单。无状态(不落盘),
|
|
2928
|
-
* 商家确认后仍走 import-drafts
|
|
2972
|
+
* 商家确认后仍走 import-drafts 严格校验与按 SKU 合并预览;服务端解析保证工作
|
|
2929
2973
|
* 台、脚本与技能共用同一实现(`product-import-tabular.ts`)。
|
|
2930
2974
|
*/
|
|
2931
2975
|
async function postProductsImportParse(req, res, url) {
|
|
@@ -3032,6 +3076,8 @@ export function createMerchantManagementApiHandler(options) {
|
|
|
3032
3076
|
return;
|
|
3033
3077
|
}
|
|
3034
3078
|
try {
|
|
3079
|
+
// 落盘前再刷一次运营规则:运营新增平台后无需重启实例即可提交。
|
|
3080
|
+
await options.marketplacePlatforms?.();
|
|
3035
3081
|
const written = importChannel.commit(table);
|
|
3036
3082
|
const marked = options.drafts.markCommitted(auth.ctx.merchantId, draftId);
|
|
3037
3083
|
if (!marked) {
|