From 01c783499fb992a5ff3f04852c9f09b72805ef4d Mon Sep 17 00:00:00 2001 From: Tobias Diez Date: Wed, 23 Aug 2023 22:55:36 +0200 Subject: [PATCH] fix: improve request body parsing (#39) * chore: improve request body parsing * fix linter * increase peer dependency version --- package.json | 2 +- src/index.ts | 13 +++---------- 2 files changed, 4 insertions(+), 11 deletions(-) diff --git a/package.json b/package.json index 269ab90..a0151ca 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/index.ts b/src/index.ts index 5191205..10e25f6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -13,8 +13,7 @@ import { HTTPMethod, isMethod, setHeaders, - readRawBody, - getRequestHeader, + readBody, } from 'h3' import type { IncomingHttpHeaders } from 'http' @@ -117,13 +116,7 @@ function normalizeQueryString(url: string | undefined): string { async function normalizeBody(event: H3Event): Promise { 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) } }