Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
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(/\/$/, "");
}
}
68 changes: 67 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,72 @@ 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("Trailing slash in URL in expression is encoded", () => {
const options1 = endpoint(
"GET /repos/{owner}/{repo}/git/matching-refs/{ref}",
{
owner: "owner",
repo: "repo",
ref: "heads/",
},
);

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

const options2 = endpoint("GET /repos/{owner}/{repo}/contents/{path}", {
owner: "owner",
repo: "repo",
path: "heads/",
ref: "feature/branch",
});

expect(options2).toEqual({
method: "GET",
url: "https://api.github.com/repos/owner/repo/contents/heads%2F?ref=feature%2Fbranch",
headers: {
accept: "application/vnd.github.v3+json",
"user-agent": userAgent,
},
});
});

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