Skip to content
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

fix: add validations and processing logics to renderTemplate #28

Open
wants to merge 1 commit into
base: master
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
4 changes: 3 additions & 1 deletion src/core/utils/templating/helpers/requestHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { MockContextParams } from "../../../../types/internal";

const requestHelpers = (params: MockContextParams) => {
const helpers = {
urlParam: (param: string) => params.urlParams[param],
urlParams: (param: string) => {
return params.urlParams[param]
},
method: () => params.method,
statusCode: () => params.statusCode,
header: (param: string, defaultValue: string = '') => {
Expand Down
35 changes: 30 additions & 5 deletions src/core/utils/templating/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,37 @@ function escapeMatchFromHandlebars(match: string) {
}

export function wrapUnexpectedTemplateCaptures(template: string, allHelpers: Record<string, unknown>) {
const helperNames = Object.keys(allHelpers)
const helperNames = Object.keys(allHelpers);

return template.replace(/{{\s*([\s\S]*?)\s*}}/g, (completeMatch, firstMatchedGroup) => {
const isMatchEmpty = firstMatchedGroup.trim() === ''; // {{}}
const matchStartsWithKnownHelper = helperNames.some(helperName => firstMatchedGroup.includes(helperName));
const isMatchEmpty = firstMatchedGroup.trim() === '';
if (isMatchEmpty) return escapeMatchFromHandlebars(completeMatch);

if(isMatchEmpty || !matchStartsWithKnownHelper) return escapeMatchFromHandlebars(completeMatch);
else return completeMatch;
const [helperName, ...args] = firstMatchedGroup.trim().split(/\s+/);

// Check if it starts with a known helper
const matchStartsWithKnownHelper = helperNames.some(name => helperName === name);
if (!matchStartsWithKnownHelper) {
return escapeMatchFromHandlebars(completeMatch);
}

// Get helper function and its required parameters
const helperFunction = allHelpers[helperName] as Function;
const requiredParams = helperFunction.length;

// Escape if not enough arguments provided
if (args.length < requiredParams) {
return escapeMatchFromHandlebars(completeMatch);
}

// Wrap unquoted arguments in quotes
if (args.length > 0) {
const wrappedArgs = args.map((arg:any) => {
return /^['"].*['"]$/.test(arg) ? arg : `"${arg}"`;
});
return `{{${helperName} ${wrappedArgs.join(' ')}}}`;
}

return completeMatch;
});
}
2 changes: 1 addition & 1 deletion src/test/dummy/mock1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export const dummyMock4: Mock = {
"x-foo": "bar",
"content-type": "text/plain",
},
body: `the id is {{urlParam 'id'}} . the url is {{url}} . not passing param to url param {{urlParam}}. Content type is {{header 'Content-Type'}}. giberish ahead: {{random values}} {{}} {{color: "something"}} {{url 'http://localhost:3000'}} {{urlParam 'id'}} {{ color: "red", display: flex}}`,
body: `the id is {{urlParams}} {{urlParams id}} no way to add space right now so {{urlParams 'name'}} . the url is {{url}} . not passing param to url param {{urlParam}}. Content type is {{header Content-Type}}. giberish ahead: {{random values}} {{}} {{color: "something"}} {{url 'http://localhost:3000'}} {{urlParam 'id'}} {{ color: "red", display: flex}}`,
},
],
};
Expand Down