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.
- package/README.md +1 -1
- package/package.json +1 -1
- package/src/cli.js +353 -321
- package/src/commands/cart.js +89 -51
- package/src/commands/order.js +618 -375
- package/src/commands/product.js +175 -175
package/src/commands/cart.js
CHANGED
|
@@ -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
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
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 };
|