httpsnippet
Advanced tools
Comparing version 1.0.0-beta.6 to 1.0.0-beta.7
{ | ||
"version": "1.0.0-beta.6", | ||
"version": "1.0.0-beta.7", | ||
"name": "httpsnippet", | ||
@@ -8,3 +8,3 @@ "description": "HTTP Request snippet generator for *most* languages", | ||
"scripts": { | ||
"test": "mocha --recursive --reporter spec", | ||
"test": "mocha --reporter spec", | ||
"coverage": "mocha -r blanket -R mocha-lcov-reporter > test/lcov.info", | ||
@@ -31,2 +31,3 @@ "html-cov": "mocha -r blanket -R html-cov > test/coverage.html", | ||
"codeclimate-test-reporter": "0.0.4", | ||
"glob": "^5.0.1", | ||
"mocha": "^2.1.0", | ||
@@ -42,4 +43,4 @@ "mocha-lcov-reporter": "0.0.2", | ||
"har-validator": "^1.1.2", | ||
"requireindex": "^1.1.0" | ||
"require-directory": "^2.1.0" | ||
} | ||
} |
@@ -56,7 +56,8 @@ # HTTP Snippet [![version][npm-version]][npm-url] [![License][npm-license]][license-url] | ||
-h, --help output usage information | ||
-V, --version output the version number | ||
-t, --target <target> target output | ||
-o, --output <directory> write output to directory | ||
-n, --output-name <name> output file name | ||
-h, --help output usage information | ||
-V, --version output the version number | ||
-t, --target <target> target output | ||
-c, --client [client] target client library | ||
-o, --output <directory> write output to directory | ||
-n, --output-name <name> output file name | ||
``` | ||
@@ -66,6 +67,33 @@ | ||
process single file: | ||
process single file (assumes [HAR Request Object](http://www.softwareishard.com/blog/har-12-spec/#request) Format): | ||
###### EXAMPLE: *my-api-endpoint.json* | ||
```json | ||
{ | ||
"method": "POST", | ||
"url": "http://mockbin.com/request", | ||
"httpVersion": "HTTP/1.1", | ||
"queryString": [ | ||
{ "name": "foo", "value": "bar" }, | ||
{ "name": "foo", "value": "baz" }, | ||
{ "name": "baz", "value": "abc" } | ||
], | ||
"headers": [ | ||
{ "name": "Accept", "value": "text/plain" }, | ||
{ "name": "Content-Type", "value": "application/json" }, | ||
{ "name": "X-Foo-Bar", "value": "Baz" } | ||
], | ||
"cookies": [ | ||
{ "name": "foo", "value": "bar" }, | ||
{ "name": "bar", "value": "baz" } | ||
], | ||
"postData": { | ||
"mimeType": "application/json", | ||
"text": "{\"foo\": \"bar\"}" | ||
} | ||
} | ||
``` | ||
```shell | ||
httpsnippet my-api-endpoint.json --langauge php --output ./snippets | ||
httpsnippet my-api-endpoint.json --target php --output ./snippets | ||
``` | ||
@@ -82,3 +110,3 @@ | ||
```shell | ||
httpsnippet /*.json --langauge nodejs --output ./snippets | ||
httpsnippet /*.json --target node --client native --output ./snippets | ||
``` | ||
@@ -110,3 +138,3 @@ | ||
method: 'GET', | ||
url: 'http://httpconsole.com/echo' | ||
url: 'http://mockbin.com/request' | ||
}); | ||
@@ -128,7 +156,7 @@ | ||
At the heart of this module is the [HAR Request object](http://www.softwareishard.com/blog/har-12-spec/#request) as the http request description format, please review some of the sample JSON HAR Request objects in [test fixtures](/test/fixtures), or read the [HAR Docs](http://www.softwareishard.com/blog/har-12-spec/#request) for more details. | ||
At the heart of this module is the [HAR Request object](http://www.softwareishard.com/blog/har-12-spec/#request) as the http request description format, please review some of the sample JSON HAR Request objects in [test fixtures](/test/fixtures/requests), or read the [HAR Docs](http://www.softwareishard.com/blog/har-12-spec/#request) for more details. | ||
### Output Targets | ||
output [targets](/src/targets) are simple modules that expose a constructor *(which handles the transformation)* and a utility `info` method | ||
output [targets](/src/targets) are simple modules that expose a constructor *(which handles the transformation)* and a meta `info` property. | ||
@@ -144,11 +172,8 @@ ```js | ||
module.exports.info = function () { | ||
// return target info | ||
return { | ||
family: 'node', // target family | ||
key: 'native', // target key | ||
ext: '.js', // preferred extension | ||
title: '', // target label | ||
description: '' // target description | ||
}; | ||
module.exports.info = { | ||
key: 'curl', | ||
title: 'cURL', | ||
link: 'http://curl.haxx.se/', | ||
description: 'curl is a command line tool and library for transferring data with URL syntax', | ||
extname: '.sh' | ||
}; | ||
@@ -155,0 +180,0 @@ ``` |
128
src/index.js
'use strict'; | ||
var debug = require('debug')('httpsnippet'); | ||
var mapper = require('./mapper'); | ||
var reducer = require('./reducer'); | ||
var qs = require('querystring'); | ||
@@ -40,3 +40,3 @@ var targets = require('./targets'); | ||
this.source.queryString.map(mapper(this.source.queryObj)); | ||
this.source.queryObj = this.source.queryString.reduce(reducer, {}); | ||
} | ||
@@ -48,3 +48,3 @@ | ||
this.source.headers.map(mapper(this.source.headersObj)); | ||
this.source.headersObj = this.source.headers.reduce(reducer, {}); | ||
} | ||
@@ -56,3 +56,3 @@ | ||
// merge all possible queryString values | ||
this.source.queryString = util._extend(this.source.uriObj.query, this.source.queryObj); | ||
this.source.queryObj = this.source.queryString = util._extend(this.source.uriObj.query, this.source.queryObj); | ||
@@ -77,12 +77,8 @@ // reset uriObj values for a clean url | ||
HTTPSnippet.prototype.getSource = function () { | ||
return this.source; | ||
}; | ||
HTTPSnippet.prototype.convert = function (family, target, opts) { | ||
if (!opts && target) { | ||
opts = target; | ||
HTTPSnippet.prototype.convert = function (target, client, opts) { | ||
if (!opts && client) { | ||
opts = client; | ||
} | ||
var func = this._matchTarget(family, target); | ||
var func = this._matchTarget(target, client); | ||
@@ -96,105 +92,47 @@ if (func) { | ||
HTTPSnippet.prototype._matchTarget = function (familyName, target) { | ||
HTTPSnippet.prototype._matchTarget = function (target, client) { | ||
// does it exist? | ||
if (targets[familyName] === undefined) { | ||
if (!targets.hasOwnProperty(target)) { | ||
return false; | ||
} | ||
// isolate the family | ||
var family = targets[familyName]; | ||
// childless targets | ||
if (typeof family === 'function') { | ||
return family; | ||
if (typeof targets[target] === 'function') { | ||
return targets[target]; | ||
} | ||
// find the first default target | ||
var defaultTarget = family._familyInfo().default; | ||
// shorthand | ||
if (!target || typeof target === 'object') { | ||
target = defaultTarget; | ||
if (typeof client === 'string' && typeof targets[target][client] === 'function') { | ||
return targets[target][client]; | ||
} | ||
// asking for a particular target | ||
if (typeof target === 'string') { | ||
// attempt to call the first one we find | ||
if (typeof family[target] !== 'function') { | ||
target = defaultTarget; | ||
} | ||
// last chance | ||
if (typeof family[target] === 'function') { | ||
return family[target]; | ||
} | ||
} | ||
return false; | ||
// default target | ||
return targets[target][targets[target].info.default]; | ||
}; | ||
// exports | ||
module.exports = HTTPSnippet; | ||
module.exports._targets = function () { | ||
return Object.keys(targets); | ||
}; | ||
module.exports.availableTargets = function () { | ||
return Object.keys(targets).map(function (key) { | ||
var target = util._extend({}, targets[key].info); | ||
var clients = Object.keys(targets[key]) | ||
module.exports._familyInfo = function (family) { | ||
if (targets[family] && targets[family]._familyInfo) { | ||
return targets[family]._familyInfo(); | ||
} | ||
.filter(function (prop) { | ||
return !~['info', 'index'].indexOf(prop); | ||
}) | ||
return false; | ||
}; | ||
.map(function (client) { | ||
return targets[key][client].info; | ||
}); | ||
module.exports.info = function (family, target) { | ||
if (!targets[family]) { | ||
return false; | ||
} | ||
if (clients.length) { | ||
target.clients = clients; | ||
} | ||
if (typeof targets[family] === 'function') { | ||
return targets[family].info(); | ||
} | ||
// get all info for all family members | ||
if (!target && typeof targets[family] === 'object') { | ||
var results = { | ||
family: family | ||
}; | ||
results.members = Object.keys(targets[family]) | ||
.filter(function (key) { | ||
return key !== '_familyInfo'; | ||
}) | ||
.map(function (target) { | ||
var info = targets[family][target].info(); | ||
delete info.family; | ||
return info; | ||
}); | ||
return results; | ||
} | ||
if (typeof targets[family] === 'object' && typeof targets[family][target] === 'function') { | ||
return targets[family][target].info(); | ||
} | ||
return target; | ||
}); | ||
}; | ||
module.exports.extname = function (family, target) { | ||
if (!targets[family]) { | ||
return ''; | ||
} | ||
if (typeof targets[family] === 'function') { | ||
return targets[family].info().extname; | ||
} | ||
// get all info for all family members | ||
if (!target && typeof targets[family] === 'object') { | ||
return targets[family]._familyInfo().extname; | ||
} | ||
module.exports.extname = function (target) { | ||
return targets[target] ? targets[target].info.extname : ''; | ||
}; |
@@ -37,9 +37,9 @@ 'use strict'; | ||
if (this.source.postData.text) { | ||
code.push(util.format('%s %s', opts.short ? '-F' : '--form', JSON.stringify(this.source.postData.text))); | ||
code.push(util.format('%s %s', opts.short ? '-d' : '--data', JSON.stringify(this.source.postData.text))); | ||
} | ||
// construct post params | ||
if (!this.source.postData.text && this.source.postData.params && this.source.postData.params.length) { | ||
if (!this.source.postData.text && this.source.postData.params) { | ||
this.source.postData.params.map(function (param) { | ||
code.push(util.format('%s "%s=%s"', opts.short ? '-d' : '--data', param.name, param.value)); | ||
code.push(util.format('%s "%s=%s"', opts.short ? '-F' : '--form', param.name, param.value)); | ||
}); | ||
@@ -51,10 +51,8 @@ } | ||
module.exports.info = function () { | ||
return { | ||
key: 'curl', | ||
title: 'cURL', | ||
link: 'http://curl.haxx.se/', | ||
description: 'curl is a command line tool and library for transferring data with URL syntax', | ||
extname: '.sh' | ||
}; | ||
module.exports.info = { | ||
key: 'curl', | ||
title: 'cURL', | ||
link: 'http://curl.haxx.se/', | ||
description: 'curl is a command line tool and library for transferring data with URL syntax', | ||
extname: '.sh' | ||
}; |
@@ -111,10 +111,8 @@ 'use strict'; | ||
module.exports.info = function () { | ||
return { | ||
key: 'httpie', | ||
title: 'HTTPie', | ||
link: 'http://httpie.org/', | ||
description: 'a CLI, cURL-like tool for humans', | ||
extname: '.sh' | ||
}; | ||
module.exports.info = { | ||
key: 'httpie', | ||
title: 'HTTPie', | ||
link: 'http://httpie.org/', | ||
description: 'a CLI, cURL-like tool for humans', | ||
extname: '.sh' | ||
}; |
'use strict'; | ||
module.exports = require('requireindex')(__dirname); | ||
module.exports = require('require-directory')(module); |
'use strict'; | ||
module.exports = require('requireindex')(__dirname); | ||
module.exports._familyInfo = function () { | ||
return { | ||
key: 'node', | ||
title: 'Node.JS', | ||
extname: '.js', | ||
default: 'native' | ||
}; | ||
}; | ||
module.exports = require('require-directory')(module); |
'use strict'; | ||
var util = require('util'); | ||
var reducer = require('.,/../../reducer'); | ||
@@ -31,2 +32,6 @@ module.exports = function (options) { | ||
if (!this.source.postData.text && this.source.postData.params) { | ||
code.push('var querystring = require("querystring");'); | ||
} | ||
code.push(null); | ||
@@ -55,22 +60,25 @@ | ||
if (this.source.postData) { | ||
code.push(null); | ||
code.push(util.format('req.write(%s)', JSON.stringify(this.source.postData.text))); | ||
code.push(null); | ||
if (this.source.postData.text) { | ||
code.push(util.format('req.write(%s);', JSON.stringify(this.source.postData.text))); | ||
} | ||
if (!this.source.postData.text && this.source.postData.params) { | ||
var postData = this.source.postData.params.reduce(reducer, {}); | ||
code.push(util.format('var postData = querystring.stringify(%s);', JSON.stringify(postData))); | ||
code.push(util.format('req.write(postData);')); | ||
} | ||
code.push('req.end();'); | ||
code.push(null); | ||
return code.join('\n'); | ||
}; | ||
module.exports.info = function () { | ||
return { | ||
family: 'node', | ||
key: 'native', | ||
title: 'HTTP', | ||
link: 'http://nodejs.org/api/http.html#http_http_request_options_callback', | ||
description: 'Node.js native HTTP interface' | ||
}; | ||
module.exports.info = { | ||
key: 'native', | ||
title: 'HTTP', | ||
link: 'http://nodejs.org/api/http.html#http_http_request_options_callback', | ||
description: 'Node.js native HTTP interface' | ||
}; |
@@ -105,10 +105,7 @@ 'use strict'; | ||
module.exports.info = function () { | ||
return { | ||
family: 'php', | ||
key: 'curl', | ||
title: 'cURL', | ||
link: 'http://php.net/manual/en/book.curl.php', | ||
description: 'PHP with libcurl' | ||
}; | ||
module.exports.info = { | ||
key: 'curl', | ||
title: 'cURL', | ||
link: 'http://php.net/manual/en/book.curl.php', | ||
description: 'PHP with libcurl' | ||
}; |
'use strict'; | ||
module.exports = require('requireindex')(__dirname); | ||
module.exports._familyInfo = function () { | ||
return { | ||
key: 'php', | ||
title: 'PHP', | ||
extname: '.php', | ||
default: 'curl' | ||
}; | ||
}; | ||
module.exports = require('require-directory')(module); |
@@ -35,3 +35,3 @@ 'use strict'; | ||
if (this.source.postData) { | ||
if (this.source.postData.text) { | ||
code.push('--body-data ' + JSON.stringify(this.source.postData.text)); | ||
@@ -47,10 +47,8 @@ } | ||
module.exports.info = function () { | ||
return { | ||
key: 'wget', | ||
title: 'Wget', | ||
link: 'https://www.gnu.org/software/wget/', | ||
description: 'a free software package for retrieving files using HTTP, HTTPS', | ||
extname: '.sh' | ||
}; | ||
module.exports.info = { | ||
key: 'wget', | ||
title: 'Wget', | ||
link: 'https://www.gnu.org/software/wget/', | ||
description: 'a free software package for retrieving files using HTTP, HTTPS', | ||
extname: '.sh' | ||
}; |
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
39862
24
592
257
6
+ Addedrequire-directory@^2.1.0
+ Addedrequire-directory@2.1.1(transitive)
- Removedrequireindex@^1.1.0
- Removedrequireindex@1.2.0(transitive)