@kevisual/router 0.0.13 → 0.0.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,7 @@
1
1
  import { Schema } from 'zod';
2
2
  export { Schema } from 'zod';
3
+ import * as querystring from 'querystring';
4
+ import { IncomingMessage } from 'node:http';
3
5
 
4
6
  type BaseRule = {
5
7
  value?: any;
@@ -312,6 +314,9 @@ declare class QueryRouter {
312
314
  }>;
313
315
  setContext(ctx: RouteContext): Promise<void>;
314
316
  getList(): RouteInfo[];
317
+ /**
318
+ * 获取handle函数, 这里会去执行parse函数
319
+ */
315
320
  getHandle<T = any>(router: QueryRouter, wrapperFn?: HandleFn<T>, ctx?: RouteContext): (msg: {
316
321
  path: string;
317
322
  key?: string;
@@ -406,5 +411,15 @@ declare class CustomError extends Error {
406
411
  };
407
412
  }
408
413
 
409
- export { CustomError, QueryRouter, QueryRouterServer, Route, createSchema };
414
+ declare const parseBody: <T = Record<string, any>>(req: IncomingMessage) => Promise<T>;
415
+ declare const parseSearch: (req: IncomingMessage) => querystring.ParsedUrlQuery;
416
+ /**
417
+ * 把url当个key 的 value 的字符串转成json
418
+ * @param value
419
+ */
420
+ declare const parseSearchValue: (value?: string, opts?: {
421
+ decode?: boolean;
422
+ }) => any;
423
+
424
+ export { CustomError, QueryRouter, QueryRouterServer, Route, createSchema, parseBody, parseSearch, parseSearchValue };
410
425
  export type { RouteContext, RouteOpts, Rule, Run };
@@ -1,3 +1,5 @@
1
+ import url from 'node:url';
2
+
1
3
  const urlAlphabet =
2
4
  'useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict';
3
5
 
@@ -6125,6 +6127,9 @@ class QueryRouter {
6125
6127
  return pick(r, pickValue);
6126
6128
  });
6127
6129
  }
6130
+ /**
6131
+ * 获取handle函数, 这里会去执行parse函数
6132
+ */
6128
6133
  getHandle(router, wrapperFn, ctx) {
6129
6134
  return async (msg, handleContext) => {
6130
6135
  try {
@@ -6233,4 +6238,44 @@ class QueryRouterServer extends QueryRouter {
6233
6238
  }
6234
6239
  }
6235
6240
 
6236
- export { CustomError, QueryRouter, QueryRouterServer, Route, ZodType as Schema, createSchema };
6241
+ const parseBody = async (req) => {
6242
+ return new Promise((resolve, reject) => {
6243
+ const arr = [];
6244
+ req.on('data', (chunk) => {
6245
+ arr.push(chunk);
6246
+ });
6247
+ req.on('end', () => {
6248
+ try {
6249
+ const body = Buffer.concat(arr).toString();
6250
+ resolve(JSON.parse(body));
6251
+ }
6252
+ catch (e) {
6253
+ resolve({});
6254
+ }
6255
+ });
6256
+ });
6257
+ };
6258
+ const parseSearch = (req) => {
6259
+ const parsedUrl = url.parse(req.url, true);
6260
+ return parsedUrl.query;
6261
+ };
6262
+ /**
6263
+ * 把url当个key 的 value 的字符串转成json
6264
+ * @param value
6265
+ */
6266
+ const parseSearchValue = (value, opts) => {
6267
+ if (!value)
6268
+ return {};
6269
+ const decode = opts?.decode ?? false;
6270
+ if (decode) {
6271
+ value = decodeURIComponent(value);
6272
+ }
6273
+ try {
6274
+ return JSON.parse(value);
6275
+ }
6276
+ catch (e) {
6277
+ return {};
6278
+ }
6279
+ };
6280
+
6281
+ export { CustomError, QueryRouter, QueryRouterServer, Route, ZodType as Schema, createSchema, parseBody, parseSearch, parseSearchValue };
package/dist/router.d.ts CHANGED
@@ -317,6 +317,9 @@ declare class QueryRouter {
317
317
  }>;
318
318
  setContext(ctx: RouteContext): Promise<void>;
319
319
  getList(): RouteInfo[];
320
+ /**
321
+ * 获取handle函数, 这里会去执行parse函数
322
+ */
320
323
  getHandle<T = any>(router: QueryRouter, wrapperFn?: HandleFn<T>, ctx?: RouteContext): (msg: {
321
324
  path: string;
322
325
  key?: string;
package/dist/router.js CHANGED
@@ -6149,6 +6149,9 @@ class QueryRouter {
6149
6149
  return pick(r, pickValue);
6150
6150
  });
6151
6151
  }
6152
+ /**
6153
+ * 获取handle函数, 这里会去执行parse函数
6154
+ */
6152
6155
  getHandle(router, wrapperFn, ctx) {
6153
6156
  return async (msg, handleContext) => {
6154
6157
  try {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package",
3
3
  "name": "@kevisual/router",
4
- "version": "0.0.13",
4
+ "version": "0.0.14",
5
5
  "description": "",
6
6
  "type": "module",
7
7
  "main": "./dist/router.js",
package/src/browser.ts CHANGED
@@ -7,3 +7,5 @@ export type { RouteContext, RouteOpts } from './route.ts';
7
7
  export type { Run } from './route.ts';
8
8
 
9
9
  export { CustomError } from './result/error.ts';
10
+
11
+ export * from './server/parse-body.ts';
package/src/route.ts CHANGED
@@ -592,6 +592,9 @@ export class QueryRouter {
592
592
  return pick(r, pickValue as any);
593
593
  });
594
594
  }
595
+ /**
596
+ * 获取handle函数, 这里会去执行parse函数
597
+ */
595
598
  getHandle<T = any>(router: QueryRouter, wrapperFn?: HandleFn<T>, ctx?: RouteContext) {
596
599
  return async (msg: { path: string; key?: string; [key: string]: any }, handleContext?: RouteContext) => {
597
600
  try {
@@ -22,3 +22,20 @@ export const parseSearch = (req: IncomingMessage) => {
22
22
  const parsedUrl = url.parse(req.url, true);
23
23
  return parsedUrl.query;
24
24
  };
25
+
26
+ /**
27
+ * 把url当个key 的 value 的字符串转成json
28
+ * @param value
29
+ */
30
+ export const parseSearchValue = (value?: string, opts?: { decode?: boolean }) => {
31
+ if (!value) return {};
32
+ const decode = opts?.decode ?? false;
33
+ if (decode) {
34
+ value = decodeURIComponent(value);
35
+ }
36
+ try {
37
+ return JSON.parse(value);
38
+ } catch (e) {
39
+ return {};
40
+ }
41
+ };