Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/better-fetch/src/create-fetch/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ export type RemoveEmptyString<T> = T extends string
: T;

export type InferParamPath<Path> =
Path extends `${infer _Start}:${infer Param}/${infer Rest}`
Path extends `${infer _Start}/:${infer Param}/${infer Rest}`
? {
[K in
| Param
| keyof InferParamPath<Rest> as RemoveEmptyString<K>]: string;
}
: Path extends `${infer _Start}:${infer Param}`
: Path extends `${infer _Start}/:${infer Param}`
? { [K in Param]: string }
: Path extends `${infer _Start}/${infer Rest}`
? InferParamPath<Rest>
Expand Down
8 changes: 3 additions & 5 deletions packages/better-fetch/src/test/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,10 +270,8 @@ describe("create-fetch-type-test", () => {
const res = await $fetch("/", {
throw: true,
});
expectTypeOf(res).toMatchTypeOf<
{ message: string }
>();
})
expectTypeOf(res).toMatchTypeOf<{ message: string }>();
});

it("should return unknown if no output is defined", () => {
const res = $fetch("/");
Expand Down Expand Up @@ -423,8 +421,8 @@ describe("create-fetch-type-test", () => {
params: {},
});
$fetch("/post/:id/:title", {
// @ts-expect-error
params: {
//@ts-expect-error
title: 1,
},
});
Expand Down
13 changes: 13 additions & 0 deletions packages/better-fetch/src/test/fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,19 @@ describe("url", () => {
expect(url.toString()).toBe("http://localhost:4001/param/%23test/item%201");
});

it("dynamic params should not collide with url", async () => {
const url = getURL("/v1/places:searchNearby", {
baseURL: "http://localhost:4000",
params: {
searchNearby: "test",
},
});

expect(url.toString()).toBe(
"http://localhost:4000/v1/places:searchNearby",
);
});

it("should expand array values into multiple query parameters", () => {
const url = getURL("/test", {
query: {
Expand Down
8 changes: 6 additions & 2 deletions packages/better-fetch/src/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,16 @@ export function getURL(url: string, option?: BetterFetchOption) {
}
} else {
for (const [key, value] of Object.entries(params)) {
path = path.replace(`:${key}`, String(value));
path = path.replace(`/:${key}`, `/${value}`);
}
}
}

path = path.split("/").map(encodeURIComponent).join("/");
path = path
.split("/")
// decode colon again so fetch doesn't break
.map((segment) => encodeURIComponent(segment).replace(/%3A/g, ":"))
.join("/");
if (path.startsWith("/")) path = path.slice(1);
let queryParamString = queryParams.toString();
queryParamString =
Expand Down