Registry / devops / koa-formatter

koa-formatter

JSON →
library0.1.0jsnpmunverified

A response body formatter middleware for Koa (v0.1.0, stable) that automatically wraps JSON API responses into a consistent envelope with ok, status, errors, and result fields. It supports custom formatter functions, error aggregation, and includes Flowtype definitions. Unmaintained since 2016, no recent releases; suitable for legacy Koa v1/before async/await middleware patterns. Alternatives: koa-respond, koa-json, or manual ctx.body assignment.

npm install koa-formatter
INSTALL
IMPORT
SIG · KOA-FORMATTER
K
koa-formatter
devopsjavascriptv0.1.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default (formatter)
const formatter = require('koa-formatter')
import formatter from 'koa-formatter'
The package uses CommonJS and does not provide ESM exports. Use require().
formatter as middleware
app.use(formatter())
app.use(formatter)
formatter() returns a middleware function. Calling without parentheses will pass the constructor/function, not the middleware.
defaultFormatter
const { defaultFormatter } = require('koa-formatter')
formatter.defaultFormatter()
defaultFormatter is a property on the exported function, accessible via destructuring or formatter.defaultFormatter.

Sets up a basic Koa app with koa-formatter middleware and uses ctx.result to output a formatted JSON response.

const Koa = require('koa'); const formatter = require('koa-formatter'); const app = new Koa(); app.use(formatter()); app.use(async (ctx) => { ctx.result = { user: 'John' }; }); app.listen(3000); // Response: {"ok":1,"status":200,"result":{"user":"John"}}
Debug
Known issues
gotchaThe middleware relies on deprecated Koa context methods (ctx.throw) which are not available in Koa v2 when using async/await; requires using generator middleware or ensure Koa v1 compatibility.
fix
Use Koa v1 or wrap async middleware with co.wrap, or migrate to modern alternatives like koa-respond.
affects: 0.1.0
breakingThe package is unmaintained and uses outdated patterns; it does not support Koa v2 natively with async/await middleware.
fix
Consider rewriting using ctx.body directly or adopt a maintained library (koa-respond, koa-json).
affects: 0.1.0
deprecatedThe package uses Flowtype definitions which are now largely superseded by TypeScript.
fix
If using TypeScript, create custom type definitions or migrate to TypeScript-native libraries.
affects: 0.1.0
Errors
Common errors & fixes
TypeError: app.use is not a function
Calling formatter() on a plain object or before creating the Koa app instance.
fix
Ensure app is an instance of Koa: const app = new Koa(); then app.use(formatter());
Cannot read property 'result' of undefined
Calling ctx.result inside a middleware that runs before the formatter middleware, or running formatter() after other middleware that reads ctx.body without setting ctx.result.
fix
Place formatter() early in the middleware chain (before routes) and ensure response logic sets ctx.result instead of ctx.body.
Upgrade
Version history
0.1.0latest on npm
Audit
Dependencies
koarequiredpeer dependency required for middleware functionality
Agent activity
7 hits · last 30 days
node
4
Resources
koa-formatter — npm install koa-formatter · libregistry