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); }