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

nss-json-server

v1.1.0

Published

JSON Server with other useful mixins

Downloads

20

Readme

🔐 NSS JSON Server

This package will pull in useful mixins created by us and other developers. Documentation and options are simplified for instruction of beginners.

Mixins:

Getting started

Package Install

# NPM
npm i -g nss-json-server

# Yarn
yarn global add nss-json-server

Create a db.json file with a users collection :

{
  "users": []
}

Alias Setup

Open your bash or zsh initialization file and add the following alias.

alias js="nss-json-server -X 7h -p 5050 -w"

Running your API

Run with following command:

js db.json

It exposes and works the same for all JSON Server flags.

Authentication flow 🔑

JSON Server Auth adds a simple JWT based authentication flow.

Register 👥

  • POST /register

email and password are required in the request body :

POST /register
{
  "email": "[email protected]",
  "password": "bestPassw0rd"
}

The response contains the JWT access token, user id and username (if exists):

201 Created
{
  "accessToken": "xxx.xxx.xxx",
  "user": {
    "id": 1,
    "username": "xxxxxxxx"
  }
}

Any other property can be added to the request body without being validated:

POST /register
{
  "email": "[email protected]",
  "password": "bestPassw0rd",
  "username": "admina",
  "firstname": "Admina",
  "lastname": "Straytor",
  "age": 32
}

Login 🛂

  • POST /login

email and password are required:

POST /login
{
  "email": "[email protected]",
  "password": "bestPassw0rd"
}

The response contains the JWT access token:

200 OK
{
  "accessToken": "xxx.xxx.xxx",
  "user": {
    "id": 1,
    "username": "xxxxxxxx"
  }
}

Creating Owned Resources

If any resource has been guarded with an ownership level route:

| Permission | Description | | -- | -- | | 600 | User must own the resource to write or read the resource. | | 640 | User must own the resource to write the resource. User must be logged to read the resource. | | 644 | User must own the resource to write the resource. Everyone can read the resource. |

Then when you make a request with the POST or PUT method, and there is an authorization header, then the userId will be automatically added to the request body.

Example

fetch("http://localhost:5050/posts", {
    method: "POST",
    headers: {
        "Content-Type": "application/json",
        Accept: "application/json",
        Authorization: "Bearer xxxxxx.xx.xxxxx"
    },
    body: JSON.stringify({
        url: "https://media.giphy.com/media/eHWWKfSp0VZ1V87Ixj/giphy.gif",
        image: null,
        timestamp: Date.now()
    })
})

Example response:

{
  "url": "https://media.giphy.com/media/eHWWKfSp0VZ1V87Ixj/giphy.gif",
  "image": null,
  "timestamp": 1575211182251,
  "userId": 4,
  "id": 8
}

For Local Development

  1. Clone repo
  2. npm i
  3. Create a routes.json and db.json
  4. Add the following to your routes.json

Sample routes.json

{
    "/users*": "/640/users$1",
    "/posts*": "/640/posts$1"
}

Starting the Dev Server

  1. npm run build
  2. node dist/bin.js -w db.json -p 5050 -X 7h -r routes.json

Basic Requests

Using Postman, or your favorite HTTP request client, create the following requests.

  • http://localhost:5050/register
    // Body (raw JSON)
    {
        "email": "[email protected]",
        "password": "Admin8*",
        "name": "Admina Straytor",
        "username": "admin",
        "location": "Nashville, TN",
        "avatar": ""
    }
    • Method - POST
    • Content-Type header - application/json
  • http://localhost:5050/posts
    // Body (raw JSON)
    {
        "url": "https://media.giphy.com/media/eHWWKfSp0VZ1V87Ixj/giphy.gif",
        "image": null,
        "timestamp": 1575211182251
    }
    • Method - POST
    • Authorization header - Use token from registration response
    • Content-Type header - application/json
    • Accept header - application/json