starlight-server 1.9.0 → 1.9.2

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.
@@ -69,7 +69,8 @@ declare class ResponseUtilsHelpers {
69
69
  * 支持传入第二个方法来对数据内容进行格式化:
70
70
  * response.success(formatSuccess(result, formatter)) => response.success(result, formatter)
71
71
  */
72
- success(data?: unknown, formatter?: <T, R>(data: T) => R): void;
72
+ success(): void;
73
+ success<T>(data: T, formatter?: (data: T) => unknown): void;
73
74
  /**
74
75
  * 输出失败结果
75
76
  */
@@ -80,7 +81,7 @@ declare class ResponseUtilsHelpers {
80
81
  * 支持传入第二个方法来对成功结果的数据内容进行格式化:
81
82
  * response.result(formatSuccess(result, formatter)) => response.result(result, formatter)
82
83
  */
83
- result(result: Result<unknown, unknown>, formatterForSuccess?: <T, R>(data: T) => R): void;
84
+ result<T>(result: Result<T, unknown>, formatterForSuccess?: (data: T) => unknown): void;
84
85
  }
85
86
  export type ResponseUtilsWithHelpers = ResponseUtils & ResponseUtilsHelpers;
86
87
  export declare function getResponseUtilsWithHelpers(response: ResponseUtils): ResponseUtilsWithHelpers;
@@ -46,18 +46,15 @@ class ResponseUtilsHelpers {
46
46
  constructor(response) {
47
47
  this.response = response;
48
48
  }
49
- // 原本这里想做成调用方法后直接结束 handler
50
- // 但因为 TypeScript 的限制,无法正确处理类方法的 never 返回值,所以作罢
51
- // https://github.com/microsoft/TypeScript/issues/36753
52
- /**
53
- * 输出成功结果
54
- * 支持传入第二个方法来对数据内容进行格式化:
55
- * response.success(formatSuccess(result, formatter)) => response.success(result, formatter)
56
- */
57
49
  success(data, formatter) {
58
- if (data instanceof Promise)
59
- throw new Error('不能用 promise 作为响应内容');
60
- this.result(success(data), formatter);
50
+ if (data === undefined) {
51
+ this.result(success());
52
+ }
53
+ else {
54
+ if (data instanceof Promise)
55
+ throw new Error('不能用 promise 作为响应内容');
56
+ this.result(success(data), formatter);
57
+ }
61
58
  }
62
59
  failed(message, code, data) {
63
60
  if (data instanceof Promise)
@@ -71,9 +68,11 @@ class ResponseUtilsHelpers {
71
68
  */
72
69
  result(result, formatterForSuccess) {
73
70
  if (formatterForSuccess) {
74
- result = formatSuccess(result, formatterForSuccess);
71
+ this.response.json(formatSuccess(result, formatterForSuccess));
72
+ }
73
+ else {
74
+ this.response.json(result);
75
75
  }
76
- this.response.json(result);
77
76
  }
78
77
  }
79
78
  export function getResponseUtilsWithHelpers(response) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "starlight-server",
3
- "version": "1.9.0",
3
+ "version": "1.9.2",
4
4
  "description": "Simple But Powerful Node.js HTTP Server",
5
5
  "type": "module",
6
6
  "keywords": [
@@ -70,9 +70,15 @@ class ResponseUtilsHelpers {
70
70
  * 支持传入第二个方法来对数据内容进行格式化:
71
71
  * response.success(formatSuccess(result, formatter)) => response.success(result, formatter)
72
72
  */
73
- success(data?: unknown, formatter?: <T, R>(data: T) => R): void {
74
- if (data instanceof Promise) throw new Error('不能用 promise 作为响应内容')
75
- this.result(success(data), formatter)
73
+ success(): void
74
+ success<T>(data: T, formatter?: (data: T) => unknown): void
75
+ success<T>(data?: T, formatter?: (data: T) => unknown): void {
76
+ if (data === undefined) {
77
+ this.result(success())
78
+ } else {
79
+ if (data instanceof Promise) throw new Error('不能用 promise 作为响应内容')
80
+ this.result(success(data), formatter)
81
+ }
76
82
  }
77
83
 
78
84
  /**
@@ -90,11 +96,12 @@ class ResponseUtilsHelpers {
90
96
  * 支持传入第二个方法来对成功结果的数据内容进行格式化:
91
97
  * response.result(formatSuccess(result, formatter)) => response.result(result, formatter)
92
98
  */
93
- result(result: Result<unknown, unknown>, formatterForSuccess?: <T, R>(data: T) => R): void {
99
+ result<T>(result: Result<T, unknown>, formatterForSuccess?: (data: T) => unknown): void {
94
100
  if (formatterForSuccess) {
95
- result = formatSuccess(result, formatterForSuccess)
101
+ this.response.json(formatSuccess(result, formatterForSuccess))
102
+ } else {
103
+ this.response.json(result)
96
104
  }
97
- this.response.json(result)
98
105
  }
99
106
  }
100
107