Skip to content

Commit

Permalink
feat: Add response.text method
Browse files Browse the repository at this point in the history
  • Loading branch information
ardalanamini committed Nov 13, 2022
1 parent 300e92c commit cc4d60c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/popular-deers-punch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@foxify/http": minor
---

Add `response.text` method
30 changes: 20 additions & 10 deletions packages/http/src/Response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -931,16 +931,9 @@ class Response extends ServerResponse<Request> {
* res.send("<p>some html</p>");
*/
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("");
Expand All @@ -949,7 +942,7 @@ class Response extends ServerResponse<Request> {
return this.json(body);
}

return this.#send(body, encoding);
return this.#send(body);
}

/**
Expand Down Expand Up @@ -1105,6 +1098,23 @@ class Response extends ServerResponse<Request> {
return this;
}

/**
* Send text response
* @param body
* @example
* res.send("<p>some html</p>");
*/
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.
Expand Down

0 comments on commit cc4d60c

Please sign in to comment.