@lytjs/api 6.5.0 → 6.6.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.
package/README.md CHANGED
@@ -1,74 +1,74 @@
1
- # @lytjs/api
2
-
3
- LytJS API 路由引擎,文件系统驱动的 API 路由解决方案。
4
-
5
- ## 安装
6
-
7
- ```bash
8
- pnpm add @lytjs/api
9
- ```
10
-
11
- ## 快速开始
12
-
13
- ```typescript
14
- import { createApiRouter } from '@lytjs/api';
15
-
16
- const apiRouter = createApiRouter({
17
- apiDir: './src/api',
18
- });
19
-
20
- // 处理请求
21
- const response = await apiRouter.handle({
22
- url: 'http://localhost/api/users',
23
- method: 'GET',
24
- headers: {},
25
- });
26
- ```
27
-
28
- ## 特性
29
-
30
- - 文件系统 API 路由
31
- - RESTful API 规范
32
- - 支持中间件
33
- - 零外部依赖
34
-
35
- ## API
36
-
37
- ### createApiRouter(options)
38
-
39
- 创建 API 路由引擎实例。
40
-
41
- ```typescript
42
- import { createApiRouter } from '@lytjs/api';
43
-
44
- const router = createApiRouter({
45
- apiDir: './src/api',
46
- });
47
- ```
48
-
49
- ### router.addRoute(route)
50
-
51
- 添加 API 路由。
52
-
53
- ### router.match(request)
54
-
55
- 匹配 API 路由。
56
-
57
- ### router.handle(request, context)
58
-
59
- 处理 API 请求。
60
-
61
- ## 文件结构约定
62
-
63
- ```
64
- src/
65
- └── api/
66
- ├── index.ts # GET /api
67
- └── users/
68
- ├── index.ts # GET /api/users, POST /api/users
69
- └── [id].ts # GET /api/users/:id, PUT /api/users/:id, etc.
70
- ```
71
-
72
- ## 许可证
73
-
74
- MIT
1
+ # @lytjs/api
2
+
3
+ LytJS API 路由引擎,文件系统驱动的 API 路由解决方案。
4
+
5
+ ## 安装
6
+
7
+ ```bash
8
+ pnpm add @lytjs/api
9
+ ```
10
+
11
+ ## 快速开始
12
+
13
+ ```typescript
14
+ import { createApiRouter } from '@lytjs/api';
15
+
16
+ const apiRouter = createApiRouter({
17
+ apiDir: './src/api',
18
+ });
19
+
20
+ // 处理请求
21
+ const response = await apiRouter.handle({
22
+ url: 'http://localhost/api/users',
23
+ method: 'GET',
24
+ headers: {},
25
+ });
26
+ ```
27
+
28
+ ## 特性
29
+
30
+ - 文件系统 API 路由
31
+ - RESTful API 规范
32
+ - 支持中间件
33
+ - 零外部依赖
34
+
35
+ ## API
36
+
37
+ ### createApiRouter(options)
38
+
39
+ 创建 API 路由引擎实例。
40
+
41
+ ```typescript
42
+ import { createApiRouter } from '@lytjs/api';
43
+
44
+ const router = createApiRouter({
45
+ apiDir: './src/api',
46
+ });
47
+ ```
48
+
49
+ ### router.addRoute(route)
50
+
51
+ 添加 API 路由。
52
+
53
+ ### router.match(request)
54
+
55
+ 匹配 API 路由。
56
+
57
+ ### router.handle(request, context)
58
+
59
+ 处理 API 请求。
60
+
61
+ ## 文件结构约定
62
+
63
+ ```
64
+ src/
65
+ └── api/
66
+ ├── index.ts # GET /api
67
+ └── users/
68
+ ├── index.ts # GET /api/users, POST /api/users
69
+ └── [id].ts # GET /api/users/:id, PUT /api/users/:id, etc.
70
+ ```
71
+
72
+ ## 许可证
73
+
74
+ MIT
package/package.json CHANGED
@@ -1,61 +1,62 @@
1
- {
2
- "name": "@lytjs/api",
3
- "version": "6.5.0",
4
- "description": "LytJS API Router Engine",
5
- "author": "lytjs",
6
- "license": "MIT",
7
- "type": "module",
8
- "main": "./dist/index.cjs",
9
- "module": "./dist/index.mjs",
10
- "types": "./dist/index.d.ts",
11
- "exports": {
12
- ".": {
13
- "types": "./dist/index.d.ts",
14
- "import": "./dist/index.mjs",
15
- "require": "./dist/index.cjs"
16
- }
17
- },
18
- "files": [
19
- "dist",
20
- "README.md",
21
- "LICENSE"
22
- ],
23
- "sideEffects": false,
24
- "scripts": {
25
- "dev": "tsup --watch",
26
- "build": "tsup",
27
- "test": "vitest run",
28
- "test:watch": "vitest",
29
- "test:coverage": "vitest run --coverage",
30
- "type-check": "tsc --noEmit",
31
- "clean": "rm -rf dist node_modules .turbo"
32
- },
33
- "dependencies": {
34
- "@lytjs/common-http": "^6.5.0"
35
- },
36
- "devDependencies": {
37
- "tsup": "^8.3.6",
38
- "typescript": "^5.7.3",
39
- "vitest": "^3.0.0"
40
- },
41
- "peerDependencies": {},
42
- "publishConfig": {
43
- "access": "public",
44
- "registry": "https://registry.npmjs.org/"
45
- },
46
- "repository": {
47
- "type": "git",
48
- "url": "https://gitee.com/lytjs/lytjs.git",
49
- "directory": "packages/ecosystem/packages/api"
50
- },
51
- "keywords": [
52
- "lytjs",
53
- "api",
54
- "router",
55
- "file-system"
56
- ],
57
- "homepage": "https://gitee.com/lytjs/lytjs",
58
- "bugs": {
59
- "url": "https://gitee.com/lytjs/lytjs/issues"
60
- }
61
- }
1
+ {
2
+ "name": "@lytjs/api",
3
+ "version": "6.6.0",
4
+ "description": "LytJS API Router Engine",
5
+ "author": "lytjs",
6
+ "license": "MIT",
7
+ "type": "module",
8
+ "main": "./dist/index.cjs",
9
+ "module": "./dist/index.mjs",
10
+ "types": "./dist/index.d.ts",
11
+ "exports": {
12
+ ".": {
13
+ "types": "./dist/index.d.ts",
14
+ "import": "./dist/index.mjs",
15
+ "require": "./dist/index.cjs"
16
+ }
17
+ },
18
+ "files": [
19
+ "dist",
20
+ "README.md",
21
+ "LICENSE"
22
+ ],
23
+ "sideEffects": false,
24
+ "scripts": {
25
+ "dev": "tsup --watch",
26
+ "build": "tsup",
27
+ "test": "vitest run",
28
+ "test:watch": "vitest",
29
+ "test:coverage": "vitest run --coverage",
30
+ "type-check": "tsc --noEmit",
31
+ "clean": "rm -rf dist node_modules .turbo"
32
+ },
33
+ "dependencies": {
34
+ "@lytjs/common-http": "workspace:*",
35
+ "@lytjs/shared-types": "workspace:*"
36
+ },
37
+ "devDependencies": {
38
+ "tsup": "^8.3.6",
39
+ "typescript": "^5.7.3",
40
+ "vitest": "^3.0.0"
41
+ },
42
+ "peerDependencies": {},
43
+ "publishConfig": {
44
+ "access": "public",
45
+ "registry": "https://registry.npmjs.org/"
46
+ },
47
+ "repository": {
48
+ "type": "git",
49
+ "url": "https://gitee.com/lytjs/lytjs.git",
50
+ "directory": "packages/ecosystem/packages/api"
51
+ },
52
+ "keywords": [
53
+ "lytjs",
54
+ "api",
55
+ "router",
56
+ "file-system"
57
+ ],
58
+ "homepage": "https://gitee.com/lytjs/lytjs",
59
+ "bugs": {
60
+ "url": "https://gitee.com/lytjs/lytjs/issues"
61
+ }
62
+ }
package/dist/index.d.cts DELETED
@@ -1,104 +0,0 @@
1
- /**
2
- * @lytjs/api - 类型定义
3
- */
4
- /** HTTP 请求方法 */
5
- type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'OPTIONS' | 'HEAD';
6
- /** API 路由配置 */
7
- interface ApiRouteConfig {
8
- /** 路由路径 */
9
- path: string;
10
- /** HTTP 方法 */
11
- methods: HttpMethod[];
12
- /** 处理函数路径 */
13
- handlerPath: string;
14
- /** 是否为动态路由 */
15
- isDynamic: boolean;
16
- /** 动态路由参数名 */
17
- params?: string[];
18
- /** 中间件路径 */
19
- middlewarePaths?: string[];
20
- }
21
- /** API 路由器配置选项 */
22
- interface ApiRouterOptions {
23
- /** API 目录路径 */
24
- apiDir: string;
25
- /** API 处理函数扩展名 */
26
- extensions?: string[];
27
- /** 中间件文件名称模式 */
28
- middlewarePattern?: string;
29
- /** 忽略文件模式 */
30
- ignorePatterns?: string[];
31
- /** 是否启用严格模式 */
32
- strictMode?: boolean;
33
- }
34
- /** API 请求上下文 */
35
- interface ApiRequestContext {
36
- /** 请求方法 */
37
- method: HttpMethod;
38
- /** 请求路径 */
39
- path: string;
40
- /** 请求头 */
41
- headers: Record<string, string>;
42
- /** 请求参数 */
43
- params: Record<string, string>;
44
- /** 查询参数 */
45
- query: Record<string, string | string[]>;
46
- /** 请求体 */
47
- body?: unknown;
48
- }
49
- /** API 响应对象 */
50
- interface ApiResponse {
51
- /** 状态码 */
52
- status: number;
53
- /** 响应头 */
54
- headers: Record<string, string>;
55
- /** 响应体 */
56
- body?: unknown;
57
- }
58
- /** API 处理函数 */
59
- type ApiHandler = (context: ApiRequestContext) => Promise<ApiResponse> | ApiResponse;
60
- /** API 中间件函数 */
61
- type ApiMiddleware = (context: ApiRequestContext, next: () => Promise<ApiResponse>) => Promise<ApiResponse>;
62
- /** API 匹配结果 */
63
- interface ApiMatch {
64
- /** 匹配的路由配置 */
65
- route: ApiRouteConfig;
66
- /** 请求方法 */
67
- method: HttpMethod;
68
- /** 路由参数 */
69
- params: Record<string, string>;
70
- /** 请求路径 */
71
- path: string;
72
- }
73
- /** API 路由器接口 */
74
- interface ApiRouter {
75
- /** 获取所有 API 路由配置 */
76
- getRoutes(): ApiRouteConfig[];
77
- /** 匹配 API 路由 */
78
- match(method: HttpMethod, path: string): ApiMatch | null;
79
- /** 添加 API 路由 */
80
- addRoute(route: ApiRouteConfig): void;
81
- /** 移除 API 路由 */
82
- removeRoute(path: string): void;
83
- /** 清除所有 API 路由 */
84
- clearRoutes(): void;
85
- /** 重新扫描文件系统 */
86
- refresh(): Promise<void>;
87
- /** 处理 API 请求 */
88
- handleRequest(method: HttpMethod, path: string, context: ApiRequestContext): Promise<ApiResponse>;
89
- }
90
-
91
- /**
92
- * @lytjs/api
93
- *
94
- * LytJS API Router Engine
95
- *
96
- * @packageDocumentation
97
- */
98
-
99
- /**
100
- * 创建 API 路由器
101
- */
102
- declare function createApiRouter(options?: ApiRouterOptions): ApiRouter;
103
-
104
- export { type ApiHandler, type ApiMatch, type ApiMiddleware, type ApiRequestContext, type ApiResponse, type ApiRouteConfig, type ApiRouter, type ApiRouterOptions, type HttpMethod, createApiRouter };
package/dist/index.d.ts DELETED
@@ -1,104 +0,0 @@
1
- /**
2
- * @lytjs/api - 类型定义
3
- */
4
- /** HTTP 请求方法 */
5
- type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'OPTIONS' | 'HEAD';
6
- /** API 路由配置 */
7
- interface ApiRouteConfig {
8
- /** 路由路径 */
9
- path: string;
10
- /** HTTP 方法 */
11
- methods: HttpMethod[];
12
- /** 处理函数路径 */
13
- handlerPath: string;
14
- /** 是否为动态路由 */
15
- isDynamic: boolean;
16
- /** 动态路由参数名 */
17
- params?: string[];
18
- /** 中间件路径 */
19
- middlewarePaths?: string[];
20
- }
21
- /** API 路由器配置选项 */
22
- interface ApiRouterOptions {
23
- /** API 目录路径 */
24
- apiDir: string;
25
- /** API 处理函数扩展名 */
26
- extensions?: string[];
27
- /** 中间件文件名称模式 */
28
- middlewarePattern?: string;
29
- /** 忽略文件模式 */
30
- ignorePatterns?: string[];
31
- /** 是否启用严格模式 */
32
- strictMode?: boolean;
33
- }
34
- /** API 请求上下文 */
35
- interface ApiRequestContext {
36
- /** 请求方法 */
37
- method: HttpMethod;
38
- /** 请求路径 */
39
- path: string;
40
- /** 请求头 */
41
- headers: Record<string, string>;
42
- /** 请求参数 */
43
- params: Record<string, string>;
44
- /** 查询参数 */
45
- query: Record<string, string | string[]>;
46
- /** 请求体 */
47
- body?: unknown;
48
- }
49
- /** API 响应对象 */
50
- interface ApiResponse {
51
- /** 状态码 */
52
- status: number;
53
- /** 响应头 */
54
- headers: Record<string, string>;
55
- /** 响应体 */
56
- body?: unknown;
57
- }
58
- /** API 处理函数 */
59
- type ApiHandler = (context: ApiRequestContext) => Promise<ApiResponse> | ApiResponse;
60
- /** API 中间件函数 */
61
- type ApiMiddleware = (context: ApiRequestContext, next: () => Promise<ApiResponse>) => Promise<ApiResponse>;
62
- /** API 匹配结果 */
63
- interface ApiMatch {
64
- /** 匹配的路由配置 */
65
- route: ApiRouteConfig;
66
- /** 请求方法 */
67
- method: HttpMethod;
68
- /** 路由参数 */
69
- params: Record<string, string>;
70
- /** 请求路径 */
71
- path: string;
72
- }
73
- /** API 路由器接口 */
74
- interface ApiRouter {
75
- /** 获取所有 API 路由配置 */
76
- getRoutes(): ApiRouteConfig[];
77
- /** 匹配 API 路由 */
78
- match(method: HttpMethod, path: string): ApiMatch | null;
79
- /** 添加 API 路由 */
80
- addRoute(route: ApiRouteConfig): void;
81
- /** 移除 API 路由 */
82
- removeRoute(path: string): void;
83
- /** 清除所有 API 路由 */
84
- clearRoutes(): void;
85
- /** 重新扫描文件系统 */
86
- refresh(): Promise<void>;
87
- /** 处理 API 请求 */
88
- handleRequest(method: HttpMethod, path: string, context: ApiRequestContext): Promise<ApiResponse>;
89
- }
90
-
91
- /**
92
- * @lytjs/api
93
- *
94
- * LytJS API Router Engine
95
- *
96
- * @packageDocumentation
97
- */
98
-
99
- /**
100
- * 创建 API 路由器
101
- */
102
- declare function createApiRouter(options?: ApiRouterOptions): ApiRouter;
103
-
104
- export { type ApiHandler, type ApiMatch, type ApiMiddleware, type ApiRequestContext, type ApiResponse, type ApiRouteConfig, type ApiRouter, type ApiRouterOptions, type HttpMethod, createApiRouter };