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 CHANGED
@@ -8,7 +8,8 @@ or
8
8
  `bun i bun-router`
9
9
 
10
10
 
11
- #### Example
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
@@ -1,5 +1,4 @@
1
1
  import { Router, http } from '..';
2
- import { Context } from '../lib/router/router.d';
3
2
 
4
3
  const Todo = () => {
5
4
  const list: Record<string, string> = {};
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
- // read a directory recursively and apply the callback to each one
9
- const readDir = async (dirpath: string, handler: (filepath: string, entry: BunFile) => void) => {
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
- for (const file of files) {
13
- const bunFile = Bun.file(file);
11
+ for (const file of files) {
12
+ const bunFile = Bun.file(file);
14
13
 
15
- if (typeof bunFile.name === 'undefined') return
14
+ if (typeof bunFile.name === 'undefined') return
16
15
 
17
- const fp = path.join(dirpath, bunFile.name);
18
- const isdir = await isDir(fp);
16
+ const fp = path.join(dirpath, bunFile.name);
17
+ const isdir = await isDir(fp);
19
18
 
20
- if (isdir) await readDir(fp, handler);
21
- else handler(fp, bunFile);
22
- }
19
+ if (isdir) await readDir(fp, handler);
20
+ else handler(fp, bunFile);
21
+ }
23
22
  }
24
23
 
25
24
 
@@ -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 }
@@ -29,7 +29,6 @@ type Context = {
29
29
  params: Map<string, string>;
30
30
  query: URLSearchParams;
31
31
  request: Request;
32
- token?: string;
33
32
  };
34
33
 
35
34
  type HttpHandler = (ctx: Context) => Response | Promise<Response>
@@ -1,6 +1,6 @@
1
1
  import path from 'path';
2
2
  import { Database } from 'bun:sqlite';
3
- import { Route, BunRouter, Context, RouterOptions, Options, HttpHandler } from './router.d';
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.methodNotAllowed());
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.notFound();
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.notFound());
92
+ return Promise.resolve(http.errNotFound());
93
93
 
94
94
  }
95
95
  });
@@ -1,4 +1,4 @@
1
- import { HttpHandler, Context, Route } from "./router.d";
1
+ import { HttpHandler, Route } from "./router.d";
2
2
  import { http } from "../http/http";
3
3
  import { createContext } from './context';
4
4
 
package/package.json CHANGED
@@ -8,5 +8,5 @@
8
8
  "peerDependencies": {
9
9
  "typescript": "^5.0.0"
10
10
  },
11
- "version": "0.7.1"
11
+ "version": "0.7.2"
12
12
  }