Package detail

@mintlify/openapi-parser

scalar145.3kMIT0.0.7

modern OpenAPI parser written in TypeScript

openapi, scalar, swagger, parser

readme

Scalar OpenAPI Parser

Version Downloads License Discord

Modern OpenAPI parser written in TypeScript with support for OpenAPI 3.1, OpenAPI 3.0 and Swagger 2.0.

Goals

  • [x] Written in TypeScript
  • [x] Runs in Node.js and in the browser (without any polyfills or configuration)
  • [x] Tested with hundreds of real world examples
  • [ ] Amazing error output
  • [ ] Support for OpenAPI 4.0 👀

Installation

npm add @mintlify/openapi-parser

Usage

Validate

import { validate } from '@mintlify/openapi-parser'

const file = `{
  "openapi": "3.1.0",
  "info": {
    "title": "Hello World",
    "version": "1.0.0"
  },
  "paths": {}
}`

const { valid, errors } = await validate(file)

console.log(valid)

if (!valid) {
  console.log(errors)
}

Resolve references

import { dereference } from '@mintlify/openapi-parser'

const specification = `{
  "openapi": "3.1.0",
  "info": {
    "title": "Hello World",
    "version": "1.0.0"
  },
  "paths": {}
}`

const { schema, errors } = await dereference(specification)

Modify an OpenAPI specification

import { filter } from '@mintlify/openapi-parser'

const specification = `{
  "openapi": "3.1.0",
  "info": {
    "title": "Hello World",
    "version": "1.0.0"
  },
  "paths": {}
}`

const { specification } = filter(specification, (schema) => !schema?.['x-internal'])

Upgrade your OpenAPI specification

There’s an upgrade command to upgrade all your OpenAPI specifications to the latest OpenAPI version.

⚠️ The upgrade from Swagger 2.0 is still experimental and probably lacks features.

import { upgrade } from '@mintlify/openapi-parser'

const { specification } = upgrade({
  swagger: '2.0',
  info: {
    title: 'Hello World',
    version: '1.0.0',
  },
  paths: {},
})

console.log(specification.openapi)
// Output: 3.1.0

Pipeline syntax

import { openapi } from '@mintlify/openapi-parser'

const specification = …

// New pipeline …
const result = openapi()
  // loads the specification …
  .load(specification)
  // upgrades to OpenAPI 3.1 …
  .upgrade()
  // removes all internal operations …
  .filter((schema) => !schema?.['x-internal'])
  // done!
  .get()

Then/Catch syntax

If you’re more the then/catch type of guy, that’s fine:

import { validate } from '@mintlify/openapi-parser'

const specification = …

validate(specification, {
  throwOnError: true,
})
.then(result => {
  // Success
})
.catch(error => {
  // Failure
})

TypeScript

If you just look for our types, you can install the package separately:

npm add @mintlify/openapi-types

And use it like this:

import type { OpenAPI } from '@mintlify/openapi-types'

const file: OpenAPI.Document = {
  openapi: '3.1.0',
  info: {
    title: 'Hello World',
    version: '1.0.0',
  },
  paths: {},
}

Advanced: URL and file references

You can reference other files, too. To do that, the parser needs to know what files are available.

import { dereference, load } from '@mintlify/openapi-parser'
import { fetchUrls } from '@mintlify/openapi-parser/plugins/fetch-urls'
import { readFiles } from '@mintlify/openapi-parser/plugins/read-files'

// Load a file and all referenced files
const { filesystem } = await load('./openapi.yaml', {
  plugins: [
    readFiles(),
    fetchUrls({
      limit: 5,
    }),
  ],
})

// Instead of just passing a single specification, pass the whole “filesystem”
const result = await dereference(filesystem)

As you see, load() supports plugins. You can write your own plugin, if you’d like to fetch API defintions from another data source, for example your database. Look at the source code of the readFiles to learn how this could look like.

Directly load URLs

Once the fetchUrls plugin is loaded, you can also just pass an URL:

import { dereference, load } from '@mintlify/openapi-parser'
import { fetchUrls } from '@mintlify/openapi-parser/plugins/fetch-urls'

// Load a file and all referenced files
const { filesystem } = await load(
  'https://cdn.jsdelivr.net/npm/@mintlify/galaxy/dist/latest.yaml',
  {
    plugins: [fetchUrls()],
  },
)

