包详细信息

protagonist

apiaryio31.2kMIT2.3.0

API Blueprint Parser

自述文件

logo

Protagonist - API Blueprint Parser for Node.js

AppVeyor

Protagonist is a Node.js wrapper for the Drafter, an API Blueprint parser. API Blueprint is a language for describing web APIs.

Install

NOTE: For general use we recommend that you use the Drafter NPM package instead of Protagonist directly as Protagonist needs to be compiled which may not be possible in every situation.

Protagonist can be installed via the Protagonist npm package by npm or yarn.

$ npm install protagonist
# or
$ yarn install protagonist

Protagonist uses the node-gyp build tool which requires Python 2.7 (3.x is not supported) along with a compiler and other build tools. Take a look at their installation steps for Linux, macOS, and Windows.

Usage

Protagonist offers the ability to both validate, and parse an API Blueprint. It offers the following APIs:

NOTE: It is not recommended to use the synchronous APIs as they can block the Node.JS event loop.

Validating an API Blueprint

You can validate an API Blueprint to determine if the source is a valid document. The parse result will contain any errors or warnings that the document would emit during parsing.

const protagonist = require('protagonist');

const parseResult = await protagonist.parse('# My API');
console.log(JSON.stringify(parseResult, null, 2));

or by using Promises:

const protagonist = require('protagonist');

protagonist.validate('# My API')
  .then((parseResult) => {
    console.log(JSON.stringify(parseResult, null, 2));
  })
  .catch((error) => {
    console.error(error);
  });

See the parse result section below for more information about the structure of the parse result.

Synchronous API

const protagonist = require('protagonist');
const parseResult = protagonist.validateSync('# My API');

Validation Options

Options can be passed to the parser as an optional second argument to both the asynchronous and synchronous interfaces:

const protagonist = require('protagonist');

const options = {
  requireBlueprintName: true,
};
const parseResult = await protagonist.validate('# My API', options);

The available options are:

Name Description
requireBlueprintName Require parsed blueprints have a title (default: false)

Parsing an API Blueprint

You can parse an API Blueprint with async/await:

const protagonist = require('protagonist');

const parseResult = await protagonist.parse('# My API');
console.log(JSON.stringify(parseResult, null, 2));

or by using Promises:

const protagonist = require('protagonist');

protagonist.parse('# My API')
  .then((parseResult) => {
    console.log(JSON.stringify(parseResult, null, 2));
  })
  .catch((error) => {
    console.error(error);
  });

See the parse result section below for more information about the structure of the parse result.

Synchronous API

const parseResult = protagonist.parseSync('# My API');

Parsing Options

Options can be passed to the parser as an optional second argument to both the asynchronous and synchronous interfaces:

const options = {
  generateSourceMap: true,
  generateMessageBody: true,
  generateMessageBodySchema: true,
};
const parseResult = await protagonist.parse('# My API', options);

The available options are:

Name Description
requireBlueprintName Require parsed blueprints have a title (default: false)
generateSourceMap Enable sourcemap generation (default: false)
generateMessageBody Enable generation of messageBody from MSON (default: true)
generateMessageBodySchema Enable generation of messageBodySchema from MSON (default: true)

Parse Result

The format of the parse result is an API Elements structure, there is also API Elements: JS which contains tooling to handle this format in JavaScript. It is recommended to use the provided API Elements tooling to prevent any tight coupling between your tool and a parse result.

As an example, parsing the following API Blueprint:

# GET /
+ Response 204

Would result in the following API Elements Parse Result:

{
  "element": "parseResult",
  "content": [
    {
      "element": "category",
      "meta": {
        "classes": {
          "element": "array",
          "content": [
            {
              "element": "string",
              "content": "api"
            }
          ]
        },
        "title": {
          "element": "string",
          "content": ""
        }
      },
      "content": [
        {
          "element": "resource",
          "meta": {
            "title": {
              "element": "string",
              "content": ""
            }
          },
          "attributes": {
            "href": {
              "element": "string",
              "content": "/"
            }
          },
          "content": [
            {
              "element": "transition",
              "meta": {
                "title": {
                  "element": "string",
                  "content": ""
                }
              },
              "content": [
                {
                  "element": "httpTransaction",
                  "content": [
                    {
                      "element": "httpRequest",
                      "attributes": {
                        "method": {
                          "element": "string",
                          "content": "GET"
                        }
                      },
                      "content": []
                    },
                    {
                      "element": "httpResponse",
                      "attributes": {
                        "statusCode": {
                          "element": "string",
                          "content": "204"
                        }
                      },
                      "content": []
                    }
                  ]
                }
              ]
            }
          ]
        }
      ]
    }
  ]
}

Developing Protagonist

You can use the following steps to build and test Protagonist.

$ git clone --recursive https://github.com/apiaryio/protagonist.git
$ cd protagonist
$ npm install

While iterating on the package, you can use npm run build to compile Protagonist:

$ npm run build
$ npm test

Protagonist is built using node-gyp, you can consult their documentation for further information on the build system.

License

MIT License. See the LICENSE file.

更新日志

Protagonist Changelog

2.3.0 (2023-05-18)

This update now uses Drafter 5.1.0. Please see Drafter 5.1.0 for the list of changes.

