0.0.1 • Published 5 years ago

@koex/extend v0.0.1

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

extend

NPM version Coverage Status Dependencies Build Status license issues

extend for koa extend.

Install

$ npm install @koex/extend

Usage

// 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');
});

Related