feat(request): add `bytes()` (#4921)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Yusuke Wada · committed by GitHub · parent e59db59 · Browse files · 2 files·+49 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+49 added−0 removed
- tests:+33 added−0 removed
- other:+16 added−0 removed
Provenance
- Commit
54f2f0c- Tree
155cf0a- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
e59db59
2 files changed+49 added−0 removed
modified:src/request.test.ts+33 added
251 unmodified lines252 )253 expect(await req.text()).toEqual(text)254 expect(await req.json()).toEqual(json)255 expect(await req.arrayBuffer()).toEqual(buffer)256+ expect(await req.bytes()).toEqual(new Uint8Array(buffer))257 expect(await req.blob()).toEqual(258 new Blob([text], {259 type: 'text/plain;charset=utf-8',260 })9 unmodified lines270 )271 expect(await req.json()).toEqual(json)272 expect(await req.text()).toEqual(text)273 expect(await req.arrayBuffer()).toEqual(buffer)274+ expect(await req.bytes()).toEqual(new Uint8Array(buffer))275 expect(await req.blob()).toEqual(276 new Blob([text], {277 type: 'text/plain;charset=utf-8',278 })22 unmodified lines301 )302 expect(await req.arrayBuffer()).toEqual(buffer)303 expect(await req.text()).toEqual(text)304 expect(await req.json()).toEqual(json)305+ expect(await req.bytes()).toEqual(new Uint8Array(buffer))306 expect(await req.blob()).toEqual(307 new Blob([text], {308 type: '',309 })310 )311 })312 313+ test('req.bytes()', async () => {314+ const bytes = new TextEncoder().encode('{"foo":"bar"}')315+ const req = new HonoRequest(316+ new Request('http://localhost', {317+ method: 'POST',318+ body: bytes,319+ })320+ )321+ const result = await req.bytes()322+ expect(result).toBeInstanceOf(Uint8Array)323+ expect(result).toEqual(bytes)324+ expect(await req.text()).toEqual(text)325+ expect(await req.json()).toEqual(json)326+ expect(await req.arrayBuffer()).toEqual(buffer)327+ expect(await req.blob()).toEqual(328+ new Blob([text], {329+ type: '',330+ })331+ )332+ })333+334 test('req.blob()', async () => {335 const blob = new Blob(['{"foo":"bar"}'], {336 type: 'application/json',337 })6 unmodified lines344 expect(await req.blob()).toEqual(blob)345 expect(await req.text()).toEqual(text)346 expect(await req.json()).toEqual(json)347 expect(await req.arrayBuffer()).toEqual(buffer)348+ expect(await req.bytes()).toEqual(new Uint8Array(buffer))349 })350 351 test('req.formData()', async () => {352 const data = new FormData()6 unmodified lines359 )360 expect((await req.formData()).get('foo')).toBe('bar')361 expect(async () => await req.text()).not.toThrow()362 expect(async () => await req.arrayBuffer()).not.toThrow()363+ expect(async () => await req.bytes()).not.toThrow()364 expect(async () => await req.blob()).not.toThrow()365 })366 367 describe('req.parseBody()', async () => {8 unmodified lines376 )377 expect((await req.parseBody())['foo']).toBe('bar')378 expect(async () => await req.text()).not.toThrow()379 expect(async () => await req.arrayBuffer()).not.toThrow()380+ expect(async () => await req.bytes()).not.toThrow()381 expect(async () => await req.blob()).not.toThrow()382 })383 384 describe('should not break body methods after parseBody() with non-form content-type', () => {23 unmodified lines408 await req.parseBody()409 expect(await req.arrayBuffer()).toBeInstanceOf(ArrayBuffer)410 })411 412+ it('bytes()', async () => {413+ const req = createReq()414+ await req.parseBody()415+ expect(await req.bytes()).toBeInstanceOf(Uint8Array)416+ })417+418 it('blob()', async () => {419 const req = createReq()420 await req.parseBody()421 expect(await req.blob()).toBeInstanceOf(Blob)179 unmodified linesmodified:src/request.ts+16 added
285 unmodified lines286 return this.#cachedBody('arrayBuffer')287 }288 289 /**290+ * `.bytes()` parses the request body as a `Uint8Array`.291+ *292+ * @see {@link https://hono.dev/docs/api/request#bytes}293+ *294+ * @example295+ * ```ts296+ * app.post('/entry', async (c) => {297+ * const body = await c.req.bytes()298+ * })299+ * ```300+ */301+ bytes(): Promise<Uint8Array> {302+ return this.#cachedBody('arrayBuffer').then((buffer: ArrayBuffer) => new Uint8Array(buffer))303+ }304+305+ /**306 * Parses the request body as a `Blob`.307 * @example308 * ```ts309 * app.post('/entry', async (c) => {196 unmodified lines