Skip to content

Commit

Permalink
Merge pull request #55 from bleu-fi/luiza/perk-1079-challenges-comple…
Browse files Browse the repository at this point in the history
…tion-table

chore: check if url already has a query param
  • Loading branch information
ribeirojose authored Apr 11, 2024
2 parents e816236 + 1822396 commit c7cffd1
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/components/DataTable/useSWRDataTable.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import useSWR from "swr";
import { useTableState } from "./useTableState";
import { serializeQuery } from "#/lib/serializeQuery";
import { constructFullUrlWithParams } from "#/lib/constructFullUrlWithParams";

function formatRequestParams(originalObj) {
return {
Expand All @@ -15,8 +15,10 @@ function formatRequestParams(originalObj) {

const dataTableFetcher = async ([url, paramsObject]) => {
const formattedParams = formatRequestParams(paramsObject);
const params = serializeQuery(formattedParams);
const response = await fetch(`${url}?${params}`, {

const fullUrl = constructFullUrlWithParams(url, formattedParams);

const response = await fetch(fullUrl, {
headers: {
Accept: "application/json",
},
Expand Down
21 changes: 21 additions & 0 deletions src/lib/constructFullUrlWithParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { deserializeQuery, serializeQuery } from "./serializeQuery";

export function constructFullUrlWithParams(
baseUrl: string,
queryParams: Record<string, unknown>
) {
if (typeof baseUrl !== "string") {
throw new Error("Base URL must be a string.");
}

const url = new URL(baseUrl);

const existingParams = deserializeQuery(url.search);

const mergedParams = { ...existingParams, ...queryParams };
const serializedParams = serializeQuery(mergedParams);

url.search = serializedParams;

return url.toString();
}
53 changes: 53 additions & 0 deletions tests/lib/constructFullUrlWithParams.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { describe, expect, it } from "vitest";
import { constructFullUrlWithParams } from "../../src/lib/constructFullUrlWithParams";

describe("constructFullUrlWithParams", () => {
it("should throw an error if the base URL is not a string", () => {
// @ts-expect-error
expect(() => constructFullUrlWithParams(null, {})).toThrow(
"Base URL must be a string."
);
});

it("should correctly add simple query parameters to a base URL", () => {
const baseUrl = "http://example.com";
const queryParams = { name: "John", age: 30 };
const result = constructFullUrlWithParams(baseUrl, queryParams);
expect(result).toBe("http://example.com/?name=John&age=30");
});

it("should handle nested objects and arrays in query parameters", () => {
const baseUrl = "http://example.com";
const queryParams = {
user: { name: "John", roles: ["admin", "user"] },
active: true,
};
const result = constructFullUrlWithParams(baseUrl, queryParams);
expect(result).toBe(
"http://example.com/?user[name]=John&user[roles][]=admin&user[roles][]=user&active=true"
);
});

it("should handle special characters in query parameters", () => {
const baseUrl = "http://example.com";
const queryParams = { greeting: "hello world", symbols: "!@#$%^&*" };
const result = constructFullUrlWithParams(baseUrl, queryParams);
expect(result).toBe(
"http://example.com/?greeting=hello%20world&symbols=!%40%23%24%25%5E%26*"
);
});

it("should merge and serialize existing query parameters with new ones", () => {
const baseUrl = "http://example.com?name=Jane&active=true";
const queryParams = { age: 30, active: false };
const result = constructFullUrlWithParams(baseUrl, queryParams);
expect(result).toBe("http://example.com/?name=Jane&active=false&age=30");
});

it("should override existing parameters with new ones", () => {
const baseUrl = "http://example.com?name=Jane";
const queryParams = { name: "John", age: 30 };
const result = constructFullUrlWithParams(baseUrl, queryParams);
expect(result).toBe("http://example.com/?name=John&age=30");
});
});

0 comments on commit c7cffd1

Please sign in to comment.