Skip to content

fix(endpoint): use regexp to construct template string instead of Function #4135

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

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion packages/util-endpoints/src/utils/evaluateErrorRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ export const evaluateErrorRule = (errorRule: ErrorRuleObject, options: EvaluateO
evaluateExpression(error, "Error", {
...options,
referenceRecord: { ...options.referenceRecord, ...referenceRecord },
})
}) as string
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ describe(evaluateTemplate.name, () => {
});

it.each(["endpointParams", "referenceRecord"])("from %s", (key: string) => {
expect(evaluateTemplate(template, { ...mockOptions, [key]: { parameterName } })).toBe(`foo ${parameterName} baz`);
expect(evaluateTemplate(template, { ...mockOptions, [key]: { parameterName } })).toBe(
`foo ${parameterName} baz`
);
});
});

Expand Down
33 changes: 24 additions & 9 deletions packages/util-endpoints/src/utils/evaluateTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { getAttr } from "../lib";
import { EvaluateOptions } from "../types";

const ATTR_SHORTHAND_REGEX = new RegExp("\\${([\\w]+)#([\\w]+)}", "g");
const TEMPLATE_VARIABLE_REGEX = /\$\{(.*?)\}/;

export const evaluateTemplate = (template: string, options: EvaluateOptions) => {
const templateToEvaluate = template
Expand All @@ -10,9 +11,9 @@ export const evaluateTemplate = (template: string, options: EvaluateOptions) =>
// Replace `{${value}}` with `{value}`
.replace(new RegExp(`\{\\$\{([^{}]+)\}\}`, "g"), "{$1}");

const templateContext = {
...options.endpointParams,
...options.referenceRecord,
const templateContext: Record<string, string> = {
...(options.endpointParams as Record<string, string>),
...(options.referenceRecord as Record<string, string>),
};

const attrShortHandList = templateToEvaluate.match(ATTR_SHORTHAND_REGEX) || [];
Expand All @@ -21,7 +22,7 @@ export const evaluateTemplate = (template: string, options: EvaluateOptions) =>
const indexOfHash = attrShortHand.indexOf("#");
const refName = attrShortHand.substring(2, indexOfHash);
const attrName = attrShortHand.substring(indexOfHash + 1, attrShortHand.length - 1);
acc[attrShortHand] = getAttr(templateContext[refName] as Record<string, any>, attrName) as string;
acc[attrShortHand] = getAttr(templateContext[refName], attrName) as string;
return acc;
}, {} as Record<string, string>);

Expand All @@ -30,9 +31,23 @@ export const evaluateTemplate = (template: string, options: EvaluateOptions) =>
templateToEvaluate
);

const templateContextNames = Object.keys(templateContext);
const templateContextValues = Object.values(templateContext);
const templateWithTildeEscaped = templateWithAttr.replace(/\`/g, "\\`");

return new Function(...templateContextNames, `return \`${templateWithTildeEscaped}\``)(...templateContextValues);
let constructedString = templateWithAttr;
let match: RegExpMatchArray | null = null;

while ((match = constructedString.match(TEMPLATE_VARIABLE_REGEX))) {
const [matched, capture] = match;
const replacement = templateContext[capture];
if (!replacement) {
throw new Error(
`key [${capture}] from "${templateWithAttr}" was not found in attribute map ${JSON.stringify(
templateContext,
null,
2
)}`
);
}
constructedString = constructedString.replace(matched, templateContext[capture]);
}

return constructedString;
};