Intercept HTTP requests

If you’re using the package in a browser environment, you may run into CORS issues when fetching from URLs. You can intercept the requests, for example to use a proxy, though:

import { dereference, load } from '@mintlify/openapi-parser'
import { fetchUrls } from '@mintlify/openapi-parser/plugins/fetch-urls'

// Load a file and all referenced files
const { filesystem } = await load(
  'https://cdn.jsdelivr.net/npm/@mintlify/galaxy/dist/latest.yaml',
  {
    plugins: [
      fetchUrls({
        fetch: (url) => fetch(url.replace('BANANA.net', 'jsdelivr.net')),
      }).get('https://cdn.BANANA.net/npm/@mintlify/galaxy/dist/latest.yaml'),
    ],
  },
)

Community

We are API nerds. You too? Let’s chat on Discord: https://discord.gg/scalar

Thank you!

Thanks a ton for all the help and inspiration:

License

The source code in this repository is licensed under MIT.

changelog

@mintlify/openapi-parser

0.8.8

Patch Changes

  • 7323370: Allow relative URLs in v3.1 documents

0.8.7

Patch Changes

  • 6394a5d: chore: switch to @mintlify/build-tooling

0.8.6

Patch Changes

  • d064a78: chore: make examples an array if it’s in a schema
  • 3db9355: feat: upgrade Swagger 2.0 securityDefinitions

0.8.5

Patch Changes

  • 983a5e4: feat: self reference the document
  • aee166c: fix: server URLs with variables are considered not valid

0.8.4

Patch Changes

  • 96c921c: feat: upgrade Swagger 2.0 formData parameters

0.8.3

Patch Changes

  • 674922f: chore: update fetch global tests

0.8.2

Patch Changes

  • 6bb85a5: feat: openapi() returns dynamic types based on the chained commands

0.8.1

Patch Changes

  • 5bd8337: feat: upgrade from Swagger 2.0 (experimental)

0.8.0

Minor Changes

  • b4f9f97: feat: new @mintlify/openapi-types package

Patch Changes

  • b4f9f97: feat: add literal versions to OpenAPI.Document types
  • b4f9f97: feat: types: allow to type custom extensions
  • b4f9f97: feat: types: allow any attribute in schemas
  • b231e7d: fix: upgrade returns correct OpenAPI document version type
  • b4f9f97: feat: expose more types under the OpenAPI namespace

0.7.2

Patch Changes

  • 89dd0ef: feat: validate and dereference, throwOnError option

0.7.1

Patch Changes

  • 5e2c2d1: fix: read files export is wrong

0.7.0

Minor Changes

  • ec01324: refactor!: use dedicated entrypoints for the plugins

Patch Changes

  • c6944f2: fix: ajv import broken in CJS environments

0.6.0

Minor Changes

  • 61252ab: refactor!: most functions return an object now

Patch Changes

  • c9dd499: feat: intercept fetch requests
  • 61252ab: fix: max call stack exceeded error

0.5.0

Minor Changes

  • 10bd75c: chore: switch to rollup to make the library tree shakeable

0.4.1

Patch Changes

  • 1023e0a: chore: reduce bundle size

0.4.0

Minor Changes

  • 0b7d7be: refactor!: resolve is renamed to dereference
  • 63f72b4: refactor!: new OpenAPI types, removed the ResolvedOpenAPI types
  • 0b7d7be: refactor!: when using the pipeline syntax all tasks are queued and only executed when you add .get(), .toJson() or toYaml()
  • 56e7033: feat: file references
  • 0b7d7be: chore!: remove loadFiles utility, use load instead

Patch Changes

  • 03569c8: fix: existsSync is not a function
  • 6436ae1: refactor: resolve should resolve (and not validate)

0.3.2

Patch Changes

  • 02ea440: fix: make errors easier to consume

0.3.1

Patch Changes

  • 2498fc2: fix: make new parser behave like previous one on missing info.version

0.3.0

Minor Changes

  • c28d766: fix: circular references can not be resolved
  • c28d766: fix: references inside references are not resolved
  • c28d766: refactor: rewrote the whole parser (again)

Patch Changes

  • 0e98fdb: chore: add light-weight browser polyfill to join paths

0.2.0

Minor Changes

  • bf4568e: refactor: rewrite the whole package :)

0.1.0

Minor Changes

  • fb3b15f: init :)