@remotezygote/koa-api-app
TypeScript icon, indicating that this package has built-in type declarations

1.0.28 • Public • Published

Koa API App

Use this as the base for your API app. It's very simple:

import app, {
  exposed,
  body,
  paginate,
  filter,
  get,
  post,
  put,
  del,
  start,
} from "@remotezygote/koa-api-app"
import { ParameterizedContext } from "koa"

const getEnvironment = async (ctx: ParameterizedContext) => {}

const getCurrentUser = async (ctx: ParameterizedContext) => {
  const { user } = ctx.state

  ctx.body = user
  ctx.status = 200
}

exposed.get("/env", getEnvironment) // listen at /env with no authentication needed
get("/me", getCurrentUser) // listen at /me with authenication needed
put("/me", body(), updateUser) // update current user with authentication needed (also read the body)
put("/users", paginate(), getUsers) // get users with pagination (ctx.state.paginate)
put("/users", filter(), getUsers) // get users with filters (ctx.state.filters)
put("/me", body(), paginate(), filter(), getCertainUsers) // read the body, paginate, and parse filters

start()

Websockets

import app, {
  start,
} from "@remotezygote/koa-api-app"
import { ParameterizedContext } from "koa"

const getWithWebsocket = async (ctx: ParameterizedContext) => {
  if (ctx.ws) {
    const ws = await ctx.ws()
    ws.send('this is a websocket')
  }

  ctx.status = 200
  ctx.body = 'this is not a websocket'
}

get("/resource", getWithWebsocket) // listen at /me with authenication needed

start()

Readme

Keywords

none

Package Sidebar

Install

npm i @remotezygote/koa-api-app

Weekly Downloads

177

Version

1.0.28

License

MIT

Unpacked Size

68.3 kB

Total Files

13

Last publish

Collaborators

  • remotezygote