Skip to content

Commit 56ae530

Browse files
committed
fix: Use status code specified in context
Fix #56 Previously the html plugin would always return 200 for HTML responses. This commit adds support for reading the status code in set.status to determine which status code to use. It fallbacks to 200 if no status code is set. It uses the StatusMap exported by Elysia to map res.status values supplied as string literals. Example for statuscode has been added to examples folder showcasing returning Forbidden status. Added test for overriding status code. Needed to upgrade Elysia peer and dev dependency to a newer version as the StatusMap value was not available in elysia 1.0.2. Running bun install and pnpm install also updated the minor versions of @kitajs/* dependencies.
1 parent b94978d commit 56ae530

File tree

8 files changed

+133
-55
lines changed

8 files changed

+133
-55
lines changed

bun.lockb

445 Bytes
Binary file not shown.

example/index.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,7 @@ app.handle(new Request('http://localhost:8080/'))
7171
app.handle(new Request('http://localhost:8080/'))
7272
.then((x) => x.headers.toJSON())
7373
.then(console.log)
74+
75+
app.handle(new Request('http://localhost:8080/'))
76+
.then((x) => x.status)
77+
.then(console.log)

example/statuscode.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Elysia } from 'elysia'
2+
import { html } from '../src'
3+
4+
const app = new Elysia()
5+
.use(html({ autoDetect: true }))
6+
.get('/a', ({ html, set }) => {
7+
set.status = 'Forbidden'
8+
return html(`<h1>Forbidden!</h1>`)
9+
})
10+
.compile()
11+
12+
console.log(app.routes[0]?.composed?.toString())
13+
14+
app.handle(new Request('http://localhost:8080/a'))
15+
.then((x) => x.status)
16+
.then(console.log)

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@
5252
"format": "prettier --write ."
5353
},
5454
"peerDependencies": {
55-
"elysia": ">= 1.0.2"
55+
"elysia": ">= 1.0.6"
5656
},
5757
"devDependencies": {
5858
"@elysiajs/stream": "^0.7.2",
5959
"@types/bun": "^1.0.4",
6060
"@types/node": "^20.7.2",
61-
"elysia": "1.0.2",
61+
"elysia": "1.0.6",
6262
"eslint": "^8.50.0",
6363
"rimraf": "^5.0.5",
6464
"typescript": "^5.2.2"

pnpm-lock.yaml

Lines changed: 78 additions & 42 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/handler.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ import { isHtml, isTagHtml } from './utils'
55
export function handleHtml(
66
value: string | Readable | Promise<string | Readable>,
77
options: HtmlOptions,
8-
hasContentType: boolean
8+
hasContentType: boolean,
9+
status?: number
910
): Promise<Response | string> | Response | string {
11+
1012
// Only use promises if value is a promise itself
1113
if (value instanceof Promise) {
1214
return value.then((v) => handleHtml(v, options, hasContentType))
@@ -24,9 +26,7 @@ export function handleHtml(
2426

2527
return new Response(
2628
value,
27-
hasContentType
28-
? undefined
29-
: { headers: { 'content-type': options.contentType! } }
29+
initValue(options, hasContentType, status)
3030
)
3131
}
3232

@@ -60,8 +60,13 @@ export function handleHtml(
6060

6161
return new Response(
6262
stream as any,
63-
hasContentType
64-
? undefined
65-
: { headers: { 'content-type': options.contentType! } }
63+
initValue(options, hasContentType, status)
6664
)
6765
}
66+
67+
68+
function initValue(options: HtmlOptions, hasContentType: boolean, status?: number) {
69+
return hasContentType
70+
? { status: status?? 200 }
71+
: { headers: { 'content-type': options.contentType! }, status: status?? 200 }
72+
}

src/html.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Elysia } from 'elysia'
1+
import { Elysia, StatusMap } from 'elysia'
22
import { Readable } from 'node:stream'
33
import { renderToStream } from '@kitajs/html/suspense'
44

@@ -17,22 +17,26 @@ export function html(options: HtmlOptions = {}) {
1717
name: '@elysiajs/html',
1818
seed: options
1919
}).derive({ as: 'global' }, ({ set }) => {
20+
2021
return {
2122
html(
2223
value: Readable | JSX.Element
2324
): Promise<Response | string> | Response | string {
24-
return handleHtml(value, options, 'content-type' in set.headers)
25+
const status = typeof set.status === 'string' ? StatusMap[set.status] : set.status
26+
return handleHtml(value, options, 'content-type' in set.headers, status)
2527
},
2628
stream<A = any>(
2729
value: (this: void, arg: A & { id: number }) => JSX.Element,
2830
args: A
2931
) {
32+
const status = typeof set.status === 'string' ? StatusMap[set.status] : set.status
3033
return handleHtml(
3134
renderToStream((id) =>
3235
(value as Function)({ ...args, id })
3336
),
3437
options,
35-
'content-type' in set.headers
38+
'content-type' in set.headers,
39+
status
3640
)
3741
}
3842
}
@@ -48,10 +52,13 @@ export function html(options: HtmlOptions = {}) {
4852
// @kitajs/html stream
4953
(value instanceof Readable && 'rid' in value)
5054
) {
55+
const status = typeof set.status === 'string' ? StatusMap[set.status] : set.status
56+
5157
const response = await handleHtml(
5258
value,
5359
options,
54-
'content-type' in set.headers
60+
'content-type' in set.headers,
61+
status
5562
)
5663

5764
if (response instanceof Response) return response

test/index.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,16 @@ describe('HTML', () => {
6969
'text/html; charset=utf8'
7070
)
7171
})
72+
73+
it('returns user provided status code', async () => {
74+
const app = new Elysia().use(html()).get('/', ({ html, set }) => {
75+
set.status = 404
76+
return html('<h1>Not Found</h1>')
77+
})
78+
79+
const res = await app.handle(req('/'))
80+
expect(res.status).toBe(404)
81+
})
7282
})
7383

7484
describe('HTML vs No html - header', () => {

0 commit comments

Comments
 (0)