-
-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Returning html always return a HTTP 200 status code #56
Comments
I will take a shot at this one 🏀 |
Fix elysiajs#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.
Fix elysiajs#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 patch versions of @kitajs/* dependencies.
I ended up just making my own plugin/middleware to handle HTML rendered using JSX with Kita HTML. You don't need much as Kita by default always renders to a string. I ended up grabbing the is html-check from the Elysia HTML plugin, but handle returning HTML as suggested in one of the documentation pages. Just transform with In tsconfig {
"jsx": "react-jsx",
"jsxImportSource": "@kitajs/html",
"plugins": [{ "name": "@kitajs/ts-html-plugin" }],
} The custom plugin module: import { Elysia } from "elysia";
/**
* A super fast and simple way to check if a string is HTML
* Check for opening and closing <tags> and if they are the same
* @param str the response
*/
export function isHTML(str: unknown): str is string {
if (typeof str !== "string") return false;
const trimmed = str.trim();
return (
trimmed.length >= 7 &&
trimmed[0] === "<" &&
trimmed[trimmed.length - 1] === ">"
);
}
/**
* Adds an onAfterHandle hook to the app that checks if the response is HTML.
* If it is, it sets the content-type header to text/html and adds a doctype.
* We only ever render HTML to string, so we don't need to care about streaming.
* @returns
*/
export const htmlPlugin = () => {
const app = new Elysia({ name: "html" })
.derive({ as: "global" }, () => {})
.onAfterHandle({ as: "global" }, ({ response, set }) => {
if (isHTML(response)) {
set.headers["content-type"] = "text/html; charset=utf-8";
return `<!doctype html>${response}`;
}
});
return app;
}; |
When using this plugin the handler will always return HTTP 200 code even if status code is explicitly set
reproduction
it returns http code 200 instead of 422.
Most of the time it's fine. But sometimes (for example using htmx) you want to be able to return html with your error
The text was updated successfully, but these errors were encountered: