cyymall-cli 0.1.10 → 0.1.11

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.
@@ -1,51 +1,89 @@
1
- "use strict";
2
-
3
- const fs = require("fs");
4
- const crypto = require("crypto");
5
- const http = require("../http");
6
- const config = require("../config");
7
- const biz = require("../biz");
8
-
9
- /**
10
- * @param {{ bodyFile?: string, bodyJson?: string }} opts
11
- */
12
- async function add(opts) {
13
- const cfg = config.requireAuthSession();
14
-
15
- let raw = opts.bodyJson;
16
- if (opts.bodyFile) {
17
- raw = fs.readFileSync(opts.bodyFile, "utf8");
18
- }
19
- if (!raw) {
20
- console.error("cyy: provide --body-file or --body-json");
21
- process.exit(1);
22
- }
23
-
24
- const url = http.moduleUrl("ORDER", "/app/order/cart");
25
- const headers = /** @type {Record<string,string>} */ (http.buildAuthHeaders(cfg));
26
-
27
- const { ok, json } = await http.request(url, {
28
- method: "POST",
29
- headers,
30
- body: raw,
31
- });
32
-
33
- const traceId = `local-${crypto.randomBytes(8).toString("hex")}`;
34
- const success = ok && biz.isBizSuccess(json);
35
- console.log(
36
- JSON.stringify(
37
- {
38
- success,
39
- code: success ? "OK" : "UPSTREAM_ERROR",
40
- message: success ? "success" : "cart failed",
41
- data: { upstream: json },
42
- traceId,
43
- },
44
- null,
45
- 2,
46
- ),
47
- );
48
- process.exit(success ? 0 : 2);
49
- }
50
-
51
- module.exports = { add };
1
+ "use strict";
2
+
3
+ const fs = require("fs");
4
+ const crypto = require("crypto");
5
+ const http = require("../http");
6
+ const config = require("../config");
7
+ const biz = require("../biz");
8
+
9
+ /** App SPConfig / gateway OrderCartModelType */
10
+ const VALID_CART_MODELS = new Set(["append", "mix", "delete", "clear.off.sell", "clear.off.stock"]);
11
+
12
+ /**
13
+ * @param {string} raw
14
+ * @param {object} cfg
15
+ */
16
+ function normalizeAddCartBody(raw, cfg) {
17
+ let obj;
18
+ try {
19
+ obj = JSON.parse(raw);
20
+ } catch (e) {
21
+ console.error(`cyy: invalid cart JSON: ${/** @type {Error} */ (e).message}`);
22
+ process.exit(1);
23
+ }
24
+ if (!obj.cartModel || String(obj.cartModel).trim() === "") {
25
+ obj.cartModel = "append";
26
+ }
27
+ if (!VALID_CART_MODELS.has(String(obj.cartModel))) {
28
+ console.error(
29
+ `cyy: invalid cartModel "${obj.cartModel}". Use one of: ${[...VALID_CART_MODELS].join(", ")}`,
30
+ );
31
+ process.exit(1);
32
+ }
33
+ if (obj.shopId == null || obj.shopId === "") {
34
+ obj.shopId = Number(cfg.shop_id);
35
+ }
36
+ if (obj.siteId == null || obj.siteId === "") {
37
+ obj.siteId = Number(cfg.site_id);
38
+ }
39
+ if (!Array.isArray(obj.cartItemList) || obj.cartItemList.length === 0) {
40
+ console.error("cyy: cartItemList must be a non-empty array");
41
+ process.exit(1);
42
+ }
43
+ return JSON.stringify(obj);
44
+ }
45
+
46
+ /**
47
+ * @param {{ bodyFile?: string, bodyJson?: string }} opts
48
+ */
49
+ async function add(opts) {
50
+ const cfg = config.requireAuthSession();
51
+
52
+ let raw = opts.bodyJson;
53
+ if (opts.bodyFile) {
54
+ raw = fs.readFileSync(opts.bodyFile, "utf8");
55
+ }
56
+ if (!raw) {
57
+ console.error("cyy: provide --body-file or --body-json");
58
+ process.exit(1);
59
+ }
60
+
61
+ const body = normalizeAddCartBody(raw, cfg);
62
+ const url = http.moduleUrl("ORDER", "/app/order/cart");
63
+ const headers = /** @type {Record<string,string>} */ (http.buildAuthHeaders(cfg));
64
+
65
+ const { ok, json } = await http.request(url, {
66
+ method: "POST",
67
+ headers,
68
+ body,
69
+ });
70
+
71
+ const traceId = `local-${crypto.randomBytes(8).toString("hex")}`;
72
+ const success = ok && biz.isBizSuccess(json);
73
+ console.log(
74
+ JSON.stringify(
75
+ {
76
+ success,
77
+ code: success ? "OK" : "UPSTREAM_ERROR",
78
+ message: success ? "success" : "cart failed",
79
+ data: { upstream: json },
80
+ traceId,
81
+ },
82
+ null,
83
+ 2,
84
+ ),
85
+ );
86
+ process.exit(success ? 0 : 2);
87
+ }
88
+
89
+ module.exports = { add, normalizeAddCartBody, VALID_CART_MODELS };