-
-
Notifications
You must be signed in to change notification settings - Fork 169
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/support generate swagge for typescript (#608) #641
Changes from 7 commits
2a6092c
591a701
ea3f814
aae3516
fdf941e
2a9e49f
05f0167
0e476fc
69a78aa
05be969
670e2a4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { FastifyInstance, FastifyPluginCallback } from "fastify"; | ||
|
||
const plugin: FastifyPluginCallback = function ( | ||
fastify: FastifyInstance, | ||
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(); | ||
}; | ||
|
||
export = plugin; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,8 +4,11 @@ const url = require('url') | |
const semver = require('semver') | ||
const pkgUp = require('pkg-up') | ||
const resolveFrom = require('resolve-from') | ||
|
||
const moduleSupport = semver.satisfies(process.version, '>= 14 || >= 12.17.0 < 13.0.0') | ||
const tsNode = require('ts-node') | ||
const moduleSupport = semver.satisfies( | ||
process.version, | ||
'>= 14 || >= 12.17.0 < 13.0.0' | ||
) | ||
|
||
function exit (message) { | ||
if (message instanceof Error) { | ||
|
@@ -55,33 +58,54 @@ async function getPackageType (cwd) { | |
function getScriptType (fname, packageType) { | ||
const modulePattern = /\.mjs$/i | ||
const commonjsPattern = /\.cjs$/i | ||
return (modulePattern.test(fname) ? 'module' : commonjsPattern.test(fname) ? 'commonjs' : packageType) || 'commonjs' | ||
return ( | ||
(modulePattern.test(fname) | ||
? 'module' | ||
: commonjsPattern.test(fname) | ||
? 'commonjs' | ||
: packageType) || 'commonjs' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why this change? |
||
) | ||
} | ||
|
||
async function requireServerPluginFromPath (modulePath) { | ||
const resolvedModulePath = path.resolve(process.cwd(), modulePath) | ||
|
||
if (!fs.existsSync(resolvedModulePath)) { | ||
throw new Error(`${resolvedModulePath} doesn't exist within ${process.cwd()}`) | ||
throw new Error( | ||
`${resolvedModulePath} doesn't exist within ${process.cwd()}` | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why this change? |
||
} | ||
|
||
const packageType = await getPackageType(resolvedModulePath) | ||
|
||
const type = getScriptType(resolvedModulePath, packageType) | ||
|
||
let serverPlugin | ||
tsNode.register({ | ||
transpileOnly: true, | ||
skipProject: true | ||
}) | ||
if (type === 'module') { | ||
if (moduleSupport) { | ||
serverPlugin = await import(url.pathToFileURL(resolvedModulePath).href) | ||
} else { | ||
throw new Error(`fastify-cli cannot import plugin at '${resolvedModulePath}'. Your version of node does not support ES modules. To fix this error upgrade to Node 14 or use CommonJS syntax.`) | ||
throw new Error( | ||
`fastify-cli cannot import plugin at '${resolvedModulePath}'. Your version of node does not support ES modules. To fix this error upgrade to Node 14 or use CommonJS syntax.` | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why this change? |
||
} | ||
} else { | ||
serverPlugin = require(resolvedModulePath) | ||
} | ||
|
||
if (isInvalidAsyncPlugin(type === 'commonjs' ? serverPlugin : serverPlugin.default)) { | ||
throw new Error('Async/Await plugin function should contain 2 arguments. ' + | ||
'Refer to documentation for more information.') | ||
if ( | ||
isInvalidAsyncPlugin( | ||
type === 'commonjs' ? serverPlugin : serverPlugin.default | ||
) | ||
) { | ||
throw new Error( | ||
'Async/Await plugin function should contain 2 arguments. ' + | ||
'Refer to documentation for more information.' | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why this change? |
||
} | ||
|
||
return serverPlugin | ||
|
@@ -94,14 +118,23 @@ function showHelpForCommand (commandName) { | |
console.log(fs.readFileSync(helpFilePath, 'utf8')) | ||
exit() | ||
} catch (e) { | ||
exit(`unable to get help for command "${commandName}"`) | ||
exit(`unable to get help for command '${commandName}'`) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why this change? |
||
} | ||
} | ||
|
||
function isKubernetes () { | ||
// Detection based on https://kubernetes.io/docs/reference/kubectl/#in-cluster-authentication-and-namespace-overrides | ||
return process.env.KUBERNETES_SERVICE_HOST !== undefined || | ||
return ( | ||
process.env.KUBERNETES_SERVICE_HOST !== undefined || | ||
fs.existsSync('/run/secrets/kubernetes.io/serviceaccount/token') | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why this change? |
||
} | ||
|
||
module.exports = { isKubernetes, exit, requireModule, requireFastifyForModule, showHelpForCommand, requireServerPluginFromPath } | ||
module.exports = { | ||
isKubernetes, | ||
exit, | ||
requireModule, | ||
requireFastifyForModule, | ||
showHelpForCommand, | ||
requireServerPluginFromPath | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why this change? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why this change?