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

jsend-express

v1.2.4

Published

jsend-express is implement to expressJs the standard format JSend. it's Simple API JSON Response Format. A JSend-compliant JSON Response Format for HTTP RESTful API. (JSend standard & JSend-extend SAJ)

Downloads

19

Readme

jsend-express

install in your project

npm i jsend-express --save

About

JSend-express is a complete implement to standard JSend in expressJs and it's extension. It's Simple API JSON Response Format, a JSend-compliant JSON Response Format for HTTP RESTful API (JSend standard & JSend-extend SAJ). JSend-express are no dependencies

Usage

To use the middleware you simply import the default JSend function. Then use it as you would any other expressJs middleware. This middleware adds a jsend object to the Express.js Response object. It has 4 functions: success, partial, fail and error.

Why use JSend Api ?

...

JSONAPI vs RESTAPI vs JSEND

JSONAPI link: https://jsonapi.org/

RESTAPI link: https://github.com/cryptlex/rest-api-response-format

JSEND link: https://github.com/omniti-labs/jsend

JSEND extend link: https://technick.net/guides/software/software_json_api_format/

Initialization JSend

JSend must be require to same level as your expressJs application.

Just like express, it must be initialized by doing anew JSend().

This object must be used with an object json content 3 properties new JSend({name, version, release}) :

  • name: your application name
  • version: your application version
  • release: you number of release or your last commit

Once JSend is initialized, its middleware is used to extend the response of express object with JSend Api methods:

  • success
  • partial
  • fail
  • error

How use JSend-express

sample project example: https://github.com/caloudinou/sample-example-jsend-express

Example server.js

/**
* require express is required
*/
const express = require('express')

/**
* init your app
*/
const app = express()



/**
* require JSend-express is required
*/
const { JSend } = require('jsend-express')

/**
 * init JSend
 * @type {JSend}
 */
const jSend = new JSend({ name: 'appName', version: 'X.X.X', release: 'XX' })

/**
 * use middleware JSend to extend express after init app and before your route
 * @description
 * add 
 * {{ success: function, fail: function, partial: function, error: function}} 
 * in response of express   
 */
app.use(jSend.middleware.bind(jSend))



// your route
// ...

use method JSend response in controller express

example success method
app.get('/', (req, res, next) => {
    const data = { example: 'example response success' }
    res.success({ data })
    // response HTTP / status 200
    // body:
    // {
    //      program: 'appName'
    //      version: '1.0.0'
    //      release: '42'
    //      datetime: '2019-03-10T16:50:38.546Z'
    //      timestamp: 1552236638546
    //      status: 'success'
    //      code: 200
    //      message: 'OK'
    //      data: { 
    //         example: 'example response success' 
    //      }
    // }
})

description of head JSend

...

description of body JSend

...

description of methods JSend

success:

/**
 *   @description` success response HTTP to type 200 - 300
 *   @type function
 *   @param {{ 
 *        status: number | undefined, 
 *        data: Object | null,
 *        message: string | undefined
 *    } | undefined}
 *   @return {{
 *       program: string,
 *       version: string,
 *       release: string,
 *       datetime: string,
 *       timestamp: number,
 *       status: string,
 *       code: number,
 *       message: string,
 *       data: Object
 *   }}
 */

by default:

  • status = 200
  • message = OK
  • data = null
example
app.get('/', (req, res, next) => {
    const status = 201
    const message = 'example'
    const data = { example: 'example response success' }
    res.success({ status, data, message })
    // response HTTP / status 201
    // {
    //      program: 'appName',
    //      version: '1.0.0',
    //      release: '42',
    //      datetime: '2019-03-10T16:50:38.546Z',
    //      timestamp: 1552236638546,
    //      status: 'success',
    //      code: 201,
    //      message: 'example',
    //      data: { 
    //         example: 'example response success' 
    //      }
    // }
})

partial:

/**
 *   @description` success response HTTP with pagination to type 200 - 300
 *   @type function
 *   @param {{ 
 *       status: number | undefined, 
 *       payload: { 
 *           paginationCount: number, 
 *           paginationPage: number, 
 *           paginationLimit: number 
 *       },
 *       message: string | undefined
 *       data: Object | null
 *    } | undefined}
 */

fail:

/**
 *   @description` error HTTP to type 400
 *   @type function
 *   @param {{ 
 *        status: number | undefined, 
 *        error: { 
 *           message: string | undefined, 
 *           errors: string[] | {key: string, value: string}[] | undefined
 *        } 
 *    } | undefined}
 */

by default:

  • status = 400
  • message = invalid request

error:

/**
 *   @description` error HTTP to type 500
 *   @type function
 *   @param {{ 
 *        status: number | undefined, 
 *        error: { 
 *           message: string | undefined
 *        } 
 *    } | undefined}
 */

by default:

  • status = 500
  • message = error server
code coverage :

File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s | ------------------------------|----------|----------|----------|----------|-------------------| All files | 100 | 100 | 100 | 100 | | index.js | 100 | 100 | 100 | 100 | | lib/ | 100 | 100 | 100 | 100 | | j-send.js | 100 | 100 | 100 | 100 | | middleware-error-handler.js | 100 | 100 | 100 | 100 | |