feat: add define tools

This commit is contained in:
熊潇 2025-05-14 20:18:58 +08:00
parent 6d148e47f1
commit fc35b531e1
6 changed files with 157 additions and 3 deletions

View File

@ -1,7 +1,7 @@
{
"$schema": "https://json.schemastore.org/package",
"name": "@kevisual/router",
"version": "0.0.14",
"version": "0.0.15",
"description": "",
"type": "module",
"main": "./dist/router.js",
@ -74,6 +74,11 @@
"require": "./dist/router-simple.js",
"types": "./dist/router-simple.d.ts"
},
"./define": {
"import": "./dist/router-define.js",
"require": "./dist/router-define.js",
"types": "./dist/router-define.d.ts"
},
"./simple-lib": {
"import": "./dist/router-simple-lib.js",
"require": "./dist/router-simple-lib.js",

View File

@ -104,6 +104,28 @@ export default [
},
plugins: [dts()],
},
{
input: 'src/router-define.ts',
output: {
file: 'dist/router-define.js',
format: 'es',
},
plugins: [
resolve({
browser: true,
}),
commonjs(),
typescript(),
],
},
{
input: 'src/router-define.ts',
output: {
file: 'dist/router-define.d.ts',
format: 'es',
},
plugins: [dts()],
},
{
input: 'src/router-simple.ts',
output: {

View File

@ -35,10 +35,22 @@ export class CustomError extends Error {
tips: e?.tips,
};
}
/**
* throw
* @param err
* @returns
*/
static isError(err: any) {
if (err instanceof CustomError || err?.code) {
return true;
}
return false;
}
parse(e?: CustomError) {
if (e) {
return CustomError.parseError(e);
} else {
const e = this;
return {
code: e?.code,
data: e?.data,

View File

@ -88,6 +88,10 @@ export type RouteOpts = {
* $#$ will be used to split path and key
*/
idUsePath?: boolean;
/**
* id $#$
*/
delimiter?: string;
isDebug?: boolean;
};
export type DefineRouteOpts = Omit<RouteOpts, 'idUsePath' | 'verify' | 'verifyKey' | 'nextRoute'>;
@ -129,7 +133,8 @@ export class Route<U = { [key: string]: any }> {
if (opts) {
this.id = opts.id || nanoid();
if (!opts.id && opts.idUsePath) {
this.id = path + '$#$' + key;
const delimiter = opts.delimiter ?? '$#$';
this.id = path + delimiter + key;
}
this.run = opts.run;
this.nextRoute = opts.nextRoute;

110
src/router-define.ts Normal file
View File

@ -0,0 +1,110 @@
import type { QueryRouterServer, Route, RouteOpts, Run } from './route.ts';
// export type RouteObject<T extends readonly string[]> = {
// [K in T[number]]: RouteOpts;
// };
export type { RouteOpts };
export type RouteObject = {
[key: string]: RouteOpts;
};
export function define<T extends Record<string, RouteOpts>>(
value: T,
): {
[K in keyof T]: T[K] & RouteOpts;
} {
return value as { [K in keyof T]: T[K] & RouteOpts };
}
export type RouteArray = RouteOpts[];
type ChainOptions = {
app: QueryRouterServer;
};
class Chain {
object: RouteOpts;
app?: QueryRouterServer;
constructor(object: RouteOpts, opts?: ChainOptions) {
this.object = object;
this.app = opts?.app;
}
get key() {
return this.object.key;
}
get path() {
return this.object.path;
}
setDescription(desc: string) {
this.object.description = desc;
return this;
}
setMeta(metadata: { [key: string]: any }) {
this.object.metadata = metadata;
return this;
}
setPath(path: string) {
this.object.path = path;
return this;
}
setMiddleware(middleware: string[] | Route[]) {
this.object.middleware = middleware;
return this;
}
setKey(key: string) {
this.object.key = key;
return this;
}
setId(key: string) {
this.object.id = key;
return this;
}
setRun(run: Run) {
this.object.run = run;
return this;
}
define(run: Run) {
this.object.run = run;
return this;
}
createRoute() {
this.app.route(this.object).addTo(this.app);
return this;
}
}
export const util = {
getChain: (obj: RouteOpts, opts?: ChainOptions) => {
return new Chain(obj, opts);
},
};
export class QueryUtil<T extends RouteObject = RouteObject> {
routeObject: T;
app: QueryRouterServer;
constructor(object: T, opts?: ChainOptions) {
this.routeObject = object;
this.app = opts?.app;
}
static createFormObj<U extends RouteObject>(object: U, opts?: ChainOptions) {
return new QueryUtil<U>(object, opts);
}
static create<U extends Record<string, RouteOpts>>(value: U, opts?: ChainOptions) {
const obj = value as { [K in keyof U]: U[K] & RouteOpts };
return new QueryUtil<U>(obj, opts);
}
get<K extends keyof T>(key: K): RouteOpts {
return this.routeObject[key] as RouteOpts;
}
chain<K extends keyof T>(key: K, opts?: ChainOptions) {
const obj = this.routeObject[key];
let newOpts = { app: this.app, ...opts };
return new Chain(obj, newOpts);
}
queryChain<K extends keyof T>(key: K) {
const value = this.routeObject[key] as RouteOpts;
return (queryData?: Record<string, any>) => {
return {
...value,
...queryData,
};
};
}
}

View File

@ -1,5 +1,5 @@
import { pathToRegexp, Key } from 'path-to-regexp';
import type { IncomingMessage, ServerResponse } from 'http';
import type { IncomingMessage, ServerResponse } from 'node:http';
import { parseBody, parseSearch } from './server/parse-body.ts';
type Req = IncomingMessage & { params?: Record<string, string> };