Skip to content

Commit b043ead

Browse files
authored
feat: support application/octet-stream (Blob) req/res bodies (#359)
- adds support for `application/octet-stream` content types for both request and response bodies - introduces some associated helper functions, notably uses `raw-body` for the request body parsing side of things. It might make sense to see if we could always use this and drop the other body parsers - may still refactor things a bit more before release, eg: avoid pulling `raw-body` into the client runtimes
1 parent 2c870c6 commit b043ead

59 files changed

Lines changed: 996 additions & 164 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

e2e/openapi.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,25 @@ paths:
323323
application/json:
324324
schema:
325325
$ref: '#/components/schemas/ProductOrder'
326+
/media-types/octet-stream:
327+
post:
328+
tags:
329+
- media types
330+
requestBody:
331+
content:
332+
application/octet-stream:
333+
schema:
334+
type: string
335+
format: binary
336+
maxLength: 20971520
337+
responses:
338+
200:
339+
description: ok
340+
content:
341+
application/octet-stream:
342+
schema:
343+
type: string
344+
format: binary
326345

327346
/escape-hatches/plain-text:
328347
get:

e2e/src/generated/client/axios/client.ts

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

e2e/src/generated/client/fetch/client.ts

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

e2e/src/generated/server/express/routes/media-types.ts

Lines changed: 54 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

e2e/src/generated/server/koa/routes/media-types.ts

Lines changed: 51 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

e2e/src/index.axios.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,20 @@ describe.each(
430430
})
431431
})
432432

433+
describe("POST /media-types/octet-stream", () => {
434+
it("can send and parse application/octet-stream request bodies", async () => {
435+
const blob = new Blob([new Uint8Array([0xde, 0xad, 0xbe, 0xef])], {
436+
type: "application/octet-stream",
437+
})
438+
const res = await client.postMediaTypesOctetStream({
439+
requestBody: blob,
440+
})
441+
expect(res.status).toBe(200)
442+
443+
await expect(res.data).toEqualBlob(blob)
444+
})
445+
})
446+
433447
describe("query parameters", () => {
434448
it("GET /params/simple-query", async () => {
435449
const {status, data} = await client.getParamsSimpleQuery({

e2e/src/index.fetch.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,20 @@ describe.each(
431431
})
432432
})
433433

434+
describe("POST /media-types/octet-stream", () => {
435+
it("can send and parse application/octet-stream request bodies", async () => {
436+
const blob = new Blob([new Uint8Array([0xde, 0xad, 0xbe, 0xef])], {
437+
type: "application/octet-stream",
438+
})
439+
const res = await client.postMediaTypesOctetStream({
440+
requestBody: blob,
441+
})
442+
expect(res.status).toBe(200)
443+
444+
await expect(await res.blob()).toEqualBlob(blob)
445+
})
446+
})
447+
434448
describe("query parameters", () => {
435449
it("GET /params/simple-query", async () => {
436450
const res = await client.getParamsSimpleQuery({

e2e/src/jest.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import type {CustomMatcherResult} from "expect"
2+
3+
declare module "expect" {
4+
interface AsymmetricMatchers {
5+
toEqualBlob(this: R, expected: Blob): Promise<CustomMatcherResult>
6+
}
7+
interface Matchers<R> {
8+
toEqualBlob(this: R, expected: Blob): Promise<CustomMatcherResult>
9+
}
10+
}

e2e/src/routes/express/media-types.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {SkipResponse} from "@nahkies/typescript-express-runtime/server"
22
import {
33
createRouter,
4+
type PostMediaTypesOctetStream,
45
type PostMediaTypesText,
56
type PostMediaTypesXWwwFormUrlencoded,
67
} from "../../generated/server/express/routes/media-types.ts"
@@ -23,9 +24,17 @@ const postMediaTypesXWwwFormUrlencoded: PostMediaTypesXWwwFormUrlencoded =
2324
return respond.with200().body(body)
2425
}
2526

27+
const postMediaTypesOctetStream: PostMediaTypesOctetStream = async (
28+
{body},
29+
respond,
30+
) => {
31+
return respond.with200().body(body)
32+
}
33+
2634
export function createMediaTypesRouter() {
2735
return createRouter({
2836
postMediaTypesText,
2937
postMediaTypesXWwwFormUrlencoded,
38+
postMediaTypesOctetStream,
3039
})
3140
}

e2e/src/routes/koa/media-types.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {SkipResponse} from "@nahkies/typescript-koa-runtime/server"
22
import {
33
createRouter,
4+
type PostMediaTypesOctetStream,
45
type PostMediaTypesText,
56
type PostMediaTypesXWwwFormUrlencoded,
67
} from "../../generated/server/koa/routes/media-types.ts"
@@ -24,9 +25,17 @@ const postMediaTypesXWwwFormUrlencoded: PostMediaTypesXWwwFormUrlencoded =
2425
return respond.with200().body(body)
2526
}
2627

28+
const postMediaTypesOctetStream: PostMediaTypesOctetStream = async (
29+
{body},
30+
respond,
31+
) => {
32+
return respond.with200().body(body)
33+
}
34+
2735
export function createMediaTypesRouter() {
2836
return createRouter({
2937
postMediaTypesText,
3038
postMediaTypesXWwwFormUrlencoded,
39+
postMediaTypesOctetStream,
3140
})
3241
}

0 commit comments

Comments
 (0)