Skip to content

Commit 139e7ec

Browse files
committed
Merge branch 'kmcgill88-refactor-content-type-parsing'
2 parents e71848a + 17a4142 commit 139e7ec

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

src/index.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,23 +80,23 @@ function normalizeRequest(req: HttpRequest): HTTPGraphQLRequest {
8080
method: req.method,
8181
headers: normalizeHeaders(req.headers),
8282
search: new URL(req.url).search,
83-
body: parseBody(req.body, req.headers['content-type']),
83+
body: parseBody(req.method, req.body, req.headers['content-type']),
8484
};
8585
}
8686

8787
function parseBody(
88+
method: string | undefined,
8889
body: string | null | undefined,
8990
contentType: string | undefined,
90-
): object | string {
91-
if (body) {
92-
if (contentType === 'application/json') {
93-
if (typeof body === 'string') {
94-
return JSON.parse(body);
95-
}
96-
return body;
97-
}
91+
): object | null {
92+
const isValidContentType = contentType?.startsWith('application/json');
93+
const isValidPostRequest =
94+
method === 'POST' && typeof body === 'string' && isValidContentType;
95+
96+
if (isValidPostRequest) {
97+
return JSON.parse(body);
9898
}
99-
return '';
99+
return null;
100100
}
101101

102102
function normalizeHeaders(headers: HttpRequestHeaders): HeaderMap {

0 commit comments

Comments
 (0)