Skip to content

Commit 54e1d46

Browse files
committed
implement all
1 parent 7a73cf1 commit 54e1d46

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

src/pico.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { Handler } from './types'
22

3-
export const METHODS = ['get', 'post', 'put', 'delete', 'head', 'options', 'patch'] as const
4-
export function defineDynamicClass(): {
3+
const METHODS = ['all', 'get', 'post', 'put', 'delete', 'head', 'options', 'patch'] as const
4+
function defineDynamicClass(): {
55
new (): {
66
[K in typeof METHODS[number]]: (path: string, handler: Handler) => Pico
77
}
@@ -23,7 +23,7 @@ class Pico extends defineDynamicClass() {
2323
pattern: new URLPattern({
2424
pathname: path,
2525
}),
26-
method: method.toLocaleUpperCase(),
26+
method: method.toUpperCase(),
2727
handler,
2828
}
2929
this.routes.push(route)
@@ -35,7 +35,8 @@ class Pico extends defineDynamicClass() {
3535
match(method: string, url: string): { handler: Handler; result: URLPatternURLPatternResult } {
3636
method = method.toUpperCase()
3737
for (const route of this.routes) {
38-
if (route.pattern.test(url) && route.method === method) {
38+
const matched = route.pattern.test(url)
39+
if ((matched && route.method === 'ALL') || (matched && route.method === method)) {
3940
return { handler: route.handler, result: route.pattern.exec(url) }
4041
}
4142
}

test/pico.test.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Pico } from '../src/pico'
22

3-
describe('Test Pico', () => {
3+
describe('Basic', () => {
44
const app = new Pico()
55
app.get('/', () => 'Hi')
66
app.get('/json', () => ({
@@ -33,3 +33,20 @@ describe('Test Pico', () => {
3333
expect(await res.text()).toBe('Custom Not Found')
3434
})
3535
})
36+
37+
describe('All', () => {
38+
const app = new Pico()
39+
app.all('/abc', () => 'Hi')
40+
41+
it('Should return 200 response with GET request', async () => {
42+
const req = new Request('http://localhost/abc')
43+
const res = app.fetch(req)
44+
expect(res.status).toBe(200)
45+
})
46+
47+
it('Should return 200 response with POST request', async () => {
48+
const req = new Request('http://localhost/abc', { method: 'POST' })
49+
const res = app.fetch(req)
50+
expect(res.status).toBe(200)
51+
})
52+
})

0 commit comments

Comments
 (0)