From 3eed3e6cb442829fe2e180e96f3848637de86920 Mon Sep 17 00:00:00 2001 From: Guillaume Date: Fri, 18 Feb 2022 17:31:14 +0100 Subject: [PATCH] Make `bodyRaw` returns the parsed body when no path is provided Closes #41 --- .../templating-helpers/request-helpers.ts | 27 ++++++++----------- .../request-helpers.spec.ts | 17 ++++++++++++ 2 files changed, 28 insertions(+), 16 deletions(-) diff --git a/src/libs/templating-helpers/request-helpers.ts b/src/libs/templating-helpers/request-helpers.ts index 94d5bd7..fe81fd4 100644 --- a/src/libs/templating-helpers/request-helpers.ts +++ b/src/libs/templating-helpers/request-helpers.ts @@ -30,17 +30,17 @@ export const RequestHelpers = function ( return new SafeString(request.body); } - let requestToParse; + let source; if (request.parsedBody) { - requestToParse = request.parsedBody; + source = request.parsedBody; } else { return new SafeString( stringify ? JSON.stringify(defaultValue) : defaultValue ); } - let value = objectGet(requestToParse, path); + let value = objectGet(source, path); value = value === undefined ? defaultValue : value; if (Array.isArray(value) || typeof value === 'object') { @@ -49,7 +49,6 @@ export const RequestHelpers = function ( return new SafeString(stringify ? JSON.stringify(value) : value); }, - // get the raw json property from body to use with each for example bodyRaw: function (...args: any[]) { let path = ''; @@ -62,23 +61,19 @@ export const RequestHelpers = function ( path = parameters[0]; defaultValue = parameters[1]; } - // if no path has been provided we want the full raw body as is - if (!path) { - return request.body; - } - - let requestToParse; if (request.parsedBody) { - requestToParse = request.parsedBody; + // if no path has been provided we want the full raw body as is + if (!path) { + return request.parsedBody; + } + + const value = objectGet(request.parsedBody, path); + + return value !== undefined ? value : defaultValue; } else { return defaultValue; } - - let value = objectGet(requestToParse, path); - value = value === undefined ? defaultValue : value; - - return value; }, // use params from url /:param1/:param2 diff --git a/test/suites/templating-helpers/request-helpers.spec.ts b/test/suites/templating-helpers/request-helpers.spec.ts index cd1eab3..f0bf290 100644 --- a/test/suites/templating-helpers/request-helpers.spec.ts +++ b/test/suites/templating-helpers/request-helpers.spec.ts @@ -126,6 +126,7 @@ describe('Template parser', () => { ); expect(parseResult).to.be.equal('1'); }); + it('should return an array without quotes', () => { const parseResult = TemplateParser( "{{bodyRaw 'prop'}}", @@ -136,6 +137,7 @@ describe('Template parser', () => { ); expect(parseResult).to.be.equal('1,2,3'); }); + it('should return a boolean without quotes', () => { const parseResult = TemplateParser( "{{bodyRaw 'prop'}}", @@ -146,6 +148,7 @@ describe('Template parser', () => { ); expect(parseResult).to.be.equal('true'); }); + it('should be usable with a each', () => { const parseResult = TemplateParser( "{{#each (bodyRaw 'myList')}}dolphin{{/each}}", @@ -160,6 +163,7 @@ describe('Template parser', () => { ); expect(parseResult).to.be.equal('dolphindolphindolphin'); }); + it('should be usable within a if clause', () => { const parseResult = TemplateParser( "{{#if (bodyRaw 'boolean')}}dolphin{{/if}}", @@ -174,6 +178,7 @@ describe('Template parser', () => { ); expect(parseResult).to.be.equal('dolphin'); }); + it('should return the default value in a each when no request body', () => { const parseResult = TemplateParser( "{{#each (bodyRaw 'dolphin' (array 1 2 3))}}dolphin{{/each}}", @@ -182,6 +187,7 @@ describe('Template parser', () => { ); expect(parseResult).to.be.equal('dolphindolphindolphin'); }); + it('should return the default value in a if clause when no request body', () => { const parseResult = TemplateParser( "{{#if (bodyRaw 'dolphin' true)}}dolphin{{/if}}", @@ -190,6 +196,17 @@ describe('Template parser', () => { ); expect(parseResult).to.be.equal('dolphin'); }); + + it('should return the enumerated strings when body contains a root array and no path is provided', () => { + const parseResult = TemplateParser( + '{{#each (bodyRaw)}}{{this}}{{/each}}', + { + parsedBody: ['string1', 'string2'] + } as any, + {} as any + ); + expect(parseResult).to.be.equal('string1string2'); + }); }); describe('Helper: queryParam', () => {