0.1.3 • Published 3 years ago

entix v0.1.3

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

entix

Getting Started: index.js

global['entix'] = require('entixio')()
const bodyparser = require('koa-body') // middleware

entix.use(bodyparser())
// load more global middleware here ...


entix.startServer()

Adding ErrorHandlers: index.js

global['entix'] = require('entixio')()
const { ValidationError } = require('entixio').joi
const { UniqueViolationError } = require('entixio').objection
const { JsonWebTokenError } = require('jsonwebtoken')
const bodyparser = require('koa-body')

const validationErrorHandler = async ({ctx, error}) => {
    if(error instanceof ValidationError){
        console.log('jackopt')
        ctx.status = 422
        const { details, _original } = error
        error = { original: _original, details: {}}
        details.map(d => error.details[d.context.key] = d.message)
        return error
    }
}

const databaseErrorHandler = async ({ctx, error}) => {
    if(error instanceof UniqueViolationError){
        ctx.status = 422
        const key = error.columns.pop()
        error = { original: ctx.request.body, details: {}}
        error.details[key] = `this ${key} is already taken`
        return error
    } 
}

const jwtErrorHandler = async ({ctx, error}) => {
    if(error instanceof JsonWebTokenError){
        const managed = ['invalid signature', 'jwt expired', 'jwt malformed', 'jwt must be provided', 'jwt token']
        if(managed.includes(error.message)){
            ctx.status = 401
            error = { message: error.message }
            return error
        }
    } 
}

entix.use(bodyparser())
entix.loadErrorHandler(validationErrorHandler)
entix.loadErrorHandler(databaseErrorHandler)
entix.loadErrorHandler(jwtErrorHandler)

entix.startServer()
0.1.2

3 years ago

0.1.1

3 years ago

0.1.3

3 years ago

0.1.0

3 years ago

0.0.9

3 years ago

0.0.8

3 years ago

0.0.7

3 years ago

0.0.6

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.1

3 years ago

0.0.0

3 years ago

0.6.5

4 years ago

0.6.2

4 years ago

0.6.4

4 years ago

0.6.1

4 years ago

0.6.0

4 years ago

0.5.10

4 years ago

0.5.9

4 years ago