24.3.0 • Published 2 days ago

oas v24.3.0

Weekly downloads
13,085
License
MIT
Repository
github
Last release
2 days ago

oas is the library that we've built at ReadMe for powering everything we do related to OpenAPI; from our Reference Guides, to our Metrics product, or other in-house tooling like code generation, request execution, and SDK code generation.


Installation

npm install oas

Usage

Note If you need to use this library within a browser you'll likely need to use a bundler like Webpack or Rollup.

oas offers a main Oas class, which will be your main entrypoint for using the library.

import Oas from 'oas';
import petstoreSpec from '@readme/oas-examples/3.0/json/petstore.json';

const petstore = new Oas(petstoreSpec);

Here the Oas constructor takes a JSON API definition (petstoreSpec). All API definitions you feed it must be JSON and must be an OpenAPI definition. If you have a YAML or Swagger definition you will need to convert it (our oas-normalize library can do this for you). And if you're in a CJS or non-import environment you can pull the library in with require('oas').default.

From here, the following APIs are at your disposal.

OpenAPI definitions

Because this library has full TypeScript types and docblocks this README is not intended to be full documentation so consult the individual method docblocks if you need more information on a specific method.

General

MethodDescription
#dereference()Dereference the current OpenAPI definition. Note that this will ignore circular references.
#getCircularReferences()Retrieve an array of any circular $ref pointer that may exist wthin the OpenAPI definition. Note that this requires #dereference() to be called first.
#getDefinition()Retrieve the OpenAPI definition that was fed into the Oas constructor.
#getTags()Retrieve an array of all tags that exist within the API definition and are set on operations.
#getPaths()Retrieve every operation that exists within the API definition. This returns an array of instances of the Operation class.
#getVersion()Retrieve the OpenAPI version that this API definition is targeted for.
#getWebhooks()Retrieve every webhook operation that exists within the API definition. This returns an array of instances of the Webhook class.
#init()An alternative for new Oas() that you can use if the typing on the Oas constructor gives you trouble. Typing OpenAPI definitions is hard!

Operations

MethodDescription
#findOperation()Discover an operation with the current OpenAPI definition that matches a given URL and HTTP method.
#findOperationWithoutMethod()Like oas.findOperation() but without supplying an HTTP method.
#getOperation()Same as oas.findOperation() but this returns an instance of the Operation class.
#operation()Retrieve an instance of the Operation or Webhook classes for a given path and HTTP method.

Servers

MethodDescription
#defaultVariables()Retrieve the default server variables for a specific server URL, while also potentially factoring in user data. You can specify user variable data to the Oas constructor. Check out Using Variables in Documentation for some background on how we use this.
#replaceUrl()Replace a given templated server URL with supplied server variable data.
#splitUrl()Chunk out a specific server URL into its individual parts.
#splitVariablesChunk out a given URL and if it matches a server URL in the OpenAPI file, extract the matched server variables that are present in the URL.
#url()Retrive a fully composed server URL. You can optionally select which of the defined server URLs to use as well as specify server variable information.
#variables()Retrieve all server variables that a specific server URL in the definition has.

Specification Extensions

Note Optionally you can also supply an instance of the Operation to both of these methods to see or retrieve a given extension if it exists on that operation but if it exists in both the operation and at the root the operation-level extension will be prioritized.

MethodDescription
#getExtension()Retrieve a given specification extension if it exists at the root of the API definition.
#hasExtension()Determine if a given specification extension exists on the root of the API definition.
#validateExtension()Determine if a given ReadMe custom OpenAPI extension is valid or not.
#validateExtensions()Validate all of our ReadMe custom OpenAPI extension, throwing exceptions when necessary.

Information about ReadMe's supported OpenAPI extensions at https://docs.readme.com/docs/openapi-extensions.

User Authentication

MethodDescription
#getAuth()Retrieve the appropriate API keys for the current OpenAPI definition from an object of user data. Check out Using Variables in Documentation for some background on how we use this.

Operations

For your convenience, the entrypoint into the Operation class should generally always be through Oas.operation(). For example:

