add parse search value
This commit is contained in:
parent
896f2f5412
commit
6d148e47f1
@ -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.13",
|
"version": "0.0.14",
|
||||||
"description": "",
|
"description": "",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"main": "./dist/router.js",
|
"main": "./dist/router.js",
|
||||||
|
@ -7,3 +7,5 @@ export type { RouteContext, RouteOpts } from './route.ts';
|
|||||||
export type { Run } from './route.ts';
|
export type { Run } from './route.ts';
|
||||||
|
|
||||||
export { CustomError } from './result/error.ts';
|
export { CustomError } from './result/error.ts';
|
||||||
|
|
||||||
|
export * from './server/parse-body.ts';
|
||||||
|
@ -592,6 +592,9 @@ export class QueryRouter {
|
|||||||
return pick(r, pickValue as any);
|
return pick(r, pickValue as any);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* 获取handle函数, 这里会去执行parse函数
|
||||||
|
*/
|
||||||
getHandle<T = any>(router: QueryRouter, wrapperFn?: HandleFn<T>, ctx?: RouteContext) {
|
getHandle<T = any>(router: QueryRouter, wrapperFn?: HandleFn<T>, ctx?: RouteContext) {
|
||||||
return async (msg: { path: string; key?: string; [key: string]: any }, handleContext?: RouteContext) => {
|
return async (msg: { path: string; key?: string; [key: string]: any }, handleContext?: RouteContext) => {
|
||||||
try {
|
try {
|
||||||
|
@ -22,3 +22,20 @@ export const parseSearch = (req: IncomingMessage) => {
|
|||||||
const parsedUrl = url.parse(req.url, true);
|
const parsedUrl = url.parse(req.url, true);
|
||||||
return parsedUrl.query;
|
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 {};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user