starlight-server 1.0.0 → 1.0.2
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.
- package/dist/demo/index.js +11 -2
- package/dist/router/router.d.ts +1 -0
- package/dist/swagger/factory.d.ts +5 -5
- package/dist/swagger/index.d.ts +2 -2
- package/dist/swagger/index.js +1 -1
- package/package.json +1 -1
- package/src/demo/index.ts +25 -2
- package/src/router/router.ts +1 -0
- package/src/swagger/factory.ts +5 -5
- package/src/swagger/index.ts +8 -4
package/dist/demo/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import path from 'node:path';
|
|
2
2
|
import { getFileDir } from '@anjianshi/utils/env-node/index.js';
|
|
3
|
-
import { getLogger, startHTTPServer,
|
|
3
|
+
import { getLogger, startHTTPServer, Router, } from '../index.js';
|
|
4
4
|
import { registerSwaggerRoute } from '../swagger/index.js';
|
|
5
5
|
const logger = getLogger({
|
|
6
6
|
level: 'debug',
|
|
@@ -9,7 +9,16 @@ const logger = getLogger({
|
|
|
9
9
|
dir: path.resolve(getFileDir(import.meta), '../../logs'),
|
|
10
10
|
},
|
|
11
11
|
});
|
|
12
|
-
|
|
12
|
+
class DemoRouter extends Router {
|
|
13
|
+
async executeWithContext(baseContext, route, pathParameters) {
|
|
14
|
+
const context = {
|
|
15
|
+
...baseContext,
|
|
16
|
+
now: () => Date.now(),
|
|
17
|
+
};
|
|
18
|
+
await route.handler(context, pathParameters);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
const router = new DemoRouter();
|
|
13
22
|
router.registerResponseReference('hello', { object: [{ name: 'hello', type: 'string' }] });
|
|
14
23
|
router.register({
|
|
15
24
|
category: 'demo',
|
package/dist/router/router.d.ts
CHANGED
|
@@ -29,6 +29,7 @@ export type RouteHandler<Ctx extends BaseContext = BaseContext, PathP extends Pa
|
|
|
29
29
|
* - method 一定为全大写
|
|
30
30
|
*/
|
|
31
31
|
export type Route<Ctx extends BaseContext = BaseContext, PathP extends PathParameters = PathParameters> = RequiredFields<RawRoute<Ctx, PathP>, 'description' | 'category' | 'method'>;
|
|
32
|
+
export type RouteDefinition = Omit<Route, 'handler'>;
|
|
32
33
|
export interface RawRoute<Ctx extends BaseContext = BaseContext, PathP extends PathParameters = PathParameters> {
|
|
33
34
|
/** 接口路径。支持变量(/abc/:xxx/def),详见 src/router/match.ts */
|
|
34
35
|
path: string;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* 把路由定义转换成 swagger API 定义的工具函数
|
|
3
3
|
*/
|
|
4
|
-
import type {
|
|
4
|
+
import type { RouteDefinition, ResponseDataType, BasicParameter } from '../router/index.js';
|
|
5
5
|
import type { Schema, Reference, PathItem } from './openapi-spec.js';
|
|
6
6
|
type AnyObject = {
|
|
7
7
|
[k: string]: unknown;
|
|
@@ -54,11 +54,11 @@ export declare function jsonMedia(schema: string | Schema): {
|
|
|
54
54
|
/**
|
|
55
55
|
* 把一系列路由转换成 swagger API 定义
|
|
56
56
|
*/
|
|
57
|
-
export declare function paths(routes:
|
|
57
|
+
export declare function paths(routes: RouteDefinition[]): Record<string, PathItem>;
|
|
58
58
|
/**
|
|
59
59
|
* 单个路由定义
|
|
60
60
|
*/
|
|
61
|
-
export declare function operation(route:
|
|
61
|
+
export declare function operation(route: RouteDefinition): {
|
|
62
62
|
tags: string[];
|
|
63
63
|
summary: string;
|
|
64
64
|
operationId: string;
|
|
@@ -83,11 +83,11 @@ export declare function operation(route: Route): {
|
|
|
83
83
|
/**
|
|
84
84
|
* 生成路由的唯一 ID
|
|
85
85
|
*/
|
|
86
|
-
export declare function operationId(route:
|
|
86
|
+
export declare function operationId(route: RouteDefinition): string;
|
|
87
87
|
/**
|
|
88
88
|
* 路由请求参数转为 swagger 定义
|
|
89
89
|
*/
|
|
90
|
-
export declare function requestBody(route:
|
|
90
|
+
export declare function requestBody(route: RouteDefinition): {
|
|
91
91
|
content: {
|
|
92
92
|
'application/json': {
|
|
93
93
|
schema: Schema | Reference;
|
package/dist/swagger/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type { Router } from '../router/index.js';
|
|
1
|
+
import type { Router, BaseContext } from '../router/index.js';
|
|
2
2
|
import type { OpenAPI } from './openapi-spec.js';
|
|
3
|
-
export declare function registerSwaggerRoute(router: Router
|
|
3
|
+
export declare function registerSwaggerRoute<Ctx extends BaseContext>(router: Router<Ctx>, endpoint?: string, customFields?: OpenAPICustomFields, swaggerOptions?: Record<string, unknown>): void;
|
|
4
4
|
/**
|
|
5
5
|
* swagger API 路由
|
|
6
6
|
*/
|
package/dist/swagger/index.js
CHANGED
|
@@ -15,7 +15,7 @@ export function registerSwaggerRoute(router, endpoint = '/swagger', customFields
|
|
|
15
15
|
category: 'swagger',
|
|
16
16
|
method: 'GET',
|
|
17
17
|
path: normalizePath(endpoint) + '/api-swagger.json',
|
|
18
|
-
handler: apiRoute
|
|
18
|
+
handler: (context) => apiRoute(router, customFields, context),
|
|
19
19
|
});
|
|
20
20
|
}
|
|
21
21
|
/**
|
package/package.json
CHANGED
package/src/demo/index.ts
CHANGED
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
import path from 'node:path'
|
|
2
2
|
import { getFileDir } from '@anjianshi/utils/env-node/index.js'
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
getLogger,
|
|
5
|
+
startHTTPServer,
|
|
6
|
+
type BaseContext,
|
|
7
|
+
type Route,
|
|
8
|
+
type PathParameters,
|
|
9
|
+
Router,
|
|
10
|
+
} from '@/index.js'
|
|
4
11
|
import { registerSwaggerRoute } from '@/swagger/index.js'
|
|
5
12
|
|
|
6
13
|
const logger = getLogger({
|
|
@@ -11,7 +18,23 @@ const logger = getLogger({
|
|
|
11
18
|
},
|
|
12
19
|
})
|
|
13
20
|
|
|
14
|
-
|
|
21
|
+
type DemoContext = BaseContext & {
|
|
22
|
+
now: () => number
|
|
23
|
+
}
|
|
24
|
+
class DemoRouter extends Router<DemoContext> {
|
|
25
|
+
async executeWithContext(
|
|
26
|
+
baseContext: BaseContext,
|
|
27
|
+
route: Route<DemoContext>,
|
|
28
|
+
pathParameters: PathParameters,
|
|
29
|
+
) {
|
|
30
|
+
const context: DemoContext = {
|
|
31
|
+
...baseContext,
|
|
32
|
+
now: () => Date.now(),
|
|
33
|
+
}
|
|
34
|
+
await route.handler(context, pathParameters)
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
const router = new DemoRouter()
|
|
15
38
|
|
|
16
39
|
router.registerResponseReference('hello', { object: [{ name: 'hello', type: 'string' }] })
|
|
17
40
|
|
package/src/router/router.ts
CHANGED
|
@@ -39,6 +39,7 @@ export type Route<
|
|
|
39
39
|
Ctx extends BaseContext = BaseContext,
|
|
40
40
|
PathP extends PathParameters = PathParameters,
|
|
41
41
|
> = RequiredFields<RawRoute<Ctx, PathP>, 'description' | 'category' | 'method'>
|
|
42
|
+
export type RouteDefinition = Omit<Route, 'handler'> // 仅含定义,不含实现
|
|
42
43
|
|
|
43
44
|
export interface RawRoute<
|
|
44
45
|
Ctx extends BaseContext = BaseContext,
|
package/src/swagger/factory.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* 把路由定义转换成 swagger API 定义的工具函数
|
|
3
3
|
*/
|
|
4
|
-
import type {
|
|
4
|
+
import type { RouteDefinition, ResponseDataType, BasicParameter } from '@/router/index.js'
|
|
5
5
|
import type { Schema, Reference, PathItem } from './openapi-spec.js'
|
|
6
6
|
|
|
7
7
|
type AnyObject = { [k: string]: unknown }
|
|
@@ -111,7 +111,7 @@ export function jsonMedia(schema: string | Schema) {
|
|
|
111
111
|
/**
|
|
112
112
|
* 把一系列路由转换成 swagger API 定义
|
|
113
113
|
*/
|
|
114
|
-
export function paths(routes:
|
|
114
|
+
export function paths(routes: RouteDefinition[]) {
|
|
115
115
|
const paths: Record<string, PathItem> = {}
|
|
116
116
|
|
|
117
117
|
for (const route of routes) {
|
|
@@ -129,7 +129,7 @@ export function paths(routes: Route[]) {
|
|
|
129
129
|
/**
|
|
130
130
|
* 单个路由定义
|
|
131
131
|
*/
|
|
132
|
-
export function operation(route:
|
|
132
|
+
export function operation(route: RouteDefinition) {
|
|
133
133
|
return {
|
|
134
134
|
tags: route.category ? [route.category] : [],
|
|
135
135
|
summary: route.description,
|
|
@@ -147,14 +147,14 @@ export function operation(route: Route) {
|
|
|
147
147
|
/**
|
|
148
148
|
* 生成路由的唯一 ID
|
|
149
149
|
*/
|
|
150
|
-
export function operationId(route:
|
|
150
|
+
export function operationId(route: RouteDefinition) {
|
|
151
151
|
return `${route.method}-${route.path}`
|
|
152
152
|
}
|
|
153
153
|
|
|
154
154
|
/**
|
|
155
155
|
* 路由请求参数转为 swagger 定义
|
|
156
156
|
*/
|
|
157
|
-
export function requestBody(route:
|
|
157
|
+
export function requestBody(route: RouteDefinition) {
|
|
158
158
|
if (!route.body) return
|
|
159
159
|
return jsonMedia({
|
|
160
160
|
type: 'object',
|
package/src/swagger/index.ts
CHANGED
|
@@ -7,8 +7,8 @@ import { normalizePath } from '@/router/match.js'
|
|
|
7
7
|
import * as factory from './factory.js'
|
|
8
8
|
import type { OpenAPI } from './openapi-spec.js'
|
|
9
9
|
|
|
10
|
-
export function registerSwaggerRoute(
|
|
11
|
-
router: Router
|
|
10
|
+
export function registerSwaggerRoute<Ctx extends BaseContext>(
|
|
11
|
+
router: Router<Ctx>,
|
|
12
12
|
endpoint: string = '/swagger',
|
|
13
13
|
customFields: OpenAPICustomFields = { info: { title: 'API Document', version: '0.0.1' } },
|
|
14
14
|
swaggerOptions?: Record<string, unknown>,
|
|
@@ -23,7 +23,7 @@ export function registerSwaggerRoute(
|
|
|
23
23
|
category: 'swagger',
|
|
24
24
|
method: 'GET',
|
|
25
25
|
path: normalizePath(endpoint) + '/api-swagger.json',
|
|
26
|
-
handler: apiRoute
|
|
26
|
+
handler: (context: Ctx) => apiRoute(router, customFields, context),
|
|
27
27
|
})
|
|
28
28
|
}
|
|
29
29
|
|
|
@@ -76,7 +76,11 @@ function replacement(abspath: string, content: Buffer, swaggerOptions?: Record<s
|
|
|
76
76
|
*/
|
|
77
77
|
type OpenAPICustomFields = Omit<OpenAPI, 'openapi' | 'paths' | 'components'>
|
|
78
78
|
|
|
79
|
-
function apiRoute
|
|
79
|
+
function apiRoute<Ctx extends BaseContext>(
|
|
80
|
+
router: Router<Ctx>,
|
|
81
|
+
customFields: OpenAPICustomFields,
|
|
82
|
+
{ response }: BaseContext,
|
|
83
|
+
) {
|
|
80
84
|
const swaggerJSON: OpenAPI = {
|
|
81
85
|
...customFields,
|
|
82
86
|
openapi: '3.1.0',
|