@koex/extend
TypeScript icon, indicating that this package has built-in type declarations

0.0.1 • Public • Published

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

Readme

Keywords

none

Package Sidebar

Install

npm i @koex/extend

Weekly Downloads

1

Version

0.0.1

License

MIT

Unpacked Size

5.92 kB

Total Files

9

Last publish

Collaborators

  • uniquecolesmith