cyymall-cli 0.1.0 → 0.1.1

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,56 +1,56 @@
1
- "use strict";
2
-
3
- const crypto = require("crypto");
4
- const http = require("../http");
5
- const config = require("../config");
6
- const biz = require("../biz");
7
-
8
- /**
9
- * @param {object} opts
10
- */
11
- async function search(opts) {
12
- const cfg = config.loadConfig();
13
- if (!cfg?.token) {
14
- console.error("cyy: not logged in. Run: cyy auth login");
15
- process.exit(1);
16
- }
17
-
18
- const shopId = Number(opts.shopId ?? cfg.shop_id);
19
- const siteId = Number(opts.siteId ?? cfg.site_id);
20
- const body = JSON.stringify({
21
- pageNum: Number(opts.page || 1),
22
- pageSize: Number(opts.pageSize || 10),
23
- shopId,
24
- siteId,
25
- spuName: opts.keyword,
26
- stockFlag: String(opts.stockFlag ?? "0"),
27
- });
28
-
29
- const url = http.moduleUrl("PRODUCT", "/app/product/getSkuList");
30
- const headers = /** @type {Record<string,string>} */ (http.buildAuthHeaders(cfg));
31
-
32
- const { ok, json } = await http.request(url, {
33
- method: "POST",
34
- headers,
35
- body,
36
- });
37
-
38
- const traceId = `local-${crypto.randomBytes(8).toString("hex")}`;
39
- const success = ok && biz.isBizSuccess(json);
40
- console.log(
41
- JSON.stringify(
42
- {
43
- success,
44
- code: success ? "OK" : "UPSTREAM_ERROR",
45
- message: success ? "success" : "search failed",
46
- data: { upstream: json },
47
- traceId,
48
- },
49
- null,
50
- 2,
51
- ),
52
- );
53
- process.exit(success ? 0 : 2);
54
- }
55
-
56
- module.exports = { search };
1
+ "use strict";
2
+
3
+ const crypto = require("crypto");
4
+ const http = require("../http");
5
+ const config = require("../config");
6
+ const biz = require("../biz");
7
+
8
+ /**
9
+ * @param {object} opts
10
+ */
11
+ async function search(opts) {
12
+ const cfg = config.loadConfig();
13
+ if (!cfg?.token) {
14
+ console.error("cyy: not logged in. Run: cyy auth login");
15
+ process.exit(1);
16
+ }
17
+
18
+ const shopId = Number(opts.shopId ?? cfg.shop_id);
19
+ const siteId = Number(opts.siteId ?? cfg.site_id);
20
+ const body = JSON.stringify({
21
+ pageNum: Number(opts.page || 1),
22
+ pageSize: Number(opts.pageSize || 10),
23
+ shopId,
24
+ siteId,
25
+ spuName: opts.keyword,
26
+ stockFlag: String(opts.stockFlag ?? "0"),
27
+ });
28
+
29
+ const url = http.moduleUrl("PRODUCT", "/app/product/getSkuList");
30
+ const headers = /** @type {Record<string,string>} */ (http.buildAuthHeaders(cfg));
31
+
32
+ const { ok, json } = await http.request(url, {
33
+ method: "POST",
34
+ headers,
35
+ body,
36
+ });
37
+
38
+ const traceId = `local-${crypto.randomBytes(8).toString("hex")}`;
39
+ const success = ok && biz.isBizSuccess(json);
40
+ console.log(
41
+ JSON.stringify(
42
+ {
43
+ success,
44
+ code: success ? "OK" : "UPSTREAM_ERROR",
45
+ message: success ? "success" : "search failed",
46
+ data: { upstream: json },
47
+ traceId,
48
+ },
49
+ null,
50
+ 2,
51
+ ),
52
+ );
53
+ process.exit(success ? 0 : 2);
54
+ }
55
+
56
+ module.exports = { search };
@@ -1,84 +1,84 @@
1
- "use strict";
2
-
3
- const http = require("http");
4
- const { executeApiCall } = require("./apiCall");
5
-
6
- /**
7
- * @param {{ port: number, host: string }} opts
8
- */
9
- function runServe(opts) {
10
- const port = Number(opts.port ?? 8787);
11
- const host = String(opts.host || "127.0.0.1");
12
-
13
- const server = http.createServer(async (req, res) => {
14
- if (req.method !== "POST" || req.url !== "/invoke") {
15
- res.writeHead(404, { "Content-Type": "application/json; charset=utf-8" });
16
- res.end(JSON.stringify({ error: "not_found", hint: "POST /invoke only" }));
17
- return;
18
- }
19
-
20
- let raw = "";
21
- for await (const chunk of req) {
22
- raw += chunk;
23
- }
24
- let payload = {};
25
- try {
26
- payload = raw ? JSON.parse(raw) : {};
27
- } catch {
28
- res.writeHead(400, { "Content-Type": "application/json; charset=utf-8" });
29
- res.end(JSON.stringify({ error: "invalid_json" }));
30
- return;
31
- }
32
-
33
- const {
34
- method = "GET",
35
- module: moduleKey = "DEFAULT",
36
- path: pathSuffix,
37
- body: bodyObj,
38
- query: queryObj,
39
- noAuth = false,
40
- } = payload;
41
-
42
- if (!pathSuffix || typeof pathSuffix !== "string") {
43
- res.writeHead(400, { "Content-Type": "application/json; charset=utf-8" });
44
- res.end(JSON.stringify({ error: "path_required" }));
45
- return;
46
- }
47
-
48
- try {
49
- const result = await executeApiCall({
50
- method: String(method).toUpperCase(),
51
- module: moduleKey,
52
- path: pathSuffix,
53
- bodyObj,
54
- queryObj: queryObj && typeof queryObj === "object" ? queryObj : undefined,
55
- query: undefined,
56
- noAuth: Boolean(noAuth),
57
- header: [],
58
- });
59
-
60
- if (result.error) {
61
- res.writeHead(400, { "Content-Type": "application/json; charset=utf-8" });
62
- res.end(JSON.stringify({ error: result.error }));
63
- return;
64
- }
65
-
66
- res.writeHead(200, { "Content-Type": "application/json; charset=utf-8" });
67
- res.end(JSON.stringify(result.envelope));
68
- } catch (e) {
69
- res.writeHead(500, { "Content-Type": "application/json; charset=utf-8" });
70
- res.end(
71
- JSON.stringify({
72
- error: "internal_error",
73
- message: e instanceof Error ? e.message : String(e),
74
- }),
75
- );
76
- }
77
- });
78
-
79
- server.listen(port, host, () => {
80
- console.error(`cyy serve listening on http://${host}:${port} (POST /invoke)`);
81
- });
82
- }
83
-
84
- module.exports = { runServe };
1
+ "use strict";
2
+
3
+ const http = require("http");
4
+ const { executeApiCall } = require("./apiCall");
5
+
6
+ /**
7
+ * @param {{ port: number, host: string }} opts
8
+ */
9
+ function runServe(opts) {
10
+ const port = Number(opts.port ?? 8787);
11
+ const host = String(opts.host || "127.0.0.1");
12
+
13
+ const server = http.createServer(async (req, res) => {
14
+ if (req.method !== "POST" || req.url !== "/invoke") {
15
+ res.writeHead(404, { "Content-Type": "application/json; charset=utf-8" });
16
+ res.end(JSON.stringify({ error: "not_found", hint: "POST /invoke only" }));
17
+ return;
18
+ }
19
+
20
+ let raw = "";
21
+ for await (const chunk of req) {
22
+ raw += chunk;
23
+ }
24
+ let payload = {};
25
+ try {
26
+ payload = raw ? JSON.parse(raw) : {};
27
+ } catch {
28
+ res.writeHead(400, { "Content-Type": "application/json; charset=utf-8" });
29
+ res.end(JSON.stringify({ error: "invalid_json" }));
30
+ return;
31
+ }
32
+
33
+ const {
34
+ method = "GET",
35
+ module: moduleKey = "DEFAULT",
36
+ path: pathSuffix,
37
+ body: bodyObj,
38
+ query: queryObj,
39
+ noAuth = false,
40
+ } = payload;
41
+
42
+ if (!pathSuffix || typeof pathSuffix !== "string") {
43
+ res.writeHead(400, { "Content-Type": "application/json; charset=utf-8" });
44
+ res.end(JSON.stringify({ error: "path_required" }));
45
+ return;
46
+ }
47
+
48
+ try {
49
+ const result = await executeApiCall({
50
+ method: String(method).toUpperCase(),
51
+ module: moduleKey,
52
+ path: pathSuffix,
53
+ bodyObj,
54
+ queryObj: queryObj && typeof queryObj === "object" ? queryObj : undefined,
55
+ query: undefined,
56
+ noAuth: Boolean(noAuth),
57
+ header: [],
58
+ });
59
+
60
+ if (result.error) {
61
+ res.writeHead(400, { "Content-Type": "application/json; charset=utf-8" });
62
+ res.end(JSON.stringify({ error: result.error }));
63
+ return;
64
+ }
65
+
66
+ res.writeHead(200, { "Content-Type": "application/json; charset=utf-8" });
67
+ res.end(JSON.stringify(result.envelope));
68
+ } catch (e) {
69
+ res.writeHead(500, { "Content-Type": "application/json; charset=utf-8" });
70
+ res.end(
71
+ JSON.stringify({
72
+ error: "internal_error",
73
+ message: e instanceof Error ? e.message : String(e),
74
+ }),
75
+ );
76
+ }
77
+ });
78
+
79
+ server.listen(port, host, () => {
80
+ console.error(`cyy serve listening on http://${host}:${port} (POST /invoke)`);
81
+ });
82
+ }
83
+
84
+ module.exports = { runServe };