refactor: update router import path and add random ID utility
- Changed the import path for router types from '@kevisual/router' to './route.ts' in src/router-define.ts. - Added a new lock file (bun.lock) to manage dependencies. - Introduced a new utility function in src/utils/random.ts to generate random IDs using the nanoid library.
This commit is contained in:
@@ -6,7 +6,7 @@ import { handleServer } from './server/handle-server.ts';
|
||||
import { IncomingMessage, ServerResponse } from 'http';
|
||||
import { isBun } from './utils/is-engine.ts';
|
||||
import { BunServer } from './server/server-bun.ts';
|
||||
import { nanoid } from 'nanoid';
|
||||
import { randomId } from './utils/random.ts';
|
||||
|
||||
type RouterHandle = (msg: { path: string;[key: string]: any }) => { code: string; data?: any; message?: string;[key: string]: any };
|
||||
type AppOptions<T = {}> = {
|
||||
@@ -48,7 +48,7 @@ export class App<U = {}> extends QueryRouter {
|
||||
if (opts?.appId) {
|
||||
this.appId = opts.appId;
|
||||
} else {
|
||||
this.appId = nanoid(16);
|
||||
this.appId = randomId(16, 'rand-');
|
||||
}
|
||||
router.appId = this.appId;
|
||||
}
|
||||
|
||||
20
src/route.ts
20
src/route.ts
@@ -1,9 +1,8 @@
|
||||
import { nanoid } from 'nanoid';
|
||||
import { CustomError } from './result/error.ts';
|
||||
import { pick } from './utils/pick.ts';
|
||||
import { listenProcess, MockProcess } from './utils/listen-process.ts';
|
||||
import { z } from 'zod';
|
||||
|
||||
import { randomId } from './utils/random.ts';
|
||||
export type RouterContextT = { code?: number;[key: string]: any };
|
||||
export type RouteContext<T = { code?: number }, S = any> = {
|
||||
/**
|
||||
@@ -114,6 +113,7 @@ export const createSkill = <T = SimpleObject>(skill: Skill<T>): Skill<T> => {
|
||||
}
|
||||
|
||||
export type RouteInfo = Pick<Route, (typeof pickValue)[number]>;
|
||||
|
||||
export class Route<U = { [key: string]: any }, T extends SimpleObject = SimpleObject> {
|
||||
/**
|
||||
* 一级路径
|
||||
@@ -137,14 +137,14 @@ export class Route<U = { [key: string]: any }, T extends SimpleObject = SimpleOb
|
||||
isDebug?: boolean;
|
||||
constructor(path: string = '', key: string = '', opts?: RouteOpts) {
|
||||
if (!path) {
|
||||
path = nanoid(8)
|
||||
path = randomId(8, 'rand-');
|
||||
}
|
||||
path = path.trim();
|
||||
key = key.trim();
|
||||
this.path = path;
|
||||
this.key = key;
|
||||
if (opts) {
|
||||
this.id = opts.id || nanoid();
|
||||
this.id = opts.id || randomId(12, 'rand-');
|
||||
if (!opts.id && opts.idUsePath) {
|
||||
const delimiter = opts.delimiter ?? '$#$';
|
||||
this.id = path + delimiter + key;
|
||||
@@ -159,7 +159,7 @@ export class Route<U = { [key: string]: any }, T extends SimpleObject = SimpleOb
|
||||
this.path = opts.path || path;
|
||||
} else {
|
||||
this.middleware = [];
|
||||
this.id = nanoid();
|
||||
this.id = randomId(12, 'rand-');
|
||||
}
|
||||
this.isDebug = opts?.isDebug ?? false;
|
||||
}
|
||||
@@ -215,10 +215,10 @@ export class Route<U = { [key: string]: any }, T extends SimpleObject = SimpleOb
|
||||
return this;
|
||||
}
|
||||
|
||||
update(opts: DefineRouteOpts, checkList?: string[]): this {
|
||||
update(opts: DefineRouteOpts, onlyUpdateList?: string[]): this {
|
||||
const keys = Object.keys(opts);
|
||||
const defaultCheckList = ['path', 'key', 'run', 'nextRoute', 'description', 'metadata', 'middleware', 'type', 'isDebug'];
|
||||
checkList = checkList || defaultCheckList;
|
||||
const checkList = onlyUpdateList || defaultCheckList;
|
||||
for (let item of keys) {
|
||||
if (!checkList.includes(item)) {
|
||||
continue;
|
||||
@@ -316,8 +316,8 @@ export class QueryRouter {
|
||||
* remove route by id
|
||||
* @param uniqueId
|
||||
*/
|
||||
removeById(unique: string) {
|
||||
this.routes = this.routes.filter((r) => r.id !== unique);
|
||||
removeById(uniqueId: string) {
|
||||
this.routes = this.routes.filter((r) => r.id !== uniqueId);
|
||||
}
|
||||
/**
|
||||
* 执行route
|
||||
@@ -724,7 +724,7 @@ export class QueryRouterServer extends QueryRouter {
|
||||
if (opts?.appId) {
|
||||
this.appId = opts.appId;
|
||||
} else {
|
||||
this.appId = nanoid(16);
|
||||
this.appId = randomId(16);
|
||||
}
|
||||
}
|
||||
setHandle(wrapperFn?: HandleFn, ctx?: RouteContext) {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import type { QueryRouterServer, RouteOpts, Run, RouteMiddleware } from '@kevisual/router';
|
||||
import type { QueryRouterServer, RouteOpts, Run, RouteMiddleware } from './route.ts';
|
||||
import type { DataOpts, Query, Result } from '@kevisual/query/query';
|
||||
// export type RouteObject<T extends readonly string[]> = {
|
||||
// [K in T[number]]: RouteOpts;
|
||||
|
||||
8
src/utils/random.ts
Normal file
8
src/utils/random.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { customAlphabet } from 'nanoid';
|
||||
|
||||
const nanoid = customAlphabet('abcdefghijklmnopqrstuvwxyz', 16);
|
||||
|
||||
export const randomId = (length: number = 8, affix: string = '') => {
|
||||
return affix + nanoid(length);
|
||||
}
|
||||
export { nanoid };
|
||||
Reference in New Issue
Block a user