fix: reuse path parameters for repeated templates

This commit is contained in:
Hermes Agent
2026-06-20 10:31:22 +10:00
parent 70cc8f5b4a
commit 4f598225b5
2 changed files with 33 additions and 3 deletions
+29
View File
@@ -34,6 +34,35 @@ describe('resolveTemplateUrl', () => {
).toBe('https://en.wikipedia.org/wiki/Django');
});
it('reuses pathParam for repeated occurrences of the first template variable', () => {
expect(
resolveTemplateUrl({
targetUrl: 'https://example.com/{query}/{query}',
pathParam: 'foo bar',
query: params(),
}),
).toBe('https://example.com/foo%20bar/foo%20bar');
});
it('lets matching query params override pathParam for all repeated occurrences', () => {
expect(
resolveTemplateUrl({
targetUrl: 'https://example.com/{query}/{query}',
pathParam: 'from-path',
query: params('query=from query'),
}),
).toBe('https://example.com/from%20query/from%20query');
});
it('reuses a default value for repeated occurrences of the same template variable', () => {
expect(
resolveTemplateUrl({
targetUrl: 'https://example.com/{query,default=hello world}/{query,default=hello world}',
query: params(),
}),
).toBe('https://example.com/hello%20world/hello%20world');
});
it('lets matching query params override pathParam and defaults', () => {
expect(
resolveTemplateUrl({
+4 -3
View File
@@ -38,12 +38,13 @@ export function resolveTemplateUrl(input: TemplateInput): string {
}
let parameterIndex = 0;
const firstParameterName = parameters[0].name;
return input.targetUrl.replace(TEMPLATE_PATTERN, () => {
const parameter = parameters[parameterIndex];
parameterIndex += 1;
const value = resolveParameterValue(parameter, input);
const value = resolveParameterValue(parameter, input, firstParameterName);
return encodeURIComponent(value);
});
}
@@ -82,13 +83,13 @@ function unquoteDefault(value: string): string {
return value;
}
function resolveParameterValue(parameter: TemplateParameter, input: TemplateInput): string {
function resolveParameterValue(parameter: TemplateParameter, input: TemplateInput, firstParameterName: string): string {
const queryValue = input.query.get(parameter.name);
if (queryValue !== null) {
return queryValue;
}
if (parameter.index === 0 && input.pathParam !== undefined) {
if (parameter.name === firstParameterName && input.pathParam !== undefined) {
return input.pathParam;
}