@less-is-more/less-js 1.2.35 → 1.3.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.
- package/package.json +4 -3
- package/src/index.js +9 -7
- package/src/redis.js +23 -12
- package/src/router.js +2 -2
- package/src/service.js +68 -0
- package/test/test-service.js +41 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@less-is-more/less-js",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.1",
|
|
4
4
|
"description": "Fast develop kit for nodejs",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -18,16 +18,17 @@
|
|
|
18
18
|
"license": "MIT",
|
|
19
19
|
"dependencies": {
|
|
20
20
|
"@alicloud/pop-core": "^1.7.10",
|
|
21
|
+
"axios": "^0.27.2",
|
|
22
|
+
"body": "^5.1.0",
|
|
21
23
|
"ejs": "^3.1.6",
|
|
22
24
|
"fs-extra": "^10.0.0",
|
|
23
25
|
"inquirer": "^8.2.0",
|
|
24
26
|
"multiparty": "^4.2.2",
|
|
25
27
|
"mysql2": "^2.3.3",
|
|
26
28
|
"pg-hstore": "^2.3.4",
|
|
27
|
-
"
|
|
29
|
+
"qs": "^6.11.0",
|
|
28
30
|
"redis": "^4.0.1",
|
|
29
31
|
"sequelize": "^6.13.0",
|
|
30
|
-
"body": "^5.1.0",
|
|
31
32
|
"validator": "^13.7.0"
|
|
32
33
|
},
|
|
33
34
|
"devDependencies": {
|
package/src/index.js
CHANGED
|
@@ -5,12 +5,14 @@ var Redis = require("./redis.js");
|
|
|
5
5
|
var Param = require("./param.js");
|
|
6
6
|
var Sms = require("./sms.js");
|
|
7
7
|
var Cache = require("./cache.js");
|
|
8
|
+
var Service = require("./service.js");
|
|
8
9
|
module.exports = {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
10
|
+
Ret,
|
|
11
|
+
Router,
|
|
12
|
+
Db,
|
|
13
|
+
Redis,
|
|
14
|
+
Param,
|
|
15
|
+
Sms,
|
|
16
|
+
Cache,
|
|
17
|
+
Service,
|
|
16
18
|
};
|
package/src/redis.js
CHANGED
|
@@ -14,23 +14,34 @@ module.exports = class Redis {
|
|
|
14
14
|
|
|
15
15
|
static async getInstance() {
|
|
16
16
|
if (!this.#client) {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
}
|
|
25
|
-
this.#client = await redis.createClient(params);
|
|
26
|
-
await this.#client.connect();
|
|
27
|
-
} else {
|
|
28
|
-
throw new Error("Redis not init");
|
|
17
|
+
await Redis.connect();
|
|
18
|
+
} else {
|
|
19
|
+
// 旧的实例判断一下是否可以链接,不能就重新建一个
|
|
20
|
+
try {
|
|
21
|
+
await this.#client.ping();
|
|
22
|
+
} catch (e) {
|
|
23
|
+
await Redis.connect();
|
|
29
24
|
}
|
|
30
25
|
}
|
|
31
26
|
return this.#client;
|
|
32
27
|
}
|
|
33
28
|
|
|
29
|
+
static async connect() {
|
|
30
|
+
if (this.#host) {
|
|
31
|
+
let params = {
|
|
32
|
+
socket: { host: this.#host },
|
|
33
|
+
};
|
|
34
|
+
if (this.#password) {
|
|
35
|
+
params.username = this.#user;
|
|
36
|
+
params.password = this.#password;
|
|
37
|
+
}
|
|
38
|
+
this.#client = redis.createClient(params);
|
|
39
|
+
await this.#client.connect();
|
|
40
|
+
} else {
|
|
41
|
+
throw new Error("Redis not init");
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
34
45
|
static async exec(name, ...args) {
|
|
35
46
|
let client = await Redis.getInstance();
|
|
36
47
|
args.unshift(name);
|
package/src/router.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
const Ret = require("./ret.js");
|
|
2
2
|
const Param = require("./param.js");
|
|
3
|
-
const
|
|
3
|
+
const qs = require("qs");
|
|
4
4
|
const body = require("body");
|
|
5
5
|
const multiparty = require("multiparty");
|
|
6
6
|
const { promisify } = require("util");
|
|
@@ -92,7 +92,7 @@ module.exports = class Router {
|
|
|
92
92
|
} else {
|
|
93
93
|
const data = await promisify(body)(req);
|
|
94
94
|
console.log("body: " + decodeURIComponent(data));
|
|
95
|
-
req.body =
|
|
95
|
+
req.body = qs.parse(decodeURIComponent(data));
|
|
96
96
|
}
|
|
97
97
|
}
|
|
98
98
|
if (req.params == undefined) {
|
package/src/service.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
const { default: axios } = require("axios");
|
|
2
|
+
const qs = require("qs");
|
|
3
|
+
const Ret = require("./ret");
|
|
4
|
+
|
|
5
|
+
module.exports = class Service {
|
|
6
|
+
static #domain;
|
|
7
|
+
static #isTest;
|
|
8
|
+
static #urlPrefix;
|
|
9
|
+
|
|
10
|
+
static init(domain, isTest) {
|
|
11
|
+
this.#domain = domain;
|
|
12
|
+
this.#isTest = isTest;
|
|
13
|
+
this.#urlPrefix =
|
|
14
|
+
"http://SERVICE" + (this.#isTest ? ".test" : ".prod") + "." + domain;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
static setDelay(async = false, asyncDelayScond = 0) {
|
|
18
|
+
let headers = {};
|
|
19
|
+
if (async) {
|
|
20
|
+
headers["X-Fc-Invocation-Type"] = "Async";
|
|
21
|
+
}
|
|
22
|
+
if (asyncDelayScond > 0) {
|
|
23
|
+
headers["x-fc-async-delay"] = asyncDelayScond;
|
|
24
|
+
}
|
|
25
|
+
return headers;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
static async get(
|
|
29
|
+
serviceName,
|
|
30
|
+
path,
|
|
31
|
+
params,
|
|
32
|
+
async = false,
|
|
33
|
+
asyncDelayScond = 0
|
|
34
|
+
) {
|
|
35
|
+
let url = this.#urlPrefix.replace("SERVICE", serviceName) + path;
|
|
36
|
+
try {
|
|
37
|
+
if (params) {
|
|
38
|
+
url += "?" + qs.stringify(params);
|
|
39
|
+
}
|
|
40
|
+
const headers = this.setDelay(async, asyncDelayScond);
|
|
41
|
+
const result = await axios.get(url, { headers });
|
|
42
|
+
return result.data;
|
|
43
|
+
} catch (e) {
|
|
44
|
+
console.error("获取失败", url, e.message);
|
|
45
|
+
return new Ret(false, "获取失败", Ret.CODE_SYSTEM);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
static async post(
|
|
50
|
+
serviceName,
|
|
51
|
+
path,
|
|
52
|
+
params = {},
|
|
53
|
+
async = false,
|
|
54
|
+
asyncDelayScond = 0
|
|
55
|
+
) {
|
|
56
|
+
let url = this.#urlPrefix.replace("SERVICE", serviceName) + path;
|
|
57
|
+
try {
|
|
58
|
+
const headers = this.setDelay(async, asyncDelayScond);
|
|
59
|
+
const result = await axios.post(url, qs.stringify(params), {
|
|
60
|
+
headers,
|
|
61
|
+
});
|
|
62
|
+
return result.data;
|
|
63
|
+
} catch (e) {
|
|
64
|
+
console.error("获取失败", url, e.message);
|
|
65
|
+
return new Ret(false, "获取失败", Ret.CODE_SYSTEM);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
const assert = require("assert");
|
|
2
|
+
const Service = require("../src/service.js");
|
|
3
|
+
|
|
4
|
+
describe("service.js", () => {
|
|
5
|
+
before(() => {
|
|
6
|
+
console.log("init");
|
|
7
|
+
Service.init("shenga.co", true);
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
describe("get()", () => {
|
|
11
|
+
it("ok", async () => {
|
|
12
|
+
const result = await Service.get("supplier", "/");
|
|
13
|
+
console.log(result);
|
|
14
|
+
assert(result.code == 1004);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it("async", async () => {
|
|
18
|
+
const result = await Service.get("supplier", "/", null, true, 2);
|
|
19
|
+
console.log(result);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it("fail", async () => {
|
|
23
|
+
const result = await Service.get("a", "/");
|
|
24
|
+
console.log(result);
|
|
25
|
+
assert(result.code == 1004);
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
describe("post()", () => {
|
|
30
|
+
it("ok", async () => {
|
|
31
|
+
const result = await Service.post("supplier", "/");
|
|
32
|
+
console.log(result);
|
|
33
|
+
assert(result.code == 1004);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it("async", async () => {
|
|
37
|
+
const result = await Service.get("supplier", "/", null, true, 2);
|
|
38
|
+
console.log(result);
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
});
|