Comparing version 6.1.0 to 6.2.0
@@ -0,2 +1,5 @@ | ||
// Native | ||
const server = require('http').Server | ||
// Packages | ||
const getRawBody = require('raw-body') | ||
@@ -7,4 +10,6 @@ const typer = require('media-typer') | ||
const DEV = 'development' === process.env.NODE_ENV | ||
const TESTING = 'test' === process.env.NODE_ENV | ||
module.exports = exports = serve | ||
exports.run = run | ||
@@ -17,2 +22,6 @@ exports.json = json | ||
function serve(fn, {onError = null} = {}) { | ||
if (onError) { | ||
console.warn('[DEPRECATED] onError is deprecated and will be removed in a future release. Please use your own try/catch as needed.') | ||
} | ||
return server((req, res) => { | ||
@@ -27,5 +36,5 @@ run(req, res, fn, onError || sendError) | ||
// return a non-null value -> send with 200 | ||
// return a non-null value -> send | ||
if (null !== val && undefined !== val) { | ||
send(res, 200, val) | ||
send(res, res.statusCode || 200, val) | ||
} | ||
@@ -111,3 +120,5 @@ } catch (err) { | ||
console.error(stack) | ||
if (!TESTING) { | ||
console.error(stack) | ||
} | ||
} | ||
@@ -114,0 +125,0 @@ |
{ | ||
"name": "micro", | ||
"version": "6.1.0", | ||
"description": "Async HTTP microservices", | ||
"version": "6.2.0", | ||
"description": "Asynchronous HTTP microservices", | ||
"main": "./dist/index.js", | ||
@@ -17,3 +17,3 @@ "files": [ | ||
"build": "mkdir -p dist && async-to-gen lib/index.js > dist/index.js", | ||
"test": "ava && xo" | ||
"test": "xo && ava" | ||
}, | ||
@@ -49,5 +49,6 @@ "ava": { | ||
"micro", | ||
"server", | ||
"service", | ||
"microservice", | ||
"serverless" | ||
"serverless", | ||
"API" | ||
], | ||
@@ -64,3 +65,3 @@ "author": { | ||
"devDependencies": { | ||
"ava": "^0.16.0", | ||
"ava": "^0.17.0", | ||
"request": "^2.74.0", | ||
@@ -70,11 +71,12 @@ "request-promise": "^4.1.1", | ||
"then-sleep": "^1.0.1", | ||
"xo": "^0.16.0" | ||
"xo": "^0.17.0" | ||
}, | ||
"dependencies": { | ||
"async-to-gen": "1.1.4", | ||
"async-to-gen": "1.3.0", | ||
"is-async-supported": "1.2.0", | ||
"isstream": "0.1.2", | ||
"media-typer": "0.3.0", | ||
"minimist": "1.2.0", | ||
"raw-body": "2.1.7" | ||
"raw-body": "2.2.0" | ||
} | ||
} |
@@ -25,7 +25,8 @@ ![](https://cldup.com/JDmmHX3uhF.svg) | ||
```js | ||
const { send } = require('micro'); | ||
const sleep = require('then-sleep'); | ||
const {send} = require('micro') | ||
const sleep = require('then-sleep') | ||
module.exports = async function (req, res) { | ||
await sleep(500); | ||
send(res, 200, 'Ready!'); | ||
await sleep(500) | ||
send(res, 200, 'Ready!') | ||
} | ||
@@ -37,3 +38,3 @@ ``` | ||
```bash | ||
$ micro -p 3000 sleep.js | ||
micro sleep.js | ||
``` | ||
@@ -44,19 +45,14 @@ | ||
```bash | ||
$ micro -p 3000 -H localhost sleep.js | ||
micro -H localhost sleep.js | ||
``` | ||
## Documentation | ||
## Usage | ||
### Installation | ||
Install the package (requires at least Node v6): | ||
**Note**: `micro` requires Node `6.0.0` or later | ||
Install from NPM: | ||
```js | ||
$ npm init | ||
$ npm install micro --save | ||
npm install --save micro | ||
``` | ||
Then in your `package.json`: | ||
And start using it in your `package.json` file: | ||
@@ -66,11 +62,12 @@ ```js | ||
"scripts": { | ||
"start": "micro -p 3000" | ||
"start": "micro" | ||
} | ||
``` | ||
Then write your `index.js` (see above for an example). To run your | ||
app and make it listen on `http://localhost:3000` run: | ||
Then write your `index.js` (see above for an example). | ||
After that, you can make the server run by executing the following command: | ||
```bash | ||
$ npm start | ||
npm start | ||
``` | ||
@@ -81,3 +78,3 @@ | ||
#### micro | ||
**`micro(fn, { onError = null })`** | ||
**`micro(fn)`** | ||
@@ -88,3 +85,2 @@ - This function is exposed as the `default` export. | ||
- The supplied function is run with `await`. It can be `async`! | ||
- The `onError` function is invoked with `req, res, err` if supplied (see [Error Handling](#error-handling)) | ||
- Example: | ||
@@ -152,3 +148,3 @@ | ||
```js | ||
module.exports = default function (req, res) { | ||
module.exports = function (req, res) { | ||
return {message: 'Hello!'}; | ||
@@ -163,3 +159,3 @@ } | ||
const sleep = require('then-sleep') | ||
module.exports = async function(req, res) => { | ||
module.exports = async function (req, res) { | ||
return new Promise(async (resolve) => { | ||
@@ -177,7 +173,7 @@ await sleep(100); | ||
- Use `require('micro').sendError`. | ||
- Used as the default handler for `onError`. | ||
- Used as the default handler for errors thrown. | ||
- Automatically sets the status code of the response based on `error.statusCode`. | ||
- Sends the `error.message` as the body. | ||
- During development (when `NODE_ENV` is set to `'development'`), stacks are printed out with `console.error` and also sent in responses. | ||
- Usually, you don't need to invoke this method yourself, as you can use the [built-in error handling](error-handling) flow with `throw`. | ||
- Usually, you don't need to invoke this method yourself, as you can use the [built-in error handling](#error-handling) flow with `throw`. | ||
@@ -190,3 +186,3 @@ #### createError | ||
- Creates an error object with a `statusCode`. | ||
- Useful for easily throwing errors with HTTP status codes, which are interpreted by the [built-in error handling](error-handling). | ||
- Useful for easily throwing errors with HTTP status codes, which are interpreted by the [built-in error handling](#error-handling). | ||
- `orig` sets `error.originalError` which identifies the original error (if any). | ||
@@ -247,16 +243,5 @@ | ||
In order to set up your own error handling mechanism, you can pass a custom `onError` function to micro: | ||
In order to set up your own error handling mechanism, you can use composition in your handler: | ||
```js | ||
const myErrorHandler = async (req, res, err) => { | ||
// your own logging here | ||
res.writeHead(500); | ||
res.end('error!'); | ||
}; | ||
micro(handler, { onError: myErrorHandler }); | ||
``` | ||
**However**, generally you want to instead use simple composition: | ||
```js | ||
module.exports = handleErrors(async (req, res) => { | ||
@@ -304,5 +289,4 @@ throw new Error('What happened here?'); | ||
We now use [async-to-gen](https://github.com/leebyron/async-to-gen), | ||
so that the only transformation that happens is converting `async` | ||
and `await` to generators. | ||
We use [is-async-supported](https://github.com/timneutkens/is-async-supported) combined with [async-to-gen](https://github.com/leebyron/async-to-gen), | ||
so that the we only convert `async` and `await` to generators when needed. | ||
@@ -342,4 +326,11 @@ If you want to do it manually, you can! `micro(1)` is idempotent and | ||
As always, you can run the [AVA](https://github.com/sindresorhus/ava) and [ESLint](http://eslint.org) tests using: `npm test` | ||
## Credits | ||
Thanks Tom Yandell and Richard Hodgson for donating the `micro` npm name. | ||
## Authors | ||
- Guillermo Rauch ([@rauchg](https://twitter.com/rauchg)) - [▲ZEIT](https://zeit.co) | ||
- Leo Lamprecht ([@notquiteleo](https://twitter.com/notquiteleo)) - [▲ZEIT](https://zeit.co) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17781
108
6
324
3
+ Addedis-async-supported@1.2.0
+ Addedasync-to-gen@1.3.0(transitive)
+ Addediconv-lite@0.4.15(transitive)
+ Addedis-async-supported@1.2.0(transitive)
+ Addedmagic-string@0.19.1(transitive)
+ Addedraw-body@2.2.0(transitive)
- Removedasync-to-gen@1.1.4(transitive)
- Removediconv-lite@0.4.13(transitive)
- Removedmagic-string@0.16.0(transitive)
- Removedraw-body@2.1.7(transitive)
Updatedasync-to-gen@1.3.0
Updatedraw-body@2.2.0