Skip to content

Commit

Permalink
Add Prettier; bump deps; bump version
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikWittern committed May 17, 2021
1 parent ce8b59e commit 3047244
Show file tree
Hide file tree
Showing 15 changed files with 6,202 additions and 3,291 deletions.
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dist
*.yml
*.md
3 changes: 3 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"singleQuote": true
}
151 changes: 80 additions & 71 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
* Author: Erik Wittern
* License: MIT
*/
'use strict'
'use strict';

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

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

const har = OpenAPIToHar.getEndpoint(openApi, path, method, values)
const har = OpenAPIToHar.getEndpoint(openApi, path, method, values);

const snippet = new HTTPSnippet(har)
const snippet = new HTTPSnippet(har);

const snippets = []
const snippets = [];
for (let j in targets) {
const target = formatTarget(targets[j])
if (!target) throw new Error('Invalid target: ' + targets[j])
const target = formatTarget(targets[j]);
if (!target) throw new Error('Invalid target: ' + targets[j]);
snippets.push({
id: targets[j],
title: target.title,
content: snippet.convert(target.language, typeof target.library !== 'undefined' ? target.library : null)
})
content: snippet.convert(
target.language,
typeof target.library !== 'undefined' ? target.library : null
),
});
}

return {
method: har.method,
url: har.url,
description: har.description,
resource: getResourceName(har.url),
snippets: snippets
}
}
snippets: snippets,
};
};

/**
* Return snippets for all endpoints in the given OpenAPI document.
*
*
* @param {object} openApi OpenAPI document
* @param {array} targets List of languages to create snippets in, e.g,
* @param {array} targets List of languages to create snippets in, e.g,
* ['cURL', 'Node']
*/
const getSnippets = function (openApi, targets) {
const harList = OpenAPIToHar.getAll(openApi)
const harList = OpenAPIToHar.getAll(openApi);

const results = []
const results = [];
for (let i in harList) {
// create HTTPSnippet object:
const har = harList[i]
const snippet = new HTTPSnippet(har.har)
const har = harList[i];
const snippet = new HTTPSnippet(har.har);

const snippets = []
const snippets = [];
for (let j in targets) {
const target = formatTarget(targets[j])
if (!target) throw new Error('Invalid target: ' + targets[j])
const target = formatTarget(targets[j]);
if (!target) throw new Error('Invalid target: ' + targets[j]);
snippets.push({
id: targets[j],
title: target.title,
content: snippet.convert(target.language, typeof target.library !== 'undefined' ? target.library : null)
})
content: snippet.convert(
target.language,
typeof target.library !== 'undefined' ? target.library : null
),
});
}

results.push({
method: har.method,
url: har.url,
description: har.description,
resource: getResourceName(har.url),
snippets
})
snippets,
});
}

// sort results:
results.sort((a, b) => {
if (a.resource < b.resource) {
return -1
return -1;
} else if (a.resource > b.resource) {
return 1
return 1;
} else {
return getMethodOrder(a.method.toLowerCase(), b.method.toLowerCase())
return getMethodOrder(a.method.toLowerCase(), b.method.toLowerCase());
}
})
});

return results
}
return results;
};

/**
* Determine the order of HTTP methods.
Expand All @@ -110,19 +116,19 @@ const getSnippets = function (openApi, targets) {
* @return {number} The order instruction for the given HTTP verbs
*/
const getMethodOrder = function (a, b) {
const order = ['get', 'post', 'put', 'delete', 'patch']
const order = ['get', 'post', 'put', 'delete', 'patch'];
if (order.indexOf(a) === -1) {
return 1
return 1;
} else if (order.indexOf(b) === -1) {
return -1
return -1;
} else if (order.indexOf(a) < order.indexOf(b)) {
return -1
return -1;
} else if (order.indexOf(a) > order.indexOf(b)) {
return 1
return 1;
} else {
return 0
return 0;
}
}
};

/**
* Determines the name of the resource exposed by the method.
Expand All @@ -132,14 +138,14 @@ const getMethodOrder = function (a, b) {
* @return {string} The determined resource name
*/
const getResourceName = function (urlStr) {
const pathComponents = urlStr.split('/')
const pathComponents = urlStr.split('/');
for (let i = pathComponents.length - 1; i >= 0; i--) {
const cand = pathComponents[i]
const cand = pathComponents[i];
if (cand !== '' && !/^{/.test(cand)) {
return cand
return cand;
}
}
}
};

/**
* Format the given target by splitting up language and library and making sure
Expand All @@ -149,64 +155,67 @@ const getResourceName = function (urlStr) {
* @return {object} Object with formatted target, or null
*/
const formatTarget = function (targetStr) {
const language = targetStr.split('_')[0]
const title = capitalizeFirstLetter(language)
let library = targetStr.split('_')[1]
const language = targetStr.split('_')[0];
const title = capitalizeFirstLetter(language);
let library = targetStr.split('_')[1];

const validTargets = HTTPSnippet.availableTargets()
let validLanguage = false
let validLibrary = false
const validTargets = HTTPSnippet.availableTargets();
let validLanguage = false;
let validLibrary = false;
for (let i in validTargets) {
const target = validTargets[i]
const target = validTargets[i];
if (language === target.key) {
validLanguage = true
validLanguage = true;
if (typeof library === 'undefined') {
library = target.default
validLibrary = true
library = target.default;
validLibrary = true;
} else {
for (let j in target.clients) {
const client = target.clients[j]
const client = target.clients[j];
if (library === client.key) {
validLibrary = true
break
validLibrary = true;
break;
}
}
}
}
}

if (!validLanguage || !validLibrary) {
return null
return null;
}

return {
title: typeof library !== 'undefined' ? title + ' + ' + capitalizeFirstLetter(library) : title,
title:
typeof library !== 'undefined'
? title + ' + ' + capitalizeFirstLetter(library)
: title,
language,
library
}
}
library,
};
};

const capitalizeFirstLetter = function (string) {
return string.charAt(0).toUpperCase() + string.slice(1)
}
return string.charAt(0).toUpperCase() + string.slice(1);
};

module.exports = {
getSnippets,
getEndpointSnippets
}
getEndpointSnippets,
};

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

// define that object
OpenAPISnippets = {
getSnippets,
getEndpointSnippets
}
getEndpointSnippets,
};

// replace/create the global namespace
window.OpenAPISnippets = OpenAPISnippets
window.OpenAPISnippets = OpenAPISnippets;
}
Loading

0 comments on commit 3047244

Please sign in to comment.