-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #55 from bleu-fi/luiza/perk-1079-challenges-comple…
…tion-table chore: check if url already has a query param
- Loading branch information
Showing
3 changed files
with
79 additions
and
3 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
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(); | ||
} |
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,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"); | ||
}); | ||
}); |