chore: 删除不再使用的文件并更新路由上下文以支持自定义字段

This commit is contained in:
2026-02-24 01:01:43 +08:00
parent f8337a1216
commit 52b10f2f03
16 changed files with 73 additions and 224 deletions

View File

@@ -31,6 +31,7 @@ app.route({
},
}).define(async (ctx) => {
ctx.body = '03';
ctx.args.test
return ctx;
}).addTo(app);
// app.server.on({

View File

@@ -18,15 +18,6 @@ route01.run = async (ctx) => {
ctx.body = '01';
return ctx;
};
app.use(
'demo',
async (ctx) => {
ctx.body = '01';
return ctx;
},
{ key: '01' },
);
const route02 = new Route('demo', '02');
route02.run = async (ctx) => {
ctx.body = '02';

View File

@@ -1,6 +0,0 @@
import { createCert } from '@kevisual/router/sign';
import { writeFileSync } from 'fs';
const { key, cert } = createCert();
writeFileSync('https-key.pem', key);
writeFileSync('https-cert.pem', cert);

View File

@@ -20,7 +20,7 @@ router
.define(async (ctx) => {
ctx.body = 'Hello, world!';
// throw new CustomError('error');
throw new CustomError(5000, 'error');
ctx.throw(5000, 'error');
})
.addTo(router);

View File

@@ -1,19 +0,0 @@
import { createCert } from '@kevisual/router/src/sign.ts';
import fs from 'node:fs';
const cert = createCert();
fs.writeFileSync('pem/https-private-key.pem', cert.key);
fs.writeFileSync('pem/https-cert.pem', cert.cert);
fs.writeFileSync(
'pem/https-config.json',
JSON.stringify(
{
createTime: new Date().getTime(),
expireDate: new Date(Date.now() + 365 * 24 * 60 * 60 * 1000).getTime(),
expireTime: new Date(Date.now() + 365 * 24 * 60 * 60 * 1000).toISOString(),
},
null,
2,
),
);

View File

@@ -16,10 +16,16 @@ const queryApp = new QueryRouterServer();
app
.route({
path: 'hello',
metadata: {
args: {
name: 'string',
},
}
})
.define(async (ctx) => {
// console.log('hello', ctx);
// console.log('hello', ctx.res);
ctx.query.name;
console.log('hello', ctx.query.cookies);
// ctx.res?.cookie?.('token', 'abc', {
// domain: '*', // 设置为顶级域名,允许跨子域共享

View File

@@ -1,14 +0,0 @@
import { createSchema } from '@kevisual/router';
const a = createSchema({
type: 'string',
minLength: 1,
maxLength: 10,
regex: '^[a-zA-Z0-9_]+$',
required: false,
});
console.log(a.safeParse('1234567890'));
console.log(a.safeParse('').error);
console.log(a.safeParse(undefined));
console.log(a.safeParse(null).error);

View File

@@ -1,35 +0,0 @@
import { SimpleRouter } from '@kevisual/router/src/router-simple.ts';
export const router = new SimpleRouter();
router.get('/', async (req, res) => {
console.log('get /');
});
router.post('/post', async (req, res) => {
console.log('post /post');
console.log('req body:', req, res);
res.end('post response');
});
router.get('/user/:id', async (req, res) => {
console.log('get /user/:id', req.params);
res.end(`user id is ${req.params.id}`);
});
router.post('/user/:id', async (req, res) => {
console.log('post /user/:id params', req.params);
const body = await router.getBody(req);
console.log('post body:', body);
res.end(`post user id is ${req.params.id}`);
});
router.post('/user/:id/a', async (req, res) => {
console.log('post /user/:id', req.params);
res.end(`post user id is ${req.params.id} a`);
});
// router.parse({ url: 'http://localhost:3000/', method: 'GET' } as any, {} as any);
// router.parse({ url: 'http://localhost:3000/post', method: 'POST' } as any, {} as any);
// router.parse({ url: 'http://localhost:3000/user/1/a', method: 'GET' } as any, {} as any);
// router.parse({ url: 'http://localhost:3000/user/1/a', method: 'POST' } as any, {} as any);

View File

@@ -1,56 +0,0 @@
import { App } from '@kevisual/router/src/app.ts';
import { router } from './a.ts';
export const app = new App();
app.server.on([{
fun: async (req, res) => {
console.log('Received request:', req.method, req.url);
const p = await router.parse(req, res);
if (p) {
console.log('Router parse result:', p);
}
}
}, {
id: 'abc',
path: '/ws',
io: true,
fun: async (data, end) => {
console.log('Custom middleware for /ws');
console.log('Data received:', data);
end({ message: 'Hello from /ws middleware' });
}
}]);
app.server.listen(3004, () => {
console.log('Server is running on http://localhost:3004');
// fetch('http://localhost:3004/', { method: 'GET' }).then(async (res) => {
// const text = await res.text();
// console.log('Response for GET /:', text);
// });
// fetch('http://localhost:3004/post', {
// method: 'POST',
// headers: { 'Content-Type': 'application/json' },
// body: JSON.stringify({ message: 'Hello, server!' }),
// }).then(async (res) => {
// const text = await res.text();
// console.log('Response for POST /post:', text);
// });
// fetch('http://localhost:3004/user/123', { method: 'GET' }).then(async (res) => {
// const text = await res.text();
// console.log('Response for GET /user/123:', text);
// });
fetch('http://localhost:3004/user/456', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ name: 'User456' }),
}).then(async (res) => {
const text = await res.text();
console.log('Response for POST /user/456:', text);
});
});

View File

@@ -65,8 +65,6 @@ app.importRoutes(app1.exportRoutes());
app.importRoutes(app2.exportRoutes());
app.importApp(app3);
app.listen(4003, () => {
console.log(`http://localhost:4003/api/router?path=app1&key=02`);
console.log(`http://localhost:4003/api/router?path=app1&key=01`);

View File

@@ -26,41 +26,6 @@ qr.add(
description: 'get project detail2',
run: async (ctx: RouteContext) => {
ctx!.body = 'project detail2';
return ctx;
},
validator: {
id: {
type: 'number',
required: true,
message: 'id is required',
},
data: {
// @ts-ignore
type: 'object',
message: 'data query is error',
properties: {
name: {
type: 'string',
required: true,
message: 'name is required',
},
age: {
type: 'number',
required: true,
message: 'age is error',
},
friends: {
type: 'object',
properties: {
hair: {
type: 'string',
required: true,
message: 'hair is required',
},
},
},
},
},
},
}),
);
@@ -73,7 +38,7 @@ const main = async () => {
id: 4,
data: {
name: 'john',
age: 's'+13,
age: 's' + 13,
friends: {
hair: 'black',
messages: 'hello',