@gasket/data
TypeScript icon, indicating that this package has built-in type declarations

6.46.8 • Public • Published

@gasket/data

Helper package for accessing embedded Gasket Data in the browser.

Installation

npm i @gasket/data

Usage

This helper is intended for use in conjunction with Gasket Data embedded in a script tag in the HTML document.

For example, if the following data is rendered...

<script id="GasketData" type="application/json">{ "something": "interesting" }</script>

...then it would be accessible as:

import gasketData from '@gasket/data';

console.log(gasketData.something); // interesting

Note that @gasket/data is only expected to be used in the browser, and not in server-side code.

Adding Data

To add to the data exposed in @gasket/data, you can write to the HTTP response object's locals.gasketData property. For example, when using the middleware lifecycle in a plugin:

module.exports = {
  hooks: {
    middleware(gasket, app) {
      return (req, res, next) => {
        res.locals.gasketData = res.locals.gasketData || {};
        res.locals.gasketData.example = { fake: 'data' }; 
        next();
      }
    }
  }
};

The results of res.locals.gasketData should then be rendering in a script as described above. Similarly, this can be done in an application lifecycle script:

// /lifecycles/middleware.js

module.exports = (gasket, app) => [
  (req, res, next) => {
    res.locals.gasketData ??= {};
    res.locals.gasketData.example = gasket.config.somethingWeWantToExpose;
  }
];

License

MIT

Readme

Keywords

Package Sidebar

Install

npm i @gasket/data

Weekly Downloads

370

Version

6.46.8

License

MIT

Unpacked Size

4.73 kB

Total Files

5

Last publish

Collaborators

  • bbetts
  • rxmarbles
  • ecarlson-godaddy
  • jpina1-godaddy
  • mmason2
  • kinetifex
  • 3rdeden
  • kawikabader