-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a generate-swagger command (#567)
* Add a generate-swagger command * Add unit test * Fix lint * Add docs
- Loading branch information
1 parent
33e5179
commit 1f7d9df
Showing
9 changed files
with
179 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
#! /usr/bin/env node | ||
|
||
'use strict' | ||
|
||
const parseArgs = require('./args') | ||
const log = require('./log') | ||
const { | ||
exit, | ||
requireFastifyForModule, | ||
requireServerPluginFromPath, | ||
showHelpForCommand | ||
} = require('./util') | ||
const fp = require('fastify-plugin') | ||
|
||
let Fastify = null | ||
|
||
function loadModules (opts) { | ||
try { | ||
Fastify = requireFastifyForModule(opts._[0]).module | ||
} catch (e) { | ||
module.exports.stop(e) | ||
} | ||
} | ||
|
||
async function generateSwagger (args) { | ||
const opts = parseArgs(args) | ||
if (opts.help) { | ||
return showHelpForCommand('generate-swagger') | ||
} | ||
|
||
if (opts._.length !== 1) { | ||
console.error('Missing the required file parameter\n') | ||
return showHelpForCommand('generate-swagger') | ||
} | ||
|
||
// we start crashing on unhandledRejection | ||
require('make-promises-safe') | ||
|
||
loadModules(opts) | ||
|
||
const fastify = await runFastify(opts) | ||
try { | ||
if (fastify.swagger == null) { | ||
log('error', '@fastify/swagger plugin not installed') | ||
process.exit(1) | ||
} | ||
|
||
return JSON.stringify(fastify.swagger(), undefined, 2) | ||
} finally { | ||
fastify.close() | ||
} | ||
} | ||
|
||
async function runFastify (opts) { | ||
require('dotenv').config() | ||
|
||
let file = null | ||
|
||
try { | ||
file = await requireServerPluginFromPath(opts._[0]) | ||
} catch (e) { | ||
return module.exports.stop(e) | ||
} | ||
|
||
const fastify = Fastify(opts.options) | ||
|
||
const pluginOptions = {} | ||
if (opts.prefix) { | ||
pluginOptions.prefix = opts.prefix | ||
} | ||
|
||
await fastify.register(fp(file), pluginOptions) | ||
await fastify.ready() | ||
|
||
return fastify | ||
} | ||
|
||
function stop (message) { | ||
exit(message) | ||
} | ||
|
||
function cli (args) { | ||
return generateSwagger(args).then(swagger => { | ||
process.stdout.write(swagger + '\n') | ||
}) | ||
} | ||
|
||
module.exports = { cli, stop, generateSwagger } | ||
|
||
if (require.main === module) { | ||
cli(process.argv.slice(2)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Usage: fastify generate-plugin [opts] <file> [--] [<plugin-options>] | ||
|
||
Generate Swagger/OpenAPI schema for a project using @fastify/cli. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
const path = require('path') | ||
const t = require('tap') | ||
const { test } = t | ||
const { generateSwagger } = require('../generate-swagger') | ||
|
||
const swaggerplugindir = path.join(__dirname, 'swaggerplugindir') | ||
const swaggerplugin = path.join(swaggerplugindir, 'plugin.js') | ||
|
||
test('should generate swagger', async (t) => { | ||
t.plan(1) | ||
|
||
try { | ||
const swagger = JSON.parse(await generateSwagger([swaggerplugin])) | ||
t.equal(swagger.openapi, '3.0.3') | ||
} catch (err) { | ||
t.error(err) | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"name": "swaggerplugindir", | ||
"version": "1.0.0", | ||
"description": "test for generator-swagger", | ||
"main": "plugin.js", | ||
"scripts": { | ||
"test": "" | ||
}, | ||
"dependencies": { | ||
"fastify-plugin": "^1.4.0" | ||
}, | ||
"author": "", | ||
"license": "ISC" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
'use strict' | ||
|
||
const fp = require('fastify-plugin') | ||
|
||
module.exports = fp(function (fastify, opts, next) { | ||
fastify.decorate('swagger', function () { | ||
return { | ||
openapi: '3.0.3', | ||
info: { | ||
version: '8.1.0', | ||
title: '@fastify/swagger' | ||
}, | ||
components: { | ||
schemas: {} | ||
}, | ||
paths: { | ||
'/': { | ||
get: { | ||
responses: { | ||
200: { | ||
description: 'Default Response' | ||
} | ||
} | ||
} | ||
}, | ||
'/example/': { | ||
get: { | ||
responses: { | ||
200: { | ||
description: 'Default Response' | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}) | ||
next() | ||
}) |