httpsnippet
Advanced tools
Comparing version 1.0.0-beta to 1.0.0-beta.1
{ | ||
"version": "1.0.0-beta", | ||
"version": "1.0.0-beta.1", | ||
"name": "httpsnippet", | ||
@@ -4,0 +4,0 @@ "description": "HTTP Request snippet generator for *most* languages", |
@@ -72,6 +72,3 @@ # HTTP Snippet [![version][npm-version]][npm-url] [![License][npm-license]][license-url] | ||
```shell | ||
tree snippets | ||
``` | ||
``` | ||
$ tree snippets | ||
snippets/ | ||
@@ -88,6 +85,3 @@ └── my-api-endpoint.php | ||
```shell | ||
tree sources/ | ||
``` | ||
``` | ||
$ tree sources/ | ||
sources/ | ||
@@ -100,6 +94,3 @@ ├── endpoint-1.json | ||
```shell | ||
tree snippets/ | ||
``` | ||
``` | ||
$ tree snippets/ | ||
snippets/ | ||
@@ -116,11 +107,15 @@ ├── endpoint-1.js | ||
var httpsnippet = require('httpsnippet'); | ||
var snippet = new httpsnippet(source); | ||
var snippet = new httpsnippet({ | ||
method: 'GET', | ||
url: 'http://httpconsole.com/echo' | ||
}); | ||
// generate cURL output | ||
console.log(snippet.curl({ | ||
indent: ' '; | ||
indent: '\t'; | ||
})); | ||
// generate nodeJS output | ||
snippet.nodejs(); | ||
console.log(snippet.nodejs()); | ||
``` | ||
@@ -134,3 +129,3 @@ | ||
output [targets](/src/targets) are simple modules that expose a constructor *(which handles the transformation)* and a utility method `extname` *(used in CLI for writing output into disk)* | ||
output [targets](/src/targets) are simple modules that expose a constructor *(which handles the transformation)* and a utility `info` method | ||
@@ -146,4 +141,10 @@ ```js | ||
module.exports.extname = function () { | ||
// return preferred filename extention | ||
module.exports.info = function () { | ||
// return target info | ||
return { | ||
key: 'node', // target key | ||
ext: '.js', // preferred extension | ||
title: '', // target label | ||
description: '' // target description | ||
} | ||
}; | ||
@@ -217,4 +218,2 @@ ``` | ||
---- | ||
[license-url]: https://github.com/ahmadnassri/httpsnippet/blob/master/LICENSE | ||
@@ -221,0 +220,0 @@ |
@@ -5,2 +5,3 @@ 'use strict'; | ||
var mapper = require('./mapper'); | ||
var qs = require('querystring'); | ||
var targets = require('./targets'); | ||
@@ -39,20 +40,20 @@ var url = require('url'); | ||
// search property is evil | ||
// prevents re-construction with new query values | ||
this.source.uriObj.search = null; | ||
// merge all possible queryString values | ||
this.source.queryString = util._extend(this.source.uriObj.query, this.source.queryObj); | ||
// update the query object | ||
// reset uriObj values for a clean url | ||
this.source.uriObj.query = null; | ||
this.source.uriObj.search = null; | ||
this.source.uriObj.path = this.source.uriObj.pathname; | ||
// keep the base url clean of queryString | ||
this.source.url = url.format(this.source.uriObj); | ||
// update the uri object | ||
this.source.uriObj.query = this.source.queryString; | ||
this.source.uriObj.search = qs.stringify(this.source.queryString); | ||
this.source.uriObj.path = this.source.uriObj.pathname + '?' + this.source.uriObj.search; | ||
// construct a full url | ||
this.source.fullUrl = url.format(this.source.uriObj); | ||
// reset queryString in url | ||
this.source.uriObj.query = null; | ||
// keep the base url clean of queryString | ||
this.source.url = url.format(this.source.uriObj); | ||
}; | ||
@@ -77,4 +78,4 @@ | ||
module.exports.extname = function (lang) { | ||
return targets[lang].extname(); | ||
module.exports.info = function (lang) { | ||
return targets[lang].info(); | ||
}; |
@@ -45,4 +45,10 @@ 'use strict'; | ||
module.exports.extname = function () { | ||
return '.sh'; | ||
module.exports.info = function () { | ||
return { | ||
key: 'curl', | ||
ext: '.sh', | ||
title: 'cURL', | ||
link: 'http://curl.haxx.se/', | ||
description: 'curl is a command line tool and library for transferring data with URL syntax' | ||
}; | ||
}; |
@@ -7,2 +7,3 @@ 'use strict'; | ||
var opts = util._extend({ | ||
queryParams: false, | ||
body: false, | ||
@@ -66,9 +67,23 @@ cert: false, | ||
code.push(util.format('http %s%s %s', flags.length ? flags.join(' ') + ' ' : '', this.source.method, this.source.url)); | ||
code.push(util.format('http %s%s %s', flags.length ? flags.join(' ') + ' ' : '', this.source.method, opts.queryParams ? this.source.url : this.source.fullUrl)); | ||
var self = this; | ||
// construct query params | ||
if (this.source.queryString && this.source.queryString.length) { | ||
this.source.queryString.map(function (query) { | ||
code.push(util.format('%s==%s', query.name, query.value)); | ||
}); | ||
if (opts.queryParams && this.source.queryString) { | ||
var queryStringKeys = Object.keys(this.source.queryString); | ||
if (queryStringKeys.length) { | ||
queryStringKeys.map(function (name) { | ||
var value = self.source.queryString[name]; | ||
if (util.isArray(value)) { | ||
value.map(function (val) { | ||
code.push(util.format('%s==%s', name, val)); | ||
}); | ||
} else { | ||
code.push(util.format('%s==%s', name, value)); | ||
} | ||
}); | ||
} | ||
} | ||
@@ -95,4 +110,10 @@ | ||
module.exports.extname = function () { | ||
return '.sh'; | ||
module.exports.info = function () { | ||
return { | ||
key: 'httpie', | ||
ext: '.sh', | ||
title: 'HTTPie', | ||
link: 'http://httpie.org/', | ||
description: 'a CLI, cURL-like tool for humans' | ||
}; | ||
}; |
@@ -5,6 +5,10 @@ 'use strict'; | ||
module.exports = function (opts) { | ||
module.exports = function (options) { | ||
var opts = util._extend({ | ||
indent: ' ' | ||
}, options); | ||
var code = []; | ||
var options = { | ||
var reqOpts = { | ||
method: this.source.method, | ||
@@ -23,28 +27,50 @@ hostname: this.source.uriObj.hostname, | ||
options.headers.Cookie = cookies.join('; '); | ||
reqOpts.headers.Cookie = cookies.join('; '); | ||
} | ||
code.push('var options = ' + JSON.stringify(options, null, 2)); | ||
code.push('var http = require("http");'); | ||
code.push([ | ||
'var req = http.request(options, function (res) {', | ||
'console.log("STATUS: " + res.statusCode);', | ||
'console.log("HEADERS: " + JSON.stringify(res.headers));', | ||
'res.setEncoding("utf8");', | ||
'res.on("data", function (chunk) {', | ||
' console.log("BODY: " + chunk);', | ||
'});' | ||
].join('\n ') + '\n});'); | ||
code.push(null); | ||
code.push(util.format('var options = %s;', JSON.stringify(reqOpts, null, opts.indent))); | ||
code.push(null); | ||
code.push('var req = http.request(options, function (res) {'); | ||
code.push(opts.indent + 'var chunks = [];'); | ||
code.push(null); | ||
code.push(opts.indent + 'res.on("data", function (chunk) {'); | ||
code.push(opts.indent + opts.indent + 'chunks.push(chunk);'); | ||
code.push(opts.indent + '});'); | ||
code.push(null); | ||
code.push(opts.indent + 'res.on("end", function () {'); | ||
code.push(opts.indent + opts.indent + 'var body = Buffer.concat(chunks);'); | ||
code.push(opts.indent + '});'); | ||
code.push('});'); | ||
if (this.source.postData) { | ||
code.push(util.format('this.source.write(%s)', JSON.stringify(this.source.postData.text))); | ||
code.push(null); | ||
code.push(util.format('req.write(%s)', JSON.stringify(this.source.postData.text))); | ||
} | ||
code.push('this.source.end();'); | ||
code.push('req.end();'); | ||
code.push(null); | ||
return code.join('\n'); | ||
}; | ||
module.exports.extname = function () { | ||
return '.js'; | ||
module.exports.info = function () { | ||
return { | ||
key: 'node', | ||
ext: '.js', | ||
title: 'Node.JS', | ||
link: 'http://nodejs.org/api/http.html#http_http_request_options_callback', | ||
description: 'Node.js native HTTP interface' | ||
}; | ||
}; |
@@ -9,2 +9,4 @@ 'use strict'; | ||
noTags: false, | ||
maxRedirects: 10, | ||
timeout: 30, | ||
closingTag: false | ||
@@ -25,3 +27,3 @@ }, options); | ||
escape: true, | ||
name: 'CURLOPT_port', | ||
name: 'CURLOPT_PORT', | ||
value: this.source.uriObj.port | ||
@@ -31,3 +33,3 @@ }, { | ||
name: 'CURLOPT_URL', | ||
value: this.source.url | ||
value: this.source.fullUrl | ||
}, { | ||
@@ -38,3 +40,15 @@ escape: false, | ||
}, { | ||
escape: true, | ||
name: 'CURLOPT_ENCODING', | ||
value: '' | ||
}, { | ||
escape: false, | ||
name: 'CURLOPT_MAXREDIRS', | ||
value: opts.maxRedirects | ||
}, { | ||
escape: false, | ||
name: 'CURLOPT_TIMEOUT', | ||
value: opts.timeout | ||
}, { | ||
escape: false, | ||
name: 'CURLOPT_HTTP_VERSION', | ||
@@ -57,3 +71,3 @@ value: this.source.httpVersion === 'HTTP/1.0' ? 'CURL_HTTP_VERSION_1_0' : 'CURL_HTTP_VERSION_1_1' | ||
curlOptions.map(function (option) { | ||
if (option.value) { | ||
if (!~[null, undefined].indexOf(option.value)) { | ||
curlopts.push(util.format('%s => %s,', option.name, option.escape ? JSON.stringify(option.value) : option.value)); | ||
@@ -96,4 +110,10 @@ } | ||
module.exports.extname = function () { | ||
return '.php'; | ||
module.exports.info = function () { | ||
return { | ||
key: 'php', | ||
ext: '.php', | ||
title: 'PHP', | ||
link: 'http://php.net/manual/en/book.curl.php', | ||
description: 'PHP with libcurl' | ||
}; | ||
}; |
@@ -43,3 +43,3 @@ 'use strict'; | ||
code.push(util.format('- "%s"', this.source.url)); | ||
code.push(util.format('- "%s"', this.source.fullUrl)); | ||
@@ -49,4 +49,10 @@ return code.join(opts.indent !== false ? ' \\\n' + opts.indent : ' '); | ||
module.exports.extname = function () { | ||
return '.sh'; | ||
module.exports.info = function () { | ||
return { | ||
key: 'wget', | ||
ext: '.sh', | ||
title: 'Wget', | ||
link: 'https://www.gnu.org/software/wget/', | ||
description: 'a free software package for retrieving files using HTTP, HTTPS' | ||
}; | ||
}; |
Sorry, the diff of this file is not supported yet
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
30731
395
228