Skip to content

Commit

Permalink
fix(query): add default parameter initializer
Browse files Browse the repository at this point in the history
This PR adds a default parameter initializer if all the properties of the object are optional.

Fixes: Default empty arguments #37
  • Loading branch information
seriouslag committed Apr 10, 2024
1 parent 721af3e commit 274b108
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 34 deletions.
3 changes: 3 additions & 0 deletions examples/react-app/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ function App() {
const [limit, _setLimit] = useState<number>(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
Expand Down
71 changes: 37 additions & 34 deletions src/createUseQuery.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}

Expand Down

0 comments on commit 274b108

Please sign in to comment.