Socket
Book a DemoInstallSign in
Socket

koa-response2

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

koa-response2

1.0.4
latest
Source
npmnpm
Version published
Weekly downloads
118
-7.81%
Maintainers
1
Weekly downloads
 
Created
Source

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

FAQs

Package last updated on 27 Mar 2020

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.