From babfbc8de696aa326f1e20c7c08461d6b1fcd410 Mon Sep 17 00:00:00 2001 From: nsrCodes Date: Wed, 8 Jan 2025 04:10:49 +0530 Subject: [PATCH] fix: add validations and processing logics to renderTemplate - validate that params are present for helpers that require params - validate if the number of params present are equal to or greater than the minimum that are required - if any of the validations above fail, then escape this part of the template (i.e. not process as helper) - add quotes if not already added around the args for the helper functions (required in the syntax for specifying them in hbsTemplate --- .../templating/helpers/requestHelpers.ts | 4 ++- src/core/utils/templating/utils.ts | 35 ++++++++++++++++--- src/test/dummy/mock1.ts | 2 +- 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/src/core/utils/templating/helpers/requestHelpers.ts b/src/core/utils/templating/helpers/requestHelpers.ts index 075929d..d45c366 100644 --- a/src/core/utils/templating/helpers/requestHelpers.ts +++ b/src/core/utils/templating/helpers/requestHelpers.ts @@ -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 = '') => { diff --git a/src/core/utils/templating/utils.ts b/src/core/utils/templating/utils.ts index d7b008e..a40322c 100644 --- a/src/core/utils/templating/utils.ts +++ b/src/core/utils/templating/utils.ts @@ -9,12 +9,37 @@ function escapeMatchFromHandlebars(match: string) { } export function wrapUnexpectedTemplateCaptures(template: string, allHelpers: Record) { - 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; }); } \ No newline at end of file diff --git a/src/test/dummy/mock1.ts b/src/test/dummy/mock1.ts index 0692862..45506db 100644 --- a/src/test/dummy/mock1.ts +++ b/src/test/dummy/mock1.ts @@ -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}}`, }, ], };