API Elements JavaScript Library

0.3.2 · active · verified Sun Apr 19

The `api-elements` package provides a JavaScript interface for working with API Elements, an abstract syntax tree (AST) for describing APIs. It acts as a core component within the larger API Elements ecosystem, enabling programmatic creation, manipulation, and serialization of API descriptions using a unified model, regardless of the original format (like OpenAPI or API Blueprint). The current stable version of this specific package is 0.3.2, though its sub-packages, like `@apielements/openapi3-parser` and `@apielements/openapi2-parser`, are more actively developed with frequent minor and patch releases, incorporating new OpenAPI specification features and bug fixes. This library differentiates itself by offering a language-agnostic, structured representation of API definitions, allowing for advanced tooling and transformations beyond simple parsing.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the API Elements namespace and create or deserialize API Element ASTs. It shows both parsing a JSON representation and constructing elements programmatically, then accessing properties like the API title.

const apiElements = require('api-elements');
const namespace = new apiElements.Namespace();

// Parsing a JSON Representation of an API Elements tree
const parseResult = namespace.serialiser.deserialise({
  element: 'parseResult',
  content: [
    {
      element: 'category',
      meta: { classes: ['api'] },
      content: [
        { element: 'copy', content: 'My Awesome API' },
        { element: 'resource', content: [] }
      ]
    }
  ]
});

console.log('Deserialized ParseResult element:', parseResult.element);
console.log('API title:', parseResult.api.copy.toValue());

// Creating API Elements directly
const directParseResult = new namespace.elements.ParseResult();
const apiCategory = new namespace.elements.Category();
apiCategory.classes.push('api');
apiCategory.content.push(new namespace.elements.Copy('Another API'));
directParseResult.content.push(apiCategory);

console.log('Directly created ParseResult element:', directParseResult.element);
console.log('Another API title:', directParseResult.api.copy.toValue());

view raw JSON →