Skip to content

Commit

Permalink
fixed the error for handling params
Browse files Browse the repository at this point in the history
  • Loading branch information
ToxicalNoob3062 committed Apr 8, 2024
1 parent b7a8ab1 commit 5b4142f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
16 changes: 16 additions & 0 deletions index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,20 @@ describe("fetchAdapter", () => {
expect(res.status).toBeLessThan(300);
expect(res.headers["content-type"]).toContain("application/json");
});

//test to check query params
test("query params", async () => {
//make the request
const res = await client.get("/users/search", {
params: {
q: "John",
},
});

console.log(res.data);

expect(res.status).toBeGreaterThanOrEqual(200);
expect(res.status).toBeLessThan(300);
expect(res.headers["content-type"]).toContain("application/json");
});
});
13 changes: 9 additions & 4 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ export default function (fetch: fetch): AxiosAdapter {
//return the adapter using that fetch implementation
return async (config: InternalAxiosRequestConfig): AxiosPromise => {
//get the url from the config
const url = (config.baseURL ? config.baseURL : "") + config.url;
let url = (config.baseURL ? config.baseURL : "") + config.url;
//append params to the url if they exist
if (config.params) {
const params = new URLSearchParams(config.params).toString();
url += "?" + params;
}
if (!url) {
throw new AxiosError(
"no url specified. @fetchAdapter",
Expand Down Expand Up @@ -67,9 +72,9 @@ export default function (fetch: fetch): AxiosAdapter {

//if the error is not something we can handle, throw a generic error
throw new AxiosError(
"an error occurred while making the request.\n" +
(err as Error).message +
"\n@fetchAdapter",
"an error occurred while making the request.(" +
err +
") @fetchAdapter",
"777 (FETCH_ERROR)",
config,
request
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "fetchify-axios",
"description": "Fetchify Axios is a fetch adapter for Axios that takes any function that implements the standard fetchAPI interface!",
"version": "1.0.1",
"version": "1.0.2",
"main": "index.js",
"module": "index.js",
"types": "index.ts",
Expand Down

0 comments on commit 5b4142f

Please sign in to comment.