Skip to content

Commit

Permalink
Merge pull request #4 from shadiabuhilal/fixIsJsonReq
Browse files Browse the repository at this point in the history
fix: Bug fix isJsonReq function to handle map headers object that is used in Next.JS
  • Loading branch information
shadiabuhilal authored Dec 10, 2023
2 parents 9b82b57 + 451008e commit 99c0a3f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
"source.fixAll.eslint": "explicit"
},
"editor.formatOnSave": false,
"editor.tabSize": 4,
Expand Down
11 changes: 10 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ const jsonContentTypes = [
'application/hal+json'
];

const getReqHeader = (req, name) => {
if (req && req.headers) {
if (req.headers.get) {
return req.headers.get(name);
}
return req.headers[name];
}
};

/**
* @function
* @description Function that check if current request is a json request or not.
Expand All @@ -18,7 +27,7 @@ const jsonContentTypes = [
* @returns {boolean}
*/
export const isJsonReq = (req) => {
const contentType = req && req.headers && req.headers['content-type'];
const contentType = getReqHeader(req,'content-type');

const normalizedContentType = contentType && contentType.toLowerCase();

Expand Down
9 changes: 9 additions & 0 deletions tests/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ describe('is-json-request util', () => {
}
})
).to.be.ok();

const headersMap = new Map();
headersMap.set('content-type', 'application/json');

expect(
isJsonReq({
headers: headersMap
})
).to.be.ok();
});

it('should return false', () => {
Expand Down

0 comments on commit 99c0a3f

Please sign in to comment.