1.0.42 • Published 1 year ago

@remotezygote/koa-api-app v1.0.42

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

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()
1.0.39

1 year ago

1.0.38

1 year ago

1.0.40

1 year ago

1.0.42

1 year ago

1.0.41

1 year ago

1.0.33

1 year ago

1.0.32

1 year ago

1.0.31

1 year ago

1.0.30

1 year ago

1.0.37

1 year ago

1.0.36

1 year ago

1.0.35

1 year ago

1.0.34

1 year ago

1.0.28

1 year ago

1.0.27

1 year ago

1.0.26

1 year ago

1.0.25

1 year ago

1.0.24

1 year ago

1.0.19

2 years ago

1.0.18

2 years ago

1.0.16

2 years ago

1.0.9

2 years ago

1.0.6

2 years ago

1.0.11

2 years ago

1.0.21

2 years ago

1.0.10

2 years ago

1.0.20

2 years ago

1.0.15

2 years ago

1.0.14

2 years ago

1.0.13

2 years ago

1.0.23

2 years ago

1.0.12

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago