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

chore: export formatParamUrl #748

Merged
merged 1 commit into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ declare namespace fastifySwagger {
preHandler?: preHandlerHookHandler,
}>

export function formatParamUrl (paramUrl: string): string

export const fastifySwagger: FastifySwagger
export { fastifySwagger as default }
}
Expand Down
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const fp = require('fastify-plugin')
const { formatParamUrl } = require('./lib/util/format-param-url')

function fastifySwagger (fastify, opts, next) {
// by default the mode is dynamic, as plugin initially was developed
Expand Down Expand Up @@ -29,3 +30,5 @@ module.exports = fp(fastifySwagger, {
})
module.exports.fastifySwagger = fastifySwagger
module.exports.default = fastifySwagger

module.exports.formatParamUrl = formatParamUrl
3 changes: 2 additions & 1 deletion lib/mode/dynamic.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const { addHook, resolveSwaggerFunction } = require('../util/common')
const { addHook } = require('../util/add-hook')
const { resolveSwaggerFunction } = require('../util/resolve-swagger-function')

module.exports = function (fastify, opts, done) {
opts = Object.assign({}, {
Expand Down
2 changes: 1 addition & 1 deletion lib/spec/openapi/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const yaml = require('yaml')
const { shouldRouteHide } = require('../../util/common')
const { shouldRouteHide } = require('../../util/should-route-hide')
const { prepareDefaultOptions, prepareOpenapiObject, prepareOpenapiMethod, prepareOpenapiSchemas, normalizeUrl, resolveServerUrls } = require('./utils')

module.exports = function (opts, cache, routes, Ref, done) {
Expand Down
4 changes: 3 additions & 1 deletion lib/spec/openapi/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
'use strict'

const { readPackageJson, formatParamUrl, resolveLocalRef } = require('../../util/common')
const { readPackageJson } = require('../../util/read-package-json')
const { formatParamUrl } = require('../../util/format-param-url')
const { resolveLocalRef } = require('../../util/resolve-local-ref')
const { xResponseDescription, xConsume, xExamples } = require('../../constants')
const { rawRequired } = require('../../symbols')

Expand Down
2 changes: 1 addition & 1 deletion lib/spec/swagger/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const yaml = require('yaml')
const { shouldRouteHide } = require('../../util/common')
const { shouldRouteHide } = require('../../util/should-route-hide')
const { prepareDefaultOptions, prepareSwaggerObject, prepareSwaggerMethod, normalizeUrl, prepareSwaggerDefinitions } = require('./utils')

module.exports = function (opts, cache, routes, Ref, done) {
Expand Down
4 changes: 3 additions & 1 deletion lib/spec/swagger/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
'use strict'

const { readPackageJson, formatParamUrl, resolveLocalRef } = require('../../util/common')
const { readPackageJson } = require('../../util/read-package-json')
const { formatParamUrl } = require('../../util/format-param-url')
const { resolveLocalRef } = require('../../util/resolve-local-ref')
const { xResponseDescription, xConsume } = require('../../constants')

function prepareDefaultOptions (opts) {
Expand Down
77 changes: 77 additions & 0 deletions lib/util/add-hook.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
'use strict'

const Ref = require('json-schema-resolver')
const cloner = require('rfdc')({ proto: true, circles: false })

function addHook (fastify, pluginOptions) {
const routes = []
const sharedSchemasMap = new Map()

fastify.addHook('onRoute', (routeOptions) => {
const routeConfig = routeOptions.config || {}
const swaggerConfig = routeConfig.swagger || {}
if (routeOptions.method === 'HEAD' && pluginOptions.exposeHeadRoutes !== true && swaggerConfig.exposeHeadRoute !== true) {
return
}

if (
routeOptions.method === 'HEAD' &&
routeOptions.schema !== undefined &&
routeOptions.schema.operationId !== undefined
) {
routes.push(
// If two routes with operationId are added to the swagger
// object, it is no longer valid.
// therefore we suffix the operationId with `-head`.
Object.assign({}, routeOptions, {
schema: Object.assign({}, routeOptions.schema, {
operationId: `${routeOptions.schema.operationId}-head`
})
})
)
return
}

routes.push(routeOptions)
})

fastify.addHook('onRegister', async (instance) => {
// we need to wait the ready event to get all the .getSchemas()
// otherwise it will be empty
// TODO: better handle for schemaId
// when schemaId is the same in difference instance
// the latter will lost
instance.addHook('onReady', (done) => {
const allSchemas = instance.getSchemas()
for (const schemaId of Object.keys(allSchemas)) {
sharedSchemasMap.set(schemaId, allSchemas[schemaId])
}
done()
})
})

fastify.addHook('onReady', (done) => {
const allSchemas = fastify.getSchemas()
for (const schemaId of Object.keys(allSchemas)) {
// it is the top-level, we do not expect to have duplicate id
sharedSchemasMap.set(schemaId, allSchemas[schemaId])
}
done()
})

return {
routes,
Ref () {
const externalSchemas = cloner(Array.from(sharedSchemasMap.values()))
return Ref(Object.assign(
{ applicationUri: 'todo.com' },
pluginOptions.refResolver,
{ clone: true, externalSchemas })
)
}
}
}

module.exports = {
addHook
}
240 changes: 0 additions & 240 deletions lib/util/common.js

This file was deleted.

Loading