Socket
Socket
Sign inDemoInstall

halifier

Package Overview
Dependencies
2
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    halifier

helper toolset for building HAL API with express


Version published
Weekly downloads
1
decreased by-50%
Maintainers
1
Install size
2.02 MB
Created
Weekly downloads
 

Readme

Source

halifier

A helper toolset for building HAL API with express

Features:

  • converts JS objects and arrays of objects to HAL responses
  • adds extra conventional field _listMeta when returning arrays of objects
  • provides simple implementation for a controller providing HAL interface to a collection

This is a basis for further concrete implementations such as:

  • halifier-sequelize
  • halifier-mongoose

Usage examples

pure AbstractItemHalifier

const {AbstractItemHalifier} = require('halifier')
const data = [
  {pass: 'ZK872343', name: 'John', eyeColor: 'blue', weight: 70},
  {pass: 'ANK34323', name: 'Marry', eyeColor: 'brown', weight: 65},
  {pass: 'LP109929', name: 'T1000', eyeColor: 'red', weight: 128}
]

const app = require('express')()

const peopleHalifier = new AbstractItemHalifier(app, {
  idFieldName: 'pass',
  baseUrl: '/humans',
  name: 'human',
  listMeta: {
    limit: 2 // number of returned objects in one response
  }
})

app.get('/humans/:pass', (req, res) => {
  const found = data.filter(human => human.pass === req.params.pass)
  if (found.length) {
    peopleHalifier.halifyItem(found[0])
      .then(result => res.json(result))
  } else {
    res.sendStatus(404)
  }
})

app.get('/humans', (req, res) => {
  // make a prototype of the response
  const responseProto = peopleHalifier.makeProtoFromReq(req)

  // provide actual list metadata
  responseProto._listMeta.stats = {
    total: data.length,
    // we want to return only first 2 records,
    // the other ones should be accessible via pagination
    returned: 2
  }
  peopleHalifier.halifyList(data.slice(2), responseProto)
    .then(result => res.json(result))
})

app.listen(3000)

FAQs

Last updated on 21 Apr 2017

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc