Skip to content

Commit 3047244

Browse files
committed
Add Prettier; bump deps; bump version
1 parent ce8b59e commit 3047244

15 files changed

+6202
-3291
lines changed

.prettierignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
dist
2+
*.yml
3+
*.md

.prettierrc.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"singleQuote": true
3+
}

index.js

Lines changed: 80 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
* Author: Erik Wittern
77
* License: MIT
88
*/
9-
'use strict'
9+
'use strict';
1010

11-
const OpenAPIToHar = require('./openapi-to-har.js')
12-
const HTTPSnippet = require('httpsnippet')
11+
const OpenAPIToHar = require('./openapi-to-har.js');
12+
const HTTPSnippet = require('httpsnippet');
1313

1414
/**
1515
* Return snippets for endpoint identified using path and method in the given
@@ -18,89 +18,95 @@ const HTTPSnippet = require('httpsnippet')
1818
* @param {object} openApi OpenAPI document
1919
* @param {string} path Path identifying endpoint, e.g., '/users'
2020
* @param {string} method HTTP method identifying endpoint, e.g., 'get'
21-
* @param {array} targets List of languages to create snippets in, e.g,
21+
* @param {array} targets List of languages to create snippets in, e.g,
2222
* ['cURL', 'Node']
2323
* @param {object} values Optional: Values for the query parameters if present
2424
*/
2525
const getEndpointSnippets = function (openApi, path, method, targets, values) {
2626
// if optional parameter is not provided, set it to empty object
2727
if (typeof values === 'undefined') {
28-
values = {}
28+
values = {};
2929
}
3030

31-
const har = OpenAPIToHar.getEndpoint(openApi, path, method, values)
31+
const har = OpenAPIToHar.getEndpoint(openApi, path, method, values);
3232

33-
const snippet = new HTTPSnippet(har)
33+
const snippet = new HTTPSnippet(har);
3434

35-
const snippets = []
35+
const snippets = [];
3636
for (let j in targets) {
37-
const target = formatTarget(targets[j])
38-
if (!target) throw new Error('Invalid target: ' + targets[j])
37+
const target = formatTarget(targets[j]);
38+
if (!target) throw new Error('Invalid target: ' + targets[j]);
3939
snippets.push({
4040
id: targets[j],
4141
title: target.title,
42-
content: snippet.convert(target.language, typeof target.library !== 'undefined' ? target.library : null)
43-
})
42+
content: snippet.convert(
43+
target.language,
44+
typeof target.library !== 'undefined' ? target.library : null
45+
),
46+
});
4447
}
4548

4649
return {
4750
method: har.method,
4851
url: har.url,
4952
description: har.description,
5053
resource: getResourceName(har.url),
51-
snippets: snippets
52-
}
53-
}
54+
snippets: snippets,
55+
};
56+
};
5457

5558
/**
5659
* Return snippets for all endpoints in the given OpenAPI document.
57-
*
60+
*
5861
* @param {object} openApi OpenAPI document
59-
* @param {array} targets List of languages to create snippets in, e.g,
62+
* @param {array} targets List of languages to create snippets in, e.g,
6063
* ['cURL', 'Node']
6164
*/
6265
const getSnippets = function (openApi, targets) {
63-
const harList = OpenAPIToHar.getAll(openApi)
66+
const harList = OpenAPIToHar.getAll(openApi);
6467

65-
const results = []
68+
const results = [];
6669
for (let i in harList) {
6770
// create HTTPSnippet object:
68-
const har = harList[i]
69-
const snippet = new HTTPSnippet(har.har)
71+
const har = harList[i];
72+
const snippet = new HTTPSnippet(har.har);
7073

71-
const snippets = []
74+
const snippets = [];
7275
for (let j in targets) {
73-
const target = formatTarget(targets[j])
74-
if (!target) throw new Error('Invalid target: ' + targets[j])
76+
const target = formatTarget(targets[j]);
77+
if (!target) throw new Error('Invalid target: ' + targets[j]);
7578
snippets.push({
7679
id: targets[j],
7780
title: target.title,
78-
content: snippet.convert(target.language, typeof target.library !== 'undefined' ? target.library : null)
79-
})
81+
content: snippet.convert(
82+
target.language,
83+
typeof target.library !== 'undefined' ? target.library : null
84+
),
85+
});
8086
}
8187

8288
results.push({
8389
method: har.method,
8490
url: har.url,
8591
description: har.description,
8692
resource: getResourceName(har.url),
87-
snippets
88-
})
93+
snippets,
94+
});
8995
}
9096

9197
// sort results:
9298
results.sort((a, b) => {
9399
if (a.resource < b.resource) {
94-
return -1
100+
return -1;
95101
} else if (a.resource > b.resource) {
96-
return 1
102+
return 1;
97103
} else {
98-
return getMethodOrder(a.method.toLowerCase(), b.method.toLowerCase())
104+
return getMethodOrder(a.method.toLowerCase(), b.method.toLowerCase());
99105
}
100-
})
106+
});
101107

