Skip to content

Commit c4f38e1

Browse files
authored
Merge branch 'master' into fix/multipart-handling
2 parents 52ef611 + 155e563 commit c4f38e1

File tree

186 files changed

+1245
-378
lines changed

Some content is hidden

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

186 files changed

+1245
-378
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"java",
2121
"javascript",
2222
"jquery",
23+
"kotlin",
2324
"objc",
2425
"objective-c",
2526
"ocaml",

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,8 @@ module.exports.addTargetClient = function (target, client) {
279279
throw new Error('The supplied custom target client must contain an `info` object.')
280280
} else if (!('key' in client.info) || !('title' in client.info)) {
281281
throw new Error('The supplied custom target client must have an `info` object with a `key` and `title` property.')
282+
} else if (targets[target].hasOwnProperty(client.info.key)) {
283+
throw new Error('The supplied custom target client already exists, please use a different key')
282284
}
283285

284286
targets[target][client.info.key] = client

src/targets/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module.exports = {
88
http: require('./http'),
99
java: require('./java'),
1010
javascript: require('./javascript'),
11+
kotlin: require('./kotlin'),
1112
node: require('./node'),
1213
objc: require('./objc'),
1314
ocaml: require('./ocaml'),

src/targets/javascript/axios.js

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/**
2+
* @description
3+
* HTTP code snippet generator for Javascript & Node.js using Axios.
4+
*
5+
* @author
6+
* @rohit-gohri
7+
*
8+
* for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
9+
*/
10+
'use strict'
11+
12+
var util = require('util')
13+
var stringifyObject = require('stringify-object')
14+
var CodeBuilder = require('../../helpers/code-builder')
15+
16+
module.exports = function (source, options) {
17+
var opts = Object.assign({
18+
indent: ' '
19+
}, options)
20+
21+
var code = new CodeBuilder(opts.indent)
22+
23+
code.push('import axios from "axios";')
24+
.blank()
25+
26+
var reqOpts = {
27+
method: source.method,
28+
url: source.url
29+
}
30+
31+
if (Object.keys(source.queryObj).length) {
32+
reqOpts.params = source.queryObj
33+
}
34+
35+
if (Object.keys(source.allHeaders).length) {
36+
reqOpts.headers = source.allHeaders
37+
}
38+
39+
switch (source.postData.mimeType) {
40+
case 'application/x-www-form-urlencoded':
41+
reqOpts.data = source.postData.paramsObj
42+
break
43+
44+
case 'application/json':
45+
if (source.postData.jsonObj) {
46+
reqOpts.data = source.postData.jsonObj
47+
}
48+
break
49+
50+
case 'multipart/form-data':
51+
code.push('const form = new FormData();')
52+
53+
source.postData.params.forEach(function (param) {
54+
code.push(
55+
'form.append(%s, %s);',
56+
JSON.stringify(param.name),
57+
JSON.stringify(param.value || param.fileName || '')
58+
)
59+
})
60+
61+
code.blank()
62+
63+
reqOpts.data = '[form]'
64+
break
65+
66+
default:
67+
if (source.postData.text) {
68+
reqOpts.data = source.postData.text
69+
}
70+
}
71+
72+
code.push('const options = %s;', stringifyObject(reqOpts, { indent: ' ', inlineCharacterLimit: 80 }).replace('"[form]"', 'form'))
73+
.blank()
74+
75+
code.push(util.format('axios.request(options).then(%s', 'function (response) {'))
76+
.push(1, 'console.log(response.data);')
77+
.push('}).catch(%s', 'function (error) {')
78+
.push(1, 'console.error(error);')
79+
.push('});')
80+
81+
return code.join()
82+
}
83+
84+
module.exports.info = {
85+
key: 'axios',
86+
title: 'Axios',
87+
link: 'https://github.com/axios/axios',
88+
description: 'Promise based HTTP client for the browser and node.js'
89+
}

src/targets/javascript/fetch.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ module.exports = function (source, options) {
4444
break
4545

4646
case 'multipart/form-data':
47-
code.push('var form = new FormData();')
47+
code.push('const form = new FormData();')
4848

4949
source.postData.params.forEach(function (param) {
5050
code.push(

src/targets/javascript/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ module.exports = {
1010

1111
jquery: require('./jquery'),
1212
fetch: require('./fetch'),
13-
xhr: require('./xhr')
13+
xhr: require('./xhr'),
14+
axios: require('./axios')
1415
}

src/targets/javascript/jquery.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ module.exports = function (source, options) {
3838
break
3939

4040
case 'multipart/form-data':
41-
code.push('var form = new FormData();')
41+
code.push('const form = new FormData();')
4242

4343
source.postData.params.forEach(function (param) {
4444
code.push('form.append(%s, %s);', JSON.stringify(param.name), JSON.stringify(param.value || param.fileName || ''))
@@ -62,7 +62,7 @@ module.exports = function (source, options) {
6262
}
6363
}
6464

65-
code.push('var settings = ' + JSON.stringify(settings, null, opts.indent).replace('"[form]"', 'form'))
65+
code.push('const settings = ' + JSON.stringify(settings, null, opts.indent).replace('"[form]"', 'form') + ';')
6666
.blank()
6767
.push('$.ajax(settings).done(function (response) {')
6868
.push(1, 'console.log(response);')

src/targets/javascript/xhr.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ module.exports = function (source, options) {
2222

2323
switch (source.postData.mimeType) {
2424
case 'application/json':
25-
code.push('var data = JSON.stringify(%s);', JSON.stringify(source.postData.jsonObj, null, opts.indent))
25+
code.push('const data = JSON.stringify(%s);', JSON.stringify(source.postData.jsonObj, null, opts.indent))
2626
.push(null)
2727
break
2828

2929
case 'multipart/form-data':
30-
code.push('var data = new FormData();')
30+
code.push('const data = new FormData();')
3131

3232
source.postData.params.forEach(function (param) {
3333
code.push('data.append(%s, %s);', JSON.stringify(param.name), JSON.stringify(param.value || param.fileName || ''))
@@ -42,11 +42,11 @@ module.exports = function (source, options) {
4242
break
4343

4444
default:
45-
code.push('var data = %s;', JSON.stringify(source.postData.text || null))
45+
code.push('const data = %s;', JSON.stringify(source.postData.text || null))
4646
.blank()
4747
}
4848

49-
code.push('var xhr = new XMLHttpRequest();')
49+
code.push('const xhr = new XMLHttpRequest();')
5050

5151
if (opts.cors) {
5252
code.push('xhr.withCredentials = true;')

src/targets/kotlin/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict'
2+
3+
module.exports = {
4+
info: {
5+
key: 'kotlin',
6+
title: 'Kotlin',
7+
extname: '.kt',
8+
default: 'okhttp'
9+
},
10+
11+
okhttp: require('./okhttp')
12+
}

src/targets/kotlin/okhttp.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/**
2+
* @description
3+
* HTTP code snippet generator for Kotlin using OkHttp.
4+
*
5+
* @author
6+
* @seanghay
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 CodeBuilder = require('../../helpers/code-builder')
14+
15+
module.exports = function (source, options) {
16+
var opts = Object.assign({
17+
indent: ' '
18+
}, options)
19+
20+
var code = new CodeBuilder(opts.indent)
21+
22+
var methods = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD']
23+
24+
var methodsWithBody = ['POST', 'PUT', 'DELETE', 'PATCH']
25+
26+
code.push('val client = OkHttpClient()')
27+
.blank()
28+
29+
if (source.postData.text) {
30+
if (source.postData.boundary) {
31+
code.push('val mediaType = MediaType.parse("%s; boundary=%s")', source.postData.mimeType, source.postData.boundary)
32+
} else {
33+
code.push('val mediaType = MediaType.parse("%s")', source.postData.mimeType)
34+
}
35+
code.push('val body = RequestBody.create(mediaType, %s)', JSON.stringify(source.postData.text))
36+
}
37+
38+
code.push('val request = Request.Builder()')
39+
code.push(1, '.url("%s")', source.fullUrl)
40+
if (methods.indexOf(source.method.toUpperCase()) === -1) {
41+
if (source.postData.text) {
42+
code.push(1, '.method("%s", body)', source.method.toUpperCase())
43+
} else {
44+
code.push(1, '.method("%s", null)', source.method.toUpperCase())
45+
}
46+
} else if (methodsWithBody.indexOf(source.method.toUpperCase()) >= 0) {
47+
if (source.postData.text) {
48+
code.push(1, '.%s(body)', source.method.toLowerCase())
49+
} else {
50+
code.push(1, '.%s(null)', source.method.toLowerCase())
51+
}
52+
} else {
53+
code.push(1, '.%s()', source.method.toLowerCase())
54+
}
55+
56+
// Add headers, including the cookies
57+
var headers = Object.keys(source.allHeaders)
58+
59+
// construct headers
60+
if (headers.length) {
61+
headers.forEach(function (key) {
62+
code.push(1, '.addHeader("%s", "%s")', key, source.allHeaders[key])
63+
})
64+
}
65+
66+
code.push(1, '.build()')
67+
.blank()
68+
.push('val response = client.newCall(request).execute()')
69+
70+
return code.join()
71+
}
72+
73+
module.exports.info = {
74+
key: 'okhttp',
75+
title: 'OkHttp',
76+
link: 'http://square.github.io/okhttp/',
77+
description: 'An HTTP Request Client Library'
78+
}

0 commit comments

Comments
 (0)