starlight-server 1.7.4 → 1.7.6

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.
@@ -7,7 +7,7 @@ declare class RequestHelpers {
7
7
  readonly request: Request;
8
8
  readonly pathParameters: PathParameters;
9
9
  constructor(request: Request, pathParameters: PathParameters);
10
- validatePathParameters<Definition extends Record<string, ValidatorDefinition>>(struct: Definition): import("@anjianshi/utils/validators/base.js").Validated<{
10
+ validatePathParameters<const Definition extends Record<string, ValidatorDefinition>>(struct: Definition): import("@anjianshi/utils/validators/base.js").Validated<{
11
11
  readonly type: "struct";
12
12
  readonly struct: Definition;
13
13
  } extends infer T ? T extends {
@@ -24,7 +24,7 @@ declare class RequestHelpers {
24
24
  } : T_1 extends import("@anjianshi/utils/validators/factory.js").RecordDefinition ? Omit<T_1, "record"> & {
25
25
  record: import("@anjianshi/utils/validators/factory.js").ValidatorForDefinition<T_1["record"]>;
26
26
  } : T_1 : never : never>;
27
- validateQuery<Definition extends Record<string, ValidatorDefinition>>(struct: Definition): import("@anjianshi/utils/validators/base.js").Validated<{
27
+ validateQuery<const Definition extends Record<string, ValidatorDefinition>>(struct: Definition): import("@anjianshi/utils/validators/base.js").Validated<{
28
28
  readonly type: "struct";
29
29
  readonly struct: Definition;
30
30
  } extends infer T ? T extends {
@@ -41,7 +41,7 @@ declare class RequestHelpers {
41
41
  } : T_1 extends import("@anjianshi/utils/validators/factory.js").RecordDefinition ? Omit<T_1, "record"> & {
42
42
  record: import("@anjianshi/utils/validators/factory.js").ValidatorForDefinition<T_1["record"]>;
43
43
  } : T_1 : never : never>;
44
- validateBody<Definition extends Record<string, ValidatorDefinition>>(struct: Definition): Promise<import("@anjianshi/utils/validators/base.js").Validated<{
44
+ validateBody<const Definition extends Record<string, ValidatorDefinition>>(struct: Definition): Promise<import("@anjianshi/utils/validators/base.js").Validated<{
45
45
  readonly type: "struct";
46
46
  readonly struct: Definition;
47
47
  } extends infer T ? T extends {
@@ -51,9 +51,13 @@ class ResponseUtilsHelpers {
51
51
  // https://github.com/microsoft/TypeScript/issues/36753
52
52
  // 输出成功结果
53
53
  success(data) {
54
+ if (data instanceof Promise)
55
+ throw new Error('不能用 promise 作为响应内容');
54
56
  this.maySuccess(success(data));
55
57
  }
56
58
  failed(message, code, data) {
59
+ if (data instanceof Promise)
60
+ throw new Error('不能用 promise 作为响应内容');
57
61
  this.maySuccess(failed(message, code, data));
58
62
  }
59
63
  // 输出 MaySuccess 对象
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "starlight-server",
3
- "version": "1.7.4",
3
+ "version": "1.7.6",
4
4
  "description": "Simple But Powerful Node.js HTTP Server",
5
5
  "type": "module",
6
6
  "keywords": [
@@ -14,7 +14,7 @@ class RequestHelpers {
14
14
  readonly pathParameters: PathParameters,
15
15
  ) {}
16
16
 
17
- validatePathParameters<Definition extends Record<string, ValidatorDefinition>>(
17
+ validatePathParameters<const Definition extends Record<string, ValidatorDefinition>>(
18
18
  struct: Definition,
19
19
  ) {
20
20
  const result = getValidator({ type: 'struct', struct })(
@@ -25,13 +25,15 @@ class RequestHelpers {
25
25
  throw new HTTPError(400, result.message)
26
26
  }
27
27
 
28
- validateQuery<Definition extends Record<string, ValidatorDefinition>>(struct: Definition) {
28
+ validateQuery<const Definition extends Record<string, ValidatorDefinition>>(struct: Definition) {
29
29
  const result = getValidator({ type: 'struct', struct })('query', this.request.query)
30
30
  if (result.success) return result.data
31
31
  throw new HTTPError(400, result.message)
32
32
  }
33
33
 
34
- async validateBody<Definition extends Record<string, ValidatorDefinition>>(struct: Definition) {
34
+ async validateBody<const Definition extends Record<string, ValidatorDefinition>>(
35
+ struct: Definition,
36
+ ) {
35
37
  const body = await this.request.body.json()
36
38
  if (typeof body !== 'object' || body === null || Array.isArray(body))
37
39
  throw new HTTPError(400, 'Invalid JSON body, should be an object.')
@@ -65,6 +67,7 @@ class ResponseUtilsHelpers {
65
67
 
66
68
  // 输出成功结果
67
69
  success(data?: unknown) {
70
+ if (data instanceof Promise) throw new Error('不能用 promise 作为响应内容')
68
71
  this.maySuccess(success(data))
69
72
  }
70
73
 
@@ -72,6 +75,7 @@ class ResponseUtilsHelpers {
72
75
  failed(message: string, code?: string | number): void
73
76
  failed<T>(message: string, code: string | number | undefined, data: T): void
74
77
  failed<T>(message: string, code?: string | number, data?: T) {
78
+ if (data instanceof Promise) throw new Error('不能用 promise 作为响应内容')
75
79
  this.maySuccess(failed(message, code, data))
76
80
  }
77
81