Skip to content

Commit

Permalink
fix: improve request body parsing (#39)
Browse files Browse the repository at this point in the history
* chore: improve request body parsing

* fix linter

* increase peer dependency version
  • Loading branch information
tobiasdiez authored Aug 23, 2023
1 parent fd9e66e commit 01c7834
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
},
"peerDependencies": {
"@apollo/server": "^4.1.1",
"h3": "^0.8.6 || ^1.0.0"
"h3": "^1.8.0"
},
"devDependencies": {
"@apollo/server": "^4.9.1",
Expand Down
13 changes: 3 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ import {
HTTPMethod,
isMethod,
setHeaders,
readRawBody,
getRequestHeader,
readBody,
} from 'h3'
import type { IncomingHttpHeaders } from 'http'

Expand Down Expand Up @@ -117,13 +116,7 @@ function normalizeQueryString(url: string | undefined): string {
async function normalizeBody(event: H3Event): Promise<unknown> {
const PayloadMethods: HTTPMethod[] = ['PATCH', 'POST', 'PUT', 'DELETE']
if (isMethod(event, PayloadMethods)) {
// We cannot use 'readBody' here because it will hide errors in the json parsing
const body = await readRawBody(event)
if (getRequestHeader(event, 'content-type')?.includes('application/json')) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return body ? JSON.parse(body) : {}
} else {
return body
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return await readBody(event)
}
}

0 comments on commit 01c7834

Please sign in to comment.