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

@travetto/openapi

v4.0.8

Published

OpenAPI integration support for the Travetto framework

Downloads

195

Readme

OpenAPI Specification

OpenAPI integration support for the Travetto framework

Install: @travetto/openapi

npm install @travetto/openapi

# or

yarn add @travetto/openapi

In the RESTful API module, the controllers and endpoints can be described via decorators, comments, or typings. This only provides the general metadata internally. This is not sufficient to generate a usable API doc, and so this module exists to bridge that gap.

The module is provides an OpenAPI v3.x representation of the API metadata provided via the RESTful API and Schema modules.

Configuration

By installing the dependency, the OpenAPI endpoint is automatically generated and exposed at the root of the application as /openapi.yml or /openapi.json (by default).

All of the high level configurations can be found in the following structure:

Code: Config: OpenAPI Configuration

import type { ServerObject, ContactObject, LicenseObject } from 'openapi3-ts/oas31';

import { Config } from '@travetto/config';
import { path, RuntimeIndex, RuntimeContext } from '@travetto/manifest';
import { Env } from '@travetto/base';
import { Required } from '@travetto/schema';

/**
 * API Information, infers as much as possible from the package.json
 */
@Config('api.info')
export class ApiInfoConfig {
  @Required(false)
  contact: ContactObject;
  @Required(false)
  description?: string;
  @Required(false)
  license: LicenseObject;
  @Required(false)
  termsOfService?: string;
  @Required(false)
  title: string;
  @Required(false)
  version: string;

  postConstruct(): void {
    this.title ??= RuntimeContext.main.name;
    this.version ??= RuntimeContext.main.version;
    this.description ??= RuntimeContext.main.description;
  }
}

/**
 * The API host, infers from rest host configuration
 */
@Config('api.host')
export class ApiHostConfig {
  /**
   * List of servers
   */
  servers?: ServerObject[];
  /**
   * OpenAPI Version
   */
  openapi = '3.0.0';
}

/**
 * The spec file configuration
 */
@Config('api.spec')
export class ApiSpecConfig {
  /**
   * Where to output file to
   */
  output: string = 'openapi.yml';
  /**
   * Should file be generated at runtime
   */
  persist?: boolean;
  /**
   * Skip emitting all routes
   */
  skipRoutes: boolean = false;
  /**
   * Expose all schemas, even if not referenced
   */
  exposeAllSchemas: boolean = false;

  async postConstruct(): Promise<void> {
    if (!this.output || this.output === '-') {
      this.persist = false;
    } else {
      this.output = path.resolve(RuntimeIndex.mainModule.sourcePath, this.output);
      this.persist ??= Env.dynamic;
    }
    if (this.persist) {
      if (!/[.](json|ya?ml) $/.test(this.output)) { // Assume a folder
        this.output = path.resolve(this.output, 'openapi.yml');
      }
    }
  }
}

Spec Generation

The framework, when in watch mode, will generate the OpenAPI specification in either JSON or YAML. This module integrates with the file watching paradigm and can regenerate the openapi spec as changes to endpoints and models are made during development. The output format is defined by the suffix of the output file, .yaml or .json.

CLI - openapi:spec

The module provides a command for the Command Line Interface to allow scripting file generation.

Terminal: OpenAPI usage

$ trv openapi:spec --help

Usage: openapi:spec [options]

Options:
  -o, --output <string>  Output files
  -m, --module <module>  Module to run for
  -h, --help             display help for command

The command will run your application, in non-server mode, to collect all the routes and model information, to produce the openapi.yml. Once produced, the code will store the output in the specified location.

Note: The module supports generating the OpenAPI spec in real-time while listening for changes to routes and models.

CLI - openapi:client

The module provides a command for the Command Line Interface to allow client generation from the API structure.

Terminal: OpenAPI usage

$ trv openapi:client --help

Usage: openapi:client [options] <format:string>

Options:
  -x, --extended-help                   Show Extended Help
  -a, --additional-properties <string>  Additional Properties (default: [])
  -i, --input <string>                  Input file (default: "./openapi.yml")
  -o, --output <string>                 Output folder (default: "./api-client")
  -d, --docker-image <string>           Docker Image to user (default: "openapitools/openapi-generator-cli:latest")
  -h, --help                            display help for command

This tool relies upon a custom build of OpenAPI client generation tools, which supports watching. This allows for fast responsive client generation as the shape of the API changes.