Skip to content

Commit 4e27e41

Browse files
authored
fix(query): add default parameter initializer (#60)
1 parent 0fb6d0a commit 4e27e41

File tree

2 files changed

+40
-34
lines changed

2 files changed

+40
-34
lines changed

examples/react-app/src/App.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ function App() {
1515
const [limit, _setLimit] = useState<number>(10);
1616

1717
const { data, error, refetch } = useDefaultServiceFindPets({ tags, limit });
18+
// This is an example of using a hook that has all parameters optional;
19+
// Here we do not have to pass in an object
20+
const {} = useDefaultServiceFindPets();
1821

1922
// This is an example of a query that is not defined in the OpenAPI spec
2023
// this defaults to any - here we are showing how to override the type

src/createUseQuery.mts

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -79,47 +79,50 @@ export function getRequestParamFromMethod(method: MethodDeclaration) {
7979

8080
// we need to get the properties of the object
8181

82+
const params = method
83+
.getParameters()
84+
.map((param) => {
85+
const paramNodes = extractPropertiesFromObjectParam(param);
86+
return paramNodes.map((refParam) => ({
87+
name: refParam.name,
88+
typeName: refParam.type.getText(),
89+
optional: refParam.optional,
90+
}));
91+
})
92+
.flat();
93+
94+
const areAllOptional = params.every((param) => param.optional);
95+
8296
return ts.factory.createParameterDeclaration(
8397
undefined,
8498
undefined,
8599
ts.factory.createObjectBindingPattern(
86-
method
87-
.getParameters()
88-
.map((param) => {
89-
const paramNodes = extractPropertiesFromObjectParam(param);
90-
return paramNodes.map((refParam) =>
91-
ts.factory.createBindingElement(
92-
undefined,
93-
undefined,
94-
ts.factory.createIdentifier(refParam.name),
95-
undefined
96-
)
97-
);
98-
})
99-
.flat()
100+
params.map((refParam) =>
101+
ts.factory.createBindingElement(
102+
undefined,
103+
undefined,
104+
ts.factory.createIdentifier(refParam.name),
105+
undefined
106+
)
107+
)
100108
),
101109
undefined,
102110
ts.factory.createTypeLiteralNode(
103-
method
104-
.getParameters()
105-
.map((param) => {
106-
const paramNodes = extractPropertiesFromObjectParam(param);
107-
return paramNodes.map((refParam) => {
108-
return ts.factory.createPropertySignature(
109-
undefined,
110-
ts.factory.createIdentifier(refParam.name),
111-
refParam.optional
112-
? ts.factory.createToken(ts.SyntaxKind.QuestionToken)
113-
: undefined,
114-
// param.hasQuestionToken() ?? param.getInitializer()?.compilerNode
115-
// ? ts.factory.createToken(ts.SyntaxKind.QuestionToken)
116-
// : param.getQuestionTokenNode()?.compilerNode,
117-
ts.factory.createTypeReferenceNode(refParam.type.getText())
118-
);
119-
});
120-
})
121-
.flat()
122-
)
111+
params.map((refParam) => {
112+
return ts.factory.createPropertySignature(
113+
undefined,
114+
ts.factory.createIdentifier(refParam.name),
115+
refParam.optional
116+
? ts.factory.createToken(ts.SyntaxKind.QuestionToken)
117+
: undefined,
118+
// param.hasQuestionToken() ?? param.getInitializer()?.compilerNode
119+
// ? ts.factory.createToken(ts.SyntaxKind.QuestionToken)
120+
// : param.getQuestionTokenNode()?.compilerNode,
121+
ts.factory.createTypeReferenceNode(refParam.typeName)
122+
);
123+
})
124+
),
125+
areAllOptional ? ts.factory.createObjectLiteralExpression() : undefined
123126
);
124127
}
125128

0 commit comments

Comments
 (0)