mirror of
https://github.com/wahyd4/heygo.git
synced 2026-08-08 21:05:48 +10:00
fix: reuse path parameters for repeated templates
This commit is contained in:
@@ -34,6 +34,35 @@ describe('resolveTemplateUrl', () => {
|
|||||||
).toBe('https://en.wikipedia.org/wiki/Django');
|
).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', () => {
|
it('lets matching query params override pathParam and defaults', () => {
|
||||||
expect(
|
expect(
|
||||||
resolveTemplateUrl({
|
resolveTemplateUrl({
|
||||||
|
|||||||
@@ -38,12 +38,13 @@ export function resolveTemplateUrl(input: TemplateInput): string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let parameterIndex = 0;
|
let parameterIndex = 0;
|
||||||
|
const firstParameterName = parameters[0].name;
|
||||||
|
|
||||||
return input.targetUrl.replace(TEMPLATE_PATTERN, () => {
|
return input.targetUrl.replace(TEMPLATE_PATTERN, () => {
|
||||||
const parameter = parameters[parameterIndex];
|
const parameter = parameters[parameterIndex];
|
||||||
parameterIndex += 1;
|
parameterIndex += 1;
|
||||||
|
|
||||||
const value = resolveParameterValue(parameter, input);
|
const value = resolveParameterValue(parameter, input, firstParameterName);
|
||||||
return encodeURIComponent(value);
|
return encodeURIComponent(value);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -82,13 +83,13 @@ function unquoteDefault(value: string): string {
|
|||||||
return value;
|
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);
|
const queryValue = input.query.get(parameter.name);
|
||||||
if (queryValue !== null) {
|
if (queryValue !== null) {
|
||||||
return queryValue;
|
return queryValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (parameter.index === 0 && input.pathParam !== undefined) {
|
if (parameter.name === firstParameterName && input.pathParam !== undefined) {
|
||||||
return input.pathParam;
|
return input.pathParam;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user