0.3.0 • Published 5 years ago

@tdqs/koa-api v0.3.0

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

@tdqs/koa-api

The MIT License (MIT)

@tdqs/koa-api koa middleware.

Table of Contents

Installation
API documentation
Example

Installation

    $ npm install @tdqs/koa-api

API documentation

Getting Started

import { api, cls, koaApi } from '@tdqs/koa-api';
import * as Koa from 'koa';

const app = new Koa();

@cls()
class Test {
  @api()
  public static hello(ctx) {
    return 'hello world';
  }
}

app.use(koaApi({}, Test)).listen(8080);

//GET localhost:8080/docs       Api document
//GET localhost:8080/hello      Response 'hello world'

jsonschema & middlewares simplification

import { api, cls, IApiContext } from '../src/index';

@cls({
  name: '测试schema',
  prefix: '/schema',
})
export class Schema {
  @api({
    method: 'POST',
    middlewares: [
      async (ctx, next) => {
        const start = new Date().getTime();
        await next();
        const ms = new Date().getTime() - start;
        ctx.body = {
          ms,
          text: ctx.body,
        };
      },
    ],
    schema: {
      properties: {
        p: { type: 'string' },
        q: { type: 'number' },
      },
      required: ['p', 'q'],
      type: 'object',
    },
  })
  public static hello(ctx: IApiContext) {
    return `hello world ${JSON.stringify(ctx.data)}`;
  }
}

Example

Following example could be found inside /tests directory.

0.3.0

5 years ago

0.2.5

5 years ago

0.2.4

5 years ago

0.2.3

5 years ago

0.2.2

5 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.1.0

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago