パッケージの詳細

@apielements/core

apiaryio23.8kMIT0.2.1

API Description SDK

readme

API Elements: Core

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 @apielements/core

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 '@apielements/core';
import apibParser from '@apielements/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 '@apielements/core';

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 four optional public functions: detect, parse, serialize and serializeSync. 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 function serializeSync({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, serializeSync};

Now you can register your adapter with Fury.js:

import fury from '@apielements/core';
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);

更新履歴

API Elements: Core

0.2.1 (2020-08-27)

Enhancements

Added serializeSync method to Fury.

0.2.0 (2020-08-05)

This package updates the version of api-elements being used. See api-elements@0.2.0 for more details on the contents of the change.

0.1.0 (2020-06-12)

The package has been renamed to @apielements/core.

3.0.0-beta.14 (2020-04-20)

This release contains an updated api-elements models. See api-elements 0.2.4 for further details.

3.0.0-beta.13 (2019-12-06)

This release contains internal changes needed for included Fury adapters.

3.0.0-beta.12 (2019-07-02)

This release contains internal changes needed to improve the performance for parsing OpenAPI 3.0 documents.

3.0.0-beta.11 (2019-06-11)

Breaking

  • The interface between Fury and adapters now uses promises. Any adapters need to be updated to use a promise interface.

  • The minim option passed down to adapters has been renamed to namespace. The underlying value is a namespace from minim.

  • Support for NodeJS 6 has been removed, upgrading to NodeJS 8 or newer is

Enhancements

  • Fury can now be used with promises or async/await. For example:

    const parseResult = await fury.parse({ source: '# Hello World' });

3.0.0-beta.10 (2019-03-26)

Breaking

  • Fury will no longer catch exceptions thrown by an adapter during a parse, or serialize.
    #158

Enhancement

  • Fury now pass into operations parse(), validate(), serialize() mediaType as one of adapter options
  • Fury now allows distinguish among Media Types for individual oprations (for more info see README)

3.0.0-beta.9 (2019-02-26)

Breaking

  • Fury asynchronous APIs will no longer include both an error and a result value. The APIs will now contain an error if the method could not proceed, or a result. In the case of the validate and parse functions, these will return a parse result with any error annotations upon a validation error and will no longer include an error in the asynchronous callback.

Enhancements

3.0.0-beta.8 (2018-12-21)

Breaking

  • Node 4 is not support anymore.

Enhancements

3.0.0-beta.7

  • Update minim to 0.20.5.

3.0.0-beta.6

  • Update minim to 0.20.1.
  • Update minim-parse-result to 0.10.0.

3.0.0-beta.5

  • Added browser build distribution

3.0.0-beta.4

Breaking

Fury was updated to use Minim 0.19.

3.0.0-beta.3

Enhancements

  • Fury contains a detect method which takes an API Description source and returns the registered adapters which can handle the API Description source.
  • Updates to minim 0.18.0 which updates API Element and Refract serialisation.

3.0.0-beta.2 (2017-06-29)

  • Update minim to 0.17.1
  • Update minim-parse-result to 0.6.1

3.0.0-beta.1 (2017-05-12)

  • Update minim to 0.16.0
  • Update minim-parse-result to 0.5.0

3.0.0-beta.0 (2017-04-10)

  • Upgrade babel-runtime dependency to v6
  • Drop support for node 0.10 and 0.12
  • Upgraded minim to 0.15.0
  • Upgraded minim-parse-result to 0.4.0

2.3.0 (2016-10-24)

Enhancements

  • Fury will now allow you to validate an API Description.

2.2.0 (2016-09-01)

Enhancements

  • Fury will now allow you to pass down parser options down to the underlying parse adapter using the adapterOptions parameter.

2.1.0 (2016-05-24)

  • Elements returned from parse will always be an instance of minim elements

2.0.0 (2016-04-28)

  • Upgrade Minim to 0.14.0 and thus remove the short hand notation in fury
  • Upgrade Minim Parse Result to 0.2.2

1.0.3 (2015-12-03)

  • Dependency update to support require support for the links element.
  • When a parser returns an error it is sometimes useful to inspect the parse result. This now gets passed back to the handler function and can be used to print more information, such as parser annotations, when an error occurs.

1.0.2 (2015-11-30)

  • Upgrade Minim Parse Result to 0.2.1

1.0.1 (2015-11-30)

  • Upgrade Minim to 0.12.3

1.0.0 (2015-11-17)

  • Remove legacy interface. The only available interface is now the Refract interface. In order to use it, you must load adapter modules to handle particular formats.

0.8.4 (2015-07-30)

This release updates the underlying Minim library for working with Refract elements and fixes a bug.

  • Refract: Update to Minim 0.9.0
  • Legacy AST: Fix a bug related to the action's URI template not having precedence over the resource URI. #40

0.8.3 (2015-07-02)

This release brings couple fixes to how Swagger Adapter and API Blueprint Adapter operate.

  • Swagger: Add support for arrays in query parameters
  • Swagger: Handle transitions with no responses
  • API Blueprint: Handle transitions with no requests and responses

0.8.2 (2015-06-26)

This release brings couple fixes to how Swagger Adapter and API Blueprint Adapter operate.

  • Swagger: Handle response schemas in Swagger 2.0
  • Swagger: Ignore default responses
  • Swagger: Handle description of transitions
  • API Blueprint: Reduce new lines in rendered output.

Drafter.js v0.2.5

0.8.1 (2015-06-19)

0.8.0 (2015-06-16)

  • Expose a new Refract-based interface through fury.parse, fury.load, and fury.serialize. This is a work in progress.
  • Add a Swagger parser.
  • Add an API Blueprint serializer with basic MSON support.
  • Update the codebase to make use of ES6 features.

0.7.1 (2015-04-10)

This release updates drafter.js to v0.2.3


Drafter.js v0.2.3

0.7.0 (2015-03-31)

This release exposes Relations and URI Template for Actions to legacy interface

0.6.0 (2015-03-26)

This release introduces API Blueprint Attributes in the legacy blueprint interface.

0.4.4 (2015-03-10)

This release removes superfluous logging using console.log.

0.4.2 (2015-03-09)

This release brings a fix to API Blueprint version matching regex for UTF8 files starting with BOM.

0.3.0 (2015-03-02)

Removed unnecessary dependencies – request.js

0.2.0 (2015-03-02)

Re-introduced parser timeouts

0.1.0 (2015-02-20)

This release provides only legacy API Blueprint / Apiary Blueprint parser & API interface