starlight-server 1.7.2 → 1.7.3

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.
@@ -1,3 +1,4 @@
1
+ import { type MaySuccess } from '@anjianshi/utils';
1
2
  import { type Definition as ValidatorDefinition } from '@anjianshi/utils/validators/index.js';
2
3
  import { type Request } from '../http/request.js';
3
4
  import { type ResponseUtils } from '../http/response.js';
@@ -63,9 +64,12 @@ export declare function getRequestWithHelpers(request: Request, pathParameters:
63
64
  declare class ResponseUtilsHelpers {
64
65
  readonly response: ResponseUtils;
65
66
  constructor(response: ResponseUtils);
66
- success(data?: unknown): void;
67
- failed(message: string, code?: string | number): void;
68
- failed<T>(message: string, code: string | number | undefined, data: T): void;
67
+ success(data?: unknown): never;
68
+ failed(message: string, code?: string | number): never;
69
+ failed<T>(message: string, code: string | number | undefined, data: T): never;
70
+ maySuccess(result: MaySuccess<unknown, unknown>): never;
71
+ }
72
+ export declare class ResponseFinish {
69
73
  }
70
74
  export type ResponseUtilsWithHelpers = ResponseUtils & ResponseUtilsHelpers;
71
75
  export declare function getResponseUtilsWithHelpers(response: ResponseUtils): ResponseUtilsWithHelpers;
@@ -46,12 +46,22 @@ class ResponseUtilsHelpers {
46
46
  constructor(response) {
47
47
  this.response = response;
48
48
  }
49
+ // 输出成功结果,并结束响应处理
49
50
  success(data) {
50
- this.response.json(success(data));
51
+ this.maySuccess(success(data));
51
52
  }
52
53
  failed(message, code, data) {
53
- this.response.json(failed(message, code, data));
54
+ this.maySuccess(failed(message, code, data));
54
55
  }
56
+ // 输出 MaySuccess 对象,并结束响应处理
57
+ maySuccess(result) {
58
+ this.response.json(result);
59
+ throw new ResponseFinish();
60
+ }
61
+ }
62
+ // 通过捕获此对象实现提前结束响应处理
63
+ // eslint-disable-next-line @typescript-eslint/no-extraneous-class
64
+ export class ResponseFinish {
55
65
  }
56
66
  export function getResponseUtilsWithHelpers(response) {
57
67
  const helpers = new ResponseUtilsHelpers(response);
@@ -2,7 +2,7 @@ import { type OptionalFields } from '@anjianshi/utils';
2
2
  import { type CORSRule } from '../http/cors.js';
3
3
  import { type Request, type ResponseUtils } from '../http/index.js';
4
4
  import { Swagger, type Method } from '../swagger/index.js';
5
- import { type RequestWithHelpers, type ResponseUtilsWithHelpers } from './helpers.js';
5
+ import { type RequestWithHelpers, type ResponseUtilsWithHelpers, ResponseFinish } from './helpers.js';
6
6
  import { type PathParameters } from './match-path.js';
7
7
  export interface BasicContext {
8
8
  pathParameters: PathParameters;
@@ -21,6 +21,8 @@ export interface Route {
21
21
  /** 此接口的 CORS 规则 */
22
22
  cors?: CORSRule;
23
23
  }
24
+ /** handler 里抛出此异常可提前结束请求处理 */
25
+ export { ResponseFinish };
24
26
  export declare class Router {
25
27
  /**
26
28
  * ----------------------
@@ -2,8 +2,10 @@ import { joinPath } from '@anjianshi/utils';
2
2
  import { getPreflightRequestMethod, handleCORS } from '../http/cors.js';
3
3
  import { HTTPError } from '../http/index.js';
4
4
  import { Swagger } from '../swagger/index.js';
5
- import { getRequestWithHelpers, getResponseUtilsWithHelpers, } from './helpers.js';
5
+ import { getRequestWithHelpers, getResponseUtilsWithHelpers, ResponseFinish, } from './helpers.js';
6
6
  import { matchPath } from './match-path.js';
7
+ /** handler 里抛出此异常可提前结束请求处理 */
8
+ export { ResponseFinish };
7
9
  export class Router {
8
10
  /**
9
11
  * ----------------------
@@ -106,8 +108,14 @@ export class Router {
106
108
  request: requestWithHelpers,
107
109
  response: responseWithHelpers,
108
110
  });
109
- const result = await this.executor(basicContext, matched.route);
110
- if (result !== undefined)
111
- throw new Error('route handler 不应该有返回值');
111
+ try {
112
+ const result = await this.executor(basicContext, matched.route);
113
+ if (result !== undefined)
114
+ throw new Error('route handler 不应该有返回值');
115
+ }
116
+ catch (error) {
117
+ if (!(error instanceof ResponseFinish))
118
+ throw error;
119
+ }
112
120
  };
113
121
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "starlight-server",
3
- "version": "1.7.2",
3
+ "version": "1.7.3",
4
4
  "description": "Simple But Powerful Node.js HTTP Server",
5
5
  "type": "module",
6
6
  "keywords": [
@@ -1,4 +1,4 @@
1
- import { success, failed } from '@anjianshi/utils'
1
+ import { type MaySuccess, success, failed } from '@anjianshi/utils'
2
2
  import {
3
3
  getValidator,
4
4
  type Definition as ValidatorDefinition,
@@ -59,17 +59,29 @@ export function getRequestWithHelpers(request: Request, pathParameters: PathPara
59
59
  class ResponseUtilsHelpers {
60
60
  constructor(readonly response: ResponseUtils) {}
61
61
 
62
- success(data?: unknown) {
63
- this.response.json(success(data))
62
+ // 输出成功结果,并结束响应处理
63
+ success(data?: unknown): never {
64
+ this.maySuccess(success(data))
64
65
  }
65
66
 
66
- failed(message: string, code?: string | number): void
67
- failed<T>(message: string, code: string | number | undefined, data: T): void
67
+ // 输出失败结果,并结束响应处理
68
+ failed(message: string, code?: string | number): never
69
+ failed<T>(message: string, code: string | number | undefined, data: T): never
68
70
  failed<T>(message: string, code?: string | number, data?: T) {
69
- this.response.json(failed(message, code, data))
71
+ this.maySuccess(failed(message, code, data))
72
+ }
73
+
74
+ // 输出 MaySuccess 对象,并结束响应处理
75
+ maySuccess(result: MaySuccess<unknown, unknown>): never {
76
+ this.response.json(result)
77
+ throw new ResponseFinish()
70
78
  }
71
79
  }
72
80
 
81
+ // 通过捕获此对象实现提前结束响应处理
82
+ // eslint-disable-next-line @typescript-eslint/no-extraneous-class
83
+ export class ResponseFinish {}
84
+
73
85
  export type ResponseUtilsWithHelpers = ResponseUtils & ResponseUtilsHelpers
74
86
 
75
87
  export function getResponseUtilsWithHelpers(response: ResponseUtils) {
@@ -7,6 +7,7 @@ import {
7
7
  getResponseUtilsWithHelpers,
8
8
  type RequestWithHelpers,
9
9
  type ResponseUtilsWithHelpers,
10
+ ResponseFinish,
10
11
  } from './helpers.js'
11
12
  import { matchPath, type PathParameters } from './match-path.js'
12
13
 
@@ -29,6 +30,9 @@ export interface Route {
29
30
  cors?: CORSRule
30
31
  }
31
32
 
33
+ /** handler 里抛出此异常可提前结束请求处理 */
34
+ export { ResponseFinish }
35
+
32
36
  export class Router {
33
37
  /**
34
38
  * ----------------------
@@ -143,7 +147,11 @@ export class Router {
143
147
  response: responseWithHelpers,
144
148
  })
145
149
 
146
- const result = await (this.executor(basicContext, matched.route) as Promise<unknown>)
147
- if (result !== undefined) throw new Error('route handler 不应该有返回值')
150
+ try {
151
+ const result = await (this.executor(basicContext, matched.route) as Promise<unknown>)
152
+ if (result !== undefined) throw new Error('route handler 不应该有返回值')
153
+ } catch (error) {
154
+ if (!(error instanceof ResponseFinish)) throw error
155
+ }
148
156
  }
149
157
  }