Skip to content

Commit

Permalink
fix: remove trailing slash for invalid URL
Browse files Browse the repository at this point in the history
  • Loading branch information
maemaemae3 committed Oct 25, 2023
1 parent 8205e23 commit 9f143b1
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/util/url-template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ export function parseUrl(template: string) {
function expand(template: string, context: object): string {
var operators = ["+", "#", ".", "/", ";", "?", "&"];

return template.replace(
template = template.replace(
/\{([^\{\}]+)\}|([^\{\}]+)/g,
function (_, expression, literal) {
if (expression) {
Expand Down Expand Up @@ -190,4 +190,10 @@ function expand(template: string, context: object): string {
}
},
);

if (template === "/") {
return template;
} else {
return template.replace(/\/$/, "");
}
}
32 changes: 31 additions & 1 deletion test/endpoint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ describe("endpoint()", () => {
expect(endpoint("/").url).toEqual("https://api.github.com/");
expect(endpoint("/").method).toEqual("GET");
expect(endpoint("https://github.acme-inc/api/v3/").url).toEqual(
"https://github.acme-inc/api/v3/",
"https://github.acme-inc/api/v3",
);
});

Expand Down Expand Up @@ -500,6 +500,36 @@ describe("endpoint()", () => {
});
});

it("Undefined placeholder value with no trailing slash in URL", () => {
const options1 = endpoint("GET /repos/{owner}/{repo}/branches/{branch}", {
owner: "owner",
repo: "repo",
});

expect(options1).toEqual({
method: "GET",
url: "https://api.github.com/repos/owner/repo/branches",
headers: {
accept: "application/vnd.github.v3+json",
"user-agent": userAgent,
},
});

const options2 = endpoint("GET /repos/{owner}/{repo}/branches{/branch}", {
owner: "owner",
repo: "repo",
});

expect(options2).toEqual({
method: "GET",
url: "https://api.github.com/repos/owner/repo/branches",
headers: {
accept: "application/vnd.github.v3+json",
"user-agent": userAgent,
},
});
});

it("Undefined header value", () => {
const options = endpoint({
method: "GET",
Expand Down

0 comments on commit 9f143b1

Please sign in to comment.