From 274b1087b7845ff132f3809f7cfb197b8fe0fd3b Mon Sep 17 00:00:00 2001 From: Landon Gavin Date: Wed, 10 Apr 2024 14:49:06 -0400 Subject: [PATCH] fix(query): add default parameter initializer This PR adds a default parameter initializer if all the properties of the object are optional. Fixes: Default empty arguments #37 --- examples/react-app/src/App.tsx | 3 ++ src/createUseQuery.mts | 71 ++++++++++++++++++---------------- 2 files changed, 40 insertions(+), 34 deletions(-) diff --git a/examples/react-app/src/App.tsx b/examples/react-app/src/App.tsx index 007340d..6d280c1 100644 --- a/examples/react-app/src/App.tsx +++ b/examples/react-app/src/App.tsx @@ -15,6 +15,9 @@ function App() { const [limit, _setLimit] = useState(10); const { data, error, refetch } = useDefaultServiceFindPets({ tags, limit }); + // This is an example of using a hook that has all parameters optional; + // Here we do not have to pass in an object + const {} = useDefaultServiceFindPets(); // This is an example of a query that is not defined in the OpenAPI spec // this defaults to any - here we are showing how to override the type diff --git a/src/createUseQuery.mts b/src/createUseQuery.mts index 2d7e4af..4e971b4 100644 --- a/src/createUseQuery.mts +++ b/src/createUseQuery.mts @@ -79,47 +79,50 @@ export function getRequestParamFromMethod(method: MethodDeclaration) { // we need to get the properties of the object + const params = method + .getParameters() + .map((param) => { + const paramNodes = extractPropertiesFromObjectParam(param); + return paramNodes.map((refParam) => ({ + name: refParam.name, + typeName: refParam.type.getText(), + optional: refParam.optional, + })); + }) + .flat(); + + const areAllOptional = params.every((param) => param.optional); + return ts.factory.createParameterDeclaration( undefined, undefined, ts.factory.createObjectBindingPattern( - method - .getParameters() - .map((param) => { - const paramNodes = extractPropertiesFromObjectParam(param); - return paramNodes.map((refParam) => - ts.factory.createBindingElement( - undefined, - undefined, - ts.factory.createIdentifier(refParam.name), - undefined - ) - ); - }) - .flat() + params.map((refParam) => + ts.factory.createBindingElement( + undefined, + undefined, + ts.factory.createIdentifier(refParam.name), + undefined + ) + ) ), undefined, ts.factory.createTypeLiteralNode( - method - .getParameters() - .map((param) => { - const paramNodes = extractPropertiesFromObjectParam(param); - return paramNodes.map((refParam) => { - return ts.factory.createPropertySignature( - undefined, - ts.factory.createIdentifier(refParam.name), - refParam.optional - ? ts.factory.createToken(ts.SyntaxKind.QuestionToken) - : undefined, - // param.hasQuestionToken() ?? param.getInitializer()?.compilerNode - // ? ts.factory.createToken(ts.SyntaxKind.QuestionToken) - // : param.getQuestionTokenNode()?.compilerNode, - ts.factory.createTypeReferenceNode(refParam.type.getText()) - ); - }); - }) - .flat() - ) + params.map((refParam) => { + return ts.factory.createPropertySignature( + undefined, + ts.factory.createIdentifier(refParam.name), + refParam.optional + ? ts.factory.createToken(ts.SyntaxKind.QuestionToken) + : undefined, + // param.hasQuestionToken() ?? param.getInitializer()?.compilerNode + // ? ts.factory.createToken(ts.SyntaxKind.QuestionToken) + // : param.getQuestionTokenNode()?.compilerNode, + ts.factory.createTypeReferenceNode(refParam.typeName) + ); + }) + ), + areAllOptional ? ts.factory.createObjectLiteralExpression() : undefined ); }