generated from Real-Dev-Squad/website-template
-
Notifications
You must be signed in to change notification settings - Fork 201
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds pagination to extension request list (#1419)
* feat : adds pagination for er list and also allows multiple search params * test : adds test cases for ER pagination * feat : updated query params * feat : query parser * refactor : change order of function parameters
- Loading branch information
1 parent
2aaaa1d
commit fb4b2b4
Showing
8 changed files
with
300 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
const { expect } = require("chai"); | ||
const { parseQueryParams } = require("../../../utils/queryParser"); | ||
|
||
describe("parseQueryParams", function () { | ||
it("parses query parameters correctly", function () { | ||
const queryString = "?q=status:APPROVED+DENIED,assignee:user1"; | ||
const parsedParams = parseQueryParams(queryString); | ||
|
||
expect(parsedParams).to.deep.equal({ | ||
status: ["APPROVED", "DENIED"], | ||
assignee: "user1", | ||
}); | ||
}); | ||
|
||
it("handles empty or malformed query parameters", function () { | ||
const queryString = "?q=;()"; | ||
const parsedParams = parseQueryParams(queryString); | ||
|
||
expect(parsedParams).to.deep.equal({}); | ||
}); | ||
|
||
it('handles multiple values for non-"q" parameters', function () { | ||
const queryString = "?status=APPROVED&status=DENIED&assignee=user1"; | ||
const parsedParams = parseQueryParams(queryString); | ||
|
||
expect(parsedParams).to.deep.equal({ | ||
status: ["APPROVED", "DENIED"], | ||
assignee: "user1", | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* Parses the query params and returns a key value object | ||
* | ||
* @param queryString {string} - string which is received on the http request | ||
* @return resultParams {Object} - contains key value pairs of query params | ||
*/ | ||
const parseQueryParams = (queryString) => { | ||
try { | ||
const urlParams = new URLSearchParams(queryString); | ||
|
||
const resultParams = {}; | ||
for (const [key, value] of urlParams) { | ||
if (!key || !value) continue; | ||
if (key !== "q") { | ||
if (!resultParams[key]) { | ||
resultParams[key] = new Set(); | ||
} | ||
resultParams[key].add(value); | ||
continue; | ||
} | ||
const queries = value.trim().replace(" ", "+").split(","); | ||
for (const query of queries) { | ||
if (!query) continue; | ||
const [searchTerm, searchValueString] = query.trim().split(":"); | ||
const searchValues = searchValueString.trim().split("+"); | ||
for (const searchValue of searchValues) { | ||
if (!searchValue) continue; | ||
if (!resultParams[searchTerm]) { | ||
resultParams[searchTerm] = new Set(); | ||
} | ||
resultParams[searchTerm].add(searchValue); | ||
} | ||
} | ||
} | ||
|
||
for (const [key, value] of Object.entries(resultParams)) { | ||
if (value.size > 1) { | ||
resultParams[key] = [...resultParams[key]]; | ||
} else { | ||
const [first] = resultParams[key]; | ||
resultParams[key] = first; | ||
} | ||
} | ||
return resultParams; | ||
} catch (error) { | ||
logger.error(`Error parsing the queries: ${error}`); | ||
} | ||
return {}; | ||
}; | ||
|
||
module.exports = { parseQueryParams }; |