2.2.0 • Published 4 months ago

@mutoe/koam-core v2.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

Koam

Implement a simple Koa-like http server with zero dependencies from scratch.

THIS FRAMEWORK HAVE NOT BEEN STRICTLY TESTED, PLEASE DO NOT USE IT IN PRODUCTION ! 许多功能未经严格测试,请勿用于生产目的!

Advantage

  • Lightweight (0 dependency)
  • TypeScript friendly
  • Built-in body parser middleware
  • Built-in response time middleware

Notes

  1. Current only support application/json type request and response body
  2. ctx.assert must explicit declare Context type. See microsoft/Typescript#34523
    app.use(async (ctx: Context, next) => {
      //                ^^^^^^^
      const val: unknown = 1.2345
      //         ^^^^^^^
      ctx.assert(typeof val === 'number', 500)
      console.log(val.toFixed(2))
      //          ^^^ now val is number type
    })

Roadmap

Configuration

Application properties / methods

  • app.use(middleware)
  • app.callback()
  • app.listen(...)
  • app.context
  • app.keys=
  • app.on('error', error) using app.onError instead
  • app.address
  • app.port

Context properties

  • ctx.req
  • ctx.res
  • ctx.request
  • ctx.response
  • ctx.state
  • ctx.app
  • ctx.app.emit using ctx.throw instead
  • ctx.cookies see https://github.com/mutoe/koam#3-cookie
  • ctx.throw([status], [message], [detail]) ctx.throw(appError)
  • ctx.assert(value, [status], [message], [detail])
  • ctx.respond

Context request

  • request.headers ctx.headers
    • request.header ctx.header
  • request.headers=
    • request.header=
  • request.get(field) ctx.get(field)
  • request.method ctx.method
  • request.method= ctx.method=
  • request.length
  • request.protocol ctx.protocol
  • request.host ctx.host
  • request.hostname ctx.hostname
  • request.url ctx.url
  • request.url= ctx.url=
  • request.originalUrl ctx.originalUrl
  • request.origin ctx.origin
  • request.URL
  • request.href ctx.href
  • request.path ctx.path
  • request.path= ctx.path=
  • request.query ctx.query
  • request.query= ctx.query=
  • request.querystring ctx.querystring
    • request.search
  • request.querystring= ctx.querystring=
    • request.search=
  • request.type (get mime-type in Content-Type header)
  • request.charset (get charset in Content-Type header)
  • request.fresh ctx.fresh
  • request.socket ctx.socket
  • request.stale ctx.stale
  • request.secure ctx.secure
  • request.ip ctx.ip
  • request.ips ctx.ips
  • request.subdomains ctx.subdomains
  • request.is() ctx.is()
  • request.accepts() ctx.accepts()
  • request.acceptsEncodings() ctx.acceptsEncodings()
  • request.acceptsCharsets() ctx.acceptsCharsets()
  • request.acceptsLanguages() ctx.acceptsLanguages()
  • request.idempotent

Context response

  • response.headers
    • response.header
  • response.headerSent ctx.headerSent
  • response.flushHeaders()
  • response.has(header)
  • response.get(header)
  • response.set(headers) ctx.set(headers)
  • response.append(header, value) ctx.append(header, value)
  • response.remove(header) ctx.remove(header)
  • response.socket
  • response.body ctx.body
  • response.body= ctx.body= (currently only finished json body and stream)
  • response.status ctx.status
  • response.status= ctx.status=
  • response.message ctx.message
  • response.message= ctx.message=
  • response.length ctx.length
  • response.length= ctx.length=
  • response.type ctx.type
  • response.type= ctx.type=
  • response.is(mimeTypes...)
  • response.redirect(url, [alt]) ctx.redirect(url, [alt])
  • response.attachment([filename]) ctx.attachment([filename])
  • response.lastModified= ctx.lastModified=
  • response.etag= ctx.etag=
  • response.vary(field)
2.2.0

4 months ago

2.0.2

10 months ago

2.1.0

10 months ago

2.0.0

12 months ago

2.0.0-alpha.0

12 months ago