Skip to content

Commit 28bee13

Browse files
feat(monorepo) WIP add httpsnippet to monorepo
* start migrating to /packages directory * add WIP lerna file * make cli and httpsnippet separate files * make targets their own directories starting out with curl/jquery
1 parent bd18e1e commit 28bee13

46 files changed

Lines changed: 1210 additions & 43 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

lerna.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"packages": [
3+
"packages/*"
4+
],
5+
"version": "0.0.0"
6+
}

package.json

Lines changed: 31 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,9 @@
22
"version": "1.16.7",
33
"name": "httpsnippet",
44
"description": "HTTP Request snippet generator for *most* languages",
5-
"author": "Ahmad Nassri <[email protected]> (https://www.mashape.com/)",
6-
"homepage": "https://github.com/Mashape/httpsnippet",
5+
"author": "Kong Inc.",
6+
"homepage": "https://github.com/kong/httpsnippet",
77
"license": "MIT",
8-
"main": "src/index.js",
9-
"bin": "bin/httpsnippet",
10-
"keywords": [
11-
"api",
12-
"clojure",
13-
"csharp",
14-
"curl",
15-
"go",
16-
"har",
17-
"http",
18-
"httpie",
19-
"java",
20-
"javascript",
21-
"jquery",
22-
"objc",
23-
"objective-c",
24-
"ocaml",
25-
"php",
26-
"python",
27-
"request",
28-
"requests",
29-
"ruby",
30-
"shell",
31-
"snippet",
32-
"swift",
33-
"swift",
34-
"unirest",
35-
"xhr",
36-
"xmlhttprequest"
37-
],
388
"engines": {
399
"node": ">=0.10"
4010
},
@@ -71,20 +41,38 @@
7141
"echint": "^3.0.0",
7242
"glob": "^6.0.1",
7343
"istanbul": "^0.4.0",
44+
"lerna": "^3.8.0",
7445
"mocha": "^2.3.4",
7546
"require-directory": "^2.1.1",
7647
"should": "^7.1.1",
7748
"standard": "^10.0.2"
7849
},
79-
"dependencies": {
80-
"chalk": "^1.1.1",
81-
"commander": "^2.9.0",
82-
"debug": "^2.2.0",
83-
"event-stream": "3.3.4",
84-
"form-data": "^1.0.0-rc3",
85-
"fs-readfile-promise": "^2.0.1",
86-
"fs-writefile-promise": "^1.0.3",
87-
"har-validator": "^5.0.0",
88-
"pinkie-promise": "^2.0.0"
89-
}
50+
"keywords": [
51+
"api",
52+
"clojure",
53+
"csharp",
54+
"curl",
55+
"go",
56+
"har",
57+
"http",
58+
"httpie",
59+
"java",
60+
"javascript",
61+
"jquery",
62+
"objc",
63+
"objective-c",
64+
"ocaml",
65+
"php",
66+
"python",
67+
"request",
68+
"requests",
69+
"ruby",
70+
"shell",
71+
"snippet",
72+
"swift",
73+
"swift",
74+
"unirest",
75+
"xhr",
76+
"xmlhttprequest"
77+
]
9078
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# @httpsnippet/cli
2+
3+
```bash
4+
npm install @httpsnippet/cli
5+
```
6+
7+
8+

