Skip to content

koexjs/extend

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

extend for koa extend

Resources

License

Stars

Watchers

Forks

Packages

No packages published