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

swaggertooth

v3.0.6

Published

Written in Typescript, this package is intended for generating sample requests & responses based on the supplied swagger yaml/json files.

Downloads

79

Readme

SwaggerTooth

Written in Typescript, this package is intended for generating sample requests & responses based on the supplied swagger yaml/json files.

SwaggerTooth fully supports $ref object references, complexly nested arrays and objects, allOf polymorphism when generating the samples.

Usage

Node.js and command line tool is available as well as a simple Angular-compatible class.

Node.js

Configurtations in file (e.g. swaggertooth.conf.json)

{
  sourceSwaggerPath: "some/folder/swagger.yaml", // any swagger file path
  outputFolderPath: "your/project/src/assets/swagger", // any folder
  
  angularServePath: "assets/swagger" // optional Angular support
}
const fs = require('fs');
const SwaggerTooth = require('swaggertooth').SwaggerTooth;
const swaggerToothConfig = SwaggerTooth.configFile('swaggertooth.conf.json');

const tooth = new SwaggerTooth(swaggerToothConfig);

tooth.generateExamples();

Command Line

npm i swaggertooth --save-dev
npx swaggertooth generate-example --input swagger.yaml --output my/folder

Sample Generation

openapi: 3.0.0
info:
  title: SwaggerTooth Test
servers:
  - url: /app
    description: base api path
paths:
  /test/{param}:
    post:
      requestBody:
        content:
          application/json: # supported: json, xml, yaml
            schema:
              type: object
              properties:
                email:
                  type: string
                  format: email
      responses:
        200:
          content:
            application/json:
              schema:
                type: object
                properties:
                  my_id:
                    type: number
                  my_uuid:
                    type: string
                    format: uuid
                  endpoint:
                    type: string
                    format: uri
        400:
          content:
            application/json:
              schema:
                type: object
                properties:
                  status:
                    type: string
                    format: number
                  message:
                    type: string
                    format: uri
                    example: Reached Forbidden!

Above swagger file as source will output the following to the output folder:

// FILENAME: app.test.$param.post.request.json
{
  "email": "[email protected]"
}

// FILENAME: app.test.$param.post.response.200.json
{
  "my_id": 1,
  "my_uuid": "2ad75fdb-6e18-4107-a561-1dda9a14b5a9",
  "endpoint": "https://103.196.56.180:61045/test-e9a671"
}

// FILENAME: app.test.$param.post.response.400.json
{
  "status": "400",
  "message": "Reached Forbidden!"
}

If content type is set to application/xml:

<!-- app.test.$param.post.request.xml -->
<email>[email protected]</email>

<!-- app.test.$param.post.response.200.xml -->
<root>
  <my_id>1</my_id>
  <my_uuid>eb157a89-fb57-49b1-9aa1-f7200f754b0f</my_uuid>
  <endpoint>https://231.248.219.173:45884/test-7a906d</endpoint>
</root>

<!-- app.test.$param.post.response.400.xml -->
<root>
  <status>400</status>
  <message>Reached Forbidden!</message>
</root>

If content type is set to application/yaml:

# app.test.$param.post.request.yaml
email: [email protected]

# app.test.$param.post.response.200.yaml
my_id: 1
my_uuid: c978c555-b7de-4460-88a6-8625517117c7
endpoint: 'https://170.130.249.140:32434/test-6773f5'

# app.test.$param.post.response.400.yaml
status: '400'
message: Reached Forbidden!

The generation function will also output generation.info.json file to the output folder to provide information on what has been generated and what options were given to the tool.

{
  "info": {
    "totalPaths": 1,
    "totalItems": 3,
    "totalRequests": 1,
    "totalResponses": 2
  },
  "configs": { "sourceSwaggerPath": "test/api.yaml", "outputFolderPath": "output" },
  "traverseOptions": { "examplesPickFirst": true, "mock": true },
  "paths": {
    "POST /test/{param}": {
      "requests": [
        "app.test.$param.post.request.json"
      ],
      "responses": [
        "app.test.$param.post.response.200.json",
        "app.test.$param.post.response.400.json"
      ]
    }
  },
  "warnings": [],
  "errors": []
}

Angular Usage

You can generate samples to your static folder (e.g. assets/swagger), and fetch the generated sample requests and responses from Angular by using provided fetcher class called SwaggerToothAngular from swaggertooth/angular.js.

declare var require: any;
const SwaggerToothAngular = require('swaggertooth/angular.js').SwaggerToothAngular;
const swaggerToothConfig = require('swaggertooth.conf.json');
// config should have `angularServePath` value (default: 'assets/swagger')

const fetch = new SwaggerToothAngular(swaggerToothConfig);
fetch.sampleResponse('POST /test/{param}', 200)
  .then(sampleResponse => {
    /** sampleResponse: {
     *    my_id: 1,
     *    my_uuid: "ea2ad296-4419-4a9f-842a-a5a76bd516bd",
     *    endpoint: "https://166.46.112.210:58592/test-e60121"
     * } */
  });

fetch.sampleRequest('POST /test/{param}')
  .then(res => {
    // Some sample request
  });

// Function signatures:
// fetch.sampleRequest<T>(path: string, asString?: boolean = false): Promise<T>
// fetch.sampleResponse<T>(path: string, statusCode: number, asString?: boolean = false): Promise<T>

License

MIT