bun-router 0.2.1 → 0.2.3
Sign up to get free protection for your applications and to get access to all the features.
- package/examples/html.ts +2 -2
- package/lib/router/router.ts +14 -2
- package/package.json +1 -1
- package/tests/router.test.ts +2 -2
package/examples/html.ts
CHANGED
package/lib/router/router.ts
CHANGED
@@ -12,7 +12,7 @@ const notFound = async (): Promise<Response> => {
|
|
12
12
|
});
|
13
13
|
}
|
14
14
|
|
15
|
-
const
|
15
|
+
const file = async (filepath: string): Promise<Response> => {
|
16
16
|
const file = Bun.file(filepath);
|
17
17
|
const exists = await file.exists();
|
18
18
|
|
@@ -34,6 +34,18 @@ const html = async (filepath: string): Promise<Response> => {
|
|
34
34
|
});
|
35
35
|
}
|
36
36
|
|
37
|
+
const html = async (content: string): Promise<Response> => {
|
38
|
+
const response = new Response(content, {
|
39
|
+
status: 200,
|
40
|
+
statusText: 'ok',
|
41
|
+
headers: {'Content-Type': 'text/html'}
|
42
|
+
});
|
43
|
+
|
44
|
+
return new Promise<Response>((resolve) => {
|
45
|
+
resolve(response);
|
46
|
+
});
|
47
|
+
}
|
48
|
+
|
37
49
|
const json = (data: any): Response => {
|
38
50
|
const jsonString = JSON.stringify(data);
|
39
51
|
|
@@ -98,4 +110,4 @@ const router: Router = (port?: number | string, options?: Options) => {
|
|
98
110
|
}
|
99
111
|
}
|
100
112
|
|
101
|
-
export { router, json,
|
113
|
+
export { router, json, file, extractParams, html }
|
package/package.json
CHANGED
package/tests/router.test.ts
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
import { describe, test, expect } from 'bun:test';
|
2
|
-
import {
|
2
|
+
import { file, json, extractParams } from '..';
|
3
3
|
import { HttpRequest, Route } from '../lib/router/router.d';
|
4
4
|
|
5
5
|
describe('Helpers', async () => {
|
6
6
|
test('html', async () => {
|
7
7
|
const fp = './examples/pages/index.html';
|
8
|
-
const res = await
|
8
|
+
const res = await file(fp);
|
9
9
|
const contentType = res.headers.get('Content-Type');
|
10
10
|
|
11
11
|
expect(contentType).toBe('text/html');
|