Skip to content

Commit 594065d

Browse files
authored
Merge pull request #1 from BackendStack21/v1.0.0
v1.0.0
2 parents 06202ab + 0c2140b commit 594065d

File tree

5 files changed

+137
-6
lines changed

5 files changed

+137
-6
lines changed

index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module.exports = (config = {}) => {
2-
const router = config.router || require('./lib/router/sequential')(config)
2+
// Sequential is default and only router implementation for now
3+
const router = require('./lib/router/sequential')(config)
34

45
return {
56
router

lib/router/sequential.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const next = require('./../next')
55

66
module.exports = (config = {}) => {
77
if (!config.defaultRoute) {
8-
config.defaultRoute = (req) => {
8+
config.defaultRoute = () => {
99
const res = new Response(null, {
1010
status: 404
1111
})
@@ -14,7 +14,7 @@ module.exports = (config = {}) => {
1414
}
1515
}
1616
if (!config.errorHandler) {
17-
config.errorHandler = (err, req) => {
17+
config.errorHandler = (err) => {
1818
const res = new Response(err.message, {
1919
status: 500
2020
})
@@ -38,7 +38,7 @@ module.exports = (config = {}) => {
3838
return this
3939
}
4040

41-
router.fetch = (req, step) => {
41+
router.fetch = (req) => {
4242
const url = new URL(req.url)
4343

4444
req.path = url.pathname || '/'

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "0http-bun",
3-
"version": "0.0.3",
4-
"description": "0http alternative for Bun",
3+
"version": "1.0.0",
4+
"description": "0http for Bun",
55
"main": "index.js",
66
"scripts": {
77
"lint": "bun x standard",

test/config.test.js

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/* global describe, it, expect, beforeAll */
2+
3+
const http = require('../index')
4+
const { router } = http({
5+
port: 3000,
6+
defaultRoute: (req) => {
7+
const res = new Response('Not Found!', {
8+
status: 404
9+
})
10+
11+
return res
12+
},
13+
errorHandler: (err) => {
14+
const res = new Response('Error: ' + err.message, {
15+
status: 500
16+
})
17+
18+
return res
19+
}
20+
})
21+
22+
describe('Router Configuration', () => {
23+
beforeAll(async () => {
24+
router.get('/error', () => {
25+
throw new Error('Unexpected error')
26+
})
27+
})
28+
29+
it('should return a 500 response for a route that throws an error', async () => {
30+
const response = await router.fetch(new Request('http://localhost:3000/error', {
31+
method: 'GET'
32+
}))
33+
expect(response.status).toBe(500)
34+
expect(await response.text()).toEqual('Error: Unexpected error')
35+
})
36+
37+
it('should return a 404 response for a route that does not exist', async () => {
38+
const response = await router.fetch(new Request('http://localhost:3000/does-not-exist', {
39+
method: 'GET'
40+
}))
41+
expect(response.status).toBe(404)
42+
expect(await response.text()).toEqual('Not Found!')
43+
})
44+
})

test/smoke.test.js

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/* global describe, it, expect, beforeAll */
2+
3+
const http = require('../index')
4+
const { router } = http({ port: 3000 })
5+
6+
describe('Router', () => {
7+
beforeAll(async () => {
8+
router.use((req, next) => {
9+
req.ctx = {
10+
engine: 'bun'
11+
}
12+
13+
return next()
14+
})
15+
16+
router.get('/get-params/:id', (req) => {
17+
return Response.json(req.params)
18+
})
19+
20+
router.delete('/get-params/:id', () => {
21+
return Response.json('OK')
22+
})
23+
24+
router.get('/error', () => {
25+
throw new Error('Unexpected error')
26+
})
27+
28+
router.post('/create', async (req) => {
29+
const body = await req.text()
30+
31+
return Response.json(JSON.parse(body))
32+
})
33+
34+
router.get('/', (req) => {
35+
return Response.json(req.ctx)
36+
})
37+
})
38+
39+
it('should return a JSON response with the request parameters for GET requests', async () => {
40+
const response = await router.fetch(new Request('http://localhost:3000/get-params/123', {
41+
method: 'GET'
42+
}))
43+
expect(response.status).toBe(200)
44+
expect(await response.json()).toEqual({ id: '123' })
45+
})
46+
47+
it('should return a JSON response with the request parameters for DELETE requests', async () => {
48+
const response = await router.fetch(new Request('http://localhost:3000/get-params/123', {
49+
method: 'DELETE'
50+
}))
51+
expect(response.status).toBe(200)
52+
expect(await response.json()).toEqual('OK')
53+
})
54+
55+
it('should return a JSON response with the request body for POST requests', async () => {
56+
const response = await router.fetch(new Request('http://localhost:3000/create', {
57+
method: 'POST',
58+
body: JSON.stringify({ foo: 'bar' })
59+
}))
60+
expect(response.status).toBe(200)
61+
expect(await response.json()).toEqual({ foo: 'bar' })
62+
})
63+
64+
it('should return a 404 response for a non-existent route', async () => {
65+
const response = await router.fetch(new Request('http://localhost:3000/non-existent', {
66+
method: 'GET'
67+
}))
68+
expect(response.status).toBe(404)
69+
})
70+
71+
it('should return a 500 response for a route that throws an error', async () => {
72+
const response = await router.fetch(new Request('http://localhost:3000/error', {
73+
method: 'GET'
74+
}))
75+
expect(response.status).toBe(500)
76+
expect(await response.text()).toEqual('Unexpected error')
77+
})
78+
79+
it('should return a 200 response for a route that returns a Response object', async () => {
80+
const response = await router.fetch(new Request('http://localhost:3000/', {
81+
method: 'GET'
82+
}))
83+
expect(response.status).toBe(200)
84+
expect(await response.json()).toEqual({ engine: 'bun' })
85+
})
86+
})

0 commit comments

Comments
 (0)