1.0.4 • Published 4 years ago

koa-response2 v1.0.4

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

koa-response2 minified + gzip npm package

Inspired by koa-respond. Checkout what's the difference.

Install

$ yarn add koa-response2

Usage

import Koa, { ParameterizedContext } from 'koa'
import koaReponse from 'koa-response2'

const app = new Koa()

app.use(koaReponse())

export default function (ctx: ParameterizedContext) {
  ctx.oK({ id: 123, name: 'Dat Boi' })  // HTTP Code: 200, Response: { id: 123, name: 'Dat Boi' }
  ctx.notFound('Not found, boii')       // HTTP Code: 404, Response: 'Not found, boii'
  ctx.internalServerError('error')      // HTTP Code: 500, Response: 'error'
  ctx.send(200, { name: 'tom' }, 'success')
}

Options

export interface KoaResponse {
  statusMap?: Record<string, number>
  format?: (status: number, payload?: any, message?: string) => any
}

statusMap

  • Type: Object
  • Required: false

Override built-in HTTP status map, e.g.:

import Koa, { ParameterizedContext } from 'koa'
import koaReponse from 'koa-response2'

const app = new Koa()

app.use(koaReponse({
  statusMap: {
    whatever: 999
  }
}))

ctx.whatever(data, message)

For better typing experience, you should override declaration file like this.

format

  • Type: Function
  • Required: false

Modify HTTP response globally, e.g.:

import Koa, { ParameterizedContext } from 'koa'
import koaReponse from 'koa-response2'

const app = new Koa()

// This will wrap all reponse with { code: number, data: T: message?: string } construct.
app.use(koaReponse({
  format (status, payload, message = '') {
    return {
      code: status,
      data: payload,
      message
    }
  }
}))

Difference

koa-response2 is inspired by koa-respond, so what's the difference between the two?

  • Better typescript typing experience
  • Ability to modify response globally
  • Comprehensive built-in HTTP status API

Availble Context API

koa-response2 add bulk APIs on koa.Context, you can see the whole list below.

namecode
oK200
created201
accepted202
nonAuthoritativeInformation203
partialContent206
multipleChoices300
badRequest400
unauthorized401
paymentRequired402
forbidden403
notFound404
methodNotAllowed405
notAcceptable406
requestTimeout408
conflict409
gone410
lengthRequired411
preconditionFailed412
payloadTooLarge413
requestURITooLong414
unsupportedMediaType415
requestedRangeNotSatisfiable416
expectationFailed417
imATeapot418
misdirectedRequest421
unprocessableEntity422
locked423
failedDependency424
upgradeRequired426
preconditionRequired428
tooManyRequests429
requestHeaderFieldsTooLarge431
unavailableForLegalReasons451
internalServerError500
notImplemented501
badGateway502
serviceUnavailable503
gatewayTimeout504
hTTPVersionNotSupported505
variantAlsoNegotiates506
insufficientStorage507
loopDetected508
notExtended510
networkAuthenticationRequired511
networkConnectTimeoutError599

License

MIT

Copyright (c) 2019-present, Army-U

1.0.4

4 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago