bun-router 0.2.5 → 0.2.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,16 @@
1
+ import { router, html, json } from '..';
2
+
3
+ const r = router();
4
+
5
+ const aliens = '👽'.repeat(30);
6
+
7
+ r.add('/', 'GET', (req) => {
8
+ const message = {
9
+ page: 'home',
10
+ space: 'home',
11
+ body: 'ok'
12
+ };
13
+ return json(message);
14
+ });
15
+
16
+ r.serve();
@@ -35,10 +35,7 @@ const file = async (filepath: string): Promise<Response> => {
35
35
  }
36
36
 
37
37
  const html = async (content: string): Promise<Response> => {
38
- const buff = Buffer.from(content);
39
- const c = buff.toString('hex');
40
-
41
- const response = new Response(c, {
38
+ const response = new Response(content, {
42
39
  status: 200,
43
40
  statusText: 'ok',
44
41
  headers: {'Content-Type': 'text/html; charset=utf-8'},
@@ -75,7 +72,7 @@ const extractParams = (route: Route, req: HttpRequest) => {
75
72
  }
76
73
 
77
74
  const match = (route: Route, req: HttpRequest): boolean => {
78
- return req.params.size !== 0 || route.method === req.request.method
75
+ return req.params.size !== 0 || route.pattern === (new URL(req.request.url)).pathname
79
76
  }
80
77
 
81
78
  const router: Router = (port?: number | string, options?: Options) => {
@@ -104,6 +101,10 @@ const router: Router = (port?: number | string, options?: Options) => {
104
101
 
105
102
  extractParams(route, httpRequest);
106
103
 
104
+ if (match(route, httpRequest))
105
+ return route.callback(httpRequest);
106
+
107
+
107
108
  if (match(route, httpRequest)) return route.callback(httpRequest);
108
109
  }
109
110
  return new Response('not found');
package/package.json CHANGED
@@ -8,5 +8,5 @@
8
8
  "peerDependencies": {
9
9
  "typescript": "^5.0.0"
10
10
  },
11
- "version": "0.2.5"
11
+ "version": "0.2.7"
12
12
  }
@@ -8,7 +8,7 @@ describe('Helpers', async () => {
8
8
  const res = await file(fp);
9
9
  const contentType = res.headers.get('Content-Type');
10
10
 
11
- expect(contentType).toBe('text/html');
11
+ expect(contentType).toBe('text/html; charset=utf-8');
12
12
  expect(res.status).toBe(200);
13
13
  });
14
14