# simple-cloudbase-router

> > 像写 koa 一样来写 微信云函数

Latest version **1.0.2** (published 2021-12-29) · MIT license · 0 weekly downloads

## Install

```sh
npm install simple-cloudbase-router
pnpm add simple-cloudbase-router
yarn add simple-cloudbase-router
bun add simple-cloudbase-router
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2021-12-29 |
| First published | 2021-11-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 30 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | SonOfMagic |
| Maintainers | icebreaker |

## Links

- npm: https://www.npmjs.com/package/simple-cloudbase-router
- Repository: git@github.com:sonofmagic/simple-cloudbase-router
- npm.io page: https://npm.io/package/simple-cloudbase-router

## Dependencies (1)

- [@types/koa-compose](https://npm.io/package/@types/koa-compose.md) ^3.2.5

## Recent versions

- 1.0.2 (latest) — 2021-12-29
- 1.0.0-alpha.2 (alpha) — 2021-12-29
- 1.0.0 (alpha.1) — 2021-12-28
- 1.0.1 — 2021-12-29
- 1.0.0-alpha.1 — 2021-12-28
- 0.1.0 — 2021-11-21
- 0.0.4 — 2021-11-19
- 0.0.3 — 2021-11-19
- 0.0.2 — 2021-11-18
- 0.0.1 — 2021-11-13

## README

# simple-cloudbase-router

> 像写 koa 一样来写 微信云函数

## Install

```bash
yarn add simple-cloudbase-router@latest
# or
npm i simple-cloudbase-router@latest
```

## Usage

### Typescript/ESM (with [`simple-cloudbase`](https://cloudbase.icebreaker.top/))

完整见[examples/modern](https://github.com/sonofmagic/simple-cloudbase-router/tree/main/examples/modern)

```ts
// app.ts
import { cloud } from '~/common/tcb'
import { compose, createServe } from 'simple-cloudbase-router'
import type { ICustomContext } from './type'
import { commonRouter } from './routers'

const fn = compose<ICustomContext>([
  async (ctx, next) => {
    ctx.cloud = cloud
    ctx.wxContext = cloud.getWXContext()
    await next()
  },
  commonRouter
])

export const serve = createServe(fn)
```

```ts
// index.ts
import { serve } from './app'
export async function main (event: any, content: any) {
  return await serve(event, content)
}
```

### Commonjs(Raw)

完整见[examples/raw](https://github.com/sonofmagic/simple-cloudbase-router/tree/main/examples/raw)

```js
// app.js
const cloud = require('wx-server-sdk')
cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})
const { compose, createServe } = require('simple-cloudbase-router')

const commonRoute = require('./routers/common')

const fn = compose([
  async (ctx, next) => {
    ctx.cloud = cloud
    ctx.wxContext = cloud.getWXContext()
    await next()
  },
  commonRoute
])
const serve = createServe(fn)
module.exports = {
  serve
}
```

```js
// index.js
const app = require('./app')

exports.main = async (event, context) => {
  return await app.serve(event, context)
}
```

---
_Source: https://npm.io/package/simple-cloudbase-router · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
