@less-is-more/less-js 1.4.10 → 1.4.12

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.4.10",
3
+ "version": "1.4.12",
4
4
  "description": "Fast develop kit for nodejs",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
package/src/router.js CHANGED
@@ -18,6 +18,8 @@ module.exports = class Router {
18
18
  */
19
19
  static async route(targets, req, res, context) {
20
20
  const startTime = new Date().getTime();
21
+ // 兼容非阿里云
22
+ await this._handleParams(req);
21
23
  const { methodName, className } = this._getPath(req);
22
24
  if (methodName) {
23
25
  const controller = targets[className];
@@ -27,8 +29,6 @@ module.exports = class Router {
27
29
  const method = controller[methodName];
28
30
  if (method) {
29
31
  try {
30
- // 兼容非阿里云
31
- await this._handleParams(req);
32
32
  // before
33
33
  if (targets.before) {
34
34
  console.log("start before action");
@@ -61,7 +61,13 @@ module.exports = class Router {
61
61
  }
62
62
 
63
63
  static _getPath(req) {
64
- console.log("path:", req.method, req.path, req.headers["content-type"]);
64
+ console.log(
65
+ "path:",
66
+ req.method,
67
+ req.path,
68
+ req.headers["content-type"],
69
+ JSON.stringify(req.params)
70
+ );
65
71
  const paths = req.path.split("/");
66
72
  const className = paths.length > 2 ? "/" + paths[paths.length - 2] : "/";
67
73
  const methodName = paths[paths.length - 1];
package/src/service.js CHANGED
@@ -52,14 +52,22 @@ module.exports = class Service {
52
52
  path,
53
53
  params = {},
54
54
  async = false,
55
- asyncDelaySecond = 0
55
+ asyncDelaySecond = 0,
56
+ formFormat = true
56
57
  ) {
57
58
  let url = this.#urlPrefix.replace("SERVICE", serviceName) + path;
58
59
  try {
59
- const headers = this.setDelay(async, asyncDelaySecond);
60
- const result = await axios.post(url, qs.stringify(params), {
61
- headers,
62
- });
60
+ let headers = this.setDelay(async, asyncDelaySecond);
61
+ if (!formFormat) {
62
+ headers["Content-Type"] = "application/json";
63
+ }
64
+ const result = await axios.post(
65
+ url,
66
+ formFormat ? qs.stringify(params) : params,
67
+ {
68
+ headers,
69
+ }
70
+ );
63
71
  return result.data;
64
72
  } catch (e) {
65
73
  console.error("获取失败", url, e.message);
@@ -37,5 +37,11 @@ describe("service.js", () => {
37
37
  const result = await Service.get("supplier", "/", null, true, 2);
38
38
  console.log(result);
39
39
  });
40
+
41
+ it("json format", async () => {
42
+ const result = await Service.post("post", "/", { a: 1 }, false, 0, false);
43
+ console.log(result);
44
+ assert(result.success);
45
+ });
40
46
  });
41
47
  });