starlight-server 1.7.0 → 1.7.1

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,5 +1,6 @@
1
1
  import { type Definition as ValidatorDefinition } from '@anjianshi/utils/validators/index.js';
2
2
  import { type BasicContext } from './index.js';
3
+ import { ResponseUtils } from '../http/response.js';
3
4
  export declare class Helpers {
4
5
  readonly context: BasicContext;
5
6
  constructor(context: BasicContext);
@@ -54,6 +55,9 @@ export declare class Helpers {
54
55
  } : T_1 extends import("@anjianshi/utils/validators/factory.js").RecordDefinition ? Omit<T_1, "record"> & {
55
56
  record: import("@anjianshi/utils/validators/factory.js").ValidatorForDefinition<T_1["record"]>;
56
57
  } : T_1 : never : never>>;
58
+ }
59
+ export declare class ResponseUtilsWithHelpers extends ResponseUtils {
60
+ constructor(response: ResponseUtils);
57
61
  success(data?: unknown): void;
58
62
  failed(message: string, code?: string | number): void;
59
63
  failed<T>(message: string, code: string | number | undefined, data: T): void;
@@ -1,6 +1,7 @@
1
1
  import { success, failed } from '@anjianshi/utils';
2
2
  import { getValidator, } from '@anjianshi/utils/validators/index.js';
3
3
  import { HTTPError } from '../index.js';
4
+ import { ResponseUtils } from '../http/response.js';
4
5
  export class Helpers {
5
6
  context;
6
7
  constructor(context) {
@@ -27,10 +28,15 @@ export class Helpers {
27
28
  return result.data;
28
29
  throw new HTTPError(400, result.message);
29
30
  }
31
+ }
32
+ export class ResponseUtilsWithHelpers extends ResponseUtils {
33
+ constructor(response) {
34
+ super(response.nodeResponse, response.logger, response.jsonReplacer);
35
+ }
30
36
  success(data) {
31
- this.context.response.json(success(data));
37
+ this.json(success(data));
32
38
  }
33
39
  failed(message, code, data) {
34
- this.context.response.json(failed(message, code, data));
40
+ this.json(failed(message, code, data));
35
41
  }
36
42
  }
@@ -2,18 +2,16 @@ 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 { Helpers } from './helpers.js';
5
+ import { Helpers, ResponseUtilsWithHelpers } from './helpers.js';
6
6
  import { type PathParameters } from './match-path.js';
7
7
  type HelpersInst = InstanceType<typeof Helpers>;
8
8
  export interface BasicContext {
9
9
  request: Request;
10
- response: ResponseUtils;
10
+ response: ResponseUtilsWithHelpers;
11
11
  pathParameters: PathParameters;
12
12
  validatePathParameters: HelpersInst['validatePathParameters'];
13
13
  validateQuery: HelpersInst['validateQuery'];
14
14
  validateBody: HelpersInst['validateBody'];
15
- success: HelpersInst['success'];
16
- failed: HelpersInst['failed'];
17
15
  }
18
16
  /** 使用者可自行补充 Context 定义 */
19
17
  export interface Context extends BasicContext {
@@ -67,6 +65,6 @@ export declare class Router {
67
65
  * 请求处理
68
66
  * ----------------------
69
67
  */
70
- readonly handle: (request: Request, response: ResponseUtils) => Promise<void>;
68
+ readonly handle: (request: Request, originResponse: ResponseUtils) => Promise<void>;
71
69
  }
72
70
  export {};
@@ -2,7 +2,7 @@ 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 { Helpers } from './helpers.js';
5
+ import { Helpers, ResponseUtilsWithHelpers } from './helpers.js';
6
6
  import { matchPath } from './match-path.js';
7
7
  export class Router {
8
8
  /**
@@ -78,7 +78,8 @@ export class Router {
78
78
  * 请求处理
79
79
  * ----------------------
80
80
  */
81
- handle = async (request, response) => {
81
+ handle = async (request, originResponse) => {
82
+ const response = new ResponseUtilsWithHelpers(originResponse);
82
83
  const pathMatchedRoutes = matchPath(this.routes.map(route => route.path), request.path).map(result => ({ route: this.routes[result.index], parameters: result.parameters }));
83
84
  if (!pathMatchedRoutes.length)
84
85
  throw new HTTPError(404); // 没有路径匹配的路由
@@ -108,8 +109,6 @@ export class Router {
108
109
  validatePathParameters: helpers.validatePathParameters.bind(helpers),
109
110
  validateQuery: helpers.validateQuery.bind(helpers),
110
111
  validateBody: helpers.validateBody.bind(helpers),
111
- success: helpers.success.bind(helpers),
112
- failed: helpers.failed.bind(helpers),
113
112
  });
114
113
  const result = await this.executor(basicContext, matched.route);
115
114
  if (result !== undefined)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "starlight-server",
3
- "version": "1.7.0",
3
+ "version": "1.7.1",
4
4
  "description": "Simple But Powerful Node.js HTTP Server",
5
5
  "type": "module",
6
6
  "keywords": [
@@ -5,6 +5,7 @@ import {
5
5
  } from '@anjianshi/utils/validators/index.js'
6
6
  import { HTTPError } from '@/index.js'
7
7
  import { type BasicContext } from './index.js'
8
+ import { ResponseUtils } from '@/http/response.js'
8
9
 
9
10
  export class Helpers {
10
11
  constructor(readonly context: BasicContext) {}
@@ -35,14 +36,20 @@ export class Helpers {
35
36
  if (result.success) return result.data
36
37
  throw new HTTPError(400, result.message)
37
38
  }
39
+ }
40
+
41
+ export class ResponseUtilsWithHelpers extends ResponseUtils {
42
+ constructor(response: ResponseUtils) {
43
+ super(response.nodeResponse, response.logger, response.jsonReplacer)
44
+ }
38
45
 
39
46
  success(data?: unknown) {
40
- this.context.response.json(success(data))
47
+ this.json(success(data))
41
48
  }
42
49
 
43
50
  failed(message: string, code?: string | number): void
44
51
  failed<T>(message: string, code: string | number | undefined, data: T): void
45
52
  failed<T>(message: string, code?: string | number, data?: T) {
46
- this.context.response.json(failed(message, code, data))
53
+ this.json(failed(message, code, data))
47
54
  }
48
55
  }
@@ -2,19 +2,17 @@ import { type OptionalFields, joinPath } from '@anjianshi/utils'
2
2
  import { getPreflightRequestMethod, handleCORS, type CORSRule } from '@/http/cors.js'
3
3
  import { HTTPError, type Request, type ResponseUtils } from '@/http/index.js'
4
4
  import { Swagger, type Method } from '@/swagger/index.js'
5
- import { Helpers } from './helpers.js'
5
+ import { Helpers, ResponseUtilsWithHelpers } from './helpers.js'
6
6
  import { matchPath, type PathParameters } from './match-path.js'
7
7
 
8
8
  type HelpersInst = InstanceType<typeof Helpers>
9
9
  export interface BasicContext {
10
10
  request: Request
11
- response: ResponseUtils
11
+ response: ResponseUtilsWithHelpers
12
12
  pathParameters: PathParameters
13
13
  validatePathParameters: HelpersInst['validatePathParameters']
14
14
  validateQuery: HelpersInst['validateQuery']
15
15
  validateBody: HelpersInst['validateBody']
16
- success: HelpersInst['success']
17
- failed: HelpersInst['failed']
18
16
  }
19
17
 
20
18
  /** 使用者可自行补充 Context 定义 */
@@ -109,7 +107,9 @@ export class Router {
109
107
  * 请求处理
110
108
  * ----------------------
111
109
  */
112
- readonly handle = async (request: Request, response: ResponseUtils) => {
110
+ readonly handle = async (request: Request, originResponse: ResponseUtils) => {
111
+ const response = new ResponseUtilsWithHelpers(originResponse)
112
+
113
113
  const pathMatchedRoutes = matchPath(
114
114
  this.routes.map(route => route.path),
115
115
  request.path,
@@ -144,8 +144,6 @@ export class Router {
144
144
  validatePathParameters: helpers.validatePathParameters.bind(helpers),
145
145
  validateQuery: helpers.validateQuery.bind(helpers),
146
146
  validateBody: helpers.validateBody.bind(helpers),
147
- success: helpers.success.bind(helpers),
148
- failed: helpers.failed.bind(helpers),
149
147
  })
150
148
 
151
149
  const result = await (this.executor(basicContext, matched.route) as Promise<unknown>)