starlight-server 1.1.0 → 1.3.0

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.
@@ -25,6 +25,10 @@ router.register({
25
25
  description: 'hello world',
26
26
  method: 'GET',
27
27
  path: '/hello',
28
+ query: [
29
+ { name: 'q1', type: 'number', required: true },
30
+ { name: 'q2', type: 'string', description: '这是q2' },
31
+ ],
28
32
  body: [
29
33
  { name: 'abc', type: 'number' },
30
34
  { name: 'def', type: { array: { type: 'string' } } },
@@ -15,7 +15,7 @@ export type CORSOptions = boolean | {
15
15
  exposeHeaders?: string;
16
16
  };
17
17
  /**
18
- * 若当前请求是 CORS Preflight 请求,返回实际请求的 method,否则返回 null
18
+ * 若当前请求是 CORS Preflight 请求,返回实际请求的 method;若不是 Preflight 请求,返回 null
19
19
  */
20
20
  export declare function getMethodFromCORSPreflight(request: Request): string | null;
21
21
  /**
@@ -1,5 +1,5 @@
1
1
  /**
2
- * 若当前请求是 CORS Preflight 请求,返回实际请求的 method,否则返回 null
2
+ * 若当前请求是 CORS Preflight 请求,返回实际请求的 method;若不是 Preflight 请求,返回 null
3
3
  */
4
4
  export function getMethodFromCORSPreflight(request) {
5
5
  if (request.method === 'OPTIONS' &&
@@ -1,9 +1,9 @@
1
1
  /**
2
2
  * 标准化路径
3
- * 标准化后,相同的两个路径一定也是相同的字符串(例如 /Abc/Def abc/def/ 都会变成 abc/def)。
3
+ * 移除首尾和重复的 '/',完成后有 path 有这几种可能的格式: ''、'abc'、'abc/def'
4
+ * 例如 /abc/def 和 abc/def/ 都会变成 abc/def
4
5
  *
5
- * - 移除首尾和重复的 '/',完成后有 path 有这几种可能的格式: ''、'abc'、'abc/def'
6
- * - 统一改为小写
6
+ * 注意:此操作不会统一大小写,因此不保证标准化后两个字符串在代码层面 ===
7
7
  */
8
8
  export declare function normalizePath(path: string): string;
9
9
  /**
@@ -12,10 +12,10 @@
12
12
  import escapeRegExp from 'lodash/escapeRegExp.js';
13
13
  /**
14
14
  * 标准化路径
15
- * 标准化后,相同的两个路径一定也是相同的字符串(例如 /Abc/Def abc/def/ 都会变成 abc/def)。
15
+ * 移除首尾和重复的 '/',完成后有 path 有这几种可能的格式: ''、'abc'、'abc/def'
16
+ * 例如 /abc/def 和 abc/def/ 都会变成 abc/def
16
17
  *
17
- * - 移除首尾和重复的 '/',完成后有 path 有这几种可能的格式: ''、'abc'、'abc/def'
18
- * - 统一改为小写
18
+ * 注意:此操作不会统一大小写,因此不保证标准化后两个字符串在代码层面 ===
19
19
  */
20
20
  export function normalizePath(path) {
21
21
  if (path.startsWith('/'))
@@ -23,7 +23,7 @@ export function normalizePath(path) {
23
23
  if (path.endsWith('/'))
24
24
  path = path.slice(0, -1);
25
25
  path = path.replace(/\/+/g, '/');
26
- return path.toLowerCase();
26
+ return path;
27
27
  }
28
28
  /**
29
29
  * 解析路由路径定义
@@ -105,12 +105,12 @@ export declare abstract class Router<Ctx extends BaseContext = BaseContext> {
105
105
  /**
106
106
  * 为未指定 CORS 配置的 route 提供默认配置
107
107
  */
108
- getCORSOptions(request: Request, routeMatch: RouteMatch<Route<Ctx>>): boolean;
108
+ getCORSOptions(request: Request, routeMatch: RouteMatch<Route<Ctx>>): CORSOptions;
109
109
  /**
110
110
  * 完善 context 对象并执行 route
111
111
  * 注意:handler 在很多时候都是异步的,要用 await 等待执行完成
112
112
  */
113
- protected abstract executeWithContext(baseContext: BaseContext, route: Route<Ctx>, pathParameters: PathParameters): void | Promise<void>;
113
+ protected abstract executeWithContext(baseContext: BaseContext, route: Route<Ctx>, pathParameters: PathParameters): unknown;
114
114
  /**
115
115
  * response 定义中可引用的数据类型
116
116
  */
@@ -62,6 +62,13 @@ export declare function operation(route: RouteDefinition): {
62
62
  tags: string[];
63
63
  summary: string;
64
64
  operationId: string;
65
+ parameters: {
66
+ name: string;
67
+ in: "query";
68
+ description: string | undefined;
69
+ required: boolean | undefined;
70
+ schema: Schema;
71
+ }[] | undefined;
65
72
  requestBody: {
66
73
  content: {
67
74
  'application/json': {
@@ -85,7 +92,17 @@ export declare function operation(route: RouteDefinition): {
85
92
  */
86
93
  export declare function operationId(route: RouteDefinition): string;
87
94
  /**
88
- * 路由请求参数转为 swagger 定义
95
+ * 路由 query 参数转为 swagger 定义
96
+ */
97
+ export declare function queryParameters(route: RouteDefinition): {
98
+ name: string;
99
+ in: "query";
100
+ description: string | undefined;
101
+ required: boolean | undefined;
102
+ schema: Schema;
103
+ }[] | undefined;
104
+ /**
105
+ * 路由 body 参数转为 swagger 定义
89
106
  */
90
107
  export declare function requestBody(route: RouteDefinition): {
91
108
  content: {
@@ -113,6 +113,7 @@ export function operation(route) {
113
113
  tags: route.category ? [route.category] : [],
114
114
  summary: route.description,
115
115
  operationId: operationId(route),
116
+ parameters: queryParameters(route),
116
117
  requestBody: requestBody(route),
117
118
  responses: {
118
119
  default: {
@@ -129,7 +130,21 @@ export function operationId(route) {
129
130
  return `${route.method}-${route.path}`;
130
131
  }
131
132
  /**
132
- * 路由请求参数转为 swagger 定义
133
+ * 路由 query 参数转为 swagger 定义
134
+ */
135
+ export function queryParameters(route) {
136
+ if (!route.query)
137
+ return;
138
+ return route.query.map(parameter => ({
139
+ name: parameter.name,
140
+ in: 'query',
141
+ description: parameter.description,
142
+ required: parameter.required,
143
+ schema: parameterSchema(parameter),
144
+ }));
145
+ }
146
+ /**
147
+ * 路由 body 参数转为 swagger 定义
133
148
  */
134
149
  export function requestBody(route) {
135
150
  if (!route.body)
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "starlight-server",
3
- "version": "1.1.0",
3
+ "version": "1.3.0",
4
4
  "description": "Simple But Powerful Node.js HTTP Server",
5
5
  "type": "module",
6
6
  "scripts": {
7
- "dev": "npm run build && (concurrently \"tsc -w\" \"tsc-alias -w\" \"nodemon dist/demo/index.js\")",
7
+ "dev": "rimraf dist && (concurrently --raw \"tsc -w\" \"sleep 5 && tsc-alias -w\" \"sleep 6 && nodemon dist/demo/index.js\")",
8
8
  "build": "rimraf dist && tsc && tsc-alias",
9
9
  "prepublishOnly": "npm run build"
10
10
  },
package/src/demo/index.ts CHANGED
@@ -43,6 +43,10 @@ router.register({
43
43
  description: 'hello world',
44
44
  method: 'GET',
45
45
  path: '/hello',
46
+ query: [
47
+ { name: 'q1', type: 'number', required: true },
48
+ { name: 'q2', type: 'string', description: '这是q2' },
49
+ ],
46
50
  body: [
47
51
  { name: 'abc', type: 'number' },
48
52
  { name: 'def', type: { array: { type: 'string' } } },
@@ -15,7 +15,7 @@ export type CORSOptions =
15
15
  | { allowOrigin?: string; allowHeaders?: string; exposeHeaders?: string }
16
16
 
17
17
  /**
18
- * 若当前请求是 CORS Preflight 请求,返回实际请求的 method,否则返回 null
18
+ * 若当前请求是 CORS Preflight 请求,返回实际请求的 method;若不是 Preflight 请求,返回 null
19
19
  */
20
20
  export function getMethodFromCORSPreflight(request: Request) {
21
21
  if (
@@ -13,16 +13,16 @@ import escapeRegExp from 'lodash/escapeRegExp.js'
13
13
 
14
14
  /**
15
15
  * 标准化路径
16
- * 标准化后,相同的两个路径一定也是相同的字符串(例如 /Abc/Def abc/def/ 都会变成 abc/def)。
16
+ * 移除首尾和重复的 '/',完成后有 path 有这几种可能的格式: ''、'abc'、'abc/def'
17
+ * 例如 /abc/def 和 abc/def/ 都会变成 abc/def
17
18
  *
18
- * - 移除首尾和重复的 '/',完成后有 path 有这几种可能的格式: ''、'abc'、'abc/def'
19
- * - 统一改为小写
19
+ * 注意:此操作不会统一大小写,因此不保证标准化后两个字符串在代码层面 ===
20
20
  */
21
21
  export function normalizePath(path: string) {
22
22
  if (path.startsWith('/')) path = path.slice(1)
23
23
  if (path.endsWith('/')) path = path.slice(0, -1)
24
24
  path = path.replace(/\/+/g, '/')
25
- return path.toLowerCase()
25
+ return path
26
26
  }
27
27
 
28
28
  /**
@@ -179,7 +179,7 @@ export abstract class Router<Ctx extends BaseContext = BaseContext> {
179
179
  * 为未指定 CORS 配置的 route 提供默认配置
180
180
  */
181
181
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
182
- getCORSOptions(request: Request, routeMatch: RouteMatch<Route<Ctx>>) {
182
+ getCORSOptions(request: Request, routeMatch: RouteMatch<Route<Ctx>>): CORSOptions {
183
183
  return false
184
184
  }
185
185
 
@@ -191,7 +191,7 @@ export abstract class Router<Ctx extends BaseContext = BaseContext> {
191
191
  baseContext: BaseContext,
192
192
  route: Route<Ctx>,
193
193
  pathParameters: PathParameters,
194
- ): void | Promise<void>
194
+ ): unknown
195
195
 
196
196
  /**
197
197
  * response 定义中可引用的数据类型
@@ -134,6 +134,7 @@ export function operation(route: RouteDefinition) {
134
134
  tags: route.category ? [route.category] : [],
135
135
  summary: route.description,
136
136
  operationId: operationId(route),
137
+ parameters: queryParameters(route),
137
138
  requestBody: requestBody(route),
138
139
  responses: {
139
140
  default: {
@@ -152,7 +153,21 @@ export function operationId(route: RouteDefinition) {
152
153
  }
153
154
 
154
155
  /**
155
- * 路由请求参数转为 swagger 定义
156
+ * 路由 query 参数转为 swagger 定义
157
+ */
158
+ export function queryParameters(route: RouteDefinition) {
159
+ if (!route.query) return
160
+ return route.query.map(parameter => ({
161
+ name: parameter.name,
162
+ in: 'query' as const,
163
+ description: parameter.description,
164
+ required: parameter.required,
165
+ schema: parameterSchema(parameter),
166
+ }))
167
+ }
168
+
169
+ /**
170
+ * 路由 body 参数转为 swagger 定义
156
171
  */
157
172
  export function requestBody(route: RouteDefinition) {
158
173
  if (!route.body) return
package/src/.DS_Store DELETED
Binary file