# @zcong/wrangler-toolkit

> toolkit for cloudflare wrangler

Latest version **0.4.7** (published 2019-07-29) · MIT license · 0 weekly downloads

## Install

```sh
npm install @zcong/wrangler-toolkit
pnpm add @zcong/wrangler-toolkit
yarn add @zcong/wrangler-toolkit
bun add @zcong/wrangler-toolkit
```

## Health

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

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

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.4.7 |
| Published | 2019-07-29 |
| First published | 2019-07-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 21.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | zcong1993 |
| Maintainers | zcong |

## Links

- npm: https://www.npmjs.com/package/@zcong/wrangler-toolkit
- npm.io page: https://npm.io/package/@zcong/wrangler-toolkit

## Dependencies (1)

- [microrouter](https://npm.io/package/microrouter.md) ^3.1.3

## Recent versions

- 0.4.7 (latest) — 2019-07-29
- 0.4.6 — 2019-07-29
- 0.4.5 — 2019-07-29
- 0.4.4 — 2019-07-28
- 0.4.3 — 2019-07-27
- 0.4.2 — 2019-07-27
- 0.4.1 — 2019-07-27
- 0.4.0 — 2019-07-27
- 0.3.2 — 2019-07-26
- 0.3.1 — 2019-07-26
- 0.3.0 — 2019-07-26
- 0.2.1 — 2019-07-26
- 0.2.0 — 2019-07-26
- 0.1.0 — 2019-07-26

## README

# wrangler-toolkit

[![NPM version](https://img.shields.io/npm/v/@zcong/wrangler-toolkit.svg?style=flat)](https://npmjs.com/package/@zcong/wrangler-toolkit) [![NPM downloads](https://img.shields.io/npm/dm/@zcong/wrangler-toolkit.svg?style=flat)](https://npmjs.com/package/@zcong/wrangler-toolkit) [![CircleCI](https://circleci.com/gh/zcong1993/wrangler-toolkit/tree/master.svg?style=shield)](https://circleci.com/gh/zcong1993/wrangler-toolkit/tree/master) [![codecov](https://codecov.io/gh/zcong1993/wrangler-toolkit/branch/master/graph/badge.svg)](https://codecov.io/gh/zcong1993/wrangler-toolkit)

> toolkit for [cloudflare/wrangler](https://github.com/cloudflare/wrangler)

## Why ?

### Before

```js
addEventListener('fetch', event => {
  event.respondWith(handleRequest(event.request))
})

async function handleRequest(request) {
  return new Response('Hello worker!', { status: 200 })
}
```

### After

```js
const { App } = require('@zcong/wrangler-toolkit')

// support middleware
const middleware = handler => async (req, res) => {
  console.log('middleware 1')
  return handler(req, res)
}

// global error handler middleware
const errorMiddleware = handler => async (req, res) => {
  try {
    await handler(req, res)
  } catch (err) {
    console.log(err)
    res.status = 500
    res.json({
      message: err.message
    })
  }
}

// router handler
const indexHandler = async (req, res) => {
  res.json({
    data: 'hello world!'
  })
}

const postHandler = async (req, res) => {
  const body = await req.json()
  res.json(body)
}

const app = new App()
const r = app
  .setDefaultHandler() // set default handler, if all router not match, default is 404 response
  .use(middleware) // use middleware
  .use(errorMiddleware) // use global error middleware
  .get('/', indexHandler) // add router, support get, post...
  .post('/', postHandler) // post router
  .get('/err', () => {
    throw new Error('err test')
  }) // global error handler test

addEventListener('fetch', event => {
  // use r.handleRequest
  event.respondWith(r.handleRequest(event.request))
})
```

more complex example is here [wrangler-pipe](https://github.com/zcong1993/wrangler-pipe)

## License

MIT &copy; zcong1993

---
_Source: https://npm.io/package/@zcong/wrangler-toolkit · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
