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

xref

v2.1.0

Published

Split large schema files and link them.

Downloads

3

Readme

Split and link large API spec files, used by tools such as Swagger

This package helps to split the larger API spec files in to smaller (JSON or YAML) files and link them.

Basic advantages:


  • Feasible to maintain large spec or schema files
  • Reuse same API spec definitions between different projects

How to use


in API spec

Look at the below definition section from a regular Swagger API spec file.

definitions:
  Product:
    properties:
      product_id:
        type: string
        description: Unique identifier.
      description:
        type: string
        description: Description of product.
  Error:
    properties:
      code:
        type: integer
        format: int32
      message:
        type: string
      fields:
        type: string

Above file can be split as follows.

  • product.yml
Product:
  properties:
    product_id:
      type: string
      description: Unique identifier.
    description:
      type: string
      description: Description of product.
  • error.yml
Error:
  properties:
    code:
      type: integer
      format: int32
    message:
      type: string
    fields:
      type: string

This how to link them in to main file.

definitions:
   Product:
    $href: "#xref#./product.yml"
   Error:
    $href: "#xref#./error.yml"

The xref tag indicates the link to the external files.

You can add more add more properties which are not mentioned in the splitted as follows.

 Error:
   $href: "#xref#./error.yml"
   properties:
    module_id:
      type: string
      description: Unique identifier.
    module_name:
      type: string

Deep paths to inner object

You can specify a path to a object. eg: consider following definition,

{
  "success" : {
    "http_200": {
      "description": "An array of price estimates by product",
      "schema": {
        "type": "array",
        "items": {
          "$ref": "#/definitions/PriceEstimate"
        }
      }
    },
    "http_400": {
        "description": "An array of price estimates by product",
        "schema": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/PriceEstimate"
          }
        }
      }
    
  }
}

You can link http_200 as follows. (using the path "#success/http_200")

{
  "http_responses": {
    "200": {
      "$ref": "#xref#./test/fixtures/json/split/definitions/response.json#success/http_200"
    },
    "default": {
      "description": "Unexpected error",
      "schema": {
        "$ref": "#/definitions/Error"
      }
    }
  }
}

Path to the splitted file should be valid and accessible


in your app

refer the below mentioned code

'use strict'
const swaggerTools = require('swagger-tools');
const xrefModule = require('xref');
xrefModule.xref('./test/fixtures/yaml/split/uber.yaml', (err, spec) => {
	if (err) {
    	return err;
    }
    swaggerTools.initializeMiddleware(spec, (middleware) => {
    /*
    continue your magic
    */
    })
});

CLI tools


xref_cli

This tool is designed to view the entire file after merge. It is bit unfeasible to debug, when file is split in to multiple files. This tool will help you to view the entire spec as one file.

usage:
./node_modules/.bin/xref_cli -s <path to your main spec file> -o <output format. default is YAML>

eg:

  • To generate YAML output:
./node_modules/.bin/xref_cli -s test/fixtures/yaml/split/uber.yaml
./node_modules/.bin/xref_cli -s test/fixtures/yaml/split/uber.yaml -o yaml
  • To generate JSON output:
./node_modules/.bin/xref_cli -s test/fixtures/yaml/split/uber.yaml -o json
  • To save the ouput to a file:
./node_modules/.bin/xref_cli -s test/fixtures/yaml/split/uber.yaml > spec.yaml

xref_swagger_validator

This tool is designed to run the validation through Swagger Tools as mentioned below

swagger-tools validate spec/api.yml
usage:
./node_modules/.bin/xref_swagger_validator -s <path to your main spec file>

eg:

./node_modules/.bin/xref_swagger_validator -s test/fixtures/yaml/split/uber.yaml