OpenAPI-Directory JS demo
// For context on this, see https://httptoolkit.tech/blog/how-to-index-200000-apis/
const { findApi } = require('openapi-directory');
/**
* The openapi-directory npm module exports a findIndex function, that takes a
* URL (without the protocol) and returns the id for that API's OpenAPI specification.
*
* You can load the specification itself with require('openapi-directory/api/<id>').
*
* Let's test it out:
*/
// Some example URLs:
const requestUrls = [
"api.nytimes.com/svc/topstories/v2/travel.json",
"sqs.us-east-2.amazonaws.com/123456789012/MyQueue/?Action=SendMessage&MessageBody=test+message",
"api.github.com/repos/httptoolkit/mockttp",
];
// For each, we look up the id, load the spec, and print the root URL for its documentation:
requestUrls.forEach((url) => {
console.log(url);
// Look up the API spec id from the URL:
const apiId = findApi(url);
// With the id, you can require() a full specification for this API:
const apiSpec = require(`openapi-directory/api/${apiId}`);
// Includes lots of things, e.g. a link straight to the API docs:
console.log(` -> Docs: ${apiSpec.externalDocs.url}`);
});
// This is just the basics info though: OpenAPI specs like these also contain per-endpoint
// info and schemas, detailed request & response examples, and a whole bunch more.
no comments