import Oas from 'oas';
import petstoreSpec from '@readme/oas-examples/3.0/json/petstore.json';

const petstore = new Oas(petstoreSpec);
const operation = petstore.operation('/pet', 'post');

General

MethodDescription
#getContentType()Retrieve the primary request body content type. If multiple are present, prefer whichever is JSON-compliant.
#getDescription()Retrieve the description that's set on this operation. This supports common descriptions that may be set at the path item level.
#getOperationId()Retrieve the operationId that's present on the operation, and if one is not present one will be created based off the method + path and returned instead.
#hasOperationId()Determine if the operation has an operationId present.
#isDeprecated()Determine if this operation is marked as deprecated.
#isFormUrlEncoded()Determine if this operation requires its payload to be delivered as application/x-www-form-urlencoded.
#isJson()Determine if this operation requires its payload to be delivered as JSON.
#isMultipart()Determine if this operation requires its data to be sent as a multipart payload.
#isXml()Determine if this operation requires its data to be sent as XML.
#getExampleGroups()Returns an object with groups of all example definitions (body/header/query/path/response/etc.). The examples are grouped by their key when defined via the examples map.
#getHeaders()Retrieve all headers that can either be sent for or returned from this operation. This includes header-based authentication schemes, common header parameters, and request body and response content types.
#getSummary()Retrieve the summary that's set on this operation. This supports common summaries that may be set at the path item level.
#getTags()Retrieve all tags, and their metadata, that exist on this operation.

Callbacks

MethodDescription
#getCallback()Retrieve a specific callback on this operation. This will return an instance of the Callback class.
#getCallbackExamples()Retrieve an array of all calback examples that this operation has defined.
#getCallbacks()Retrieve all callbacks that this operation has. Similar to Oas.getPaths() returning an array of Operation instances this will return an array of Callback instances.
#hasCallbacks()Determine if this operation has any callbacks defined.

Parameters

Note All parameter accessors here support, and will automatically retrieve and handle, common parameters that may be set at the path item level.

MethodDescription
#getParameters()Retrieve all parameters that may be used with on this operation.
#getParametersAsJSONSchema()Retrieve and convert the operations parameters into an array of JSON Schema schemas for each available type of parameter available on the operation: path, query, body, cookie, formData, and header.
#hasParameters()Determine if the operation has any parameters to send.
#hasRequiredParameters()Determine if any of the parameters on this operation are required.

Request Body

MethodDescription
#getRequestBody()Retrieve the raw request body object for a given content type. If none is specified it will return either the first that's JSON-like, or the first defined.
#getRequestBodyExamples()Retrieve an array of all request body examples that this operation has defined.
#getRequestBodyMediaTypes()Retrieve a list of all the media/content types that the operation can accept a request body payload for.
#hasRequestBody()Determine if this operation has a request body defined.

Responses

MethodDescription
#getResponseAsJSONSchema()Retrive and convert a response on this operation into JSON Schema.
#getResponseByStatusCode()Retrieve the raw response object for a given status code.
#getResponseExamples()Retrieve an array of all response examples that this operation has defined.
#getResponseStatusCodes()Retrieve all status codes that this operation may respond with.
#hasRequiredRequestBody()Determine if this operation has a required request body.

Security

MethodDescription
#getSecurity()Return all security requirements that are applicable for either this operation, or if the operation has none specific to it, then for the entire API.
#getSecurityWithTypes()Return a collection of all security schemes applicable to this operation (using #getSecurity()), grouped by how the security should be handled (either AND or OR auth requirements).
#prepareSecurityReturn an object of every security scheme that can be used on this operation, indexed by the type of security scheme it is (eg. Basic, OAuth2, APIKey, etc.).

Specification Extensions

MethodDescription
#hasExtension()Determine if a given specification extension exists on this operation.

Information about ReadMe's supported OpenAPI extensions at https://docs.readme.com/docs/openapi-extensions.

Callbacks

The Callback class inherits Operation so every API available on instances of Operation is available here too. Much like Operation, we also support common parameters, summaries, and descriptions that may be set at the path item level within a callbacks definition.

