Skip to content

Commit cc59137

Browse files
authored
feat: params and url (#1)
1 parent c4817cb commit cc59137

File tree

4 files changed

+36
-15
lines changed

4 files changed

+36
-15
lines changed

README.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,23 +36,22 @@ const app = new Pico()
3636
app.get('/', () => 'Hello Pico!')
3737

3838
// capture the path parameter and return JSON response
39-
app.post('/entry/:id', ({ pathname }) => {
40-
const { id } = pathname.groups
39+
app.post('/entry/:id', ({ params }) => {
40+
const { id } = params
4141
return {
4242
'your id is': id,
4343
}
4444
})
4545

4646
// capture the parameters with RegExp
47-
app.get('/post/:date(\\d+)/:title([a-z]+)', ({ pathname }) => {
48-
const { date, title } = pathname.groups
47+
app.get('/post/:date(\\d+)/:title([a-z]+)', ({ params }) => {
48+
const { date, title } = params
4949
return { post: { date, title } }
5050
})
5151

5252
// get the query parameter
53-
app.get('/search', ({ search }) => {
54-
const params = new URLSearchParams(search.input)
55-
return `Your query is ${params.get('q')}`
53+
app.get('/search', ({ url }) => {
54+
return `Your query is ${url.searchParams.get('q')}`
5655
})
5756

5857
// handle the PURGE method and return Redirect response

src/pico.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,13 @@ class Pico extends defineDynamicClass() {
4949
fetch = (request: Request, env?: object, executionContext?: ExecutionContext) => {
5050
const match = this.match(request.method, request.url)
5151
if (match === undefined) return this.notFound()
52-
const response = match.handler({ request, ...match.result, env, executionContext })
52+
const response = match.handler({
53+
request,
54+
params: match.result.pathname.groups,
55+
url: new URL(request.url),
56+
env,
57+
executionContext,
58+
})
5359
if (response instanceof Response) return response
5460
if (typeof response === 'string') {
5561
return new Response(response, {

src/types.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
export type Handler = (context: Context) => Response | string | object
2-
export type Context = {
2+
export interface Context {
33
request: Request
44
env: object
55
executionContext: ExecutionContext
6-
} & URLPatternURLPatternResult
6+
params: Record<string, string>
7+
get url(): URL
8+
}

test/pico.test.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,12 @@ describe('Basic', () => {
3636

3737
describe('RegExp', () => {
3838
const app = new Pico()
39-
app.get('/post/:date(\\d+)/:title([a-z]+)', ({ pathname }) => {
40-
const { date, title } = pathname.groups
39+
app.get('/post/:date(\\d+)/:title([a-z]+)', ({ params }) => {
40+
const { date, title } = params
4141
return { post: { date, title } }
4242
})
43-
app.get('/assets/:filename(.*.png)', ({ pathname }) => {
44-
const { filename } = pathname.groups
45-
return { filename }
43+
app.get('/assets/:filename(.*.png)', ({ params }) => {
44+
return params
4645
})
4746

4847
it('Should capture regexp path parameters', async () => {
@@ -66,6 +65,21 @@ describe('RegExp', () => {
6665
})
6766
})
6867

68+
describe('URL', () => {
69+
const app = new Pico()
70+
app.get('/search', ({ url }) => {
71+
const query = url.searchParams.get('q') || ''
72+
return query
73+
})
74+
75+
it('Should get query parameters', async () => {
76+
const req = new Request('http://localhost/search?q=foo')
77+
const res = app.fetch(req)
78+
expect(res.status).toBe(200)
79+
expect(await res.text()).toBe('foo')
80+
})
81+
})
82+
6983
describe('All', () => {
7084
const app = new Pico()
7185
app.all('/abc', () => 'Hi')

0 commit comments

Comments
 (0)