starlight-server 1.7.3 → 1.7.4

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.
@@ -64,12 +64,10 @@ export declare function getRequestWithHelpers(request: Request, pathParameters:
64
64
  declare class ResponseUtilsHelpers {
65
65
  readonly response: ResponseUtils;
66
66
  constructor(response: ResponseUtils);
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 {
67
+ success(data?: unknown): void;
68
+ failed(message: string, code?: string | number): void;
69
+ failed<T>(message: string, code: string | number | undefined, data: T): void;
70
+ maySuccess(result: MaySuccess<unknown, unknown>): void;
73
71
  }
74
72
  export type ResponseUtilsWithHelpers = ResponseUtils & ResponseUtilsHelpers;
75
73
  export declare function getResponseUtilsWithHelpers(response: ResponseUtils): ResponseUtilsWithHelpers;
@@ -46,23 +46,21 @@ class ResponseUtilsHelpers {
46
46
  constructor(response) {
47
47
  this.response = response;
48
48
  }
49
- // 输出成功结果,并结束响应处理
49
+ // 原本这里想做成调用方法后直接结束 handler
50
+ // 但因为 TypeScript 的限制,无法正确处理类方法的 never 返回值,所以作罢
51
+ // https://github.com/microsoft/TypeScript/issues/36753
52
+ // 输出成功结果
50
53
  success(data) {
51
54
  this.maySuccess(success(data));
52
55
  }
53
56
  failed(message, code, data) {
54
57
  this.maySuccess(failed(message, code, data));
55
58
  }
56
- // 输出 MaySuccess 对象,并结束响应处理
59
+ // 输出 MaySuccess 对象
57
60
  maySuccess(result) {
58
61
  this.response.json(result);
59
- throw new ResponseFinish();
60
62
  }
61
63
  }
62
- // 通过捕获此对象实现提前结束响应处理
63
- // eslint-disable-next-line @typescript-eslint/no-extraneous-class
64
- export class ResponseFinish {
65
- }
66
64
  export function getResponseUtilsWithHelpers(response) {
67
65
  const helpers = new ResponseUtilsHelpers(response);
68
66
  return new Proxy(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, ResponseFinish } from './helpers.js';
5
+ import { type RequestWithHelpers, type ResponseUtilsWithHelpers } from './helpers.js';
6
6
  import { type PathParameters } from './match-path.js';
7
7
  export interface BasicContext {
8
8
  pathParameters: PathParameters;
@@ -21,8 +21,6 @@ export interface Route {
21
21
  /** 此接口的 CORS 规则 */
22
22
  cors?: CORSRule;
23
23
  }
24
- /** handler 里抛出此异常可提前结束请求处理 */
25
- export { ResponseFinish };
26
24
  export declare class Router {
27
25
  /**
28
26
  * ----------------------
@@ -2,10 +2,8 @@ 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, ResponseFinish, } from './helpers.js';
5
+ import { getRequestWithHelpers, getResponseUtilsWithHelpers, } from './helpers.js';
6
6
  import { matchPath } from './match-path.js';
7
- /** handler 里抛出此异常可提前结束请求处理 */
8
- export { ResponseFinish };
9
7
  export class Router {
10
8
  /**
11
9
  * ----------------------
@@ -108,14 +106,8 @@ export class Router {
108
106
  request: requestWithHelpers,
109
107
  response: responseWithHelpers,
110
108
  });
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
- }
109
+ const result = await this.executor(basicContext, matched.route);
110
+ if (result !== undefined)
111
+ throw new Error('route handler 不应该有返回值');
120
112
  };
121
113
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "starlight-server",
3
- "version": "1.7.3",
3
+ "version": "1.7.4",
4
4
  "description": "Simple But Powerful Node.js HTTP Server",
5
5
  "type": "module",
6
6
  "keywords": [
@@ -59,29 +59,28 @@ export function getRequestWithHelpers(request: Request, pathParameters: PathPara
59
59
  class ResponseUtilsHelpers {
60
60
  constructor(readonly response: ResponseUtils) {}
61
61
 
62
- // 输出成功结果,并结束响应处理
63
- success(data?: unknown): never {
62
+ // 原本这里想做成调用方法后直接结束 handler
63
+ // 但因为 TypeScript 的限制,无法正确处理类方法的 never 返回值,所以作罢
64
+ // https://github.com/microsoft/TypeScript/issues/36753
65
+
66
+ // 输出成功结果
67
+ success(data?: unknown) {
64
68
  this.maySuccess(success(data))
65
69
  }
66
70
 
67
- // 输出失败结果,并结束响应处理
68
- failed(message: string, code?: string | number): never
69
- failed<T>(message: string, code: string | number | undefined, data: T): never
71
+ // 输出失败结果
72
+ failed(message: string, code?: string | number): void
73
+ failed<T>(message: string, code: string | number | undefined, data: T): void
70
74
  failed<T>(message: string, code?: string | number, data?: T) {
71
75
  this.maySuccess(failed(message, code, data))
72
76
  }
73
77
 
74
- // 输出 MaySuccess 对象,并结束响应处理
75
- maySuccess(result: MaySuccess<unknown, unknown>): never {
78
+ // 输出 MaySuccess 对象
79
+ maySuccess(result: MaySuccess<unknown, unknown>): void {
76
80
  this.response.json(result)
77
- throw new ResponseFinish()
78
81
  }
79
82
  }
80
83
 
81
- // 通过捕获此对象实现提前结束响应处理
82
- // eslint-disable-next-line @typescript-eslint/no-extraneous-class
83
- export class ResponseFinish {}
84
-
85
84
  export type ResponseUtilsWithHelpers = ResponseUtils & ResponseUtilsHelpers
86
85
 
87
86
  export function getResponseUtilsWithHelpers(response: ResponseUtils) {
@@ -7,7 +7,6 @@ import {
7
7
  getResponseUtilsWithHelpers,
8
8
  type RequestWithHelpers,
9
9
  type ResponseUtilsWithHelpers,
10
- ResponseFinish,
11
10
  } from './helpers.js'
12
11
  import { matchPath, type PathParameters } from './match-path.js'
13
12
 
@@ -30,9 +29,6 @@ export interface Route {
30
29
  cors?: CORSRule
31
30
  }
32
31
 
33
- /** handler 里抛出此异常可提前结束请求处理 */
34
- export { ResponseFinish }
35
-
36
32
  export class Router {
37
33
  /**
38
34
  * ----------------------
@@ -147,11 +143,7 @@ export class Router {
147
143
  response: responseWithHelpers,
148
144
  })
149
145
 
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
- }
146
+ const result = await (this.executor(basicContext, matched.route) as Promise<unknown>)
147
+ if (result !== undefined) throw new Error('route handler 不应该有返回值')
156
148
  }
157
149
  }