@pyx-industries/vc-render-template-utils
v1.1.0
Published
A lightweight utility library for constructing, extracting and rendering verifiable credential render templates.
Readme
vc-render-template-utils
A lightweight utility library for constructing, extracting and rendering verifiable credential render templates.
Installation
Install the package:
# Yarn
yarn add @pyx-industries/vc-render-template-utils
# NPM
npm install @pyx-industries/vc-render-template-utilsUsage
Basic Example
import {
constructRenderMethod,
extractRenderTemplate,
populateTemplate,
RenderMethodType,
TemplatingEngineType,
} from '@pyx-industries/vc-render-template-utils';
// Construct a render method
const template = '<div>Hello, {{name}}!</div>';
const renderMethod = constructRenderMethod(
template,
RenderMethodType.RenderTemplate2024,
);
// Extract the template
const extractedTemplate = await extractRenderTemplate(renderMethod);
// Populate the template with data
const data = { name: 'World' };
const result = populateTemplate(
TemplatingEngineType.Handlebars,
extractedTemplate,
data,
);
console.log(result); // Output: <div>Hello, World!</div>Advanced Example with URL Fetching
import {
constructRenderMethod,
extractRenderTemplate,
populateTemplate,
RenderMethodType,
TemplatingEngineType,
} from '@pyx-industries/vc-render-template-utils';
// Construct a render method with a URL
const renderMethod = constructRenderMethod(
'',
RenderMethodType.RenderTemplate2024,
{ url: 'http://example.com/template.html' },
);
// Extract the template from the URL
const extractedTemplate = await extractRenderTemplate(renderMethod);
// Populate the template
const data = { title: 'My Page' };
const result = populateTemplate(
TemplatingEngineType.Handlebars,
extractedTemplate,
data,
);
console.log(result); // Output: Rendered template contentAPI
constructRenderMethod
constructRenderMethod(template: string, renderMethodType: RenderMethodType, extra?: Record<string, unknown>)Constructs a render method object for the specified template and type.
template: The template string or empty if using a URL.renderMethodType: EitherRenderTemplate2024orWebRenderingTemplate2022.extra: Optional metadata (e.g.,urlormediaQuery).
extractRenderTemplate
extractRenderTemplate(renderMethod: RenderMethod)Extracts the template content, fetching from a URL if necessary.
renderMethod: The render method object created byconstructRenderMethod.
populateTemplate
populateTemplate(templatingEngineType: TemplatingEngineType, template: string, data: Record<string, unknown>)Populates the template with data using the specified templating engine.
templatingEngineType: Currently supportsHandlebars.template: The template string to populate.data: The data object to populate the template.
Supported Render Methods
RenderTemplate2024: Supports remote and embedded templates.WebRenderingTemplate2022: Designed for embedded templates.
Supported Templating Engines
Handlebars: A robust templating engine for dynamic content.
Development
Prerequisites
- Node.js (v22, as specified in
.nvmrc) - yarn
Setup
Clone the repository:
git clone https://github.com/pyx-industries/vc-render-template-utils.git cd vc-render-template-utilsInstall dependencies:
yarn installBuild the project:
yarn buildRun tests:
yarn test
Scripts
yarn build: Compiles TypeScript to JavaScript.yarn test: Runs Jest tests.yarn test:ci: Runs tests with coverage.yarn format: Checks code formatting with Prettier.yarn format:fix: Auto-fixes formatting issues.yarn lint: Runs ESLint.yarn lint:fix: Auto-fixes linting issues.
Contributing
Contributions are welcome! Please follow these steps:
- Fork the repository.
- Create a feature branch (
git checkout -b feature/your-feature). - Commit your changes (
git commit -m 'Add your feature'). - Push to the branch (
git push origin feature/your-feature). - Open a pull request.
Ensure your code adheres to the project's linting and formatting standards by running yarn lint and yarn format.
License
This project is licensed under the Apache License 2.0. See the LICENSE file for details.
Issues
Report bugs or suggest features by opening an issue on the GitHub Issues page.
