bun-router 0.7.4-experimental.6 → 0.7.4-experimental.8
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.
- package/examples/ssr/index.ts +1 -1
- package/examples/static.ts +2 -1
- package/lib/logger/logger.ts +1 -1
- package/lib/router/router.ts +19 -21
- package/package.json +1 -1
package/examples/ssr/index.ts
CHANGED
package/examples/static.ts
CHANGED
package/lib/logger/logger.ts
CHANGED
@@ -9,7 +9,7 @@ _ _
|
|
9
9
|
|___|___|_|_| |_| |___|___|_| |___|_|
|
10
10
|
|
11
11
|
`;
|
12
|
-
const VERSION = '0.7.4-experimental.
|
12
|
+
const VERSION = '0.7.4-experimental.8';
|
13
13
|
const Logger = (): BunLogger => {
|
14
14
|
return {
|
15
15
|
info: async (statusCode: number, routePath: string, method: string, message?: string) => {
|
package/lib/router/router.ts
CHANGED
@@ -12,12 +12,24 @@ const Router: BunRouter = (port?: number | string, options?: RouterOptions<Optio
|
|
12
12
|
const { addRoute, findRoute } = RouteTree();
|
13
13
|
const logger = Logger();
|
14
14
|
|
15
|
-
async function loadComponent(name: string) {
|
16
|
-
const
|
17
|
-
const module = await import(modulePath);
|
15
|
+
async function loadComponent(root: string, name: string) {
|
16
|
+
const module = await import(path.join(process.cwd(), root, name));
|
18
17
|
return module.default;
|
19
18
|
}
|
20
19
|
|
20
|
+
function normalizePath(pattern: string, pathname: string) {
|
21
|
+
const extension = path.extname(pathname);
|
22
|
+
let base = path.basename(pathname);
|
23
|
+
|
24
|
+
if (extension === '.html' || extension === '.tsx') base = base.replace(extension, '');
|
25
|
+
|
26
|
+
let patternPath = [pattern, base].join('/');
|
27
|
+
|
28
|
+
if (base === 'index') patternPath = pattern;
|
29
|
+
|
30
|
+
return { patternPath, extension, base }
|
31
|
+
}
|
32
|
+
|
21
33
|
return {
|
22
34
|
// add a route to the router tree
|
23
35
|
add: (pattern, method, callback) => { addRoute(pattern, method, callback); },
|
@@ -33,29 +45,17 @@ const Router: BunRouter = (port?: number | string, options?: RouterOptions<Optio
|
|
33
45
|
static: async (pattern: string, root: string) => {
|
34
46
|
if (!exists(root)) console.log(`Cannot find directory ${root}`);
|
35
47
|
await readDir(root, async (fp) => {
|
36
|
-
const
|
37
|
-
|
38
|
-
let base = path.basename(fp);
|
39
|
-
|
40
|
-
if (ext === '.html' || ext === '.tsx') base = base.replace(ext, '');
|
41
|
-
|
42
|
-
if (pattern[0] !== '/') pattern = '/' + pattern;
|
43
|
-
|
44
|
-
let patternPath = pattern + '/' + base;
|
45
|
-
|
46
|
-
if (base === 'index') patternPath = pattern;
|
47
|
-
|
48
|
-
const purePath = path.join(root, patternPath);
|
48
|
+
const { patternPath, extension, base } = normalizePath(pattern, fp);
|
49
49
|
|
50
50
|
const route: Route = {
|
51
51
|
children: new Map(),
|
52
52
|
dynamicPath: '',
|
53
53
|
isLast: true,
|
54
|
-
path: patternPath.slice(1),
|
54
|
+
path: patternPath.slice(1), // remove the leading '/'
|
55
55
|
method: 'GET',
|
56
56
|
handler: async () => {
|
57
|
-
if (
|
58
|
-
const component = await loadComponent(
|
57
|
+
if (extension === '.tsx') {
|
58
|
+
const component = await loadComponent(root, base);
|
59
59
|
return await http.render(component());
|
60
60
|
} else {
|
61
61
|
return await http.file(200, fp);
|
@@ -63,8 +63,6 @@ const Router: BunRouter = (port?: number | string, options?: RouterOptions<Optio
|
|
63
63
|
},
|
64
64
|
};
|
65
65
|
|
66
|
-
console.log(route.path);
|
67
|
-
|
68
66
|
addRoute(route.path, 'GET', route.handler);
|
69
67
|
});
|
70
68
|
},
|