Skip to content

Instantly share code, notes, and snippets.

@anotheredward
Created July 10, 2016 22:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anotheredward/f967079366158182d0bb40c555836ee0 to your computer and use it in GitHub Desktop.
Save anotheredward/f967079366158182d0bb40c555836ee0 to your computer and use it in GitHub Desktop.
Script for hitting all of the endpoints of a swagger API exposed by a Loopback Application
// Download swagger.json from /explorer/swagger.json
'use strict'
const api = require('./swagger.json')
const rp = require('request-promise')
const apiUrl = 'http://something.com/api'
let requests = []
for (let path of Object.keys(api.paths)) {
for (let method of Object.keys(api.paths[path])) {
requests.push({
method: method.toUpperCase(),
uri: apiUrl + path.replace(/{.*?}/g, '1'),
simple: false,
json: true,
originalURI: `${method} ${path}`
})
}
}
for (let request of requests) {
rp(request)
.then(response => formatOutput(response))
.then(response => console.log(response, request.originalURI))
}
function formatOutput(response) {
if (response.errors)
return response.errors.map(error => `${error.status}: ${error.detail}`).join('\n')
return (response)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment