-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.ts
155 lines (134 loc) · 3.7 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import {
Collection,
Item,
ItemGroup,
VariableList,
Property,
Variable
} from 'postman-collection';
const codegen = require('postman-code-generators');
import { readFileSync } from 'fs';
import * as commander from 'commander';
const languageVariantPairs = [
'C#,RestSharp',
'Dart,http',
'cURL,cURL',
'Go,Native',
'HTTP,HTTP',
'Java,OkHttp',
'Java,Unirest',
'JavaScript,Fetch',
'JavaScript,jQuery',
'JavaScript,XHR',
'NodeJs,Native',
'NodeJs,Request',
'NodeJs,Unirest',
'Objective-C,NSURLSession',
'OCaml,Cohttp',
'PHP,cURL',
'PHP,pecl_http',
'PHP,HTTP_Request2',
'PowerShell,RestMethod',
'Python,http.client',
'Python,Requests',
'Ruby,Net:HTTP',
'Shell,Httpie',
'Shell,wget',
'Swift,URLSession'
].map(v => v.toLowerCase());
function parseTuple(
value: string,
dummy: string
): { language: string; variant: string } {
const v = value.trim().toLowerCase();
if (!languageVariantPairs.includes(v)) {
throw `Not a valid <langauge,variant> pair.Try one of:\n${languageVariantPairs.join(
'\n'
)}\n`;
}
const tuple = v.split(',');
console.assert(tuple.length === 2);
return { language: tuple[0], variant: tuple[1] };
}
const program = new commander.Command('generate');
program.version('0.2');
program
.requiredOption(
'-c, --collection <path>',
'Path to the Postman 2.1 Collection JSON'
)
.option(
'-l,--language_variant <tuple>',
'Language,Variant pair to output',
parseTuple,
{ language: 'curl', variant: 'curl' }
)
.option(
'-e,--envvars <path>',
`Path to environment variables exported from Postman. NOTE: Environment variables will not override variables provided in collection`
)
.option('-d, --debug', 'Output additional debugging info');
program.parse(process.argv);
function debugPrint(message: any) {
if (program.debug) {
console.log('DEBUG:', message);
}
}
debugPrint(program.opts());
const collectionPath: string = program['collection'];
const collection = new Collection(
JSON.parse(readFileSync(collectionPath).toString())
);
debugPrint(collection);
const options = {
trimRequestBody: true,
followRedirect: true
};
function isItem(itemG: Item | ItemGroup<Item>): itemG is Item {
return (<Item>itemG).request !== undefined;
}
function isItemGroup(itemG: Item | ItemGroup<Item>): itemG is ItemGroup<Item> {
return (<ItemGroup<Item>>itemG).items !== undefined;
}
function printSnippet(item: Item | ItemGroup<Item>) {
if (isItem(item)) {
codegen.convert(lvp.language, lvp.variant, item.request, options, function(
error: any,
snippet: any
) {
if (error) {
console.error(
'Error trying to generate code for request:',
item.request,
error
);
}
const completeSnippet = collection.variables.replace(
snippet,
environmentVariables
);
const re = /(?:\{\{(.+?)\}\})/g;
const matches = re.exec(completeSnippet);
if (matches && matches.length > 0) {
matches.forEach(m => console.warn(`${m} : Variable not provided`));
}
console.log(completeSnippet);
});
} else if (isItemGroup(item)) {
item.items.all().forEach(printSnippet);
}
}
const environmentVariables = new VariableList(
new Property({ name: 'environmentVariables' }),
[]
);
if (program['envvars']) {
const environment = JSON.parse(readFileSync(program['envvars']).toString());
debugPrint(environment);
environment['values'].forEach((v: { value: string; key: string }) => {
environmentVariables.append(new Variable(v));
});
}
const lvp: { language: string; variant: string } = program['language_variant'];
debugPrint(environmentVariables);
collection.items.all().forEach(printSnippet);