Skip to content
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

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"pkg-up": "^3.1.0",
"resolve-from": "^5.0.0",
"semver": "^7.3.5",
"ts-node": "^10.9.1",
"yargs-parser": "^21.1.1"
},
"devDependencies": {
Expand All @@ -82,11 +83,11 @@
"standard": "^17.0.0",
"strip-ansi": "^6.0.1",
"tap": "^16.1.0",
"ts-node": "^10.4.0",
"ts-standard": "^12.0.1",
"tsd": "^0.16.0",
"typescript": "^4.5.4",
"walker": "^1.0.8"
"walker": "^1.0.8",
"ts-node": "^10.9.1"
},
"tsd": {
"directory": "test"
Expand Down
11 changes: 11 additions & 0 deletions test/generate-swagger.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const { generateSwagger } = require('../generate-swagger')

const swaggerplugindir = path.join(__dirname, 'swaggerplugindir')
const swaggerplugin = path.join(swaggerplugindir, 'plugin.js')
const swaggerpluginTs = path.join(swaggerplugindir, 'plugin.ts')

test('should generate swagger', async (t) => {
t.plan(1)
Expand All @@ -16,3 +17,13 @@ test('should generate swagger', async (t) => {
t.error(err)
}
})
test('should generate swagger with ts file', async (t) => {
t.plan(1)

try {
const swagger = JSON.parse(await generateSwagger([swaggerpluginTs]))
t.equal(swagger.openapi, '3.0.3')
} catch (err) {
t.error(err)
}
})
43 changes: 43 additions & 0 deletions test/swaggerplugindir/plugin.ts
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;
55 changes: 44 additions & 11 deletions util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this change?


function exit (message) {
if (message instanceof Error) {
Expand Down Expand Up @@ -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'
Copy link
Member

Choose a reason for hiding this comment

The 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()}`
)
Copy link
Member

Choose a reason for hiding this comment

The 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.`
)
Copy link
Member

Choose a reason for hiding this comment

The 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.'
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this change?

}

return serverPlugin
Expand All @@ -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}'`)
Copy link
Member

Choose a reason for hiding this comment

The 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')
)
Copy link
Member

Choose a reason for hiding this comment

The 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
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this change?