Giter VIP home page Giter VIP logo

fury.js's Introduction

Fury.js

Build Status Test Coverage 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'
fury.parse({source}, function(err, result) {
  console.log(result.api.title);
});

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

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();

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.

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) {
  // 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.
  return source.match(/some-test/i) !== null;
}

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

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

export function serialize({api, minim}, done) {
  // Here you convert `api` from javascript element objects to the serialized
  // source format.
  // ...
  done(null, 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!
fury.parse({source: 'some-test\n...'}, function (err, api) {
  console.log(api.title);
});

Development

Building & Testing

Fury is written in ES6 and is transpiled before publishing. All of the build/test/etc commands are run through npm:

# Run the unit and integration tests
npm test

# Lint the source code
npm run lint

# Generate a coverage report
npm run cover

# Open the HTML report
open coverage/lcov-report/index.html

fury.js's People

Contributors

danielgtaylor avatar pksunkara avatar smizell avatar kylef avatar zdne avatar klokane avatar honzajavorek avatar abtris avatar m-baumgartner avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.