1.0.4 • Published 4 years ago

generate-curl v1.0.4

Weekly downloads
15
License
ISC
Repository
github
Last release
4 years ago

generate-curl

在後端收到requests時,常常需要將這些requests參數複製下來至postman或者insomnia重組,這十分的麻煩,generate-curl提供將requests轉換成curl的功能,讓後端人員可以使用此curl直接轉換至postman, insomnia, terminal來debug,另外,generate-curl也提供函數來讓重要資訊隱藏。

安裝

npm install generate-curl

如何使用

對於express.js server

express.js可直接使用

const express = require('express')
const bodyParser = require('body-parser')
const curl = require('generate-curl').curl()
const { utils: curlUtils } = require('generate-curl')

const app = express();

const upload = require('multer')()
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())

app.use('/monitor', upload.none(), function(req, res) {
  const option = {
    headers: {
      '$["authorization"]': curlUtils.hide()
    }
  }
  console.log(curl(req, option))
  // curl -d '{"data":"12345"}' -H "host: localhost:3003" -H "content-type: application/json" -H "authorization: hide" -X POST 'http://localhost:3003/monitor?query=abc'
  res.send('ok')
})

app.listen(3003, function () {
  console.log('3003');
})

對於其他node.js server

只需在初始化generate-curl時設置不同框架的req所對應至express的參數即可

const curl = require('generate-curl').curl(req => {
  protocol: req.protocol,
  path: req.path,
  headers: req.headers,
  method: req.method,
  body: req.body,
  query: req.query,
  get (host) {
    return req.get(host)
  }
})

目前支援content-type

  • application/json
  • application/x-www-form-urlencoded
  • multipart/form-data //對req.file不支援

提供的utils function

當有敏感資訊不想顯示出來時,可以使用option對此做調整,例如authorization不想顯示時,可使用hide隱藏,目前有以下特性:

  • 使用jsonPath來對應路徑,可以使用jsonPath的語法來對應複雜的巢狀json
  • utils.hide(): 隱藏value
  • utils.hash(): 雜湊value
  • utils.slice(start, end): 切割字串
  • 可自訂處理function

例子

const option = {
  headers: {
    '$["authorization"]': curlUtils.hide(),// authorization會被隱藏,即 authorization: hide
    '$["userId"]': curlUtils.hash(),// userId會被hash,即 hashuserId: 9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08
    '$["uuid"]': curlUtils.slice(0, 3)// uuid會被字串切割,即 uuid: 123
  }
  body: {
    '$..name': value => `${value}1234`// 在body裡的name會被自訂fucntion處理,此處即加上1234字串
  }
}
console.log(curl(req, option))
1.0.2

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago

0.0.0

4 years ago