status and headers args on context text/json/html method (#34)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Yusuke Wada · committed by GitHub · parent f1117c0 · Browse files · 3 files·+30 added−27 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+30 added−27 removed
- tests:+9 added−6 removed
- other:+21 added−21 removed
Provenance
- Commit
7920f9a- Tree
65c99af- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
f1117c0
3 files changed+30 added−27 removed
modified:example/basic/index.js+3 added-1 removed
63 unmodified lines64656667686869707172737463 unmodified lines { id: 2, title: 'Good Aternoon' }, { id: 3, title: 'Good Evening' }, { id: 4, title: 'Good Night' }, ] return c.json(posts, null, 2) return c.json(posts)})// status codeapp.post('/api/posts', (c) => c.json({ message: 'Created!' }, 201))
// addEventListenerapp.fire()modified:src/context.ts+18 added-20 removed
9 unmodified lines1011121314151415161718192021222319202122232425262727282930313232333435353637383637383940424341424344454647484950514748495051529 unmodified lines newResponse(body?: BodyInit | null | undefined, init?: ResponseInit | undefined): Response { return new Response(body, init) }
text(body: string): Response { if (typeof body !== 'string') { text(text: string, status: number = 200, headers: { [key: string]: string } = {}): Response { if (typeof text !== 'string') { throw new TypeError('text method arg must be a string!') }
return this.newResponse(body, { status: 200, headers: { 'Content-Type': 'text/plain', }, headers['Content-Type'] = 'text/plain'
return this.newResponse(text, { status: status, headers: headers, }) }
json(object: object, replacer?: (string | number)[], space?: string | number): Response { json(object: object, status: number = 200, headers: { [key: string]: string } = {}): Response { if (typeof object !== 'object') { throw new TypeError('json method arg must be a object!') }
const body = JSON.stringify(object, replacer, space) const body = JSON.stringify(object) headers['Content-Type'] = 'application/json; charset=UTF-8'
return this.newResponse(body, { status: 200, headers: { 'Content-Type': 'application/json; charset=UTF-8', }, status: status, headers: headers, }) }
html(body: string): Response { if (typeof body !== 'string') { html(html: string, status: number = 200, headers: { [key: string]: string } = {}): Response { if (typeof html !== 'string') { throw new TypeError('html method arg must be a string!') } headers['Content-Type'] = 'text/html; charset=UTF-8'
return this.newResponse(body, { status: 200, headers: { 'Content-Type': 'text/html; charset=UTF-8', }, return this.newResponse(html, { status: status, headers: headers, }) }}modified:test/context.test.ts+9 added-6 removed
8 unmodified lines9101112131413141516171819202021212223242526272829302930313233343536378 unmodified lines const req = new Request('/') const c = new Context(req, new Response())
it('c.text', async () => { const res = c.text('text in c') expect(res.status).toBe(200) const res = c.text('text in c', 201, { 'X-Custom': 'Message' }) expect(res.status).toBe(201) expect(res.headers.get('Content-Type')).toBe('text/plain') expect(await res.text()).toBe('text in c') expect(res.headers.get('X-Custom')).toBe('Message') })
it('c.json', async () => { const res = c.json({ message: 'Hello' }) expect(res.status).toBe(200) const res = c.json({ message: 'Hello' }, 201, { 'X-Custom': 'Message' }) expect(res.status).toBe(201) expect(res.headers.get('Content-Type')).toMatch('application/json') const text = await res.text() const object = JSON.parse(text) expect(object.message).toBe('Hello') expect(res.headers.get('X-Custom')).toBe('Message') })
it('c.html', async () => { const res = c.html('<h1>Hello! Hono!</h1>') expect(res.status).toBe(200) const res = c.html('<h1>Hello! Hono!</h1>', 201, { 'X-Custom': 'Message' }) expect(res.status).toBe(201) 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') })})