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

oas-loader

v0.7.4

Published

Open API spec webpack loader

Downloads

50

Readme

Rationale

Open API spec is (as of writing) still missing some key features:

  • excluding properties when inheriting schemes
  • merging (i.e. allOf) response schemes

The loader attempts to solve those problems while keeping the final spec valid.
See the full list of features below.

Features

  • exclude properties when merging schemes (nested properties exclusion not supported)
  • merging response schemes
  • assembles final spec from scattered files across the project

Usage

Loader

npm i oas-loader -D
// webspack.config.js

module.exports = {
    // ...
  
    module: {
        rules: [
          {
            test: /oas-spec.yml$/,
            use: [
              {
                loader: 'oas-loader',
                options: {/*...*/}, // See below
              },
              'json-loader',
              'yaml-loader',
            ],
          },
        ],
    }
  
    // ...
}

(i) NOTE: Here implied that yml files are used for OAS. To use json files simply drop yaml-loader

Swagger setup

The example below relies on swagger-ui.

// src/index.ts

import * as SwaggerUI from 'swagger-ui';
import * as spec from 'oas-spec.yml';

SwaggerUI({
  spec,
  dom_id: '#doc-app',
});

(i) NOTE: Import of oas-spec.yml is resolved via webpack loader (see above)
(i) NOTE2: The swagger setup here isn't complete. Refer to the docs

Spec files

Now spec files can be placed everywhere in the project.
This come in handy when e.g. developing REST API where spec files are more conveniently reside along the handlers.
(See options description and examples below).

Options

{
  loader: 'oas-loader',
  options: {
    // Scrape and include 'paths' into the final spec
    paths: true,

    // Glob to filter out paths files (default: '<path-to-oas-spec.yml>/**/*.spec.yml')
    pathsGlob: '<path-to-src>/**/*.endp.yml',
    
    // Scrape and include 'components.schemes' into the final spec
    schemas: true,
    
    // Glob to filter out schemes files (default: '<path-to-oas-spec.yml>/**/*.spec.yml')
    schemasGlob: '<path-to-src>/types/**/*.type.yml',
  
    // Scrape and include 'components.parameters' into the final spec
    params: true,
    
    // Glob to filter out params files (default: '<path-to-oas-spec.yml>/**/*.spec.yml')
    paramsGlob: '<path-to-src>/params/**/*.param.yml',
  
    // Scrape and include 'components.responses' into the final spec
    responses: true,
    
    // Glob to filter out responses files (default: '<path-to-oas-spec.yml>/**/*.spec.yml')
    responsesGlob: '<path-to-src>/responses/**/*.resp.yml',

    // Whether to set the 'info.version' from package.json version
    infoVersionFromPackageJson: '<path-to-package.json>',
  },
}

Examples

Typical REST API codebase

/src
  /handlers
    /fooHandler.ts
    /fooHandler.endp.yml

/doc
  /spec
    /types
    /params
    /responses
    /oas-spec.yml
  /src
    /index.ts
  /webpack
    webpack.config.js
    
/package.json
# fooHandler.endp.yml

/foo
  get:
    operationId: "GetFoo"
    summary: "..."

    # ...

Webpack config

// /doc/webpack/webspack.config.js

module.exports = {
    module: {
        rules: [
          {
            test: /oas-spec.yml$/,
            use: [
              {
                loader: 'oas-loader',
                options: {
                  paths: true,
                  pathsGlob: '/src/**/*.endp.yml',

                  schemas: true,
                  schemasGlob: '/doc/spec/types/**/*.type.yml',

                  params: true,
                  paramsGlob: '/doc/spec/params/**/*.param.yml',

                  responses: true,
                  responsesGlob: '/doc/spec/responses/**/*.resp.yml',

                  infoVersionFromPackageJson: '/package.json',
                },
              },
              'json-loader',
              'yaml-loader',
            ],
          },
        ],
    }
}

Excluding properties when merging schemes ($excludeProperties)

# ...
requestBody:
  content:
    application/json:
      schema:
        allOf:
          - $ref: "#/components/schemas/FooScheme"
          - $excludeProperties: # NOTE: oas-loader feature
              - bar

This will produce a scheme with request body being a result of merging FooScheme but excluding bar property out of it.

Merging response schemes ($merge)

# ...
responses:
  200:
    $merge:
      - $ref: "#/components/responses/CommonResponse"
      - description: "Non custom description"

Here the 200 response will be a result of merging CommonResponse with overriding description field

Status

The lib is in active development and might emit unexpected results for complex cases !

Publish