bun-router 0.5.5 → 0.5.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/todo.ts +49 -0
- package/lib/router/router.d.ts +2 -0
- package/lib/router/router.ts +21 -31
- package/package.json +1 -1
package/examples/todo.ts
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
import { router, http } from '..';
|
2
|
+
import { Context } from '../lib/router/router.d';
|
3
|
+
|
4
|
+
const Todo = () => {
|
5
|
+
const list: Record<string, string> = {};
|
6
|
+
|
7
|
+
return {
|
8
|
+
add: (key: string, value: string) => { list[key] = value },
|
9
|
+
get: (key: string) => list[key],
|
10
|
+
remove: (key: string) => { delete list[key] },
|
11
|
+
size: () => Object.entries(list).length,
|
12
|
+
export: () => list,
|
13
|
+
}
|
14
|
+
}
|
15
|
+
|
16
|
+
const todo = Todo();
|
17
|
+
|
18
|
+
const r = router();
|
19
|
+
|
20
|
+
r.add('/api/new', 'POST', ctx => {
|
21
|
+
const query = new URL(ctx.request.url).searchParams;
|
22
|
+
const key = query.get('key');
|
23
|
+
const content = query.get('content');
|
24
|
+
|
25
|
+
if (!key || !content) return http.message(400, 'invalid query params');
|
26
|
+
ctx.logger.message(`Adding ${key} with ${content}`);
|
27
|
+
todo.add(key, content);
|
28
|
+
|
29
|
+
return ctx.json({ message: 'ok' });
|
30
|
+
});
|
31
|
+
|
32
|
+
r.add('/api/todo/:key', 'GET', ctx => {
|
33
|
+
const key = ctx.params.get('key');
|
34
|
+
if (!key) return http.message(400, 'invalid params');
|
35
|
+
|
36
|
+
const content = todo.get(key);
|
37
|
+
if (!content) return http.notFound();
|
38
|
+
|
39
|
+
return ctx.json({key: key, content: content});
|
40
|
+
});
|
41
|
+
|
42
|
+
r.add('/api/get/all', 'GET', ctx => {
|
43
|
+
return ctx.json(todo.export());
|
44
|
+
});
|
45
|
+
|
46
|
+
r.serve();
|
47
|
+
|
48
|
+
|
49
|
+
|
package/lib/router/router.d.ts
CHANGED
@@ -5,10 +5,12 @@ import { Database } from 'bun:sqlite';
|
|
5
5
|
|
6
6
|
type Context = {
|
7
7
|
request: Request,
|
8
|
+
route: Route,
|
8
9
|
params: Map<string, string>,
|
9
10
|
token?: string,
|
10
11
|
db: Database,
|
11
12
|
logger: Logger,
|
13
|
+
json: (data: any) => Response | Promise<Response>,
|
12
14
|
}
|
13
15
|
|
14
16
|
type Route = {
|
package/lib/router/router.ts
CHANGED
@@ -1,9 +1,9 @@
|
|
1
|
+
import path from 'path';
|
1
2
|
import { Database } from 'bun:sqlite';
|
2
3
|
import { Route, Router, Context, RouterOptions, Options } from './router.d';
|
3
4
|
import { httpStatusCodes } from '../http/status';
|
4
5
|
import { readDir } from '../fs/fsys';
|
5
6
|
import { logger } from '../logger/logger';
|
6
|
-
import path from 'path';
|
7
7
|
import { Logger } from '../logger/logger.d';
|
8
8
|
import { http } from '../http/generic-methods';
|
9
9
|
|
@@ -47,17 +47,17 @@ const match = (route: Route, ctx: Context): boolean => {
|
|
47
47
|
}
|
48
48
|
|
49
49
|
// set the context for the reuest
|
50
|
-
const setContext = (req: Request, lgr: Logger, opts: Options): Context => {
|
50
|
+
const setContext = (req: Request, lgr: Logger, opts: Options, route: Route): Context => {
|
51
51
|
return {
|
52
52
|
request: req,
|
53
53
|
params: new Map(),
|
54
54
|
db: new Database(opts.db ?? ':memory:'),
|
55
55
|
logger: lgr,
|
56
|
+
route: route,
|
57
|
+
json: (data: any) => http.json(data),
|
56
58
|
}
|
57
59
|
}
|
58
60
|
|
59
|
-
|
60
|
-
|
61
61
|
const router: Router = (port?: number | string, options?: RouterOptions<Options>) => {
|
62
62
|
const routes: Array<Route> = new Array();
|
63
63
|
const lgr = logger();
|
@@ -107,6 +107,7 @@ const router: Router = (port?: number | string, options?: RouterOptions<Options>
|
|
107
107
|
method: 'GET',
|
108
108
|
callback: async () => await http.file(pure),
|
109
109
|
};
|
110
|
+
|
110
111
|
routes.push(route);
|
111
112
|
});
|
112
113
|
},
|
@@ -129,38 +130,27 @@ const router: Router = (port?: number | string, options?: RouterOptions<Options>
|
|
129
130
|
let statusCode = 404;
|
130
131
|
|
131
132
|
for (const route of routes) {
|
132
|
-
const ctx = setContext(req, lgr, opts);
|
133
|
-
|
134
|
-
if (
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
continue;
|
141
|
-
}
|
142
|
-
|
143
|
-
if (match(route, ctx)) {
|
144
|
-
const res = await route.callback(ctx);
|
145
|
-
if (res) {
|
146
|
-
statusCode = 200;
|
147
|
-
lgr.info(statusCode, url.pathname, req.method, httpStatusCodes[statusCode]);
|
148
|
-
return res
|
133
|
+
const ctx = setContext(req, lgr, opts, route);
|
134
|
+
|
135
|
+
if (match(route, ctx) || route.pattern === url.pathname) {
|
136
|
+
if (route.method === ctx.request.method) {
|
137
|
+
const res = await route.callback(ctx);
|
138
|
+
statusCode = res.status;
|
139
|
+
lgr.info(res.status, route.pattern, req.method, httpStatusCodes[res.status]);
|
140
|
+
return Promise.resolve(res);
|
149
141
|
} else {
|
150
|
-
|
151
|
-
|
142
|
+
const res = new Response(httpStatusCodes[405], {
|
143
|
+
status: 405,
|
144
|
+
statusText: httpStatusCodes[305]
|
145
|
+
});
|
146
|
+
lgr.info(405, route.pattern, req.method, httpStatusCodes[405])
|
147
|
+
return Promise.resolve(res);
|
152
148
|
}
|
153
|
-
}
|
154
|
-
}
|
155
|
-
|
156
|
-
if (statusCode === 405) {
|
157
|
-
lgr.info(statusCode, url.pathname, req.method, httpStatusCodes[statusCode]);
|
158
|
-
return http.message(statusCode, httpStatusCodes[statusCode]);
|
149
|
+
}
|
159
150
|
}
|
160
151
|
|
161
152
|
lgr.info(statusCode, url.pathname, req.method, httpStatusCodes[statusCode]);
|
162
|
-
return http.message(statusCode, httpStatusCodes[statusCode]);
|
163
|
-
|
153
|
+
return Promise.resolve(http.message(statusCode, httpStatusCodes[statusCode]));
|
164
154
|
}
|
165
155
|
});
|
166
156
|
},
|
package/package.json
CHANGED