1.0.2 • Published 4 years ago

api-formatter v1.0.2

Weekly downloads
2
License
MIT
Repository
github
Last release
4 years ago

Api Middleware

CircleCI

A utility to send formatted json responses as an express response

Usage

const express = require('express')
const Api = require('api-formatter').Api

let app = express()

// Register the middleware
app.use(Api.middleware({ name: 'My Fancy Api' }))

// Sending formatted data
app.get('/', (req, res) => {
  res.api.sendData({ something: 'cool!' })
})

// Sending a formatted failure
app.get('/wip', (req, res) => {
  res.api.sendFail('Not implemented yet', 418)
})

// Automatically catching errors
app.get('/bad', (req, res) => {
  req.api.catch(api => {
    throw new Error('Something went wrong :S')
  })
})

Success Response, HTTP 200

{
  "meta": {
    "success": true,
    "messages": [],
    "name": "My Fancy Api",
    "version": "0.1.2"
  },
  "data": {
    "something": "cool!"
  }
}

Failure Response, HTTP 400

{
  "meta": {
    "success": false,
    "messages": ["Something went wrong :S"],
    "name": "My Fancy Api",
    "version": "0.1.2"
  },
  "data": null
}

Middleware options

NameUse
nameThe name of your api, defaults to your package.json name
versionThe version of your api, defaults to your package.json version
httpErrorWhether a failed response should return a HTTP/400, defaults to true

A Full example

import express from 'express'
import { Api } from 'api-formatter'

let app = express()

app.use(Api.middleware({ name: 'dogs-api', version: 'v1' }))

app.get('/', (req, res) => {
  res.api.sendData({ msg: 'Hey!' })
})

app.get('/error', (req, res) => {
  res.api.sendFail(['Oops, something went wrong :S'])
})

app.listen(3000, () => console.log('Listening on :3000'))

The success response will be:

GET http://localhost:3000 → http/200

{
  "meta": {
    "success": true,
    "messages": [],
    "status": 200,
    "name": "dogs-api",
    "version": "v1"
  },
  "data": { "msg": "Hey!" }
}

And the fail response will be:

GET http://localhost:3000/error → http/400

{
  "meta": {
    "success": false,
    "messages": ["Oops, something went wrong :S"],
    "status": 400,
    "name": "dogs-api",
    "version": "v1"
  },
  "data": null
}
1.0.2

4 years ago

1.0.1

5 years ago

1.0.0

6 years ago

0.3.1

6 years ago

0.3.0

6 years ago

0.2.0

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago