extend for koa extend.
$ npm install @koex/extend
// See more in test
import extend, { extendBatch } from '@koex/extend';
import * as Koa from 'koa';
declare module 'koa' {
export interface Context {
render(html: string): Promise<any>;
logger(html: string): void;
}
}
const app = new Koa();
extend(app, 'context', 'render', async function (html: string) {
const ctx: Koa.Context = this;
ctx.body = html;
});
// or extend batch
extendBatch(app, 'context', {
async render(html: string) {
const ctx: Koa.Context = this;
ctx.body = html;
},
logger(message: string) {
console.log(message);
},
});
app.use(router.post('/', ctx => {
ctx.logger('post: ', ctx.path, ctx.query, ctx.request.body);
await ctx.render(`
<h1>Hello, World</h1>
`);
}));
app.listen(8000, '0.0.0.0', () => {
console.log('koa server start at port: 8000');
});