From 6c2f50e80f905cdb6fb0f7b0546086e5fc71348d Mon Sep 17 00:00:00 2001 From: Ardalan Amini Date: Sun, 13 Nov 2022 21:03:43 +0330 Subject: [PATCH] feat: Add `response.bin` method --- .changeset/swift-grapes-sleep.md | 5 +++++ packages/http/src/Response.ts | 26 ++++++++++++++++++-------- 2 files changed, 23 insertions(+), 8 deletions(-) create mode 100644 .changeset/swift-grapes-sleep.md diff --git a/.changeset/swift-grapes-sleep.md b/.changeset/swift-grapes-sleep.md new file mode 100644 index 0000000..2433624 --- /dev/null +++ b/.changeset/swift-grapes-sleep.md @@ -0,0 +1,5 @@ +--- +"@foxify/http": minor +--- + +Add `response.bin` method diff --git a/packages/http/src/Response.ts b/packages/http/src/Response.ts index 0996332..93d6126 100644 --- a/packages/http/src/Response.ts +++ b/packages/http/src/Response.ts @@ -409,6 +409,18 @@ class Response extends ServerResponse { 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`. * @@ -933,14 +945,12 @@ class Response extends ServerResponse { 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); }