@less-is-more/less-js 1.2.23 → 1.2.26

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@less-is-more/less-js",
3
- "version": "1.2.23",
3
+ "version": "1.2.26",
4
4
  "description": "Fast develop kit for nodejs",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
package/src/cache.js CHANGED
@@ -50,12 +50,16 @@ module.exports = class Cache {
50
50
  if (!Param.isBlank(result)) {
51
51
  // 处理JSON
52
52
  if (result instanceof Object) {
53
- await Redis.exec(
54
- "setex",
55
- fullKey,
56
- timeSecond + "",
57
- JSON.stringify(result)
58
- );
53
+ if (result.success === false) {
54
+ console.log("Do not cache fail result");
55
+ } else {
56
+ await Redis.exec(
57
+ "setex",
58
+ fullKey,
59
+ timeSecond + "",
60
+ JSON.stringify(result)
61
+ );
62
+ }
59
63
  } else {
60
64
  await Redis.exec(
61
65
  "setex",
package/src/ret.js CHANGED
@@ -6,65 +6,64 @@ module.exports = class Ret {
6
6
  static CODE_SYSTEM = 1004;
7
7
 
8
8
  constructor(success, message, code) {
9
- this.#instance.success = success;
9
+ this.success = success;
10
10
  if (message) {
11
- this.#instance.message = message;
11
+ this.message = message;
12
12
  }
13
13
  if (code) {
14
- this.#instance.code = code;
14
+ this.code = code;
15
15
  }
16
16
  }
17
17
 
18
- #instance = {};
19
18
  ok() {
20
- this.#instance.success = true;
19
+ this.success = true;
21
20
  return this;
22
21
  }
23
- isOk(){
24
- return this.#instance.success;
22
+ isOk() {
23
+ return this.success;
25
24
  }
26
25
  fail() {
27
- this.#instance.success = false;
26
+ this.success = false;
28
27
  return this;
29
28
  }
30
- isFail(){
31
- return !this.#instance.success;
29
+ isFail() {
30
+ return !this.success;
32
31
  }
33
32
  setMessage(message) {
34
- this.#instance.message = message;
33
+ this.message = message;
35
34
  return this;
36
35
  }
37
36
  getMessage() {
38
- return this.#instance.message;
37
+ return this.message;
39
38
  }
40
39
  setCode(code) {
41
- this.#instance.code = code;
40
+ this.code = code;
42
41
  return this;
43
42
  }
44
43
  setData(data) {
45
- this.#instance.data = data;
44
+ this.data = data;
46
45
  return this;
47
46
  }
48
47
  getData() {
49
- return this.#instance.data;
48
+ return this.data;
50
49
  }
51
50
  toString() {
52
- return JSON.stringify(this.#instance);
51
+ return JSON.stringify(this);
53
52
  }
54
53
  getJson() {
55
- return this.#instance;
54
+ return this;
56
55
  }
57
56
  toJson() {
58
- return this.#instance;
57
+ return this;
59
58
  }
60
59
 
61
60
  filter(props = []) {
62
61
  if (props.length > 0) {
63
- const data = this.#instance.data;
62
+ const data = this.data;
64
63
  if (data instanceof Array) {
65
- this.#instance.data = this._filterArray(data, props);
64
+ this.data = this._filterArray(data, props);
66
65
  } else {
67
- this.#instance.data = this._filterObject(data, props);
66
+ this.data = this._filterObject(data, props);
68
67
  }
69
68
  }
70
69
  return this;
package/src/router.js CHANGED
@@ -37,6 +37,7 @@ module.exports = class Router {
37
37
  console.log("start after action");
38
38
  await targets.after(req, res, context);
39
39
  }
40
+ console.log("end", (new Date().getTime() - startTime) / 1000, "s");
40
41
  // controller方法有返回值的自动send
41
42
  this._handleReturn(result, res);
42
43
  } catch (e) {
@@ -49,7 +50,6 @@ module.exports = class Router {
49
50
  } else {
50
51
  return this._sendError("请输入方法", res);
51
52
  }
52
- console.log("end", (new Date().getTime() - startTime) / 1000, "s");
53
53
  }
54
54
 
55
55
  static _getPath(req) {
@@ -104,18 +104,22 @@ module.exports = class Router {
104
104
 
105
105
  static _handleReturn(result, res) {
106
106
  if (!Param.isBlank(result)) {
107
+ let content;
107
108
  if (result instanceof Ret) {
108
- console.log("return:", result.toString());
109
- res.send(result.toString());
109
+ content = result.toString();
110
110
  } else {
111
111
  if (typeof result === "string") {
112
- console.log("return:", result);
113
- res.send(result);
112
+ content = result;
114
113
  } else {
115
- console.log("return:", JSON.stringify(result));
116
- res.send(JSON.stringify(result));
114
+ content = Json.stringify(result);
117
115
  }
118
116
  }
117
+ if (content.length > 500) {
118
+ console.log("return:", content.substring(0, 500));
119
+ } else {
120
+ console.log("return:", content);
121
+ }
122
+ res.send(content);
119
123
  }
120
124
  }
121
125
 
@@ -1,6 +1,7 @@
1
1
  const assert = require("assert");
2
2
  const Cache = require("../src/cache.js");
3
3
  const Redis = require("../src/redis.js");
4
+ const Ret = require("../src/ret.js");
4
5
 
5
6
  describe("cache.js", () => {
6
7
  before(() => {
@@ -92,5 +93,15 @@ describe("cache.js", () => {
92
93
  console.log("result", JSON.stringify(result));
93
94
  assert(result.length === 1);
94
95
  });
96
+ it("no cache fail", async () => {
97
+ async function test(a, b) {
98
+ return await Cache.auto("test5", 20, arguments, () => {
99
+ return new Ret(false);
100
+ });
101
+ }
102
+ const result = await test(1, 2);
103
+ console.log("result", JSON.stringify(result));
104
+ assert(result.success === false);
105
+ });
95
106
  });
96
107
  });
package/test/test-ret.js CHANGED
@@ -5,6 +5,7 @@ describe("ret.js", () => {
5
5
  describe("new()", () => {
6
6
  it("ok", () => {
7
7
  let ret = new Ret(true).setData({ name: "good" });
8
+ console.log(ret)
8
9
  assert(ret.getJson().data.name, "good");
9
10
  });
10
11
  it("mutli params", () => {