@orikami/fn-to-json-api
Advanced tools
Comparing version 0.1.0 to 0.2.0
16
index.js
@@ -1,8 +0,18 @@ | ||
const { json } = require("micro"); | ||
const { json, text } = require("micro"); | ||
const query = require("micro-query"); | ||
const { parse } = require("qs"); | ||
module.exports = handler => async (req, res) => { | ||
const postData = req.method === "POST" ? await json(req) : {}; | ||
let postData = {}; | ||
if (req.method === "POST") { | ||
try { | ||
postData = await json(req); | ||
} catch (err) { | ||
postData = await text(req); | ||
postData = parse(postData); | ||
} | ||
} | ||
const queryParams = query(req); | ||
const data = Object.assign(queryParams, postData); | ||
return await handler(data, req, res); | ||
}; | ||
}; |
@@ -7,2 +7,3 @@ const request = require("micro-test-request"); | ||
test("200 POST sync function", async () => { | ||
expect.assertions(2); | ||
const res = await request({ | ||
@@ -24,2 +25,3 @@ method: "POST", | ||
test("200 POST async function", async () => { | ||
expect.assertions(2); | ||
const res = await request({ | ||
@@ -34,9 +36,27 @@ method: "POST", | ||
expect(JSON.parse(res.body)).toEqual({ | ||
a: "post", // POST body overwrites query params | ||
b: "query", | ||
c: "post" | ||
}); | ||
a: "post", // POST body overwrites query params | ||
b: "query", | ||
c: "post" | ||
}); | ||
}); | ||
test("200 POST with formdata", async () => { | ||
expect.assertions(2); | ||
const res = await request({ | ||
method: "POST", | ||
url: "/?a=query&b=query", | ||
body: "a=post&c=post", | ||
handler: toJsonApi(echo) | ||
}); | ||
expect(res.statusCode).toBe(200); | ||
expect(JSON.parse(res.body)).toEqual({ | ||
a: "post", // POST body overwrites query params | ||
b: "query", | ||
c: "post" | ||
}); | ||
}); | ||
test("200 GET async function", async () => { | ||
expect.assertions(2); | ||
const res = await request({ | ||
@@ -55,5 +75,3 @@ method: "GET", | ||
const echo = data => { | ||
return data; | ||
}; | ||
const echo = data => data | ||
@@ -60,0 +78,0 @@ const echoAsync = async data => { |
{ | ||
"name": "@orikami/fn-to-json-api", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "Convert (jsonData) => {} to (req, res) => {}", | ||
@@ -12,3 +12,4 @@ "main": "index.js", | ||
"dependencies": { | ||
"micro-query": "^0.3.0" | ||
"micro-query": "^0.3.0", | ||
"qs": "^6.5.2" | ||
}, | ||
@@ -15,0 +16,0 @@ "devDependencies": { |
@@ -32,3 +32,4 @@ # fn-to-json-api | ||
0.2.0 - Add support for formencoded POST request | ||
0.1.0 - Refactor | ||
0.0.2 - Initial release |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
3671
83
34
0
2
+ Addedqs@^6.5.2
+ Addedcall-bind-apply-helpers@1.0.2(transitive)
+ Addedcall-bound@1.0.4(transitive)
+ Addeddunder-proto@1.0.1(transitive)
+ Addedes-define-property@1.0.1(transitive)
+ Addedes-errors@1.3.0(transitive)
+ Addedes-object-atoms@1.1.1(transitive)
+ Addedfunction-bind@1.1.2(transitive)
+ Addedget-intrinsic@1.3.0(transitive)
+ Addedget-proto@1.0.1(transitive)
+ Addedgopd@1.2.0(transitive)
+ Addedhas-symbols@1.1.0(transitive)
+ Addedhasown@2.0.2(transitive)
+ Addedmath-intrinsics@1.1.0(transitive)
+ Addedobject-inspect@1.13.4(transitive)
+ Addedqs@6.14.0(transitive)
+ Addedside-channel@1.1.0(transitive)
+ Addedside-channel-list@1.0.0(transitive)
+ Addedside-channel-map@1.0.1(transitive)
+ Addedside-channel-weakmap@1.0.2(transitive)