diff --git a/package.json b/package.json index 764fd56..0a2ff76 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/rollup.config.js b/rollup.config.js index 210187b..522efe4 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -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: { diff --git a/src/result/error.ts b/src/result/error.ts index 0ac6e53..645d466 100644 --- a/src/result/error.ts +++ b/src/result/error.ts @@ -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, diff --git a/src/route.ts b/src/route.ts index dde3265..55a9dfa 100644 --- a/src/route.ts +++ b/src/route.ts @@ -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; @@ -129,7 +133,8 @@ export class Route { 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; diff --git a/src/router-define.ts b/src/router-define.ts new file mode 100644 index 0000000..5e4801d --- /dev/null +++ b/src/router-define.ts @@ -0,0 +1,110 @@ +import type { QueryRouterServer, Route, RouteOpts, Run } from './route.ts'; + +// export type RouteObject = { +// [K in T[number]]: RouteOpts; +// }; +export type { RouteOpts }; +export type RouteObject = { + [key: string]: RouteOpts; +}; + +export function define>( + 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 { + routeObject: T; + app: QueryRouterServer; + constructor(object: T, opts?: ChainOptions) { + this.routeObject = object; + this.app = opts?.app; + } + static createFormObj(object: U, opts?: ChainOptions) { + return new QueryUtil(object, opts); + } + static create>(value: U, opts?: ChainOptions) { + const obj = value as { [K in keyof U]: U[K] & RouteOpts }; + return new QueryUtil(obj, opts); + } + get(key: K): RouteOpts { + return this.routeObject[key] as RouteOpts; + } + chain(key: K, opts?: ChainOptions) { + const obj = this.routeObject[key]; + let newOpts = { app: this.app, ...opts }; + return new Chain(obj, newOpts); + } + queryChain(key: K) { + const value = this.routeObject[key] as RouteOpts; + return (queryData?: Record) => { + return { + ...value, + ...queryData, + }; + }; + } +} diff --git a/src/router-simple.ts b/src/router-simple.ts index 5ae4605..101bad6 100644 --- a/src/router-simple.ts +++ b/src/router-simple.ts @@ -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 };