102-
return results
103-
}
108+
return results;
109+
};
104110

105111
/**
106112
* Determine the order of HTTP methods.
@@ -110,19 +116,19 @@ const getSnippets = function (openApi, targets) {
110116
* @return {number} The order instruction for the given HTTP verbs
111117
*/
112118
const getMethodOrder = function (a, b) {
113-
const order = ['get', 'post', 'put', 'delete', 'patch']
119+
const order = ['get', 'post', 'put', 'delete', 'patch'];
114120
if (order.indexOf(a) === -1) {
115-
return 1
121+
return 1;
116122
} else if (order.indexOf(b) === -1) {
117-
return -1
123+
return -1;
118124
} else if (order.indexOf(a) < order.indexOf(b)) {
119-
return -1
125+
return -1;
120126
} else if (order.indexOf(a) > order.indexOf(b)) {
121-
return 1
127+
return 1;
122128
} else {
123-
return 0
129+
return 0;
124130
}
125-
}
131+
};
126132

127133
/**
128134
* Determines the name of the resource exposed by the method.
@@ -132,14 +138,14 @@ const getMethodOrder = function (a, b) {
132138
* @return {string} The determined resource name
133139
*/
134140
const getResourceName = function (urlStr) {
135-
const pathComponents = urlStr.split('/')
141+
const pathComponents = urlStr.split('/');
136142
for (let i = pathComponents.length - 1; i >= 0; i--) {
137-
const cand = pathComponents[i]
143+
const cand = pathComponents[i];
138144
if (cand !== '' && !/^{/.test(cand)) {
139-
return cand
145+
return cand;
140146
}
141147
}
142-
}
148+
};
143149

144150
/**
145151
* Format the given target by splitting up language and library and making sure
@@ -149,64 +155,67 @@ const getResourceName = function (urlStr) {
149155
* @return {object} Object with formatted target, or null
150156
*/
151157
const formatTarget = function (targetStr) {
152-
const language = targetStr.split('_')[0]
153-
const title = capitalizeFirstLetter(language)
154-
let library = targetStr.split('_')[1]
158+
const language = targetStr.split('_')[0];
159+
const title = capitalizeFirstLetter(language);
160+
let library = targetStr.split('_')[1];
155161

156-
const validTargets = HTTPSnippet.availableTargets()
157-
let validLanguage = false
158-
let validLibrary = false
162+
const validTargets = HTTPSnippet.availableTargets();
163+
let validLanguage = false;
164+
let validLibrary = false;
159165
for (let i in validTargets) {
160-
const target = validTargets[i]
166+
const target = validTargets[i];
161167
if (language === target.key) {
162-
validLanguage = true
168+
validLanguage = true;
163169
if (typeof library === 'undefined') {
164-
library = target.default
165-
validLibrary = true
170+
library = target.default;
171+
validLibrary = true;
166172
} else {
167173
for (let j in target.clients) {
168-
const client = target.clients[j]
174+
const client = target.clients[j];
169175
if (library === client.key) {
170-
validLibrary = true
171-
break
176+
validLibrary = true;
177+
break;
172178
}
173179
}
174180
}
175181
}
176182
}
177183

178184
if (!validLanguage || !validLibrary) {
179-
return null
185+
return null;
180186
}
181187

182188
return {
183-
title: typeof library !== 'undefined' ? title + ' + ' + capitalizeFirstLetter(library) : title,
189+
title:
190+
typeof library !== 'undefined'
191+
? title + ' + ' + capitalizeFirstLetter(library)
192+
: title,
184193
language,
185-
library
186-
}
187-
}
194+
library,
195+
};
196+
};
188197

189198
const capitalizeFirstLetter = function (string) {
190-
return string.charAt(0).toUpperCase() + string.slice(1)
191-
}
199+
return string.charAt(0).toUpperCase() + string.slice(1);
200+
};
192201

193202
module.exports = {
194203
getSnippets,
195-
getEndpointSnippets
196-
}
204+
getEndpointSnippets,
205+
};
197206

198207
// The if is only for when this is run from the browser
199208
if (typeof window !== 'undefined') {
200209
// grab existing namespace object, or create a blank object
201210
// if it doesn't exist
202-
let OpenAPISnippets = window.OpenAPISnippets || {}
211+
let OpenAPISnippets = window.OpenAPISnippets || {};
203212

204213
// define that object
205214
OpenAPISnippets = {
206215
getSnippets,
207-
getEndpointSnippets
208-
}
216+
getEndpointSnippets,
217+
};
209218

210219
// replace/create the global namespace
211-
window.OpenAPISnippets = OpenAPISnippets
220+
window.OpenAPISnippets = OpenAPISnippets;
212221
}

0 commit comments

Comments
 (0)