fury
API Description SDK
Last updated a month ago by kylef .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install fury 
SYNC missed versions from official npm registry.

Fury.js

NPM version License

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:

import fury from 'fury';
import apibParser from 'fury-adapter-apib-parser';

// The input as a string
const source = 'FORMAT: 1A\n# My API\n...';

// Use the API Blueprint parser adapter
fury.use(apibParser);

// Parse the input and print 'My API'
const parseResult = await fury.parse({ source });
console.log(parseResult.api.title);

Once you have a parsed API it is easy to traverse:

parseResult.api.resourceGroups.forEach(function (resourceGroup) {
  console.log(resourceGroup.title);

  resourceGroup.resources.forEach(function (resource) {
    console.log(resource.title);

    resource.transitions.forEach(function (transition) {
      console.log(transition.title);

      transition.transactions.forEach(function (transaction) {
        const request = transaction.request;
        const response = transaction.response;

        console.log(`${request.method} ${request.href}`);
        console.log(`${response.statusCode} (${response.header('Content-Type')})`);
        console.log(response.messageBody);
      });
    });
  });
});

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}
 */
function filterFunc(item) {
  if (item.element === 'httpRequest' && item.statusCode === 200) {
    return true;
  }

  return false;
}

console.log('All API request URIs:');

api.find(filterFunc).forEach(function (request) {
  console.log(`${request.method} ${request.href}`)
});

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:

import {Fury} from 'fury';

const fury1 = new Fury();
const fury2 = new Fury();

await fury1.parse(...);

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:

export const mediaTypes = ['text/vnd.my-adapter'];

If you need to distinguish among supported input Media Types for methods use:

export 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:

export const name = 'my-adapter';
export const mediaTypes = ['text/vnd.my-adapter'];

export function detect(source[, method]) {
  // 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.match(/some-test/i) !== null;
}

export async function parse({minim, generateSourceMap, mediaType, source}) {
  // Here you convert the source into refract elements. Use the `minim`
  // variable to access refract element classes.
  const Resource = minim.getElementByClass('resource');
  // ...
  return elements;
}

export async function validate({minim, mediaType, source}) {
  // 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;
}

export async function serialize({api, mediaType, minim}) {
  // Here you convert `api` from javascript element objects to the serialized
  // source format.
  // ...
  return outputString;
}

export default {name, mediaTypes, detect, parse, serialize};

Now you can register your adapter with Fury.js:

import fury from 'fury';
import myAdapter from './my-adapter';

// Register my custom adapter
fury.use(myAdapter);

// Now parse my custom input format!
const parseResult = await fury.parse({ source: 'some-test\n...' });
console.log(parseResult.api.title);

Current Tags

  • 3.0.0-beta.14                                ...           latest (a month ago)

46 Versions

  • 3.0.0-beta.14                                ...           a month ago
  • 3.0.0-beta.13                                ...           6 months ago
  • 3.0.0-beta.12                                ...           a year ago
  • 3.0.0-beta.11                                ...           a year ago
  • 3.0.0-beta.10                                ...           a year ago
  • 3.0.0-beta.9                                ...           a year ago
  • 3.0.0-beta.8                                ...           a year ago
  • 3.0.0-beta.7                                ...           2 years ago
  • 3.0.0-beta.6                                ...           2 years ago
  • 3.0.0-beta.5                                ...           3 years ago
  • 3.0.0-beta.4                                ...           3 years ago
  • 3.0.0-beta.3                                ...           3 years ago
  • 3.0.0-beta.2                                ...           3 years ago
  • 3.0.0-beta.1                                ...           3 years ago
  • 3.0.0-beta.0                                ...           3 years ago
  • 2.3.0                                ...           4 years ago
  • 2.2.0                                ...           4 years ago
  • 2.1.0                                ...           4 years ago
  • 2.0.0                                ...           4 years ago
  • 1.0.3                                ...           4 years ago
  • 1.0.2                                ...           4 years ago
  • 1.0.1                                ...           4 years ago
  • 1.0.0                                ...           5 years ago
  • 0.9.0                                ...           5 years ago
  • 0.8.6                                ...           5 years ago
  • 0.8.5                                ...           5 years ago
  • 0.8.4-refract1                                ...           5 years ago
  • 0.8.4-refract                                ...           5 years ago
  • 0.8.4                                ...           5 years ago
  • 0.8.3                                ...           5 years ago
  • 0.8.2                                ...           5 years ago
  • 0.8.1                                ...           5 years ago
  • 0.8.0                                ...           5 years ago
  • 0.7.2                                ...           5 years ago
  • 0.7.1                                ...           5 years ago
  • 0.7.0                                ...           5 years ago
  • 0.6.0                                ...           5 years ago
  • 0.4.4                                ...           5 years ago
  • 0.4.3                                ...           5 years ago
  • 0.4.2                                ...           5 years ago
  • 0.4.1                                ...           5 years ago
  • 0.4.0                                ...           5 years ago
  • 0.3.1                                ...           5 years ago
  • 0.3.0                                ...           5 years ago
  • 0.2.0                                ...           5 years ago
  • 0.1.0                                ...           5 years ago
Downloads
Today 2
This Week 4
This Month 24
Last Day 2
Last Week 15
Last Month 103
Dependencies (1)
Dev Dependencies (3)

Copyright 2014 - 2016 © taobao.org |