Skip to content

Commit

Permalink
perf: use optional chaining (#850)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fdawgs authored Jan 5, 2025
1 parent 4d6c358 commit 79384b1
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 18 deletions.
4 changes: 2 additions & 2 deletions lib/mode/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ module.exports = function (fastify, opts, done) {
}

function swagger (opts) {
if (opts && opts.yaml) {
if (opts?.yaml) {
if (cache.swaggerString) return cache.swaggerString
} else {
if (cache.swaggerObject) return cache.swaggerObject
}

if (opts && opts.yaml) {
if (opts?.yaml) {
const swaggerString = yaml.stringify(swaggerObject, { strict: false })
cache.swaggerString = swaggerString
return swaggerString
Expand Down
4 changes: 2 additions & 2 deletions lib/spec/openapi/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = function (opts, cache, routes, Ref, done) {
const defOpts = prepareDefaultOptions(opts)

return function (opts) {
if (opts && opts.yaml) {
if (opts?.yaml) {
if (cache.string) return cache.string
} else {
if (cache.object) return cache.object
Expand Down Expand Up @@ -74,7 +74,7 @@ module.exports = function (opts, cache, routes, Ref, done) {
? defOpts.transformObject({ openapiObject })
: openapiObject

if (opts && opts.yaml) {
if (opts?.yaml) {
cache.string = yaml.stringify(transformObjectResult, { strict: false })
return cache.string
}
Expand Down
14 changes: 7 additions & 7 deletions lib/spec/openapi/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ function schemaToMediaRecursive (schema) {
function resolveBodyParams (body, schema, consumes, ref) {
const resolved = convertJsonSchemaToOpenapi3(ref.resolve(schema))

if (resolved.content && resolved.content[Object.keys(resolved.content)[0]].schema) {
if (resolved.content?.[Object.keys(resolved.content)[0]].schema) {
for (const contentType in schema.content) {
body.content[contentType] = schemaToMediaRecursive(resolved.content[contentType].schema)
}
Expand All @@ -279,11 +279,11 @@ function resolveBodyParams (body, schema, consumes, ref) {
body.content[consume] = media
})

if (resolved && resolved.required && resolved.required.length) {
if (resolved?.required?.length) {
body.required = true
}

if (resolved && resolved.description) {
if (resolved?.description) {
body.description = resolved.description
}
}
Expand All @@ -294,7 +294,7 @@ function resolveCommonParams (container, parameters, schema, ref, sharedSchemas,
let resolved = convertJsonSchemaToOpenapi3(ref.resolve(schema))

// if the resolved definition is in global schema
if (resolved.$ref && resolved.$ref.startsWith(schemasPath)) {
if (resolved.$ref?.startsWith(schemasPath)) {
const parts = resolved.$ref.split(schemasPath)
const pathParts = parts[1].split('/')
resolved = pathParts.reduce((resolved, pathPart) => resolved[pathPart], ref.definitions().definitions)
Expand Down Expand Up @@ -361,7 +361,7 @@ function resolveResponse (fastifyResponseJson, produces, ref) {

// add schema when type is not 'null'
if (rawJsonSchema.type !== 'null') {
if (resolved.content && resolved.content[Object.keys(resolved.content)[0]].schema) {
if (resolved.content?.[Object.keys(resolved.content)[0]].schema) {
response.content = resolved.content
} else {
const content = {}
Expand Down Expand Up @@ -446,8 +446,8 @@ function prepareOpenapiMethod (schema, ref, openapiObject, url) {

// Parse out the security prop keys to ignore
const securityIgnores = [
...(openapiObject && openapiObject.security ? openapiObject.security : []),
...(schema && schema.security ? schema.security : [])
...(openapiObject?.security || []),
...(schema?.security || [])
]
.reduce((acc, securitySchemeGroup) => {
Object.keys(securitySchemeGroup).forEach((securitySchemeLabel) => {
Expand Down
4 changes: 2 additions & 2 deletions lib/spec/swagger/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = function (opts, cache, routes, Ref, done) {
const defOpts = prepareDefaultOptions(opts)

return function (opts) {
if (opts && opts.yaml) {
if (opts?.yaml) {
if (cache.string) return cache.string
} else {
if (cache.object) return cache.object
Expand Down Expand Up @@ -66,7 +66,7 @@ module.exports = function (opts, cache, routes, Ref, done) {
? defOpts.transformObject({ swaggerObject })
: swaggerObject

if (opts && opts.yaml) {
if (opts?.yaml) {
cache.string = yaml.stringify(transformObjectResult, { strict: false })
return cache.string
}
Expand Down
6 changes: 3 additions & 3 deletions lib/spec/swagger/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ function resolveBodyParams (parameters, schema, ref) {
parameters.push({
name: 'body',
in: 'body',
description: resolved && resolved.description ? resolved.description : undefined,
description: resolved?.description,
schema: resolved
})
}
Expand Down Expand Up @@ -274,8 +274,8 @@ function prepareSwaggerMethod (schema, ref, swaggerObject, url) {

// Parse out the security prop keys to ignore
const securityIgnores = [
...(swaggerObject && swaggerObject.security ? swaggerObject.security : []),
...(schema && schema.security ? schema.security : [])
...(swaggerObject?.security || []),
...(schema?.security || [])
]
.reduce((acc, securitySchemeGroup) => {
Object.keys(securitySchemeGroup).forEach((securitySchemeLabel) => {
Expand Down
4 changes: 2 additions & 2 deletions lib/util/should-route-hide.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
function shouldRouteHide (schema, opts) {
const { hiddenTag, hideUntagged } = opts

if (schema && schema.hide) {
if (schema?.hide) {
return true
}

const tags = (schema && schema.tags) || []
const tags = schema?.tags || []

if (tags.length === 0 && hideUntagged) {
return true
Expand Down

0 comments on commit 79384b1

Please sign in to comment.