Breaking

  • Drop support for node.js versions < 12.

Enhancements

  • Added support for building Protagonist with Node 16, and 18.

2.2.1 (2020-06-10)

Enhancements

  • Added support for building Protagonist with Node 13/14.

2.2.0 (2020-04-20)

Enhancements

  • Drafter contains two new options for disabling messageBody and messageBodySchema generation from MSON. generateMessageBody and generatedMessageBodySchema respectively.

2.1.0 (2020-03-17)

This update now uses Drafter 5.0.0-rc.1. Please see Drafter 5.0.0-rc.1 for the list of changes.

2.0.2 (2019-10-29)

This update now uses Drafter 4.0.2. Please see Drafter 4.0.2 for the list of changes.

2.0.1 (2019-09-17)

This update now uses Drafter 4.0.1. Please see Drafter 4.0.1 for the list of changes.

Enhancements

  • The Protagonist NPM package now contains a THIRD_PARTY_LICENSES.txt file which contains the licenses of the vendored C++ dependencies of the library.

2.0.0 (2019-07-02)

This update now uses Drafter 4.0.0-pre.8. Please see Drafter 4.0.0-pre.8 for the list of changes.

2.0.0-pre.10 (2019-05-31)

This update now uses Drafter 4.0.0-pre.7. Please see Drafter 4.0.0-pre.7 for the list of changes.

2.0.0-pre.9 (2019-05-20)

This update now uses Drafter 4.0.0-pre.6. Please see Drafter 4.0.0-pre.6 for the list of changes.

Enhancements

  • Added support for Node 12.

2.0.0-pre.8 (2019-05-09)

This is a re-release of Protagonist 2.0.0-pre.7 due to problems with NPM preventing the release of 2.0.0-pre.7.

2.0.0-pre.7 (2019-05-07)

This update now uses Drafter 4.0.0-pre.5. Please see Drafter 4.0.0-pre.5 for the list of changes.

2.0.0-pre.6 (2019-04-26)

This update now uses Drafter 4.0.0-pre.4. Please see Drafter 4.0.0-pre.4 for the list of changes.

2.0.0-pre.5 (2019-04-08)

This update now uses Drafter 4.0.0-pre.3. Please see Drafter 4.0.0-pre.3 for the list of changes.

2.0.0-pre.4

Enhancements

  • Added support for node 11.

2.0.0-pre.3

Bug Fixes

  • Fixed a segfault while handling invalid options that contains unsupported properties with unsupported types.

2.0.0-pre.2

This update now uses Drafter 4.0.0-pre.2. Please see Drafter 4.0.0-pre.2 for the list of changes.

2.0.0-pre.1

This update now uses Drafter 4.0.0-pre.1. Please see Drafter 4.0.0-pre.1 for the list of changes.

2.0.0-pre.0

This update now uses Drafter 4.0.0-pre.0. Please see Drafter 4.0.0-pre.0 for the list of changes.

  • updated Async call to use Nan::AsyncQueue instead of node v8 functionality directly

Breaking

  • Removed the option to select AST Type. The ouput will be only refract

  • Drop support for node.js versions 0.10 and 0.12

1.6.8

This update now uses Drafter 3.2.7. Please see Drafter 3.2.7 for the list of changes.

Bug Fixes

  • Fixed a bug where an option when set to false overrides the previous options.

1.6.7

This update now uses Drafter 3.2.6. Please see Drafter 3.2.6 for the list of changes.

1.6.6

This update now uses Drafter 3.2.5. Please see Drafter 3.2.5 for the list of changes.

1.6.5

This update now uses Drafter 3.2.4. Please see Drafter 3.2.4 for the list of changes.

1.6.4

This update now uses Drafter 3.2.3. Please see Drafter 3.2.3 for the list of changes.

1.6.3

This update now uses Drafter 3.2.2. Please see Drafter 3.2.2 for the list of changes.

1.6.2

This update now uses Drafter 3.2.1. Please see Drafter 3.2.1 for the list of changes.

1.6.1

This update now uses Drafter 3.2.0. Please see Drafter 3.2.0 for the list of changes.

1.6.0

Enhancements

  • Added validate and validateSync to just return the warnings and errors after parsing a blueprint.

1.5.2

This update now uses Drafter 3.1.3. Please see Drafter 3.1.3 for the list of changes.

1.5.1

This update now uses Drafter 3.1.2. Please see Drafter 3.1.2 for the list of changes.

1.5.0

This update now uses Drafter 3.1.1. Please see Drafter 3.1.1 for the list of changes.

1.5.0-pre.0

This update now uses Drafter 3.1.0-pre.0. Please see Drafter 3.1.0-pre.0 for the list of changes.

1.4.1

Bug Fixes

  • Fixes a problem when installing Protagonist on macOS.

1.4.0

This update now uses Drafter 3.0.0 Please see Drafter 3.0.0 for the list of changes.

Breaking

  • Protagonist now uses C++11.

    The following compiler versions are supported:

    • Microsoft Visual C++ 2013 or higher
    • GCC 4.8 or higher
    • Clang 3.5 or higher

1.3.3

This update now uses Drafter 2.3.1 Please see Drafter 2.3.1 for the list of changes.

1.3.2

This update adds support for node.js 5 and 6.