From 2f6aaa3ef8d8ef19b82b02e399df958c5fd87560 Mon Sep 17 00:00:00 2001 From: Jairo Panduro Date: Wed, 13 Mar 2024 18:54:26 +0100 Subject: [PATCH] feat: STRF-11819 Pass down to hbs request params --- index.js | 26 +++++++++++++++++++++++--- package.json | 2 +- spec/index.js | 13 +++++++++++++ 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 8629b10..330af21 100644 --- a/index.js +++ b/index.js @@ -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; } @@ -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. */ diff --git a/package.json b/package.json index be19205..8d6f408 100644 --- a/package.json +++ b/package.json @@ -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" }, diff --git a/spec/index.js b/spec/index.js index afe35eb..cb54208 100644 --- a/spec/index.js +++ b/spec/index.js @@ -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(); + }); +}); \ No newline at end of file