packages/@httpsnippet/cli/index.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#!/usr/bin/env node
2+
3+
'use strict'
4+
5+
var chalk = require('chalk')
6+
var cmd = require('commander')
7+
var fs = require('fs')
8+
var readFile = require('fs-readfile-promise')
9+
var writeFile = require('fs-writefile-promise')
10+
var HTTPSnippet = require('../httpsnippet')
11+
var path = require('path')
12+
var pkg = require('./package.json')
13+
14+
cmd
15+
.version(pkg.version)
16+
.usage('[options] <files ...>')
17+
.option('-t, --target <target>', 'target output')
18+
.option('-c, --client [client]', 'target client library')
19+
.option('-o, --output <directory>', 'write output to directory')
20+
.parse(process.argv)
21+
22+
if (!cmd.args.length || !cmd.target) {
23+
cmd.help()
24+
}
25+
26+
if (cmd.output) {
27+
var dir = path.resolve(cmd.output)
28+
29+
if (!fs.existsSync(dir)) {
30+
fs.mkdirSync(dir)
31+
}
32+
}
33+
34+
cmd.args.forEach(function (fileName) {
35+
var file = path.basename(fileName)
36+
37+
readFile(fileName)
38+
.then(JSON.parse)
39+
40+
.catch(function (e) {
41+
console.error('%s %s failed to read JSON: %s', chalk.red('✖'), chalk.cyan.bold(file), chalk.red(e.message))
42+
})
43+
44+
.then(function (data) {
45+
return new HTTPSnippet(data)
46+
})
47+
48+
.catch(function (e) {
49+
e.errors.forEach(function (err) {
50+
console.error('%s %s failed validation: (%s: %s) %s', chalk.red('✖'), chalk.cyan.bold(file), chalk.cyan.italic(err.field), chalk.magenta.italic(err.value), chalk.red(err.message))
51+
})
52+
})
53+
54+
.then(function (snippet) {
55+
return snippet.convert(cmd.target, cmd.client)
56+
})
57+
58+
.then(function (output) {
59+
// print
60+
if (!cmd.output) {
61+
return console.log('%s %s > %s [%s] :\n%s', chalk.green('✓'), chalk.cyan.bold(file), chalk.yellow(cmd.target), chalk.yellow(cmd.client ? cmd.client : 'default'), output)
62+
}
63+
64+
// write to file
65+
var name = path.basename(file, path.extname(file))
66+
67+
var filename = path.format({
68+
dir: dir,
69+
base: name + HTTPSnippet.extname(cmd.target)
70+
})
71+
72+
return writeFile(filename, output + '\n', function () {
73+
console.log('%s %s > %s', chalk.green('✓'), chalk.cyan.bold(file), filename)
74+
})
75+
})
76+
77+
.catch(function (e) {
78+
console.error('%s %s fail: %s', chalk.red('✖'), chalk.cyan.bold(file), chalk.red(e.message))
79+
})
80+
})
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "@httpsnippet/cli",
3+
"description": "Command line interface for httpsnippet",
4+
"version": "0.1.0",
5+
"main": "index.js",
6+
"dependencies": {
7+
"chalk": "^1.1.1",
8+
"commander": "^2.9.0",
9+
"debug": "^2.2.0",
10+
"event-stream": "3.3.4",
11+
"form-data": "^1.0.0-rc3",
12+
"fs-readfile-promise": "^2.0.1",
13+
"fs-writefile-promise": "^1.0.3",
14+
"har-validator": "^5.0.0",
15+
"pinkie-promise": "^2.0.0"
16+
}
17+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/**
2+
* @description
3+
* HTTP code snippet generator for the Shell using cURL.
4+
*
5+
* @author
6+
* @AhmadNassri
7+
*
8+
* for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
9+
*/
10+
11+
'use strict'
12+
13+
var util = require('util')
14+
var helpers = require('../httpsnippet/helpers/shell')
15+
var CodeBuilder = require('../httpsnippet/helpers/code-builder')
16+
17+
module.exports = function (source, options) {
18+
var opts = util._extend({
19+
indent: ' ',
20+
short: false,
21+
binary: false
22+
}, options)
23+
24+
var code = new CodeBuilder(opts.indent, opts.indent !== false ? ' \\\n' + opts.indent : ' ')
25+
26+
code.push('curl %s %s', opts.short ? '-X' : '--request', source.method)
27+
.push(util.format('%s%s', opts.short ? '' : '--url ', helpers.quote(source.fullUrl)))
28+
29+
if (source.httpVersion === 'HTTP/1.0') {
30+
code.push(opts.short ? '-0' : '--http1.0')
31+
}
32+
33+
// construct headers
34+
Object.keys(source.headersObj).sort().forEach(function (key) {
35+
var header = util.format('%s: %s', key, source.headersObj[key])
36+
code.push('%s %s', opts.short ? '-H' : '--header', helpers.quote(header))
37+
})
38+
39+
if (source.allHeaders.cookie) {
40+
code.push('%s %s', opts.short ? '-b' : '--cookie', helpers.quote(source.allHeaders.cookie))
41+
}
42+
43+
// construct post params
44+
switch (source.postData.mimeType) {
45+
case 'multipart/form-data':
46+
source.postData.params.map(function (param) {
47+
var post = util.format('%s=%s', param.name, param.value)
48+
49+
if (param.fileName && !param.value) {
50+
post = util.format('%s=@%s', param.name, param.fileName)
51+
}
52+
53+
code.push('%s %s', opts.short ? '-F' : '--form', helpers.quote(post))
54+
})
55+
break
56+
57+
case 'application/x-www-form-urlencoded':
58+
if (source.postData.params) {
59+
source.postData.params.map(function (param) {
60+
code.push(
61+
'%s %s', opts.binary ? '--data-binary' : (opts.short ? '-d' : '--data'),
62+
helpers.quote(util.format('%s=%s', param.name, param.value))
63+
)
64+
})
65+
} else {
66+
code.push(
67+
'%s %s', opts.binary ? '--data-binary' : (opts.short ? '-d' : '--data'),
68+
helpers.escape(helpers.quote(source.postData.text))
69+
)
70+
}
71+
break
72+
73+
default:
74+
// raw request body
75+
if (source.postData.text) {
76+
code.push(
77+
'%s %s', opts.binary ? '--data-binary' : (opts.short ? '-d' : '--data'),
78+
helpers.escape(helpers.quote(source.postData.text))
79+
)
80+
}
81+
}
82+
83+
return code.join()
84+
}
85+
86+
module.exports.info = {
87+
key: 'curl',
88+
title: 'cURL',
89+
link: 'http://curl.haxx.se/',
90+
description: 'cURL is a command line tool and library for transferring data with URL syntax'
91+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
curl --request POST \
2+
--url http://mockbin.com/har \
3+
--header 'content-type: application/x-www-form-urlencoded' \
4+
--data foo=bar \
5+
--data hello=world
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
curl --request POST \
2+
--url http://mockbin.com/har \
3+
--header 'content-type: application/json' \
4+
--data '{"number":1,"string":"f\"oo","arr":[1,2,3],"nested":{"a":"b"},"arr_mix":[1,"a",{"arr_mix_nested":{}}],"boolean":false}'
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
curl --request POST \
2+
--url http://mockbin.com/har \
3+
--cookie 'foo=bar; bar=baz'
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
curl --request PROPFIND \
2+
--url http://mockbin.com/har

0 commit comments

Comments
 (0)