@kenote/core
v1.0.47
Published
The core library of the Kenote.js.
Downloads
16
Readme
@kenote/core
The core library of the Kenote.js.
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.