Skip to content
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

fix(javascript): move logic to custom gens #486

Merged
merged 6 commits into from
May 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,40 @@ public Map<String, Object> postProcessOperationsWithModels(
additionalProperties
);

List<CodegenOperation> operations =
((Map<String, List<CodegenOperation>>) results.get("operations")).get(
"operation"
);

// We read operations and detect if we should wrap parameters under an object.
// We only wrap if there is a mix between body parameters and other parameters.
for (CodegenOperation ope : operations) {
// Nothing to wrap as there is no parameters
if (!ope.hasParams) {
continue;
}

boolean hasBodyParams = !ope.bodyParams.isEmpty();
boolean hasHeaderParams = !ope.headerParams.isEmpty();
boolean hasQueryParams = !ope.queryParams.isEmpty();
boolean hasPathParams = !ope.pathParams.isEmpty();

// If there is nothing but body params, we just check if it's a single param
if (
hasBodyParams && !hasHeaderParams && !hasQueryParams && !hasPathParams
) {
// At this point the single parameter is already an object, to avoid double wrapping
// we skip it
if (ope.bodyParams.size() == 1 && !ope.bodyParams.get(0).isArray) {
ope.vendorExtensions.put("x-is-single-body-param", true);
continue;
}
}

// Any other cases here are wrapped
ope.vendorExtensions.put("x-create-wrapping-object", true);
}

return results;
}

