add parse search value

This commit is contained in:
2025-05-12 04:32:26 +08:00
parent 896f2f5412
commit 6d148e47f1
4 changed files with 23 additions and 1 deletions

View File

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

View File

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

View File

@@ -592,6 +592,9 @@ export class QueryRouter {
return pick(r, pickValue as any);
});
}
/**
* 获取handle函数, 这里会去执行parse函数
*/
getHandle<T = any>(router: QueryRouter, wrapperFn?: HandleFn<T>, ctx?: RouteContext) {
return async (msg: { path: string; key?: string; [key: string]: any }, handleContext?: RouteContext) => {
try {

View File

@@ -22,3 +22,20 @@ export const parseSearch = (req: IncomingMessage) => {
const parsedUrl = url.parse(req.url, true);
return parsedUrl.query;
};
/**
* 把url当个key 的 value 的字符串转成json
* @param value
*/
export const parseSearchValue = (value?: string, opts?: { decode?: boolean }) => {
if (!value) return {};
const decode = opts?.decode ?? false;
if (decode) {
value = decodeURIComponent(value);
}
try {
return JSON.parse(value);
} catch (e) {
return {};
}
};