npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@smarterservices/hapi-json-view

v1.0.0

Published

JSON view engine for hapi.

Downloads

4

Readme

Hapi JSON View

npm Build Status Dependency Status

A view engine for the hapi framework.

Installation

npm install --save @ideapod/hapi-json-view

Usage

server.js:

const Hapi = require('hapi');
const HapiJsonView = require('@ideapod/hapi-json-view');
const Path = require('path');
const Vision = require('vision');

const server = new Hapi.Server();
server.connection({ port: 8080 });

server.register(Vision, (err) => {
  if (err) throw err;

  server.views({
    engines: {
      js: {
        module: HapiJsonView.create(),
        contentType: 'application/json',
      }
    },
    path: Path.join(__dirname, 'templates'),
  });

  server.route({
    method: 'GET',
    path: '/article',
    handler: function(request, reply) {
      const article = {
        _id: '507f1f77bcf86cd799439011',
        title: 'Node.js',
        author: {
          _id: '507f191e810c19729de860ea',
          name: 'John Doe',
        },
      };

      reply.view('article', { article: article });
    },
  });
});

templates/article.js:

json.set('title', article.title);
json.set('author', (json) => {
  json.set('name', article.author.name);
});

This template generates the following object:

{
  title: 'Node.js',
  author: {
    name: 'John Doe'
  }
}

Functions

set()

It assigns a value to a key.

json.set('title', 'Node.js');

// => { title: 'Node.js' }

The value can be a function. If json.set() is called with a key, it creates an object:

json.set('author', (json) => {
  json.set('name', 'John Doe');
});

// => { author: { name: 'John Doe' } }

If json.set() is called without a key, it assign the value to the parent key:

json.set('title', (json) => {
  json.set('Node.js');
});

// => { title: 'Node.js' }

array()

It creates a new array by iterating through an existing array:

const numbers = ['one', 'two'];

json.set('numbers', json.array(numbers, (json, number) => {
  json.set('number', number);
}));

// => { numbers: [{ number: 'one' }, { number: 'two' }] }

extract()

It extracts values from an object and assigns them to the result object:

const numbers = { one: 'one', two: 'two', three: 'three' };

json.extract(numbers, ['two', 'three']);

// => { two: 'two', three: 'three' }

helper()

Helpers can be registered through the engine configuration:

const Hapi = require('hapi');
const HapiJsonView = require('@ideapod/hapi-json-view');
const Path = require('path');
const Vision = require('vision');

const server = new Hapi.Server();
server.connection({ port: 8080 });

server.register(Vision, (err) => {
  if (err) throw err;

  server.views({
    engines: {
      js: {
        module: HapiJsonView.create(),
        contentType: 'application/json',
      }
    },
    path: Path.join(__dirname, 'templates'),
    helpersPath: Path.join(__dirname, 'templates/helpers'),
  });
});

They can then be used by their name:

json.set('title', json.helper('uppercase', article.title));

partial()

Partials can be registered through the engine configuration:

const Hapi = require('hapi');
const HapiJsonView = require('@ideapod/hapi-json-view');
const Path = require('path');
const Vision = require('vision');

const server = new Hapi.Server();
server.connection({ port: 8080 });

server.register(Vision, (err) => {
  if (err) throw err;

  server.views({
    engines: {
      js: {
        module: HapiJsonView.create(),
        contentType: 'application/json',
      }
    },
    path: Path.join(__dirname, 'templates'),
    partialsPath: Path.join(__dirname, 'templates/partials'),
  });
});

They can then be used by their name:

json.set('author', json.partial('author', { author: article.author }));