Skip to content

Commit

Permalink
feat: STRF-11819 Pass down to hbs request params
Browse files Browse the repository at this point in the history
  • Loading branch information
jairo-bc committed Mar 13, 2024
1 parent 145a60f commit 2f6aaa3
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
26 changes: 23 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,19 @@ class Paper {
* @param {String} rendererType - One of ['handlebars-v3', 'handlebars-v4']
* @param {Object} logger - a console-like logger object
* @param {String} logLevel - log level used by handlebars logger (debug, info, warning, error)
* @param {Object} params - Request-level parameters, part of stencil context
*/
constructor(siteSettings, themeSettings, assembler, rendererType, logger = console, logLevel = 'info') {
constructor(siteSettings, themeSettings, assembler, rendererType, logger = console, logLevel = 'info', params = {}) {
this._assembler = assembler || {};

// Build renderer based on type
switch(rendererType) {
case 'handlebars-v4':
this.renderer = new HandlebarsRenderer(siteSettings, themeSettings, 'v4', logger, logLevel);
this.renderer = new HandlebarsRenderer(siteSettings, themeSettings, 'v4', logger, logLevel, params);
break;
case 'handlebars-v3':
default:
this.renderer = new HandlebarsRenderer(siteSettings, themeSettings, 'v3', logger, logLevel);
this.renderer = new HandlebarsRenderer(siteSettings, themeSettings, 'v3', logger, logLevel, params);
break;
}

Expand Down Expand Up @@ -109,6 +110,25 @@ class Paper {
this.renderer.setThemeSettings(settings);
};


/**
* Get the requestParams object containing the request parameters
*
* @return {object} requestParams An object containing the request parameters
*/
getRequestParams() {
return this.renderer.getRequestParams();
};

/**
* Set the requestParams object containing the request parameters
*
* @param {object} params An object containing the request parameters
*/
setRequestParams(params) {
this.renderer.setRequestParams(params);
};

/**
* Reset decorator list.
*/
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
},
"homepage": "https://github.com/bigcommerce/paper",
"dependencies": {
"@bigcommerce/stencil-paper-handlebars": "5.9.5",
"@bigcommerce/stencil-paper-handlebars": "^5.10.0",
"accept-language-parser": "~1.4.1",
"messageformat": "~0.2.2"
},
Expand Down
13 changes: 13 additions & 0 deletions spec/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,16 @@ describe('loadTranslations', () => {
});
});
});

describe('Request Params', function() {
it('should set and retrieve request params ', done => {
const params = { security: { nonce: '1234' } };
const paper = new Paper(null, null, null, 'v4', console, 'error', params);
expect(paper.getRequestParams()).to.equal(params);

const newParams = { security: { nonce: '5678' } };
paper.setRequestParams(newParams);
expect(paper.getRequestParams()).to.equal(newParams);
done();
});
});

0 comments on commit 2f6aaa3

Please sign in to comment.