diff --git a/.changeset/popular-deers-punch.md b/.changeset/popular-deers-punch.md new file mode 100644 index 0000000..22b1e03 --- /dev/null +++ b/.changeset/popular-deers-punch.md @@ -0,0 +1,5 @@ +--- +"@foxify/http": minor +--- + +Add `response.text` method diff --git a/packages/http/src/Response.ts b/packages/http/src/Response.ts index b1fe79c..0996332 100644 --- a/packages/http/src/Response.ts +++ b/packages/http/src/Response.ts @@ -931,16 +931,9 @@ class Response extends ServerResponse { * res.send("

some html

"); */ public send(body?: Buffer | JsonT | string): this { - let encoding: BufferEncoding | undefined; + if (typeof body === "string") return this.text(body); - if (typeof body === "string") { - encoding = "utf-8"; - const type = this.get("content-type"); - - // Reflect this in content-type - if (typeof type === "string") this.set("Content-Type", setCharset(type, encoding)); - else this.set("Content-Type", setCharset("text/html", encoding)); - } else if (Buffer.isBuffer(body)) { + if (Buffer.isBuffer(body)) { if (!this.hasHeader("content-type")) this.type("bin"); } else if (body === null) { return this.#send(""); @@ -949,7 +942,7 @@ class Response extends ServerResponse { return this.json(body); } - return this.#send(body, encoding); + return this.#send(body); } /** @@ -1105,6 +1098,23 @@ class Response extends ServerResponse { return this; } + /** + * Send text response + * @param body + * @example + * res.send("

some html

"); + */ + public text(body: string): this { + const encoding = "utf-8"; + const type = this.get("content-type"); + + // Reflect this in content-type + if (typeof type === "string") this.set("Content-Type", setCharset(type, encoding)); + else this.set("Content-Type", setCharset("text/html", encoding)); + + return this.#send(body); + } + /** * Add `field` to Vary. If already present in the Vary set, then * this call is simply ignored.