Skip to content

Add pathVariableNames and queryVariableNames getters in UriTemplate #609

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
48 changes: 48 additions & 0 deletions src/shared/uriTemplate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,4 +273,52 @@ describe("UriTemplate", () => {
expect(() => template.expand(vars)).not.toThrow();
});
});

describe("variable classification", () => {
it("should identify path variables", () => {
const template = new UriTemplate(
"/users/{id}/posts/{postId}{?limit,offset}"
);
expect(template.pathVariableNames).toEqual(["id", "postId"]);
});

it("should identify query variables", () => {
const template = new UriTemplate(
"/users/{id}/posts/{postId}{?limit,offset}"
);
expect(template.queryVariableNames).toEqual(["limit", "offset"]);
});

it("should classify different operators correctly", () => {
const template = new UriTemplate(
"{base}{+path}{#fragment}{.ext}{/segments}{?query}{&more}"
);
expect(template.pathVariableNames).toEqual([
"base",
"path",
"fragment",
"ext",
"segments",
]);
expect(template.queryVariableNames).toEqual(["query", "more"]);
});

it("should handle templates with only path variables", () => {
const template = new UriTemplate("/users/{id}/profile");
expect(template.pathVariableNames).toEqual(["id"]);
expect(template.queryVariableNames).toEqual([]);
});

it("should handle templates with only query variables", () => {
const template = new UriTemplate("/search{?q,limit,offset}");
expect(template.pathVariableNames).toEqual([]);
expect(template.queryVariableNames).toEqual(["q", "limit", "offset"]);
});

it("should handle templates with no variables", () => {
const template = new UriTemplate("/static/path");
expect(template.pathVariableNames).toEqual([]);
expect(template.queryVariableNames).toEqual([]);
});
});
});
35 changes: 35 additions & 0 deletions src/shared/uriTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,41 @@ export class UriTemplate {
return this.parts.flatMap((part) => typeof part === 'string' ? [] : part.names);
}

/**
* Returns variable names used in path-like expansions.
* These include simple expansion, reserved expansion, fragment expansion,
* label expansion, and path segment expansion.
*/
get pathVariableNames(): string[] {
return this.parts
.filter((part) => typeof part !== "string")
.filter((part) => {
// Path-like expansions: simple, reserved, fragment, label, path segments
return (
part.operator === "" ||
part.operator === "+" ||
part.operator === "#" ||
part.operator === "." ||
part.operator === "/"
);
})
.flatMap((part) => part.names);
}

/**
* Returns variable names used in query-like expansions.
* These include form-style query and query continuation expansions.
*/
get queryVariableNames(): string[] {
return this.parts
.filter((part) => typeof part !== "string")
.filter((part) => {
// Query-like expansions: form-style query and continuation
return part.operator === "?" || part.operator === "&";
})
.flatMap((part) => part.names);
}

constructor(template: string) {
UriTemplate.validateLength(template, MAX_TEMPLATE_LENGTH, "Template");
this.template = template;
Expand Down