Skip to content

Commit

Permalink
feat: allow async factories
Browse files Browse the repository at this point in the history
  • Loading branch information
devthejo committed Dec 15, 2023
1 parent cb3c3a6 commit 6e1fdaa
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
10 changes: 10 additions & 0 deletions packages/core/utils/object/traverse-async.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = async function traverseAsync(o, fn, keys = []) {
for (const [k, v] of Object.entries(o)) {
const keysBranch = [...keys]
keysBranch.push(k)
fn.apply(this, [k, v, o, keysBranch])
if (o[k] !== null && typeof o[k] === "object") {
await traverseAsync(o[k], fn, keysBranch)
}
}
}
22 changes: 12 additions & 10 deletions plugins/oa/openapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const camelCase = require("lodash.camelcase")
const OpenApiValidator = require("express-openapi-validator")
const { default: OpenAPISchemaValidator } = require("openapi-schema-validator")
const { reqCtx } = require("@modjo/express/ctx")
const traverse = require("@modjo/core/utils/object/traverse")
const traverseAsync = require("@modjo/core/utils/object/traverse-async")
const createOptions = require("@modjo/core/utils/schema/options")
const deepMapKeys = require("@modjo/core/utils/object/deep-map-keys")
const ctx = require("./ctx")
Expand Down Expand Up @@ -244,7 +244,7 @@ module.exports = async function createOpenApi(options = {}) {
const addonHandler = factory(addonsHandlers)
addonHandlers[name] = addonHandler
}
traverse(addonTree, addonLoader)
await traverseAsync(addonTree, addonLoader)
}

const operationsRouter = express.Router({ strict: true, caseSensitive: true })
Expand Down Expand Up @@ -296,10 +296,10 @@ module.exports = async function createOpenApi(options = {}) {
})
}
}
traverse(operationsTree, operationMethodLoader)
await traverseAsync(operationsTree, operationMethodLoader)

// load operations
function operationLoader(filename, factory, _dirFiles, keys) {
async function operationLoader(filename, factory, _dirFiles, keys) {
if (typeof factory !== "function") {
return
}
Expand Down Expand Up @@ -418,9 +418,11 @@ module.exports = async function createOpenApi(options = {}) {
return `:${param}`
}
)
const handlers = (
Array.isArray(handlerStack) ? [...handlerStack] : [handlerStack]
).map((handler) => {
let handlers = await handlerStack
handlers = await Promise.all(
Array.isArray(handlers) ? [...handlers] : [handlers]
)
handlers = handlers.map((handler) => {
return async (req, res, next) => {
try {
const result = await handler(req, res, next)
Expand All @@ -439,7 +441,7 @@ module.exports = async function createOpenApi(options = {}) {
operationsRouter[method](expressFormatedOperationPath, ...handlers)
}
}
traverse(operationsTree, operationLoader)
await traverseAsync(operationsTree, operationLoader)

// load formats
const formats = []
Expand All @@ -450,7 +452,7 @@ module.exports = async function createOpenApi(options = {}) {
}
formats.push(format)
}
traverse(formatsTree, formatsLoader)
await traverseAsync(formatsTree, formatsLoader)

// load security handlers
const securityHandlers = {}
Expand All @@ -468,7 +470,7 @@ module.exports = async function createOpenApi(options = {}) {
return false
}
}
traverse(securityTree, securityLoader)
await traverseAsync(securityTree, securityLoader)

// errors handling
function errorMiddleware(err, _, res, _next) {
Expand Down

0 comments on commit 6e1fdaa

Please sign in to comment.