Comparing version 6.2.0 to 6.2.1
@@ -43,2 +43,6 @@ // Native | ||
// maps requests to buffered raw bodies so that | ||
// multiple calls to `json` work as expected | ||
const rawBodyMap = new WeakMap() | ||
function json(req, {limit = '1mb'} = {}) {return __async(function*(){ | ||
@@ -49,4 +53,9 @@ try { | ||
const encoding = typer.parse(type).parameters.charset | ||
const str = yield getRawBody(req, {limit, length, encoding}) | ||
let str = rawBodyMap.get(req) | ||
if (!str) { | ||
str = yield getRawBody(req, {limit, length, encoding}) | ||
rawBodyMap.set(req, str) | ||
} | ||
try { | ||
@@ -53,0 +62,0 @@ return JSON.parse(str) |
{ | ||
"name": "micro", | ||
"version": "6.2.0", | ||
"version": "6.2.1", | ||
"description": "Asynchronous HTTP microservices", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.js", |
@@ -100,2 +100,3 @@ ![](https://cldup.com/JDmmHX3uhF.svg) | ||
- Exposes an `async` function that can be run with `await`. | ||
- Can be called multiple times, as it caches the raw request body the first time. | ||
- `limit` is how much data is aggregated before parsing at max. Otherwise, an `Error` is thrown with `statusCode` set to `413` (see [Error Handling](#error-handling)). It can be a `Number` of bytes or [a string](https://www.npmjs.com/package/bytes) like `'1mb'`. | ||
@@ -259,14 +260,15 @@ - If JSON parsing fails, an `Error` is thrown with `statusCode` set to `400` (see [Error Handling](#error-handling)) | ||
```js | ||
const micro = require('micro'); | ||
const test = require('ava'); | ||
const listen = require('./listen'); | ||
const send = require('micro').send; | ||
const listen = require('test-listen'); | ||
const request = require('request-promise'); | ||
test('my endpoint', async t => { | ||
const fn = async function (req, res) { | ||
send(res, 200, { test: 'woot' }); | ||
}; | ||
const url = await listen(fn); | ||
const service = micro(async function (req, res) { | ||
micro.send(res, 200, { test: 'woot' }) | ||
}); | ||
const url = await listen(service); | ||
const body = await request(url); | ||
t.same(body.test, 'woot'); | ||
t.deepEqual(JSON.parse(body).test, 'woot'); | ||
}); | ||
@@ -273,0 +275,0 @@ ``` |
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
18113
115
326