General

MethodDescription
#getIdentifier()Retrieve the primary identifier of this callback.

Webhooks

Because our Webhook class extends Operation, every API that's available on the Operation class is available on webhooks.

FAQ

Can I create an OpenAPI definition with this?

Though oas used to offer functionality related to this, it does no longer. If you need an OpenAPI (or Swagger) definition for your API we recommend checking out the API editing experience within ReadMe, manually maintaining JSON/YAML files (it sounds worse than it actually is), or the language-agnostic swagger-inline.

24.3.0

2 days ago

24.2.2

15 days ago

24.2.1

22 days ago

24.2.0

23 days ago

21.1.3

7 months ago

21.1.4

7 months ago

22.0.0

7 months ago

23.0.0

7 months ago

23.0.2

6 months ago

23.0.1

7 months ago

23.1.0

6 months ago

24.1.0

6 months ago

24.0.0

6 months ago

21.0.3

8 months ago

21.1.1

8 months ago

21.1.0

8 months ago

21.1.2

7 months ago

21.0.2

8 months ago

21.0.1

8 months ago

20.10.0

10 months ago

20.10.1

9 months ago

20.10.2

9 months ago

20.10.3

8 months ago

20.8.9

10 months ago

20.8.8

10 months ago

20.8.7

10 months ago

20.8.6

10 months ago

20.8.5

10 months ago

20.8.4

11 months ago

20.9.0

10 months ago

20.6.2

1 year ago

20.6.1

1 year ago

20.6.0

1 year ago

20.7.0

1 year ago

20.8.1

12 months ago

20.8.0

12 months ago

20.8.3

12 months ago

20.8.2

12 months ago

20.4.0

1 year ago

20.5.0

1 year ago

20.5.4

1 year ago

20.5.3

1 year ago

20.5.2

1 year ago

20.5.1

1 year ago

19.0.1

2 years ago

19.0.0

2 years ago

19.0.3

2 years ago

19.0.2

2 years ago

19.0.4

2 years ago

20.2.1

1 year ago

20.2.0

1 year ago

20.3.0

1 year ago

20.0.1

1 year ago

20.0.0

2 years ago

20.1.0

1 year ago

20.1.3

1 year ago

20.1.2

1 year ago

20.1.1

1 year ago

18.4.4

2 years ago

18.4.3

2 years ago

18.4.2

2 years ago

18.4.1

2 years ago

18.4.0

2 years ago

18.3.4

2 years ago

18.3.3

2 years ago

18.3.2

2 years ago

18.3.1

2 years ago

18.3.0

2 years ago

18.2.3

2 years ago

18.2.2

2 years ago

18.2.1

2 years ago

18.2.0

2 years ago

18.1.2

2 years ago

18.1.1

2 years ago

18.1.0

2 years ago

17.8.2

2 years ago

18.0.8

2 years ago

18.0.7

2 years ago

18.0.6

2 years ago

18.0.5

2 years ago

18.0.4

2 years ago

18.0.3

2 years ago

18.0.2

2 years ago

18.0.1

2 years ago

18.0.0

2 years ago

17.8.1

2 years ago

17.8.0

2 years ago

17.3.0

2 years ago

17.3.2

2 years ago

17.3.1

2 years ago

17.7.3

2 years ago

17.7.0

2 years ago

17.7.2

2 years ago

17.7.1

2 years ago

17.4.1

2 years ago

17.4.0

2 years ago

17.4.3

2 years ago

17.4.2

2 years ago

17.5.0

2 years ago

17.1.7

2 years ago

17.2.0

2 years ago

17.6.0

2 years ago

14.9.0

2 years ago

17.1.2

2 years ago

17.1.1

2 years ago

17.1.4

2 years ago

17.1.3

2 years ago

17.1.0

2 years ago

17.1.6

2 years ago

17.1.5

2 years ago

15.0.0

3 years ago

17.0.0

2 years ago

14.8.1

3 years ago

14.8.2

3 years ago

16.0.2

3 years ago

16.0.1

