From ddfd757277c15f4d6577c28e708aa7f5924a1347 Mon Sep 17 00:00:00 2001 From: Shahed Nasser Date: Thu, 30 May 2024 16:47:16 +0300 Subject: [PATCH] fix(docs-util): recognize optional zod parameters (#7538) * fix(docs-util): recognize optional zod parameters * remove admin/auth as an authenticated route path --- .../src/classes/kinds/oas.ts | 43 +++++++++++++++---- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/www/utils/packages/docblock-generator/src/classes/kinds/oas.ts b/www/utils/packages/docblock-generator/src/classes/kinds/oas.ts index 546f46a426..3c98a4abaa 100644 --- a/www/utils/packages/docblock-generator/src/classes/kinds/oas.ts +++ b/www/utils/packages/docblock-generator/src/classes/kinds/oas.ts @@ -729,11 +729,9 @@ class OasKindGenerator extends FunctionKindGenerator { statement.getText().includes("AUTHENTICATE = false") ) const isAdminAuthenticated = - !isAuthenticationDisabled && - oasPath.startsWith("admin") && - !oasPath.startsWith("admin/auth") + !isAuthenticationDisabled && oasPath.startsWith("admin") const isStoreAuthenticated = - !isAuthenticationDisabled && oasPath.startsWith("store/me") + !isAuthenticationDisabled && oasPath.startsWith("store/customers/me") const isAuthenticated = isAdminAuthenticated || isStoreAuthenticated return { @@ -1601,13 +1599,28 @@ class OasKindGenerator extends FunctionKindGenerator { * Check whether a symbol is required. * * @param symbol - The symbol to check. + * @param level - The current recursion level to avoid max-stack error * @returns Whether the symbol is required. */ - isRequired(symbol: ts.Symbol): boolean { + isRequired(symbol: ts.Symbol, level = 0): boolean { let isRequired = true const checkNode = (node: ts.Node) => { - if (node.kind === ts.SyntaxKind.QuestionToken) { - isRequired = false + switch (node.kind) { + case ts.SyntaxKind.CallExpression: + const expression = + "expression" in node + ? (node.expression as ts.Expression) + : undefined + + if (!expression || !("name" in expression)) { + break + } + + isRequired = + (expression.name as ts.Identifier).getText() !== "optional" + break + case ts.SyntaxKind.QuestionToken: + isRequired = false } if (!isRequired) { @@ -1616,7 +1629,21 @@ class OasKindGenerator extends FunctionKindGenerator { node.forEachChild(checkNode) } - symbol.valueDeclaration?.forEachChild(checkNode) + if ( + !symbol.valueDeclaration && + symbol.declarations?.length && + "symbol" in symbol.declarations[0] && + level < this.MAX_LEVEL + ) { + return this.isRequired( + symbol.declarations[0].symbol as ts.Symbol, + level + 1 + ) + } + + ;(symbol.valueDeclaration || symbol.declarations?.[0])?.forEachChild( + checkNode + ) return isRequired }