feat(openapi): endpoints.middleware to protect the spec and UI routes#4
Open
mixxtor wants to merge 1 commit into
Open
feat(openapi): endpoints.middleware to protect the spec and UI routes#4mixxtor wants to merge 1 commit into
mixxtor wants to merge 1 commit into
Conversation
Adds an optional `endpoints.middleware` factory to the config, applied to both the /openapi.json and /docs routes, so the docs can be served behind authentication instead of publicly. It is a factory (rather than a plain middleware value) because named middleware lives in #start/kernel, which cannot be imported from a config file — config files are evaluated before container services resolve. For the same reason, route registration moves from the provider's boot() to start(): start() runs once the app state is 'booted' (services resolve), and still before preloads run and the router commits, so the behavior of existing setups is unchanged. Verified end-to-end in an AdonisJS 7.3 app: anonymous requests to /docs and /openapi.json get redirected by middleware.auth(), no boot-order crashes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Body:
Closes #3
Adds an optional
endpoints.middlewarefactory applied to both the/openapi.jsonand/docsroutes, so the docs can be served behind authentication (e.g.middleware.auth()) instead of publicly.Design notes
#start/kernel, which cannot be imported at config-evaluation time (container services are not resolved yet; a top-level import crashes withCannot read properties of undefined (reading 'errorHandler')). The callback defers the import until the provider resolves it.boot()tostart()— required so the factory can import the kernel: services resolve only once the app state isbooted.start()runs right after that, still before preloads are imported and before the router commits, so nothing observable changes for existing setups (verified: routes register and match exactly as before whenmiddlewareis not configured).OneOrMore<T>is aliased locally intypes.tsbecause the real one lives in@poppinss/utils/types, which is not a direct dependency of this package.Route.use()accepts exactlyOneOrMore<MiddlewareFn | ParsedNamedMiddleware>.MiddlewareFnis itself a function, so avalue | factoryunion cannot be discriminated at runtime.endpoints.middlewarefield documented inguide/configuration.Verification
Ran the built package inside an AdonisJS 7.3 application (session
webguard):enabled: true+ the factory above → anonymousGET /docsandGET /openapi.jsonredirect to the login page (302), authenticated session renders Scalar as usual.enabled: truewithoutmiddleware→ behaves exactly as today.pnpm typecheck,pnpm build,pnpm lintclean inpackages/openapi.🤖 Generated with Claude Code