feat: add define tools
This commit is contained in:
parent
6d148e47f1
commit
fc35b531e1
@ -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.14",
|
"version": "0.0.15",
|
||||||
"description": "",
|
"description": "",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"main": "./dist/router.js",
|
"main": "./dist/router.js",
|
||||||
@ -74,6 +74,11 @@
|
|||||||
"require": "./dist/router-simple.js",
|
"require": "./dist/router-simple.js",
|
||||||
"types": "./dist/router-simple.d.ts"
|
"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": {
|
"./simple-lib": {
|
||||||
"import": "./dist/router-simple-lib.js",
|
"import": "./dist/router-simple-lib.js",
|
||||||
"require": "./dist/router-simple-lib.js",
|
"require": "./dist/router-simple-lib.js",
|
||||||
|
@ -104,6 +104,28 @@ export default [
|
|||||||
},
|
},
|
||||||
plugins: [dts()],
|
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',
|
input: 'src/router-simple.ts',
|
||||||
output: {
|
output: {
|
||||||
|
@ -35,10 +35,22 @@ export class CustomError extends Error {
|
|||||||
tips: e?.tips,
|
tips: e?.tips,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* 判断 throw 的错误是否不是当前这个错误
|
||||||
|
* @param err
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
|
static isError(err: any) {
|
||||||
|
if (err instanceof CustomError || err?.code) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
parse(e?: CustomError) {
|
parse(e?: CustomError) {
|
||||||
if (e) {
|
if (e) {
|
||||||
return CustomError.parseError(e);
|
return CustomError.parseError(e);
|
||||||
} else {
|
} else {
|
||||||
|
const e = this;
|
||||||
return {
|
return {
|
||||||
code: e?.code,
|
code: e?.code,
|
||||||
data: e?.data,
|
data: e?.data,
|
||||||
|
@ -88,6 +88,10 @@ export type RouteOpts = {
|
|||||||
* $#$ will be used to split path and key
|
* $#$ will be used to split path and key
|
||||||
*/
|
*/
|
||||||
idUsePath?: boolean;
|
idUsePath?: boolean;
|
||||||
|
/**
|
||||||
|
* id 合并的分隔符,默认为 $#$
|
||||||
|
*/
|
||||||
|
delimiter?: string;
|
||||||
isDebug?: boolean;
|
isDebug?: boolean;
|
||||||
};
|
};
|
||||||
export type DefineRouteOpts = Omit<RouteOpts, 'idUsePath' | 'verify' | 'verifyKey' | 'nextRoute'>;
|
export type DefineRouteOpts = Omit<RouteOpts, 'idUsePath' | 'verify' | 'verifyKey' | 'nextRoute'>;
|
||||||
@ -129,7 +133,8 @@ export class Route<U = { [key: string]: any }> {
|
|||||||
if (opts) {
|
if (opts) {
|
||||||
this.id = opts.id || nanoid();
|
this.id = opts.id || nanoid();
|
||||||
if (!opts.id && opts.idUsePath) {
|
if (!opts.id && opts.idUsePath) {
|
||||||
this.id = path + '$#$' + key;
|
const delimiter = opts.delimiter ?? '$#$';
|
||||||
|
this.id = path + delimiter + key;
|
||||||
}
|
}
|
||||||
this.run = opts.run;
|
this.run = opts.run;
|
||||||
this.nextRoute = opts.nextRoute;
|
this.nextRoute = opts.nextRoute;
|
||||||
|
110
src/router-define.ts
Normal file
110
src/router-define.ts
Normal 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,
|
||||||
|
};
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
@ -1,5 +1,5 @@
|
|||||||
import { pathToRegexp, Key } from 'path-to-regexp';
|
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';
|
import { parseBody, parseSearch } from './server/parse-body.ts';
|
||||||
|
|
||||||
type Req = IncomingMessage & { params?: Record<string, string> };
|
type Req = IncomingMessage & { params?: Record<string, string> };
|
||||||
|
Loading…
x
Reference in New Issue
Block a user