Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 29 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var HTTPSnippet = function (data) {
var entries
var self = this
var input = Object.assign({}, data)
var boundary

// prep the main container
self.requests = []
Expand Down Expand Up @@ -50,7 +51,29 @@ var HTTPSnippet = function (data) {
})
}

HTTPSnippet.prototype._generateBoundary = function () {
var self = this
// This generates a 50 character boundary similar to those used by Firefox.
// They are optimized for boyer-moore parsing.
var boundary = '--------------------------';
for (var i = 0; i < 24; i++) {
boundary += Math.floor(Math.random() * 10).toString(16);
}

self._boundary = boundary;
}

HTTPSnippet.prototype.getBoundary = function () {
var self = this
if (!self._boundary) {
self._generateBoundary();
}

return self._boundary;
}

HTTPSnippet.prototype.prepare = function (request) {
var self = this
// construct utility properties
request.queryObj = {}
request.headersObj = {}
Expand Down Expand Up @@ -105,7 +128,7 @@ HTTPSnippet.prototype.prepare = function (request) {
var form = new MultiPartForm()

// easter egg
form._boundary = '---011000010111000001101001'
this._boundary = '---011000010111000001101001'

request.postData.params.forEach(function (param) {
form.append(param.name, param.value || '', {
Expand All @@ -114,12 +137,12 @@ HTTPSnippet.prototype.prepare = function (request) {
})
})

form.pipe(es.map(function (data, cb) {
request.postData.text += data
}))
// form.pipe(es.map(function (data, cb) {
// request.postData.text += data
// }))

request.postData.boundary = form.getBoundary()
request.headersObj['content-type'] = 'multipart/form-data; boundary=' + form.getBoundary()
request.postData.boundary = this.getBoundary()
request.headersObj['content-type'] = 'multipart/form-data; boundary=' + this.getBoundary()
}
break

Expand Down
106 changes: 106 additions & 0 deletions src/targets/node/axios.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/**
* @description
* HTTP code snippet generator for Node.js using Unirest.
*
* @author
* @AhmadNassri
*
* for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
*/

'use strict'

let CodeBuilder = require('../../helpers/code-builder')

function contentBodyFactory(contentType, postData, includeFS = false) {
switch (contentType) {
case 'application/x-www-form-urlencoded':
return [postData.paramsObj, includeFS]
case 'application/json':
return [postData.jsonObj, includeFS]
case 'multipart/form-data':
let multipart = []

postData.params.forEach((param) => {
let part = {}

if (param.fileName && !param.value) {
includeFS = true

part.body = 'fs.createReadStream("' + param.fileName + '")'
} else if (param.value) {
part.body = param.value
}

if (part.body) {
if (param.contentType) {
part['content-type'] = param.contentType
}

multipart.push(part)
}
})
return [multipart, includeFS]
default:
if (postData.text) {
return [postData.text, includeFS]
}
return [null, includeFS]
}

}
module.exports = function (source, options) {
let opts = Object.assign({
indent: ' '
}, options)


let code = new CodeBuilder(opts.indent)

code.push('const axios = require("axios");')
.blank()

const requestOptions = {
method: `${source.method}`,
headers: {
'content-type': `${source.postData.mimeType}`,
...(Object.keys(source.headersObj).length && source.headersObj)
},
params: Object.keys(source.queryObj).length ? source.queryObj : undefined,
url: `${source.url}`
};
const { postData } = source
const { mimeType } = postData
const [data, includeFS] = contentBodyFactory(mimeType, postData)
if (data) {
requestOptions['data'] = data
}
if (includeFS) {
code.unshift('const fs = require("fs");')
}
const formatedOptions = JSON.stringify(requestOptions)
.replace(/{/g, `{
`)
.replace(/}/g, `
}`)
.replace(/",/g, `",
`)
code.push(`axios(${
formatedOptions
})
.then((response)=>{
console.log(response)
})
.catch((error)=>{
console.log(error)
})`)

return code.join().replace(/"fs\.createReadStream\(\\"(.+)\\"\)"/, 'fs.createReadStream("$1")')
}

module.exports.info = {
key: 'axios',
title: 'Axios',
link: 'https://github.com/axios/axios',
description: 'Promise based HTTP client for the browser and node.js - axios/axios.'
}
3 changes: 2 additions & 1 deletion src/targets/node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ module.exports = {

native: require('./native'),
request: require('./request'),
unirest: require('./unirest')
unirest: require('./unirest'),
axios: require('./axios')
}