Created
July 10, 2016 22:49
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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