Fury.js
API Description SDK
Wardaddy: Best job I ever had.
Fury provides uniform interface to API description formats such as API Blueprint and Swagger.
Note: Fury requires adapters to support parsing and serializing. You will need to install at least one adapter along with Fury. You can find Fury adapters via npm.
Usage
Install
Fury.js is available as an npm module.
$ npm install --save fury
Refract Interface
Fury.js offers an interface based on the Refract Project element specification and makes use of the API description and data structure namespaces. Adapters convert from formats such as API Blueprint into Refract elements and Fury.js exposes these with API-related convenience functionality. For example:
;; // The input as a stringconst source = 'FORMAT: 1A\n# My API\n...'; // Use the API Blueprint parser adapterfury; // Parse the input and print 'My API'const parseResult = await fury;console;
Once you have a parsed API it is easy to traverse:
parseResultapiresourceGroups;
It is also possible to do complex document-wide searching and filtering. For example, to print out a listing of HTTP methods and paths for all defined example requests:
/* * Prints out something like: * * POST /frobs * GET /frobs * GET /frobs/{id} * PUT /frobs/{id} * DELETE /frobs/{id} */ { if itemelement === 'httpRequest' && itemstatusCode === 200 return true; return false;} console; api;
Reference:
Multiple Fury Instances
There may come a day when you need to have multiple Fury instances with different adapters or other options set up in the same program. This is possible via the Fury
class:
; const fury1 = ;const fury2 = ; await fury1;
Writing an Adapter
Adapters convert from an input format such as API Blueprint into refract elements. This allows a single, consistent interface to be used to interact with multiple input API description formats. Writing your own adapter allows you to add support for new input formats.
Note about mediaTypes
: it allows two kinds of definitions. First one is array type. It is intended to "catch all" implementation.
It is useful if your adapter implements only one of fury methods, or if all methods accepts same types of Media Type.
Another possible option is to use object with mapping name of method to array of MediaTypes. It allows better granularity for detection.
Examples:
If your adapter support just one method or all methods supports same kind of input Media Type:
const mediaTypes = 'text/vnd.my-adapter';
If you need to distinguish among supported input Media Types for methods use:
const mediaTypes = parse: 'text/vnd.my-parsing' 'text/vnd.another-supported-parsing' serialize: 'text/vnd.my-serialization';
Adapters are made up of a name, a list of media types, and up to three optional public functions: detect
, parse
, and serialize
. A simple example might look like this:
const name = 'my-adapter';const mediaTypes = 'text/vnd.my-adapter'; { // If no media type is know, then we fall back to auto-detection. Here you // can check the source and see if you think you can parse it. // // optional parmeter `method` give you hint about caller is going to invoke // note that value can be undefined return source !== null;} { // Here you convert the source into refract elements. Use the `minim` // variable to access refract element classes. const Resource = minim; // ... return elements;} { // Here you validate the source and return a parse result for any warnings or // errors. // // NOTE: Implementing `validate` is optional, Fury will fallback to using // `parse` to find warnings or errors. return null;} { // Here you convert `api` from javascript element objects to the serialized // source format. // ... return outputString;} name mediaTypes detect parse serialize;
Now you can register your adapter with Fury.js:
;; // Register my custom adapterfury; // Now parse my custom input format!const parseResult = await fury;console;