Socket
Socket
Sign inDemoInstall

@apidevtools/swagger-parser

Package Overview
Dependencies
17
Maintainers
3
Versions
8
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @apidevtools/swagger-parser

Swagger 2.0 and OpenAPI 3.0 parser and validator for Node and browsers


Version published
Weekly downloads
1.5M
increased by0.86%
Maintainers
3
Install size
2.75 MB
Created
Weekly downloads
 

Package description

What is @apidevtools/swagger-parser?

The @apidevtools/swagger-parser npm package is a tool that allows you to parse, validate, and dereference Swagger files (now known as OpenAPI Specifications). It supports Swagger 2.0 and OpenAPI 3.0, providing a way to ensure that API documentation is correct and adheres to the specification standards.

What are @apidevtools/swagger-parser's main functionalities?

Parse and Validate

This feature allows you to parse and validate a Swagger/OpenAPI file, ensuring it is correct and follows the specification.

const SwaggerParser = require('@apidevtools/swagger-parser');

SwaggerParser.validate('path/to/swagger.yaml')
  .then(api => {
    console.log('API name: %s, Version: %s', api.info.title, api.info.version);
  })
  .catch(err => {
    console.error(err);
  });

Dereference

Dereferencing resolves all $ref pointers in the Swagger/OpenAPI document, replacing them with their referenced objects or values.

const SwaggerParser = require('@apidevtools/swagger-parser');

SwaggerParser.dereference('path/to/swagger.yaml')
  .then(api => {
    console.log('Dereferenced API:', api);
  })
  .catch(err => {
    console.error(err);
  });

Bundle

Bundling takes all the separate files that make up a Swagger/OpenAPI document and bundles them into a single file.

const SwaggerParser = require('@apidevtools/swagger-parser');

SwaggerParser.bundle('path/to/swagger.yaml')
  .then(api => {
    console.log('Bundled API:', api);
  })
  .catch(err => {
    console.error(err);
  });

Other packages similar to @apidevtools/swagger-parser

Readme

Source

Swagger 2.0 and OpenAPI 3.0 parser/validator

Build Status Coverage Status Tested on APIs.guru

npm Dependencies License Buy us a tree

OS and Browser Compatibility

Online Demo

Features

  • Parses Swagger specs in JSON or YAML format
  • Validates against the Swagger 2.0 schema or OpenAPI 3.0 Schema
  • Resolves all $ref pointers, including external files and URLs
  • Can bundle all your Swagger files into a single file that only has internal $ref pointers
  • Can dereference all $ref pointers, giving you a normal JavaScript object that's easy to work with
  • Tested in Node.js and all modern web browsers on Mac, Windows, and Linux
  • Tested on over 1,500 real-world APIs from Google, Microsoft, Facebook, Spotify, etc.
  • Supports circular references, nested references, back-references, and cross-references
  • Maintains object reference equality — $ref pointers to the same value always resolve to the same object instance

Example

SwaggerParser.validate(myAPI, (err, api) => {
  if (err) {
    console.error(err);
  }
  else {
    console.log("API name: %s, Version: %s", api.info.title, api.info.version);
  }
});

Or use async/await or Promise syntax instead. The following example is the same as above:

try {
  let api = await SwaggerParser.validate(myAPI);
  console.log("API name: %s, Version: %s", api.info.title, api.info.version);
}
catch(err) {
  console.error(err);
}

For more detailed examples, please see the API Documentation

Installation

Install using npm:

npm install @apidevtools/swagger-parser

Usage

When using Swagger Parser in Node.js apps, you'll probably want to use CommonJS syntax:

const SwaggerParser = require("@apidevtools/swagger-parser");

When using a transpiler such as Babel or TypeScript, or a bundler such as Webpack or Rollup, you can use ECMAScript modules syntax instead:

import SwaggerParser from "@apidevtools/swagger-parser";

Browser support

Swagger Parser supports recent versions of every major web browser. Older browsers may require Babel and/or polyfills.

To use Swagger Parser in a browser, you'll need to use a bundling tool such as Webpack, Rollup, Parcel, or Browserify. Some bundlers may require a bit of configuration, such as setting browser: true in rollup-plugin-resolve.

API Documentation

Full API documentation is available right here

Contributing

I welcome any contributions, enhancements, and bug-fixes. Open an issue on GitHub and submit a pull request.

Building/Testing

To build/test the project locally on your computer:

  1. Clone this repo
    git clone https://github.com/APIDevTools/swagger-parser.git

  2. Install dependencies
    npm install

  3. Run the build script
    npm run build

  4. Run the tests
    npm test

  5. Check the code coverage
    npm run coverage

License

Swagger Parser is 100% free and open-source, under the MIT license. Use it however you want.

This package is Treeware. If you use it in production, then we ask that you buy the world a tree to thank us for our work. By contributing to the Treeware forest you’ll be creating employment for local families and restoring wildlife habitats.

Big Thanks To

Thanks to these awesome companies for their support of Open Source developers ❤

GitHub NPM Coveralls

Keywords

FAQs

Last updated on 13 May 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc