@kenote/core
TypeScript icon, indicating that this package has built-in type declarations

1.0.47 • Public • Published

@kenote/core

The core library of the Kenote.js.

NPM Version NPM Downloads Gratipay

Usage

import { Module, ServerFactory } from '@kenote/core'
import { ServiceEngine } from '@kenote/koa'

@Module({
  imports: [],
  plugins: [],
  middlewares: [],
  httpException: {
    notFound: (ctx: Context) => {
      return ctx.status(404)
    },
    exception: (err: any, ctx: Context) => {
      ctx.renderException('error', { message: 'This page could internal server error' })
    }
  }
})
class AppModule {}

async bootstarp () {
  let factory = await ServerFactory(new ServiceEngine()).create(AppModule)
  factory.server.listen(4000)
}

Controller

import { Context, Controller, Get, NextHandler, Post } from '@kenote/core'


@Controller()
class MainController {

  @Get('/path')
  async handle (ctx: Context, next: NextHandler) {
    // ...
  }

  @Post('/path')
  async handle (ctx: Context, next: NextHandler) {
    // ...
  }

  @Put('/path')
  async handle (ctx: Context, next: NextHandler) {
    // ...
  }

  @Delete('/path')
  async handle (ctx: Context, next: NextHandler) {
    // ...
  }
}

Middleware

import { Middleware, Action, Context, Property } from '@kenote/core'
import { HttpError } from 'http-errors'

const service = {
  // ...
}

@Middleware()
class Restful {

  @Action()
  api (ctx: Context) {
    return (data: any, error?: HttpError) => {
      if (error != null) {
        let { message } = error
        ctx.json({ error: message })
      } else {
        ctx.json({ data })
      }
    }
  }

  @Property()
  service (ctx: Context) {
    return service
  }
}

declare module '@kenote/core' {
  interface Context {

    api (data: any, error?: HttpError): void
    
    service: typeof service
  }
}

MIT License.

Readme

Keywords

Package Sidebar

Install

npm i @kenote/core

Weekly Downloads

3

Version

1.0.47

License

MIT

Unpacked Size

125 kB

Total Files

60

Last publish

Collaborators

  • thondery