Skip to content
This repository has been archived by the owner on Jul 12, 2024. It is now read-only.

Commit

Permalink
Merge branch 'fix/bodyraw-no-path' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Guillaume committed Feb 18, 2022
2 parents 852a13e + 3eed3e6 commit aa073a8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
27 changes: 11 additions & 16 deletions src/libs/templating-helpers/request-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand All @@ -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 = '';
Expand All @@ -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
Expand Down
17 changes: 17 additions & 0 deletions test/suites/templating-helpers/request-helpers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'}}",
Expand All @@ -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'}}",
Expand All @@ -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}}",
Expand All @@ -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}}",
Expand All @@ -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}}",
Expand All @@ -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}}",
Expand All @@ -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', () => {
Expand Down

0 comments on commit aa073a8

Please sign in to comment.