Skip to content

Commit 6c2f50e

Browse files
committed
feat: Add response.bin method
1 parent cc4d60c commit 6c2f50e

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

.changeset/swift-grapes-sleep.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@foxify/http": minor
3+
---
4+
5+
Add `response.bin` method

packages/http/src/Response.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,18 @@ class Response extends ServerResponse<Request> {
409409
return this.set("Content-Disposition", contentDisposition(filename));
410410
}
411411

412+
/**
413+
* Send Buffer response
414+
* @param body
415+
* @example
416+
* res.send(Buffer.from("wahoo"));
417+
*/
418+
public bin(body: Buffer): this {
419+
if (!this.hasHeader("content-type")) this.type("bin");
420+
421+
return this.#send(body);
422+
}
423+
412424
/**
413425
* Clear cookie `name`.
414426
*
@@ -933,14 +945,12 @@ class Response extends ServerResponse<Request> {
933945
public send(body?: Buffer | JsonT | string): this {
934946
if (typeof body === "string") return this.text(body);
935947

936-
if (Buffer.isBuffer(body)) {
937-
if (!this.hasHeader("content-type")) this.type("bin");
938-
} else if (body === null) {
939-
return this.#send("");
940-
// eslint-disable-next-line no-undefined
941-
} else if (body !== undefined) {
942-
return this.json(body);
943-
}
948+
if (Buffer.isBuffer(body)) return this.bin(body);
949+
950+
if (body === null) return this.#send("");
951+
952+
// eslint-disable-next-line no-undefined
953+
if (body !== undefined) return this.json(body);
944954

945955
return this.#send(body);
946956
}

0 commit comments

Comments
 (0)