bun-router 0.7.1 → 0.7.2
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +17 -1
- package/examples/todo.ts +0 -1
- package/lib/fs/fsys.ts +10 -11
- package/lib/router/context.ts +17 -15
- package/lib/router/router.d.ts +0 -1
- package/lib/router/router.ts +4 -4
- package/lib/router/tree.ts +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
@@ -8,7 +8,8 @@ or
|
|
8
8
|
`bun i bun-router`
|
9
9
|
|
10
10
|
|
11
|
-
####
|
11
|
+
#### Examples
|
12
|
+
##### URL Parameters
|
12
13
|
```ts
|
13
14
|
import { Router, http } from 'bun-router';
|
14
15
|
|
@@ -38,3 +39,18 @@ router.static('/assets', 'static');
|
|
38
39
|
router.serve();
|
39
40
|
```
|
40
41
|
|
42
|
+
##### SQLite
|
43
|
+
```ts
|
44
|
+
import { Router } from 'bun-router'
|
45
|
+
|
46
|
+
const router = Router(3000, { db: 'test.db'});
|
47
|
+
|
48
|
+
router.post('/register', ctx => {
|
49
|
+
const query = ctx.db.query("select 'Hello' as message;");
|
50
|
+
|
51
|
+
return http.ok(query.get());
|
52
|
+
});
|
53
|
+
|
54
|
+
```
|
55
|
+
|
56
|
+
|
package/examples/todo.ts
CHANGED
package/lib/fs/fsys.ts
CHANGED
@@ -5,21 +5,20 @@ import path from 'path';
|
|
5
5
|
// check if the file path is a directory
|
6
6
|
const isDir = async (fp: string): Promise<boolean> => (await fs.lstat(fp)).isDirectory();
|
7
7
|
|
8
|
-
|
9
|
-
const
|
10
|
-
const files = await fs.readdir(dirpath);
|
8
|
+
async function readDir(dirpath: string, handler: (filepath: string, entry: BunFile) => void) {
|
9
|
+
const files = await fs.readdir(dirpath);
|
11
10
|
|
12
|
-
|
13
|
-
|
11
|
+
for (const file of files) {
|
12
|
+
const bunFile = Bun.file(file);
|
14
13
|
|
15
|
-
|
14
|
+
if (typeof bunFile.name === 'undefined') return
|
16
15
|
|
17
|
-
|
18
|
-
|
16
|
+
const fp = path.join(dirpath, bunFile.name);
|
17
|
+
const isdir = await isDir(fp);
|
19
18
|
|
20
|
-
|
21
|
-
|
22
|
-
|
19
|
+
if (isdir) await readDir(fp, handler);
|
20
|
+
else handler(fp, bunFile);
|
21
|
+
}
|
23
22
|
}
|
24
23
|
|
25
24
|
|
package/lib/router/context.ts
CHANGED
@@ -2,6 +2,21 @@ import { Route, Context } from "./router.d";
|
|
2
2
|
import { Logger } from "../..";
|
3
3
|
import { http } from "./router";
|
4
4
|
|
5
|
+
async function createContext(path: string, route: Route, request: Request): Promise<Context> {
|
6
|
+
const params = extractParams(path, route);
|
7
|
+
const query = new URLSearchParams(path);
|
8
|
+
const formData = isMultiPartForm(request.headers) ? await request.formData() : new FormData();
|
9
|
+
|
10
|
+
return Promise.resolve({
|
11
|
+
params,
|
12
|
+
request,
|
13
|
+
query,
|
14
|
+
formData,
|
15
|
+
logger: Logger(),
|
16
|
+
json: (statusCode: number, data: any) => http.json(statusCode, data),
|
17
|
+
});
|
18
|
+
}
|
19
|
+
|
5
20
|
function extractParams(path: string, route: Route): Map<string, string> {
|
6
21
|
const params: Map<string, string> = new Map();
|
7
22
|
const pathSegments = path.split('/');
|
@@ -20,21 +35,6 @@ function extractParams(path: string, route: Route): Map<string, string> {
|
|
20
35
|
return params;
|
21
36
|
}
|
22
37
|
|
23
|
-
async function createContext(path: string, route: Route, request: Request): Promise<Context> {
|
24
|
-
const params = extractParams(path, route);
|
25
|
-
const query = new URLSearchParams(path);
|
26
|
-
const formData = isMultiPartForm(request.headers) ? await request.formData() : new FormData();
|
27
|
-
|
28
|
-
return Promise.resolve({
|
29
|
-
params,
|
30
|
-
request,
|
31
|
-
query,
|
32
|
-
formData,
|
33
|
-
logger: Logger(),
|
34
|
-
json: (statusCode: number, data: any) => http.json(statusCode, data),
|
35
|
-
});
|
36
|
-
}
|
37
|
-
|
38
38
|
function getContentType(headers: Headers): string {
|
39
39
|
const contentType = headers.get('Content-Type');
|
40
40
|
if (!contentType) return '';
|
@@ -46,4 +46,6 @@ function isMultiPartForm(headers: Headers): boolean {
|
|
46
46
|
return contentType.includes('multipart/form-data');
|
47
47
|
}
|
48
48
|
|
49
|
+
|
50
|
+
|
49
51
|
export { createContext }
|
package/lib/router/router.d.ts
CHANGED
package/lib/router/router.ts
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
import path from 'path';
|
2
2
|
import { Database } from 'bun:sqlite';
|
3
|
-
import { Route, BunRouter,
|
3
|
+
import { Route, BunRouter, RouterOptions, Options, HttpHandler } from './router.d';
|
4
4
|
import { httpStatusCodes } from '../http/status';
|
5
5
|
import { readDir } from '../fs/fsys';
|
6
6
|
import { Logger, startMessage } from '../logger/logger';
|
@@ -73,7 +73,7 @@ const Router: BunRouter = (port?: number | string, options?: RouterOptions<Optio
|
|
73
73
|
if (route) {
|
74
74
|
if (route.method !== req.method) {
|
75
75
|
logger.info(405, url.pathname, req.method, httpStatusCodes[405]);
|
76
|
-
return Promise.resolve(http.
|
76
|
+
return Promise.resolve(http.errMethodNotAllowed());
|
77
77
|
}
|
78
78
|
|
79
79
|
const context = await createContext(path, route, req);
|
@@ -86,10 +86,10 @@ const Router: BunRouter = (port?: number | string, options?: RouterOptions<Optio
|
|
86
86
|
}
|
87
87
|
|
88
88
|
// if no route is found, return 404
|
89
|
-
const response = await http.
|
89
|
+
const response = await http.errNotFound();
|
90
90
|
|
91
91
|
logger.info(response.status, url.pathname, req.method, httpStatusCodes[response.status]);
|
92
|
-
return Promise.resolve(http.
|
92
|
+
return Promise.resolve(http.errNotFound());
|
93
93
|
|
94
94
|
}
|
95
95
|
});
|
package/lib/router/tree.ts
CHANGED
package/package.json
CHANGED