Skip to content

Commit

Permalink
feat: update constructQueryUrl function to return string if URL is no…
Browse files Browse the repository at this point in the history
…t valid
  • Loading branch information
JoeSiu committed Mar 24, 2024
1 parent 333dc42 commit a16de3c
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 6 deletions.
21 changes: 17 additions & 4 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,23 @@ import { XCRecording, XCResponse } from "../types/response";
* @param {string} query - The query string to be appended to the base URL.
* @param {XCQueryOption} [options] - Optional additional query options.
* @param {number} [page] - Optional page number.
* @return {URL} The constructed query URL.
* @return {URL | string} The constructed query URL.
*/
export function constructQueryUrl(
baseUrl: string,
query: string,
options?: XCQueryOption,
page?: number,
): URL {
let url = new URL(baseUrl);
): URL | string {
let url: URL | string;

try {
url = new URL(baseUrl);
} catch (error) {
// If the base URL is not a valid URL, fallback to a string URL
url = baseUrl;
}

let parms = new URLSearchParams();

// Append query to search parameters
Expand All @@ -41,7 +49,12 @@ export function constructQueryUrl(
}

// Set search parameters
url.search = parms.toString();
if (typeof url === "object" && url instanceof URL) {
url.search = parms.toString();
} else {
const queryString = parms.toString();
url += (url.includes("?") ? "&" : "?") + queryString;
}

return url;
}
Expand Down
40 changes: 38 additions & 2 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { beforeEach, describe, expect, test } from "vitest";
import { AdditionalWrapperOption, XCQueryOption, search } from "../src";
import { AdditionalWrapperOption, XCQueryOption, constructQueryUrl, convertJsonToXCResponse, search } from "../src";

describe("Search function", () => {
beforeEach(async () => {
Expand Down Expand Up @@ -220,7 +220,7 @@ describe("Search function", () => {
};
const additionalOptions: AdditionalWrapperOption = {
baseUrl:
"https://run.mocky.io/v3/9f08db9a-cfba-4b1d-8c4a-765932f6cf3b?query=", // Fake data
"https://run.mocky.io/v3/9f08db9a-cfba-4b1d-8c4a-765932f6cf3b", // Fake data
};

const result = await search(query, options, undefined, additionalOptions);
Expand All @@ -234,5 +234,41 @@ describe("Search function", () => {
expect(result.xcResponse.numRecordings).toBeGreaterThan(0);
expect(result.xcResponse.recordings.length).toBeGreaterThan(0);
});

test("Custom fetch (URL object)", async () => {
const baseUrl = "https://run.mocky.io/v3/9f08db9a-cfba-4b1d-8c4a-765932f6cf3b"
const query = "Sparrow";
const options: XCQueryOption = {
grp: "birds",
cnt: "Brazil",
};

const url = constructQueryUrl(baseUrl, query, options);

const response = await fetch(url);
const json = await response.json();
const result = convertJsonToXCResponse(json);

console.log(`Requested URL: ${url}`);
console.log(`numRecordings: ${result.numRecordings}`);
console.log(`numPages: ${result.numPages}`);
expect(result).toBeDefined();
expect(result.numRecordings).toBeGreaterThan(0);
expect(result.recordings.length).toBeGreaterThan(0);
});

test("Custom fetch (string object)", async () => {
const baseUrl = "/api/xeno-canto"
const query = "Sparrow";
const options: XCQueryOption = {
grp: "birds",
cnt: "Brazil",
};

const url = constructQueryUrl(baseUrl, query, options);

console.log(`Requested URL: ${url}`);
expect(url).toBeTypeOf("string");
});
});
});

0 comments on commit a16de3c

Please sign in to comment.