Skip to content

Commit

Permalink
fix: support non-json bodies (#76)
Browse files Browse the repository at this point in the history
Co-authored-by: timbastin <[email protected]>
  • Loading branch information
aeneasr and timbastin authored Jan 2, 2025
1 parent e6520e1 commit 240fba8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/next-edge/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function createApp(options: CreateApiHandlerOptions): AppResult {

const handler = createApiHandler({
apiBaseUrlOverride:
"https://youthful-feynman-ml50dfb20g.projects.staging.oryapis.dev",
"https://youthful-feynman-ml50dfb20g.projects.staging.oryapis.dev",
...options,
})
const router = express.Router()
Expand Down
12 changes: 10 additions & 2 deletions src/next-edge/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Buffer } from "buffer"
import { SerializeOptions as CookieSerializeOptions, serialize } from "cookie"
import { IncomingHttpHeaders } from "http"
import { isText } from "istextorbinary"
Expand All @@ -10,6 +9,15 @@ import { defaultForwardedHeaders } from "../common/default-forwarded-headers"
import { processLocationHeader } from "../common/process-location-header"
import { guessCookieDomain } from "../common/get-cookie-domain"

function readRawBody(req: NextApiRequest): Promise<Buffer> {
return new Promise((resolve, reject) => {
const chunks: Uint8Array[] = []
req.on("data", (chunk) => chunks.push(chunk))
req.on("end", () => resolve(Buffer.concat(chunks)))
req.on("error", (err) => reject(err))
})
}

export function filterRequestHeaders(
headers: IncomingHttpHeaders,
forwardAdditionalHeaders?: string[],
Expand Down Expand Up @@ -112,7 +120,7 @@ export function createApiHandler(options: CreateApiHandlerOptions) {
headers,
body:
req.method !== "GET" && req.method !== "HEAD"
? JSON.stringify(req.body)
? await readRawBody(req)
: null,
redirect: "manual",
})
Expand Down

0 comments on commit 240fba8

Please sign in to comment.