Skip to content

Commit c938558

Browse files
authored
Common functions required for app and documenter (#15)
* Common repo functions for documenter integration * Added postman-collection in dependencies * Updated common functions to incorporate pre-package script * Updated pre-package.js, changed syntax_mode for golang, powershell, ruby and remove require statement from functions * Included homepage urls for respective codegens and added it to the languages.js pre-package script to be accessible by common functions * Updated repository test
1 parent a56f974 commit c938558

21 files changed

+598
-20
lines changed

.eslintrc

+1-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@
168168
"id-blacklist": "error",
169169
"id-length": "off",
170170
"id-match": "error",
171-
"indent": ["error", 4, {
171+
"indent": ["error", 2, {
172172
"VariableDeclarator": { "var": 1, "let": 1, "const": 1 },
173173
"SwitchCase": 1
174174
}],

.npmignore

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
### NPM Specific: Disregard recursive project files
2+
### ===============================================
3+
/.editorconfig
4+
/.gitmodules
5+
/test
6+
7+
### Borrowed from .gitignore
8+
### ========================
9+
10+
# Logs
11+
logs
12+
*.log
13+
npm-debug.log*
14+
yarn-debug.log*
15+
yarn-error.log*
16+
17+
# Runtime data
18+
pids
19+
*.pid
20+
*.seed
21+
*.pid.lock
22+
23+
# Prevent IDE stuff
24+
.idea
25+
.vscode
26+
*.sublime-*
27+
28+
# Directory for instrumented libs generated by jscoverage/JSCover
29+
lib-cov
30+
31+
# Coverage directory used by tools like istanbul
32+
.coverage
33+
34+
# nyc test coverage
35+
.nyc_output
36+
37+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
38+
.grunt
39+
40+
# Bower dependency directory (https://bower.io/)
41+
bower_components
42+
43+
# node-waf configuration
44+
.lock-wscript
45+
46+
# Compiled binary addons (http://nodejs.org/api/addons.html)
47+
build/Release
48+
49+
# Dependency directories
50+
node_modules/
51+
jspm_packages/
52+
53+
# Typescript v1 declaration files
54+
typings/
55+
56+
# Optional npm cache directory
57+
.npm
58+
59+
# Optional eslint cache
60+
.eslintcache
61+
62+
# Optional REPL history
63+
.node_repl_history
64+
65+
# Output of 'npm pack'
66+
*.tgz
67+
68+
# Yarn Integrity file
69+
.yarn-integrity
70+
71+
# dotenv environment variables file
72+
.env
73+
74+
out/

codegens/.gitkeep

Whitespace-only changes.

codegens/curl/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
},
2121
"author": "Postman Labs <help@getpostman.com>",
2222
"license": "Apache-2.0",
23-
"homepage": "",
23+
"homepage": "https://github.com/postmanlabs/code-generators/tree/master/codegens/curl",
2424
"dependencies": {},
2525
"devDependencies": {
2626
"async": "2.6.0",

codegens/golang/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"type": "code_generator",
88
"lang": "go",
99
"variant": "native",
10-
"syntax_mode": "go"
10+
"syntax_mode": "golang"
1111
},
1212
"directories": {
1313
"lib": "lib",
@@ -25,7 +25,7 @@
2525
},
2626
"author": "Postman Labs <help@getpostman.com>",
2727
"license": "Apache-2.0",
28-
"homepage": "",
28+
"homepage": "https://github.com/postmanlabs/code-generators/tree/master/codegens/golang",
2929
"dependencies": {},
3030
"devDependencies": {
3131
"async": "2.6.0",

codegens/js-fetch/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
},
2525
"author": "Postman Labs <help@getpostman.com>",
2626
"license": "Apache-2.0",
27+
"homepage": "https://github.com/postmanlabs/code-generators/tree/master/codegens/js-fetch",
2728
"dependencies": {},
2829
"devDependencies": {
2930
"async": "2.6.0",

codegens/js-jquery/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
},
2525
"author": "Postman Labs <help@getpostman.com>",
2626
"license": "Apache-2.0",
27-
"homepage": "",
27+
"homepage": "https://github.com/postmanlabs/code-generators/tree/master/codegens/js-jquery",
2828
"dependencies": {},
2929
"devDependencies": {
3030
"async": "2.5.0",

codegens/nodejs-native/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
},
2525
"author": "Postman Labs <help@getpostman.com>",
2626
"license": "Apache-2.0",
27+
"homepage": "https://github.com/postmanlabs/code-generators/tree/master/codegens/nodejs-native",
2728
"dependencies": {},
2829
"devDependencies": {
2930
"async": "2.5.0",

codegens/php-curl/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
},
2626
"author": "Postman Labs <help@getpostman.com>",
2727
"license": "Apache-2.0",
28-
"homepage": "",
28+
"homepage": "https://github.com/postmanlabs/code-generators/tree/master/codegens/php-curl",
2929
"dependencies": {},
3030
"devDependencies": {
3131
"async": "2.5.0",

codegens/powershell-restmethod/package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
"description": "Converts postman request to powershell rest method",
55
"com_postman_plugin": {
66
"type": "code_generator",
7-
"lang": "bash",
8-
"variant": "powershell - rest method",
9-
"syntax_mode": "bash"
7+
"lang": "powershell",
8+
"variant": "restmethod",
9+
"syntax_mode": "powershell"
1010
},
1111
"directories": {
1212
"lib": "lib",
@@ -24,7 +24,7 @@
2424
},
2525
"author": "Postman Labs <help@getpostman.com>",
2626
"license": "Apache-2.0",
27-
"homepage": "",
27+
"homepage": "https://github.com/postmanlabs/code-generators/tree/master/codegens/powershell-restmethod",
2828
"dependencies": {},
2929
"devDependencies": {
3030
"async": "2.6.0",

codegens/python-requests/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
},
2626
"author": "Postman Labs <help@getpostman.com>",
2727
"license": "Apache-2.0",
28-
"homepage": "",
28+
"homepage": "https://github.com/postmanlabs/code-generators/tree/master/codegens/python-requests",
2929
"dependencies": {},
3030
"devDependencies": {
3131
"async": "2.5.0",

codegens/ruby/package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"com_postman_plugin": {
66
"type": "code_generator",
77
"lang": "Ruby",
8-
"variant": "net/http"
8+
"variant": "net/http",
9+
"syntax_mode": "ruby"
910
},
1011
"main": "index.js",
1112
"directories": {
@@ -23,7 +24,7 @@
2324
},
2425
"author": "Postman Labs <help@getpostman.com>",
2526
"license": "Apache-2.0",
26-
"homepage": "",
27+
"homepage": "https://github.com/postmanlabs/code-generators/tree/master/codegens/ruby",
2728
"dependencies": {},
2829
"devDependencies": {
2930
"async": "2.5.0",

codegens/swift/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
},
2626
"author": "Postman Labs <help@getpostman.com>",
2727
"license": "Apache-2.0",
28-
"homepage": "",
28+
"homepage": "https://github.com/postmanlabs/code-generators/tree/master/codegens/swift",
2929
"dependencies": {},
3030
"devDependencies": {
3131
"async": "2.6.0",

index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1+
module.exports = require('./lib');

lib/assets/languageLabels.json

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"bash": "bash",
3+
"curl": "cURL",
4+
"javascript": "JavaScript",
5+
"http": "HTTP",
6+
"python": "Python",
7+
"ruby": "Ruby",
8+
"java": "Java",
9+
"c": "C",
10+
"php": "PHP",
11+
"objective-c": "Objective-C",
12+
"perl": "Perl",
13+
"go": "Go",
14+
"swift": "Swift",
15+
"typescript": "TypeScript",
16+
"powershell": "Powershell",
17+
"csharp": "C#",
18+
"nodejs": "NodeJs",
19+
"ocaml": "OCaml",
20+
"shell": "Shell"
21+
}

lib/index.js

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
var sdk = require('postman-collection'),
2+
labelList = require('./assets/languageLabels.json'),
3+
languageMap = require('./assets/languages.js');
4+
5+
module.exports = {
6+
/**
7+
* Gets the options specific to a given language.
8+
*
9+
* @param {Object} language - language key provided by getLanguageList function
10+
* @param {Array} variant - variant key provided by getLanguageList function
11+
* @param {Function} callback - callback function with arguments (error, object)
12+
*/
13+
getOptions (language, variant, callback) {
14+
var validCodegen = languageMap.filter((codegen) => {
15+
var lang = codegen.lang.trim(),
16+
currentVariant = codegen.variant.trim();
17+
return language === lang.toLowerCase() && variant.toLowerCase() === currentVariant.toLowerCase();
18+
});
19+
20+
validCodegen.forEach((codegen) => {
21+
main = codegen.main;
22+
if (typeof main.getOptions !== 'function') {
23+
return callback('Codegen~getOptions: getOptions is not a function');
24+
}
25+
if (!main.getOptions) {
26+
return callback('Codegen~convert: Could not find condegen corresponding to provided language, variant pair');
27+
}
28+
29+
return callback(null, main.getOptions());
30+
});
31+
},
32+
33+
/**
34+
* Returns an object of supported languages
35+
*
36+
*/
37+
getLanguageList () {
38+
var langMap = {},
39+
supportedLanguages = [];
40+
languageMap.forEach((codegen) => {
41+
var lang = codegen.lang.trim(),
42+
syntax_mode = codegen.syntax_mode.trim(),
43+
variant = codegen.variant.trim();
44+
lang = lang.toLowerCase();
45+
if (!langMap[lang]) {
46+
langMap[lang] = {
47+
key: lang,
48+
label: labelList[lang] ? labelList[lang] : lang,
49+
syntax_mode: syntax_mode.toLowerCase(),
50+
variants: [
51+
{
52+
key: variant
53+
}
54+
]
55+
};
56+
}
57+
else {
58+
langMap[lang].variants.push({
59+
key: variant
60+
});
61+
}
62+
});
63+
64+
supportedLanguages = Object.keys(langMap).map(function (lang) {
65+
return langMap[lang];
66+
});
67+
68+
return supportedLanguages;
69+
},
70+
71+
/**
72+
* Converts a request to a preferred language snippet
73+
*
74+
* @param {Object} language - language key provided by getLanguageList function
75+
* @param {Array} variant - variant key provided by getLanguageList function
76+
* @param {String} request - valid postman request
77+
* @param {Object} [options] - contains convert level options
78+
* @param {Number} [options.indentType] - indentation based on tab or spaces
79+
* @param {Number} [options.indentCount] - count/frequency of indentType
80+
* @param {Number} [options.requestTimeout] : time in milli-seconds after which request will bail out
81+
* @param {Boolean} [options.requestBodyTrim] : whether to trim request body fields
82+
* @param {Boolean} [options.addCacheHeader] : whether to add cache-control header to postman SDK-request
83+
* @param {Boolean} [options.followRedirect] : whether to allow redirects of a request
84+
* @param {Function} callback - callback function with arguments (error, string)
85+
*/
86+
convert (language, variant, request, options, callback) {
87+
var convert, main;
88+
89+
if (!sdk.Request.isRequest(request)) {
90+
return callback('Codegen~convert: Invalid request');
91+
}
92+
93+
languageMap.forEach((codegen) => {
94+
var lang = codegen.lang.trim(),
95+
currentVariant = codegen.variant.trim();
96+
if (language.toLowerCase() === lang.toLowerCase() && variant.toLowerCase() === currentVariant.toLowerCase()) {
97+
main = codegen.main;
98+
convert = main.convert;
99+
100+
if (typeof convert !== 'function') {
101+
return callback('Codegen~convert: Convert is not a function');
102+
}
103+
}
104+
});
105+
if (!convert) {
106+
return callback('Codegen~convert: Could not find condegen corresponding to provided language, variant pair');
107+
}
108+
109+
110+
convert(request, options, function (err, snippet) {
111+
if (err) {
112+
return callback(err);
113+
}
114+
115+
return callback(null, snippet);
116+
});
117+
}
118+
};

npm/deepinstall.sh

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
#!/bin/bash
22
set -e; # stop on error
3+
4+
echo "Running pre-package script"
5+
node npm/pre-package.js
6+
echo "Run successful languages.js saved in lib/assets"
7+
38
if [ -n "$1" ];
49
then
510
CODEGEN=$1 # Code gen where npm install is desired to run , first input argument

0 commit comments

Comments
 (0)