Skip to content

Commit b7f2da1

Browse files
author
Omri Peleg
committed
WIP
1 parent 38c668d commit b7f2da1

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

src/targets/rapidql/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: 'rapidql',
6+
title: 'RapidQL',
7+
extname: '.js',
8+
default: 'rapidql'
9+
},
10+
11+
rapidql: require('./rapidql'),
12+
}

src/targets/rapidql/rapidql.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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('var axios = require("axios").default;')
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+
default:
51+
if (source.postData.text) {
52+
reqOpts.data = source.postData.text
53+
}
54+
}
55+
56+
code.push('var options = %s;', stringifyObject(reqOpts, { indent: ' ', inlineCharacterLimit: 80 }))
57+
.blank()
58+
59+
code.push(util.format('axios.request(options).then(%s', 'function (response) {'))
60+
.push(1, 'console.log(response.data);')
61+
.push('}).catch(%s', 'function (error) {')
62+
.push(1, 'console.error(error);')
63+
.push('});')
64+
65+
return code.join()
66+
}
67+
68+
module.exports.info = {
69+
key: 'rapidql',
70+
title: 'RapidQL',
71+
link: 'https://github.com/RapidAPI/rapidql',
72+
description: ''
73+
}

0 commit comments

Comments
 (0)