Skip to content

Commit

Permalink
feat: res (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
yusukebe authored Nov 24, 2022
1 parent 5929cae commit cc3aa93
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 8 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ app.post('/entry/:id', ({ params }) => {
}
})

// use `res` function to create Response object
app.get('/money', ({ res }) => res('Payment required', { status: 402 }))

// capture the parameters with RegExp
app.get('/post/:date(\\d+)/:title([a-z]+)', ({ params }) => {
const { date, title } = params
Expand All @@ -55,8 +58,8 @@ app.get('/search', ({ url }) => {
})

// handle the PURGE method and return Redirect response
app.on('PURGE', '/cache', () => {
return new Response(null, {
app.on('PURGE', '/cache', ({ res }) => {
return res(null, {
status: 302,
headers: {
Location: '/',
Expand Down
1 change: 1 addition & 0 deletions examples/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ app.get('/entry/:id', ({ params }) => {
const { id } = params
return { 'your id is': id }
})
app.get('/money', ({ res }) => res('Payment required', { status: 402 }))

export default app
9 changes: 5 additions & 4 deletions src/pico.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,27 +44,28 @@ class Pico extends defineDynamicClass() {
}
}

notFound = () => new Response('Not Found', { status: 404 })
res = (body?: BodyInit, init?: ResponseInit) => new Response(body, init)

fetch = (request: Request, env?: object, executionContext?: ExecutionContext) => {
const match = this.match(request.method, request.url)
if (match === undefined) return this.notFound()
if (match === undefined) return this.res('Not Found', { status: 404 })
const response = match.handler({
request,
params: match.result.pathname.groups,
url: new URL(request.url),
env,
executionContext,
res: this.res,
})
if (response instanceof Response) return response
if (typeof response === 'string') {
return new Response(response, {
return this.res(response, {
headers: {
'Content-Type': 'text/plain; charset="utf-8"',
},
})
} else if (typeof response === 'object') {
return new Response(JSON.stringify(response), {
return this.res(JSON.stringify(response), {
headers: {
'Content-Type': 'application/json',
},
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export interface Context {
executionContext: ExecutionContext
params: Record<string, string>
get url(): URL
res: (body?: BodyInit, init?: ResponseInit) => Response
}
4 changes: 2 additions & 2 deletions test/pico.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ describe('Basic', () => {
app.get('/json', () => ({
message: 'hello',
}))
app.get('*', () => {
return new Response('Custom Not Found', {
app.get('*', ({ res }) => {
return res('Custom Not Found', {
status: 404,
})
})
Expand Down

0 comments on commit cc3aa93

Please sign in to comment.