Skip to content

Commit

Permalink
Merge branch 'refactor-content-type-parsing' of https://github.com/km…
Browse files Browse the repository at this point in the history
…cgill88/apollo-server-integration-azure-functions into kmcgill88-refactor-content-type-parsing
  • Loading branch information
aaronpowell committed Mar 16, 2023
2 parents e71848a + 83623cc commit 17a4142
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,23 +80,23 @@ function normalizeRequest(req: HttpRequest): HTTPGraphQLRequest {
method: req.method,
headers: normalizeHeaders(req.headers),
search: new URL(req.url).search,
body: parseBody(req.body, req.headers['content-type']),
body: parseBody(req.method, req.body, req.headers['content-type']),
};
}

function parseBody(
method: string | undefined,
body: string | null | undefined,
contentType: string | undefined,
): object | string {
if (body) {
if (contentType === 'application/json') {
if (typeof body === 'string') {
return JSON.parse(body);
}
return body;
}
): object | null {
const isValidContentType = contentType?.startsWith('application/json');
const isValidPostRequest =
method === 'POST' && typeof body === 'string' && isValidContentType;

if (isValidPostRequest) {
return JSON.parse(body);
}
return '';
return null;
}

function normalizeHeaders(headers: HttpRequestHeaders): HeaderMap {
Expand Down

0 comments on commit 17a4142

Please sign in to comment.