1.0.0 • Published 6 years ago

swagger-strip v1.0.0

Weekly downloads
5
License
MIT
Repository
github
Last release
6 years ago

swagger-strip

build status Test coverage NPM version NPM Downloads

strip swagger from one api

Usage

install

npm install swagger-strip

default usage

A demo, data from swagger deafult demo

const swaggerStrip = require('swagger-strip');
swaggerStrip(sourceObject, '/pet/findByStatus');

// => result
{
    paths: {
        tags: [ 'pet' ],
        summary: 'Finds Pets by status',
        ...
    },
    refs: { 
        '#/definitions/Pet': {
            type: 'object',
            required: [Array],
            properties: [Object],
            xml: [Object]
        },
        '#/definitions/Category': {...},
        '#/definitions/Tag': {...} },
    tags: [{
        name: 'pet',
        description: 'Everything about your Pets',
        ...
    }]
}

parse json file

const fs = require('fs');
const path = require('path');
const swaggerStrip = require('swagger-strip');

const sourceJsonFile = path.join(__dirname, './test.json');
const sourceObject = JSON.parse(fs.readFileSync(sourceJsonFile, 'utf8'));

const res = swaggerStrip(sourceObject, '/pet/findByStatus');

parse yml file

parse .yml with js-yaml or what you like;

const sourceYmlFile = path.join(__dirname, './data/test.yml');
const sourceObject = yaml.safeLoad(fs.readFileSync(sourceYmlFile, 'utf8'));
const stripResult = swaggerStrip(sourceObject, '/pet', {
    method: 'post'
});

API

swaggerStrip(sourceObject, path, options)

sourceObject

swagger source data, maybe you should parse json/yml file to object first

Type: Object

Default: {}

path

some api you want to strip

Type: string

Default: ''

Example: '/pet/findByStatus'

options

Type: Object

Params:

  • method: api method
    • when use swaggerStrip(obj, '/pets'), return paths will equal obj['paths']['/pet/findByStatus']['get']
    • when use swaggerStrip(obj, '/pets', {method: 'post'}), return paths equal obj['paths']['/pet/findByStatus']['post']

Default:

{
    method: `'get'`
}

Development

npm run install

npm run test

License

MIT