Skip to content

Commit

Permalink
feat: Add response.bin method
Browse files Browse the repository at this point in the history
  • Loading branch information
ardalanamini committed Nov 13, 2022
1 parent cc4d60c commit 6c2f50e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/swift-grapes-sleep.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@foxify/http": minor
---

Add `response.bin` method
26 changes: 18 additions & 8 deletions packages/http/src/Response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,18 @@ class Response extends ServerResponse<Request> {
return this.set("Content-Disposition", contentDisposition(filename));
}

/**
* Send Buffer response
* @param body
* @example
* res.send(Buffer.from("wahoo"));
*/
public bin(body: Buffer): this {
if (!this.hasHeader("content-type")) this.type("bin");

return this.#send(body);
}

/**
* Clear cookie `name`.
*
Expand Down Expand Up @@ -933,14 +945,12 @@ class Response extends ServerResponse<Request> {
public send(body?: Buffer | JsonT | string): this {
if (typeof body === "string") return this.text(body);

if (Buffer.isBuffer(body)) {
if (!this.hasHeader("content-type")) this.type("bin");
} else if (body === null) {
return this.#send("");
// eslint-disable-next-line no-undefined
} else if (body !== undefined) {
return this.json(body);
}
if (Buffer.isBuffer(body)) return this.bin(body);

if (body === null) return this.#send("");

// eslint-disable-next-line no-undefined
if (body !== undefined) return this.json(body);

return this.#send(body);
}
Expand Down

0 comments on commit 6c2f50e

Please sign in to comment.