-
-
Notifications
You must be signed in to change notification settings - Fork 77
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add --argumentStyle option #549
Conversation
Hi @Liooo! Thank you for the contribution! This is looking great already! Can you add a I will try to find time for a deeper review in the coming days 👍 |
Thanks, I ran generate-demo. I found an issue that when there's no input for the endpoint an empty object argument is generated, so fixed that and added some tests. Apart from that it's looking good to me. Please check out when you've got time :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great Job with this. I think the implementation in generate.ts can be organized a little cleaner other then that LGTM
switch (this.opts.argumentStyle ?? "positional") { | ||
case "positional": |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the generateApi
method already is quite convoluted. Can we refactor the body of this switch to standalone methods/functions such as getPositionalMethodParams
and getObjectMethodParams
?
if (requestBody) { | ||
body = this.resolve(requestBody); | ||
const schema = this.getSchemaFromContent(body.content); | ||
const type = this.getTypeFromSchema( | ||
schema, | ||
undefined, | ||
"writeOnly", | ||
); | ||
bodyVar = toIdentifier( | ||
(type as any).name || getReferenceName(schema) || "body", | ||
); | ||
methodParams.push( | ||
cg.createParameter(bodyVar, { | ||
type, | ||
questionToken: !body.required, | ||
}), | ||
); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems to be duplicated in both switch cases, can we keep remove the duplication?
fa7b5a8
to
144916d
Compare
Hey thanks for the review 👋 I've looked into them but,
the switch block depends on
the last // the method name is temporary
const getBodyish = (requestBody: OpenAPIV3.ReferenceObject | OpenAPIV3.RequestBodyObject) => {
//..brah
return { body, bodyVar, type };
}; then since let body, bodyVar;
switch(argumentStyle) {
case 'positional':
// ..brah
if (requestBody) {
let type: TypeNode;
({body, bodyVar, type} = getBodyish(requestBody))
// ..
}
// ..brah
case 'object':
// ..brah
if (requestBody) {
let type: TypeNode;
({body, bodyVar, type} = getBodyish(requestBody))
// ..
}
// ..brah
} or the maybe the I'm happy to keep going, but IMHO it better either stay as is for now, or expand the refactoring scope and apply bit of structural change inside What do you think? |
Hi @Liooo Thank you for the explanation haven't seen those details you're mentioning. Gonna merge this now and open a refactor ticket. |
🎉 This PR is included in version 5.1.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
abstract
This PR adds an option that lets us opt in to object style argument by
--argumentStyle=object
:as opposed to
--argumentStyle=positional
one (which is default):motivation
Parameters<typeof func>[0]
, instead of struggling with a tuple of variable length)a few other points
positional
argument is somewhat canonical, couldn't find good one forobject
style (technically speaking this is just destructuring assignment in function argument)export
-ed, if this is wanted I'm happy to revert this behavior but as another option, maybe--argumentStyle=objectExported
, or add new one like--exportObjcectArgumentTypes=true
P.S. Thanks for the great work. As far as I know, oazapfts is the best option for openapi ts codegen out there, in terms of generated code tidiness, supported openapi features (kudos to readOnly/writeOnly support) and all. I think this library should get more attention.