0.1.8 • Published 3 years ago

cargo-io v0.1.8

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

Cargo-IO

Simple structured response object for koa-js.

Koa & Cargo

getting started

  1. Create a node project
  2. $ npm i koa koa-router cargo-io
  3. $ 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'
0.1.8

3 years ago

0.1.7

3 years ago

0.1.6

3 years ago

0.1.0

3 years ago

0.1.2

3 years ago

0.1.4

3 years ago

0.1.3

3 years ago

0.1.5

3 years ago

0.0.20

3 years ago

0.0.19

3 years ago

0.0.18

3 years ago

0.0.16

3 years ago

0.0.17

3 years ago

0.0.15

3 years ago

0.0.14

4 years ago

0.0.13

4 years ago

0.0.11

4 years ago

0.0.12

4 years ago

0.0.10

4 years ago

0.0.9

4 years ago

0.0.8

4 years ago

0.0.7

4 years ago

0.0.6

4 years ago

0.0.5

4 years ago

0.0.4

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago

0.0.0

4 years ago