Expand Down
147 changes: 24 additions & 123 deletions templates/javascript/api-single.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -112,89 +112,36 @@ export function create{{capitalizedApiName}}(options: CreateClientOptions{{#hasR
{{#summary}}
* @summary {{&summary}}
{{/summary}}
{{#allParams.0}}
{{^bodyParams.0}}
* @param {{nickname}} - The {{nickname}} object.
{{#allParams}}
* @param {{nickname}}.{{paramName}} - {{^description}}The {{paramName}} object.{{/description}}{{#description}}{{{description}}}{{/description}}
{{/allParams}}
{{/bodyParams.0}}
{{#bodyParams.0}}
{{^queryParams.0}}
{{^pathParams.0}}
{{#bodyParams.0.isArray}}
{{^bodyParams.1}}
* @param {{nickname}} - The {{nickname}} object.
{{#allParams}}
* @param {{nickname}}.{{paramName}} - {{^description}}The {{paramName}} object.{{/description}}{{#description}}{{{description}}}{{/description}}
{{/allParams}}
{{/bodyParams.1}}
{{/bodyParams.0.isArray}}
{{^bodyParams.0.isArray}}
* @param {{paramName}} - {{^description}}The {{paramName}} object.{{/description}}{{#description}}{{{description}}}{{/description}}
{{/bodyParams.0.isArray}}
{{/pathParams.0}}
{{#pathParams.0}}
* @param {{nickname}} - The {{nickname}} object.
{{#allParams}}
* @param {{nickname}}.{{paramName}} - {{^description}}The {{paramName}} object.{{/description}}{{#description}}{{{description}}}{{/description}}
{{/allParams}}
{{/pathParams.0}}
{{/queryParams.0}}
{{#queryParams.0}}
* @param {{nickname}} - The {{nickname}} object.
{{#allParams}}
* @param {{nickname}}.{{paramName}} - {{^description}}The {{paramName}} object.{{/description}}{{#description}}{{{description}}}{{/description}}
{{/allParams}}
{{/queryParams.0}}
{{/bodyParams.0}}
{{/allParams.0}}
{{#vendorExtensions}}
{{#x-create-wrapping-object}}
* @param {{nickname}} - The {{nickname}} object.
{{#allParams}}
* @param {{nickname}}.{{paramName}} - {{^description}}The {{paramName}} object.{{/description}}{{#description}}{{{description}}}{{/description}}
{{/allParams}}
{{/x-create-wrapping-object}}
{{#x-is-single-body-param}}
{{#bodyParams}}
* @param {{paramName}} - {{^description}}The {{paramName}} object.{{/description}}{{#description}}{{{description}}}{{/description}}
{{/bodyParams}}
{{/x-is-single-body-param}}
{{/vendorExtensions}}
* @param requestOptions - The requestOptions to send along with the query, they will be merged with the transporter requestOptions.
*/
{{nickname}}(
{{#allParams.0}}
{{^bodyParams.0}}
{{#vendorExtensions}}
shortcuts marked this conversation as resolved.
Show resolved Hide resolved
{{#x-create-wrapping-object}}
{
{{#allParams}}
{{paramName}},
{{/allParams}}
}: {{#lambda.titlecase}}{{nickname}}{{/lambda.titlecase}}Props,
{{/bodyParams.0}}
{{#bodyParams.0}}
{{^queryParams.0}}
{{^pathParams.0}}
{{#bodyParams.0.isArray}}
{{^bodyParams.1}}
{
{{#allParams}}
{{paramName}},
{{/allParams}}
}: {{#lambda.titlecase}}{{nickname}}{{/lambda.titlecase}}Props,
{{/bodyParams.1}}
{{/bodyParams.0.isArray}}
{{^bodyParams.0.isArray}}
{{#bodyParams}}
{{paramName}}: {{{dataType}}},
{{/bodyParams}}
{{/bodyParams.0.isArray}}
{{/pathParams.0}}
{{#pathParams.0}}
{
{{#allParams}}
{{paramName}},
{{/allParams}}
}: {{#lambda.titlecase}}{{nickname}}{{/lambda.titlecase}}Props,
{{/pathParams.0}}
{{/queryParams.0}}
{{#queryParams.0}}
{
{{#allParams}}
{{paramName}},
{{/allParams}}
}: {{#lambda.titlecase}}{{nickname}}{{/lambda.titlecase}}Props,
{{/queryParams.0}}
{{/bodyParams.0}}
{{/allParams.0}}
{{/x-create-wrapping-object}}
{{#x-is-single-body-param}}
{{#bodyParams}}
{{paramName}}: {{{dataType}}},
{{/bodyParams}}
{{/x-is-single-body-param}}
{{/vendorExtensions}}
requestOptions?: RequestOptions
) : Promise<{{{returnType}}}> {
{{#allParams}}
Expand Down Expand Up @@ -252,51 +199,7 @@ export function create{{capitalizedApiName}}(options: CreateClientOptions{{#hasR
export type {{capitalizedApiName}} = ReturnType<typeof create{{capitalizedApiName}}>;

{{#operation}}
{{#allParams.0}}
{{^bodyParams.0}}
export type {{#lambda.titlecase}}{{nickname}}{{/lambda.titlecase}}Props = {
{{#allParams}}
{{#description}}
/**
* {{{description}}}
*/
{{/description}}
{{paramName}}{{^required}}?{{/required}}: {{{dataType}}};
{{/allParams}}
}
{{/bodyParams.0}}
{{#bodyParams.0}}
{{^queryParams.0}}
{{^pathParams.0}}
{{#bodyParams.0.isArray}}
{{^bodyParams.1}}
export type {{#lambda.titlecase}}{{nickname}}{{/lambda.titlecase}}Props = {
{{#allParams}}
{{#description}}
/**
* {{{description}}}
*/
{{/description}}
{{paramName}}{{^required}}?{{/required}}: {{{dataType}}};
{{/allParams}}
}
{{/bodyParams.1}}
{{/bodyParams.0.isArray}}
{{/pathParams.0}}
{{#pathParams.0}}
export type {{#lambda.titlecase}}{{nickname}}{{/lambda.titlecase}}Props = {
{{#allParams}}
{{#description}}
/**
* {{{description}}}
*/
{{/description}}
{{paramName}}{{^required}}?{{/required}}: {{{dataType}}};
{{/allParams}}
}
{{/pathParams.0}}
{{/queryParams.0}}
{{#queryParams.0}}
{{#vendorExtensions.x-create-wrapping-object}}
export type {{#lambda.titlecase}}{{nickname}}{{/lambda.titlecase}}Props = {
{{#allParams}}
{{#description}}
Expand All @@ -307,9 +210,7 @@ export type {{#lambda.titlecase}}{{nickname}}{{/lambda.titlecase}}Props = {
{{paramName}}{{^required}}?{{/required}}: {{{dataType}}};
{{/allParams}}
}
{{/queryParams.0}}
{{/bodyParams.0}}
{{/allParams.0}}
{{/vendorExtensions.x-create-wrapping-object}}

{{/operation}}

Expand Down