3 years ago

16.0.0

3 years ago

16.0.4

3 years ago

16.0.3

3 years ago

14.7.0

3 years ago

14.8.0

3 years ago

14.6.1

3 years ago

14.6.0

3 years ago

14.5.1

3 years ago

14.5.0

3 years ago

14.4.0

3 years ago

14.3.2

3 years ago

14.3.1

3 years ago

14.3.0

3 years ago

14.2.0

3 years ago

14.2.1

3 years ago

14.1.1

3 years ago

14.1.0

3 years ago

14.0.0

3 years ago

13.1.0

3 years ago

13.0.5

3 years ago

13.0.4

3 years ago

13.0.2

3 years ago

13.0.3

3 years ago

13.0.1

3 years ago

12.0.0

3 years ago

13.0.0

3 years ago

10.7.0

3 years ago

10.7.1

3 years ago

10.7.2

3 years ago

10.7.3

3 years ago

10.7.4

3 years ago

11.0.0

3 years ago

11.0.1

3 years ago

10.6.3

3 years ago

10.6.2

3 years ago

10.6.0

3 years ago

10.6.1

3 years ago

10.5.0

3 years ago

10.4.1

3 years ago

10.4.0

3 years ago

10.3.0

3 years ago

10.2.0

3 years ago

10.1.2

3 years ago

10.1.1

3 years ago

10.1.0

3 years ago

10.0.4

3 years ago

10.0.3

3 years ago

10.0.2

3 years ago

10.0.1

3 years ago

10.0.0

3 years ago

9.0.0

3 years ago

6.1.1

3 years ago

8.0.1

3 years ago

8.0.0

3 years ago

7.0.0

3 years ago

6.1.0

3 years ago

6.0.0

3 years ago

5.2.1

3 years ago

5.2.0

3 years ago

5.1.0

3 years ago

5.0.1

3 years ago

5.0.0

4 years ago

4.1.0

4 years ago

4.0.0

4 years ago

3.6.1

4 years ago

3.5.14

4 years ago

3.5.12

4 years ago

3.5.9

4 years ago

3.5.7

4 years ago

3.5.6

4 years ago

3.5.4

4 years ago

3.5.1

4 years ago

3.5.0

4 years ago

3.4.6

4 years ago

3.4.4

4 years ago

3.4.3

4 years ago

3.4.2

4 years ago

3.4.1

4 years ago

3.4.0

4 years ago

3.3.6

4 years ago

3.3.2

4 years ago

3.2.0

4 years ago

3.1.9

4 years ago

3.1.7

4 years ago

3.1.6

4 years ago

3.1.5

4 years ago

3.1.4

4 years ago

3.1.3

4 years ago

3.1.2

4 years ago

3.1.1

4 years ago

3.1.0

4 years ago

3.0.1

4 years ago

3.0.0

4 years ago

2.1.1

4 years ago

2.1.0

4 years ago

2.1.0-alpha.0

4 years ago

2.0.0

4 years ago

2.0.0-alpha.0

4 years ago

1.5.2

4 years ago

1.5.1

4 years ago

1.5.0

4 years ago

1.4.2

4 years ago

1.4.1

4 years ago

1.4.0

4 years ago

1.3.0

4 years ago

1.2.0

4 years ago

1.1.0

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

0.8.18

5 years ago

0.8.17

5 years ago

0.8.16

5 years ago

0.8.15

5 years ago

0.8.14

5 years ago

0.8.13

5 years ago

0.8.12

5 years ago

0.8.10

5 years ago

0.8.9

5 years ago

0.8.8

6 years ago

0.8.7

6 years ago

0.8.6

6 years ago

0.8.5

6 years ago

0.8.4

6 years ago

0.8.3

6 years ago

0.8.2

6 years ago

0.8.1

6 years ago

0.6.9

6 years ago

0.6.8

6 years ago

0.6.7

7 years ago

0.6.6

7 years ago

0.6.5

7 years ago

0.6.4

7 years ago

0.6.3

7 years ago

0.6.2

7 years ago

0.6.1

7 years ago