bump: 0.0.6
This commit is contained in:
parent
803687b219
commit
986b5687c4
22
package.json
22
package.json
@ -1,7 +1,7 @@
|
||||
{
|
||||
"$schema": "https://json.schemastore.org/package",
|
||||
"name": "@kevisual/router",
|
||||
"version": "0.0.6-alpha-5",
|
||||
"version": "0.0.6",
|
||||
"description": "",
|
||||
"main": "dist/index.js",
|
||||
"module": "dist/index.js",
|
||||
@ -20,22 +20,22 @@
|
||||
"author": "abearxiong",
|
||||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"@rollup/plugin-commonjs": "^28.0.1",
|
||||
"@rollup/plugin-node-resolve": "^15.3.0",
|
||||
"@rollup/plugin-typescript": "^12.1.1",
|
||||
"@rollup/plugin-commonjs": "^28.0.2",
|
||||
"@rollup/plugin-node-resolve": "^16.0.0",
|
||||
"@rollup/plugin-typescript": "^12.1.2",
|
||||
"@types/lodash-es": "^4.17.12",
|
||||
"@types/node": "^22.8.6",
|
||||
"@types/ws": "^8.5.13",
|
||||
"@types/node": "^22.13.4",
|
||||
"@types/ws": "^8.5.14",
|
||||
"cookie": "^1.0.2",
|
||||
"lodash-es": "^4.17.21",
|
||||
"nanoid": "^5.0.8",
|
||||
"rollup": "^4.24.3",
|
||||
"nanoid": "^5.1.0",
|
||||
"rollup": "^4.34.8",
|
||||
"rollup-plugin-dts": "^6.1.1",
|
||||
"ts-loader": "^9.5.1",
|
||||
"ts-loader": "^9.5.2",
|
||||
"ts-node": "^10.9.2",
|
||||
"tslib": "^2.8.1",
|
||||
"typescript": "^5.6.3",
|
||||
"zod": "^3.23.8"
|
||||
"typescript": "^5.7.3",
|
||||
"zod": "^3.24.2"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
|
@ -1,5 +1,6 @@
|
||||
import { pathToRegexp, match, Key } from 'path-to-regexp';
|
||||
import type { IncomingMessage, ServerResponse } from 'http';
|
||||
import { parseBody, parseSearch } from './server/parse-body.ts';
|
||||
|
||||
type Req = IncomingMessage & { params?: Record<string, string> };
|
||||
interface Route {
|
||||
@ -17,7 +18,12 @@ export class SimpleRouter {
|
||||
constructor() {
|
||||
// console.log('AppSimple initialized');
|
||||
}
|
||||
|
||||
getBody(req: Req) {
|
||||
return parseBody(req);
|
||||
}
|
||||
getSearch(req: Req) {
|
||||
return parseSearch(req);
|
||||
}
|
||||
use(method: string, route: string, ...fns: Array<(req: Req, res: ServerResponse) => Promise<void> | void>) {
|
||||
const handlers = Array.isArray(fns) ? fns.flat() : [];
|
||||
const pattern = pathToRegexp(route);
|
||||
@ -31,6 +37,17 @@ export class SimpleRouter {
|
||||
post(route: string, ...fns: Array<(req: Req, res: ServerResponse) => Promise<void> | void>) {
|
||||
return this.use('post', route, ...fns);
|
||||
}
|
||||
all(route: string, ...fns: Array<(req: Req, res: ServerResponse) => Promise<void> | void>) {
|
||||
this.use('post', route, ...fns);
|
||||
this.use('get', route, ...fns);
|
||||
return this;
|
||||
}
|
||||
/**
|
||||
* 解析 req 和 res 请求
|
||||
* @param req
|
||||
* @param res
|
||||
* @returns
|
||||
*/
|
||||
parse(req: Req, res: ServerResponse) {
|
||||
const { pathname } = new URL(req.url, 'http://localhost');
|
||||
const method = req.method.toLowerCase();
|
||||
|
@ -29,8 +29,6 @@ export const handleServer = async (req: IncomingMessage, res: ServerResponse) =>
|
||||
if (token) {
|
||||
token = token.replace('Bearer ', '');
|
||||
}
|
||||
//@ts-ignore
|
||||
console.log('token', req.cookies, res.cookie);
|
||||
// 获取查询参数
|
||||
const param = parsedUrl.query;
|
||||
let body: Record<any, any>;
|
||||
|
@ -1,4 +1,5 @@
|
||||
import * as http from 'http';
|
||||
import url from 'url';
|
||||
|
||||
export const parseBody = async (req: http.IncomingMessage) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
@ -16,3 +17,8 @@ export const parseBody = async (req: http.IncomingMessage) => {
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
export const parseSearch = (req: http.IncomingMessage) => {
|
||||
const parsedUrl = url.parse(req.url, true);
|
||||
return parsedUrl.query;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user