1.0.47 • Published 10 months ago

@kenote/core v1.0.47

Weekly downloads
18
License
MIT
Repository
github
Last release
10 months ago

@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.

1.0.47

10 months ago

1.0.44

2 years ago

1.0.42

2 years ago

1.0.41

2 years ago

1.0.34

2 years ago

1.0.32

2 years ago

1.0.31

2 years ago

1.0.30

2 years ago

1.0.27

3 years ago

1.0.21

3 years ago

1.0.20

3 years ago

1.0.19

3 years ago

1.0.13

3 years ago

1.0.12

3 years ago

1.0.11

3 years ago

1.0.5

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago