Ariadocs

parseOpenAPI

Edit on GitHub

Load, validate and normalize a spec in one call.

import { parseOpenAPI } from "@ariadocs/openapi";

const api = await parseOpenAPI({ source: "./openapi.yaml" });

The source

source can be:

  • a spec object you already have,
  • JSON or YAML text,
  • an http(s) URL, which is fetched,
  • a file path or file: URL, which is read from disk.

A file path that doesn't exist throws a "file not found" error.

Options

OptionDefaultWhat it does
validatetrueThrow an OpenAPIError if the spec is missing required parts
resolveRefstrueReplace $refs with their targets. Each resolved schema keeps its origin in ref.
strictfalseThrow if a $ref can't be resolved, instead of adding a warning
allowFilestrueAllow file paths
allowRemotetrueAllow URLs
fetchglobal fetchYour own fetch, for auth headers or tests
timeout30000Request timeout in milliseconds
maxSize20 MBLargest spec it will read

If the source comes from a user, read untrusted specs first.

In the browser

Pass the text or the object. File paths don't work in the browser.

const api = await parseOpenAPI({ source: text, allowFiles: false, allowRemote: false });

You can try this on the client rendering demo.