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

feathers-hooks-jsonapify

v0.1.11

Published

Feathers hook for outputting data in a JSON-API-compliant way.

Downloads

4

Readme

feathers-hooks-jsonapify

Feathers hook for outputting data in a JSON-API-compliant way.

Currently works great with Sequelize as an adapter. There are plans to support more adapters in the future.

npm version dependency status

Installing

Simply run npm install --save feathers-hooks-jsonapify and you're good to go!

Usage

This hook is intended to use with feathers-rest, since it'll convert that provider's response to a JSON-API compliant document.

Require the hook:

const jsonapify = require('feathers-hooks-jsonapify');

Then choose how to implement it.

Tied up to a service

app.service('messages').hooks({
  after: {
    find: [ jsonapify() ],
    get: [ jsonapify() ]
  }
});

As a global hook

app.hooks({
  after: {
    find: [ jsonapify() ],
    get: [ jsonapify() ]
  }
});

Relationships

Available since: v0.1.4

feathers-hooks-jsonapify will automatically detect metadata for relationships in the model. It'll create an included top-level array in the document when the hook is called via find.

Currently working and tested with belongsTo and hasMany associations. This feature works only with a Sequelize adapter.

Example document for a self-referencing model

{
  "data": {
    "type": "topics",
    "id": "sports-cars",
    "attributes": {
      "name": "Cars",
      "created-at": "2017-04-14T22:22:03.000Z",
      "updated-at": null
    },
    "relationships": {
      "parent-topic": {
        "data": {
          "type": "topics",
          "id": "sports"
        }
      }
    },
    "links": {
      "self": "/topics/sports-cars",
      "parent": "/topics"
    }
  },
  "included": [
    {
      "type": "topics",
      "id": "sports",
      "attributes": {
        "name": "Sports",
        "parent-topic-id": null,
        "created-at": "2017-04-14T22:22:03.000Z",
        "updated-at": null
      },
      "links": {
        "self": "/topics/sports"
      }
    }
  ]
}

Pagination

Available since: v0.1.4

The hook will also detect if hook.result.skip, hook.result.limit and hook.result.total are available as part of the feathers-rest provider. If available, it'll create first, prev, next and last links accordingly.

The raw pagination data is moved to a meta object.

Example document with pagination links

{
  "data": [
    {
      "type": "topics",
      "id": "cinema",
      "attributes": {
        "name": "Cinema",
        "show-role-title": null,
        "created-at": "2017-04-14T22:22:03.000Z",
        "updated-at": null
      },
      "links": {
        "self": "/topics/cinema"
      }
    },
    {
      "type": "topics",
      "id": "comedy",
      "attributes": {
        "name": "Comedy",
        "show-role-title": null,
        "created-at": "2017-04-14T22:22:03.000Z",
        "updated-at": null
      },
      "links": {
        "self": "/topics/comedy"
      }
    }
  ],
  "links": {
    "next": "/topics?$skip=2",
    "last": "/topics?$skip=14"
  },
  "meta": {
    "total": 15,
    "limit": 2,
    "skip": 0
  }
}

Plain Object Serialization (POS) :new:

Available since: v0.1.8

Common Object arrays can also be jsonapified for any custom service's result:

Multiple objects

// Sample hook result, with multiple objects, from a `person` custom service.
hook.result = [{
  firstName: 'Joel',
  lastName: 'Villarreal',
  isEnabled: true
}, {
  firstName: 'Alejandro',
  lastName: 'Bertoldi',
  isEnabled: false
}];

JSONAPIfied result:

{
  "data": [
    {
      "id": "2f1faeefc0edc081b012113e08cd9960773a70eb4d16626fade328adb9be4477",
      "type": "person",
      "attributes": {
        "first-name": "Joel",
        "last-name": "Villarreal",
        "isEnabled": true
      },
      "links": {
        "self": "/person/2f1faeefc0edc081b012113e08cd9960773a70eb4d16626fade328adb9be4477"
      }
    },
    {
      "id": "5ad0e862ce3db03640bb696d1ca77a0905ef4400070549622e577c4001f3e96d",
      "type": "person",
      "attributes": {
        "first-name": "Alejandro",
        "last-name": "Bertoldi",
        "isEnabled": false
      },
      "links": {
        "self": "/person/5ad0e862ce3db03640bb696d1ca77a0905ef4400070549622e577c4001f3e96d"
      }
    }
  ]
}

Single object

// Sample hook result, with a single object in an array, from a `person` custom service.
hook.result = [{
  firstName: 'Joel',
  lastName: 'Villarreal',
  isEnabled: true
}];

// same as:
hook.result = {
  firstName: 'Joel',
  lastName: 'Villarreal',
  isEnabled: true
};

JSONAPIfied result:

{
  "data": {
    "id": "2f1faeefc0edc081b012113e08cd9960773a70eb4d16626fade328adb9be4477",
    "type": "person",
    "attributes": {
      "first-name": "Joel",
      "last-name": "Villarreal",
      "isEnabled": true
    },
    "links": {
      "self": "/person/2f1faeefc0edc081b012113e08cd9960773a70eb4d16626fade328adb9be4477"
    }
  }
}

Identifier and type mapping

The jsonapify hook receives an options object that accepts two settings for POS:

  • identifierKey: the name of the property to convert into id
  • typeKey: the name of the property to convert into type

What happens if I don't use identifierKey?

The hook's got your back. Using crypto.createHash, it creates a unique SHA-256 digest using the contents of the object.

What happens if I don't use typeKey?

The hook will use the service's name (hook.service.options.name) as each model's type.

TODOs

Check out the issues.

Feel like contributing?

Knock yourself out! Fork the repo and make a PR.

Licence

MIT