Skip to content

Commit e5ebfa9

Browse files
committed
Revert to building query using string concatenation
1 parent f675776 commit e5ebfa9

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

apps/question-service/src/app/services/question.ts

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { stringify } from 'querystring'
2-
31
export interface Question {
42
id: number;
53
docRefId: string;
@@ -33,35 +31,42 @@ export const GetQuestions = async (
3331
title?: string
3432
): Promise<QuestionListResponse> => {
3533
let query_url = `${process.env.NEXT_PUBLIC_API_URL}questions`;
36-
const params: NodeJS.Dict<number | string | string[]> = {}
34+
let query_params = "";
3735

3836
if (currentPage) {
39-
params.offset = (currentPage - 1) * 10;
37+
query_params += `?offset=${(currentPage - 1) * 10}`;
4038
}
4139

4240
if (limit) {
43-
params.limit = limit;
41+
query_params += `${query_params.length > 0 ? "&" : "?"}limit=${limit}`;
4442
}
4543

4644
if (sortBy) {
4745
const [field, order] = sortBy.split(" ");
48-
params.sortField = field;
49-
params.sortValue = order;
46+
query_params += `${
47+
query_params.length > 0 ? "&" : "?"
48+
}sortField=${field}&sortValue=${order}`;
5049
}
5150

5251
if (difficulties && difficulties.length > 0) {
53-
params.complexity = difficulties;
54-
}
55-
56-
if (title && title != "") {
57-
params.title = title
52+
const value = difficulties.join(","); // Change them from ["easy", "medium"] to "easy,medium"
53+
query_params += `${query_params.length > 0 ? "&" : "?"}complexity=${value}`;
5854
}
59-
55+
6056
if (categories && categories.length > 0) {
61-
params.categories = categories;
57+
const value = categories.join(",");
58+
query_params += `${query_params.length > 0 ? "&" : "?"}categories=${value}`;
59+
}
60+
61+
if (title && title != "") {
62+
const urlEncodedTitle = encodeURIComponent(title);
63+
query_params += `${
64+
query_params.length > 0 ? "&" : "?"
65+
}title=${urlEncodedTitle}`;
6266
}
6367

64-
const response = await fetch(`${query_url}?${stringify(params)}`);
68+
query_url += query_params;
69+
const response = await fetch(query_url);
6570
const data = await response.json();
6671
return data;
6772
};

0 commit comments

Comments
 (0)