npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@api-components/api-schema

v0.1.5

Published

Schema generator based on the AMF graph LD model.

Downloads

19

Readme

API schema

A library that generates schemas from the AMF graph ld+json model.

Usage

Installation

npm install --save @api-components/api-schema

Generating schemas

The library takes an AMF shape as the argument, a media type of the generated schema as the other argument and generates schema depending what is defined in the model and the configuration.

Scalars processing:

  1. If the shape is not required and renderOptional is not set return undefined.
  2. If there's an example and renderExamples is set then return parsed example value according to schema's data type
  3. If there's a default value, return the default value according to schema's data type
  4. If there's an enum value, return the first enum value according to schema's data type
  5. Otherwise return a value that is a "default" value for the given data type ('', 0, false, null).

Objects are processed property-by-property as defined above.

Note, that the examples take precedence over the default or enum values. This is to make example generation consistent. If examples are processed after default value or enums then the result is a mix of examples and the other two, which is not what you expect.

For unions you can pass the selectedUnions[] configuration option which the ids of all explicitly selected union members. When the library cannot find a member in the selectedUnions array then it takes the first available member to process the schema.

Union of a scalar and nil (or multiple scalars and nil) is treated like an optional property and it doesn't render values if renderOptional is not set.

import { ApiSchemaGenerator } from '@api-components/api-schema';

const shape = readAmfShapeSomehow()
const result = ApiSchemaGenerator.asSchema(shape, 'application/json', {
  renderOptional: true,
  renderExamples: true,
});
console.log(result);

Generating Monaco schemas

Currently only NodeShape is supported when generating a Monaco schema.

import { ApiMonacoSchemaGenerator } from '@api-components/api-schema';

const shape = readAmfShapeSomehow();
const reader = new ApiMonacoSchemaGenerator();
const result = reader.generate(shape, 'https://domain.com');
console.log(result);

Reading schema values for form inputs

This class reads a value that should be rendered in a form input for the generated schema. Note, there are two methods to read scalar and array values. Also, this class operates on Parameters rather than Shapes.

import { ApiSchemaValues } from '@api-components/api-schema';

const parameter = readAmfParameterSomehow();
const result = ApiSchemaValues.readInputValue(parameter);
console.log(result); // string, number, boolean, null, undefined, object, array

const arrayResult = ApiSchemaValues.readInputValues(parameter);
console.log(arrayResult); // list of: string, number, boolean, null, undefined, object, array

const inputType = ApiSchemaValues.readInputType('http://a.ml/vocabularies/shapes#number');
console.log(inputType); // "number"

const parsedValue = ApiSchemaValues.parseScalarInput('25', numberScalarShape);
console.log(parsedValue); // 25

Reading example values from examples

This class specializes in reading and processing AMF examples to a form that should be rendered to the user as an example value of a scheme or a property.

Note, that you need to provide a media type for which generate the value for. Currently the following media types are supported:

  • application/json
  • application/xml
  • application/x-www-form-urlencoded
import { ApiExampleGenerator } from '@api-components/api-schema';
const reader = new ApiExampleGenerator();
const shape = readAmfShapeSomehow();
const result = reader.read(shape.examples[0], 'application/json');
console.log(result); // a string representing a JSON value.

Note, when the example comes from a scalar shape the returned value is this scalar value which is not a valid JSON.

Development

git clone https://github.com/advanced-rest-client/api-schema
cd api-schema
npm install

Running the demo locally

npm start

Running the tests

npm test