Socket
Book a DemoInstallSign in
Socket

cargo-io

Package Overview
Dependencies
Maintainers
1
Versions
29
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cargo-io

Structured messaging interface for APIs

0.1.8
latest
Source
npmnpm
Version published
Weekly downloads
2
-93.33%
Maintainers
1
Weekly downloads
 
Created
Source

Cargo-IO

Simple structured response object for koa-js.

Koa & Cargo

getting started

  • Create a node project
  • $ npm i koa koa-router cargo-io
  • $ touch index.js

index.js

const app = new (require('koa'))
const router = require('koa-router')()
const { kcargo, kcatcher } = require('./cargo')
const port = process.env.PORT || 3000

const handler = async (error, ctx, next) => {
    // mutate your errors here by using the ctx.cargo object
}

app.use(kcargo())
app.use(kcatcher(handler))
app.use(router.routes())

app.listen(port)

Sending Data

Note if no status is set, then it will default to 200, and the state will defualt to "success"

router.get('/', async (ctx) => {
    const someObject = {}
    ctx.body = ctx.cargo.status(201).message('object created').payload(someObject)
})
output
{
    "isCargo": true,
    "status": 201,
    "serial": 434473,
    "message": "object created",
    "payload": {},
    "state": "success"
}

Throwning an Error

Note: if you don't specify the status of the error, it will default to 500.

router.get('/', async (ctx) => {
    /* THROWING ERROR */
    ctx.cargo.status(401).error('invalid token') // Note: no code will run after this (as it throws an error wich invokes the kcatcher middleware.)
    ctx.body = ctx.body = ctx.cargo.status(201).message('object created').payload({})
})
output
{
    "isCargo": true,
    "status": 401,
    "serial": 461151,
    "message": "invalid token",
    "state": "danger"
}

Unhandled Error

if you dont handle an error, it will get masked as an unknow error in your response with a serial number, which you can use to track it in your logs.

router.get('/', async (ctx) => {
    /* UNKNOWN ERROR */
    throw(new Error()) // Note: no code will run after this (as it throws an error wich invokes the kcatcher middleware.)
    ctx.body = ctx.body = ctx.cargo.status(201).message('object created').payload({})
})
output
{
    "isCargo": true,
    "status": 500,
    "serial": 520259,
    "message": "unknown error: E520259",
    "state": "danger"
}

Validation Error Response

router.get('/', async (ctx) => {
    /* VALIDATION */
    const validationErrors = [
        {key:'username', message:'invalid username'},
        {key:'password', message:'invalid password'}
    ]
    ctx.cargo.status(422)
    validationErrors.map(o => ctx.cargo.messages(o))
    ctx.cargo.error() // Note: no code will run after this (as it throws an error wich invokes the kcatcher middleware.)
    
    ctx.body = ctx.body = ctx.cargo.status(201).message('object created').payload({})
})
output
{
    "isCargo": true,
    "status": 422,
    "serial": 871000,
    "messages": [
        {
            "key": "username",
            "message": "invalid username"
        },
        {
            "key": "password",
            "message": "invalid password"
        }
    ],
    "state": "validation"
}

Default States

if(this._status <= 230) this._state = 'success'
if(this._status >= 231 && this._status < 400) this._state = 'warning'
if(this._status > 400) this._state = 'danger'
if(this._status == 422) this._state = 'validation'

Keywords

structured-response

FAQs

Package last updated on 27 Jul 2021

Did you know?

Socket

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

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.