New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@orikami/fn-to-json-api

Package Overview
Dependencies
Maintainers
3
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@orikami/fn-to-json-api - npm Package Compare versions

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);
};
};

32

index.test.js

@@ -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
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc