fix: fix types

This commit is contained in:
熊潇 2025-05-14 21:04:25 +08:00
parent fc35b531e1
commit aeeb205e1e
12 changed files with 64 additions and 15 deletions

1
.gitignore vendored
View File

@ -1,7 +1,6 @@
node_modules node_modules
src/app.config.json5 src/app.config.json5
dist dist
.npmrc
.turbo .turbo

2
.npmrc Normal file
View File

@ -0,0 +1,2 @@
//npm.xiongxiao.me/:_authToken=${ME_NPM_TOKEN}
//registry.npmjs.org/:_authToken=${NPM_TOKEN}

View File

@ -0,0 +1,11 @@
// import { App } from '@kevisual/router';
import { QueryRouterServer as App } from '@kevisual/router';
import { QueryUtil } from '@kevisual/router/define';
const app = new App();
const w = QueryUtil.create({
a: { path: 'a', description: 'sdf' },
});
app.route(w.get('a'));

View File

@ -8,6 +8,7 @@
"allowJs": true, "allowJs": true,
"newLine": "LF", "newLine": "LF",
"baseUrl": "./", "baseUrl": "./",
"rootDir": "./",
"typeRoots": [ "typeRoots": [
"node_modules/@types", "node_modules/@types",
], ],

View File

@ -1,7 +1,7 @@
{ {
"$schema": "https://json.schemastore.org/package", "$schema": "https://json.schemastore.org/package",
"name": "@kevisual/router", "name": "@kevisual/router",
"version": "0.0.15", "version": "0.0.18",
"description": "", "description": "",
"type": "module", "type": "module",
"main": "./dist/router.js", "main": "./dist/router.js",

View File

@ -125,6 +125,7 @@ export default [
format: 'es', format: 'es',
}, },
plugins: [dts()], plugins: [dts()],
external: ['@kevisual/router'],
}, },
{ {
input: 'src/router-simple.ts', input: 'src/router-simple.ts',

View File

@ -9,3 +9,5 @@ export type { Run } from './route.ts';
export { CustomError } from './result/error.ts'; export { CustomError } from './result/error.ts';
export * from './server/parse-body.ts'; export * from './server/parse-body.ts';
export * from './router-define.ts';

View File

@ -1,7 +1,7 @@
export { Route, QueryRouter, QueryRouterServer } from './route.ts'; export { Route, QueryRouter, QueryRouterServer } from './route.ts';
export { Connect, QueryConnect } from './connect.ts'; export { Connect, QueryConnect } from './connect.ts';
export type { RouteContext, RouteOpts } from './route.ts'; export type { RouteContext, RouteOpts, RouteMiddleware } from './route.ts';
export type { Run } from './route.ts'; export type { Run } from './route.ts';
@ -14,3 +14,5 @@ export { CustomError } from './result/error.ts';
export { Rule, Schema, createSchema } from './validator/index.ts'; export { Rule, Schema, createSchema } from './validator/index.ts';
export { App } from './app.ts'; export { App } from './app.ts';
export * from './router-define.ts';

View File

@ -57,10 +57,17 @@ export type RouteContext<T = { code?: number }, S = any> = {
/** 是否需要序列化 */ /** 是否需要序列化 */
needSerialize?: boolean; needSerialize?: boolean;
} & T; } & T;
export type SimpleObject = Record<string, any>;
export type Run<T = any> = (ctx: RouteContext<T>) => Promise<typeof ctx | null | void>; export type Run<T extends SimpleObject = {}> = (ctx: RouteContext<T>) => Promise<typeof ctx | null | void>;
export type NextRoute = Pick<Route, 'id' | 'path' | 'key'>; export type NextRoute = Pick<Route, 'id' | 'path' | 'key'>;
export type RouteMiddleware =
| {
path: string;
key?: string;
id?: string;
}
| string;
export type RouteOpts = { export type RouteOpts = {
path?: string; path?: string;
key?: string; key?: string;
@ -69,7 +76,7 @@ export type RouteOpts = {
nextRoute?: NextRoute; // route to run after this route nextRoute?: NextRoute; // route to run after this route
description?: string; description?: string;
metadata?: { [key: string]: any }; metadata?: { [key: string]: any };
middleware?: Route[] | string[]; // middleware middleware?: RouteMiddleware[]; // middleware
type?: 'route' | 'middleware'; type?: 'route' | 'middleware';
/** /**
* validator: { * validator: {
@ -112,7 +119,7 @@ export class Route<U = { [key: string]: any }> {
nextRoute?: NextRoute; // route to run after this route nextRoute?: NextRoute; // route to run after this route
description?: string; description?: string;
metadata?: { [key: string]: any }; metadata?: { [key: string]: any };
middleware?: (Route | string)[]; // middleware middleware?: RouteMiddleware[]; // middleware
type? = 'route'; type? = 'route';
private _validator?: { [key: string]: Rule }; private _validator?: { [key: string]: Rule };
schema?: { [key: string]: Schema<any> }; schema?: { [key: string]: Schema<any> };
@ -379,7 +386,14 @@ export class QueryRouter {
if (isString) { if (isString) {
route = this.routes.find((r) => r.id === item); route = this.routes.find((r) => r.id === item);
} else { } else {
route = this.routes.find((r) => r.path === item.path && r.key === item.key); route = this.routes.find((r) => {
if (item.id) {
return r.id === item.id;
} else {
// key 可以是空,所以可以不严格验证
return r.path === item.path && r.key == item.key;
}
});
} }
if (!route) { if (!route) {
if (isString) { if (isString) {

View File

@ -1,4 +1,4 @@
import type { QueryRouterServer, Route, RouteOpts, Run } from './route.ts'; import type { QueryRouterServer, RouteOpts, Run, RouteMiddleware } from '@kevisual/router';
// export type RouteObject<T extends readonly string[]> = { // export type RouteObject<T extends readonly string[]> = {
// [K in T[number]]: RouteOpts; // [K in T[number]]: RouteOpts;
@ -7,7 +7,7 @@ export type { RouteOpts };
export type RouteObject = { export type RouteObject = {
[key: string]: RouteOpts; [key: string]: RouteOpts;
}; };
type SimpleObject = Record<string, any>;
export function define<T extends Record<string, RouteOpts>>( export function define<T extends Record<string, RouteOpts>>(
value: T, value: T,
): { ): {
@ -45,7 +45,7 @@ class Chain {
this.object.path = path; this.object.path = path;
return this; return this;
} }
setMiddleware(middleware: string[] | Route[]) { setMiddleware(middleware: RouteMiddleware[]) {
this.object.middleware = middleware; this.object.middleware = middleware;
return this; return this;
} }
@ -57,11 +57,11 @@ class Chain {
this.object.id = key; this.object.id = key;
return this; return this;
} }
setRun(run: Run) { setRun<U extends SimpleObject = {}>(run: Run<U>) {
this.object.run = run; this.object.run = run;
return this; return this;
} }
define(run: Run) { define<U extends SimpleObject = {}>(run: Run<U>) {
this.object.run = run; this.object.run = run;
return this; return this;
} }
@ -98,8 +98,8 @@ export class QueryUtil<T extends RouteObject = RouteObject> {
let newOpts = { app: this.app, ...opts }; let newOpts = { app: this.app, ...opts };
return new Chain(obj, newOpts); return new Chain(obj, newOpts);
} }
queryChain<K extends keyof T>(key: K) { queryChain<K extends keyof T>(key: K): (queryData?: Record<string, any>) => RouteOpts {
const value = this.routeObject[key] as RouteOpts; const value = this.routeObject[key];
return (queryData?: Record<string, any>) => { return (queryData?: Record<string, any>) => {
return { return {
...value, ...value,

14
src/test/define.ts Normal file
View File

@ -0,0 +1,14 @@
import { App } from '@/app.ts';
import { QueryUtil } from '@/router-define.ts';
const v = QueryUtil.create({
a: {
path: 'a',
key: 'b',
},
});
const app = new App();
app.route(v.get('a'));
v.chain('a').define<{ f: () => {} }>(async (ctx) => {
// ctx.f = 'sdf';
});

View File

@ -22,6 +22,9 @@
"@/*": [ "@/*": [
"src/*" "src/*"
], ],
"@kevisual/router": [
"src/index.ts"
]
} }
}, },
"include": [ "include": [