Node.js SDK

Reference documentation for Jahuty's Node.js SDK.

Installation

This library requires Node 14.

Install the package with:

npm install jahuty --save
# or
yarn add jahuty

Usage

The package needs to be configured with your API key before use, ideally once during startup. Then, you can use the Snippet.render() method, which returns a Promise, to render a snippet:

var { Jahuty, Snippet }  = require('@jahuty/jahuty');

Jahuty.setKey('YOUR_API_KEY');

Snippet.render(YOUR_SNIPPET_ID).then(render => console.log(render.content));

You can pass parameters into your snippet using the options hash and the params key:

Snippet.render(YOUR_SNIPPET_ID, { params: { foo: "bar" }}).then(
  render => console.log(render.content)
);

The parameters above would be equivalent to assigning the variable below in your snippet:

{% assign foo = "bar" %}

Errors

If you don’t set your API key before calling Snippet.render(), an Error will be thrown. If Jahuty’s API returns any status code other than 2xx, a NotOk exception will be thrown:

var { Jahuty, Snippet }  = require('@jahuty/jahuty');

Snippet.render(YOUR_SNIPPET_ID, { params: { foo: "bar" }})
  .then(render => console.log(render.content))
  .catch(error => {
    if (error instanceof NotOk) {
      // The API returned something besides 2xx status code. The error contains
      // a problem property with more information.
      console.error(error.problem);
    } else if (error instanceof Error) {
      // Something else went wrong. Perhaps the key is not set?
      console.error(error.message);
    }
  });

Example

If you’d like to see the SDK in action, see jahuty/jahuty-node-example.

Contributing

Bug reports and pull requests are welcome on GitHub.

Find a typo? Something is wrong in this documentation? Fork and edit it!