Feature/redirect (#36)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* Add redirect method * Make Headers type * Typecheck
authored by Yusuke Wada · committed by Yusuke Wada · parent f9fa200 · Browse files · 4 files·+44 added−8 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+44 added−8 removed
- tests:+6 added−0 removed
- other:+38 added−8 removed
Provenance
- Commit
4741fd3- Tree
b76446e- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
Yusuke Wada <yusuke@kamawada.com>- Parent
f9fa200
4 files changed+44 added−8 removed
modified:example/basic/index.js+10 added-4 removed
37 unmodified lines383940414243444546474849505152475354555056575258596061626358646566676813 unmodified lines37 unmodified lines})
// Routingapp.get('/', (c) => c.text('Hono!!'))// Use Response object directlyapp.get('/hello', () => new Response('This is /hello'))
// Named parameterapp.get('/entry/:id', (c) => { const id = c.req.params('id') return c.text(`Your ID is ${id}`)})// Redirectapp.get('/redirect', (c) => c.redirect('/'))// Authentication requiredapp.get('/auth/*', async (ctx) => ctx.text('You are authorized'))app.get('/auth/*', (c) => c.text('You are authorized'))
// Asyncapp.get('/fetch-url', async () => {app.get('/fetch-url', async (c) => { const response = await fetch('https://example.com/') return new Response(`https://example.com/ is ${response.status}`) return c.text(`https://example.com/ is ${response.status}`)})
// Request headersapp.get('/user-agent', (c) => { const userAgent = c.req.headers.get('User-Agent') return new Response(`Your UserAgent is ${userAgent}`) return c.text(`Your UserAgent is ${userAgent}`)})
// JSONapp.get('/api/posts', (c) => {13 unmodified linesmodified:src/context.ts+18 added-3 removed
1234565 unmodified lines121314151416171819204 unmodified lines252627282729303132335 unmodified lines394041424143444546472 unmodified lines505152535455565758596061626364656667type Headers = { [key: string]: string }
export class Context { req: Request res: Response
5 unmodified lines newResponse(body?: BodyInit | null | undefined, init?: ResponseInit | undefined): Response { return new Response(body, init) }
text(text: string, status: number = 200, headers: { [key: string]: string } = {}): Response { text(text: string, status: number = 200, headers: Headers = {}): Response { if (typeof text !== 'string') { throw new TypeError('text method arg must be a string!') }
4 unmodified lines headers: headers, }) }
json(object: object, status: number = 200, headers: { [key: string]: string } = {}): Response { json(object: object, status: number = 200, headers: Headers = {}): Response { if (typeof object !== 'object') { throw new TypeError('json method arg must be a object!') }
5 unmodified lines headers: headers, }) }
html(html: string, status: number = 200, headers: { [key: string]: string } = {}): Response { html(html: string, status: number = 200, headers: Headers = {}): Response { if (typeof html !== 'string') { throw new TypeError('html method arg must be a string!') } headers['Content-Type'] = 'text/html; charset=UTF-8'2 unmodified lines status: status, headers: headers, }) }
redirect(location: string, status: number = 302, headers: Headers = {}): Response { if (typeof location !== 'string') { throw new TypeError('location must be a string!') }
headers['Location'] = location
return this.newResponse('', { status: status, headers: headers, }) }}modified:test/context.test.ts+6 added
32 unmodified lines333435363738394041424332 unmodified lines expect(res.headers.get('Content-Type')).toMatch('text/html') expect(await res.text()).toBe('<h1>Hello! Hono!</h1>') expect(res.headers.get('X-Custom')).toBe('Message') })
it('c.redirect', async () => { const res = c.redirect('/destination') expect(res.status).toBe(302) expect(res.headers.get('Location')).toBe('/destination') })})modified:README.md+10 added-1 removed
261 unmodified lines262 263 Render HTML as `Content-Type:text/html`:264 265 ```js266-app.get('/api', (c) => {266+app.get('/', (c) => {267 return c.html('<h1>Hello! Hono!</h1>')268 })269 ```270 271+### c.redirect()272+273+Redirect, default status code is `302`:274+275+```js276+app.get('/redirect', (c) => c.redirect('/'))277+app.get('/redirect-permanently', (c) => c.redirect('/', 301))278+```279+280 ## fire281 282 `app.fire()` do:283 105 unmodified lines