Skip to content

Commit f516c24

Browse files
authored
fix(javascript): move logic to custom gens (#486)
1 parent 4dc48f4 commit f516c24

File tree

2 files changed

+58
-123
lines changed

2 files changed

+58
-123
lines changed

generators/src/main/java/com/algolia/codegen/AlgoliaJavascriptGenerator.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,40 @@ public Map<String, Object> postProcessOperationsWithModels(
8181
additionalProperties
8282
);
8383

84+
List<CodegenOperation> operations =
85+
((Map<String, List<CodegenOperation>>) results.get("operations")).get(
86+
"operation"
87+
);
88+
89+
// We read operations and detect if we should wrap parameters under an object.
90+
// We only wrap if there is a mix between body parameters and other parameters.
91+
for (CodegenOperation ope : operations) {
92+
// Nothing to wrap as there is no parameters
93+
if (!ope.hasParams) {
94+
continue;
95+
}
96+
97+
boolean hasBodyParams = !ope.bodyParams.isEmpty();
98+
boolean hasHeaderParams = !ope.headerParams.isEmpty();
99+
boolean hasQueryParams = !ope.queryParams.isEmpty();
100+
boolean hasPathParams = !ope.pathParams.isEmpty();
101+
102+
// If there is nothing but body params, we just check if it's a single param
103+
if (
104+
hasBodyParams && !hasHeaderParams && !hasQueryParams && !hasPathParams
105+
) {
106+
// At this point the single parameter is already an object, to avoid double wrapping
107+
// we skip it
108+
if (ope.bodyParams.size() == 1 && !ope.bodyParams.get(0).isArray) {
109+
ope.vendorExtensions.put("x-is-single-body-param", true);
110+
continue;
111+
}
112+
}
113+
114+
// Any other cases here are wrapped
115+
ope.vendorExtensions.put("x-create-wrapping-object", true);
116+
}
117+
84118
return results;
85119
}
86120

templates/javascript/api-single.mustache

Lines changed: 24 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -112,89 +112,36 @@ export function create{{capitalizedApiName}}(options: CreateClientOptions{{#hasR
112112
{{#summary}}
113113
* @summary {{&summary}}
114114
{{/summary}}
115-
{{#allParams.0}}
116-
{{^bodyParams.0}}
117-
* @param {{nickname}} - The {{nickname}} object.
118-
{{#allParams}}
119-
* @param {{nickname}}.{{paramName}} - {{^description}}The {{paramName}} object.{{/description}}{{#description}}{{{description}}}{{/description}}
120-
{{/allParams}}
121-
{{/bodyParams.0}}
122-
{{#bodyParams.0}}
123-
{{^queryParams.0}}
124-
{{^pathParams.0}}
125-
{{#bodyParams.0.isArray}}
126-
{{^bodyParams.1}}
127-
* @param {{nickname}} - The {{nickname}} object.
128-
{{#allParams}}
129-
* @param {{nickname}}.{{paramName}} - {{^description}}The {{paramName}} object.{{/description}}{{#description}}{{{description}}}{{/description}}
130-
{{/allParams}}
131-
{{/bodyParams.1}}
132-
{{/bodyParams.0.isArray}}
133-
{{^bodyParams.0.isArray}}
134-
* @param {{paramName}} - {{^description}}The {{paramName}} object.{{/description}}{{#description}}{{{description}}}{{/description}}
135-
{{/bodyParams.0.isArray}}
136-
{{/pathParams.0}}
137-
{{#pathParams.0}}
138-
* @param {{nickname}} - The {{nickname}} object.
139-
{{#allParams}}
140-
* @param {{nickname}}.{{paramName}} - {{^description}}The {{paramName}} object.{{/description}}{{#description}}{{{description}}}{{/description}}
141-
{{/allParams}}
142-
{{/pathParams.0}}
143-
{{/queryParams.0}}
144-
{{#queryParams.0}}
145-
* @param {{nickname}} - The {{nickname}} object.
146-
{{#allParams}}
147-
* @param {{nickname}}.{{paramName}} - {{^description}}The {{paramName}} object.{{/description}}{{#description}}{{{description}}}{{/description}}
148-
{{/allParams}}
149-
{{/queryParams.0}}
150-
{{/bodyParams.0}}
151-
{{/allParams.0}}
115+
{{#vendorExtensions}}
116+
{{#x-create-wrapping-object}}
117+
* @param {{nickname}} - The {{nickname}} object.
118+
{{#allParams}}
119+
* @param {{nickname}}.{{paramName}} - {{^description}}The {{paramName}} object.{{/description}}{{#description}}{{{description}}}{{/description}}
120+
{{/allParams}}
121+
{{/x-create-wrapping-object}}
122+
{{#x-is-single-body-param}}
123+
{{#bodyParams}}
124+
* @param {{paramName}} - {{^description}}The {{paramName}} object.{{/description}}{{#description}}{{{description}}}{{/description}}
125+
{{/bodyParams}}
126+
{{/x-is-single-body-param}}
127+
{{/vendorExtensions}}
152128
* @param requestOptions - The requestOptions to send along with the query, they will be merged with the transporter requestOptions.
153129
*/
154130
{{nickname}}(
155-
{{#allParams.0}}
156-
{{^bodyParams.0}}
131+
{{#vendorExtensions}}
132+
{{#x-create-wrapping-object}}
157133
{
158134
{{#allParams}}
159135
{{paramName}},
160136
{{/allParams}}
161137
}: {{#lambda.titlecase}}{{nickname}}{{/lambda.titlecase}}Props,
162-
{{/bodyParams.0}}
163-
{{#bodyParams.0}}
164-
{{^queryParams.0}}
165-
{{^pathParams.0}}
166-
{{#bodyParams.0.isArray}}
167-
{{^bodyParams.1}}
168-
{
169-
{{#allParams}}
170-
{{paramName}},
171-
{{/allParams}}
172-
}: {{#lambda.titlecase}}{{nickname}}{{/lambda.titlecase}}Props,
173-
{{/bodyParams.1}}
174-
{{/bodyParams.0.isArray}}
175-
{{^bodyParams.0.isArray}}
176-
{{#bodyParams}}
177-
{{paramName}}: {{{dataType}}},
178-
{{/bodyParams}}
179-
{{/bodyParams.0.isArray}}
180-
{{/pathParams.0}}
181-
{{#pathParams.0}}
182-
{
183-
{{#allParams}}
184-
{{paramName}},
185-
{{/allParams}}
186-
}: {{#lambda.titlecase}}{{nickname}}{{/lambda.titlecase}}Props,
187-
{{/pathParams.0}}
188-
{{/queryParams.0}}
189-
{{#queryParams.0}}
190-
{
191-
{{#allParams}}
192-
{{paramName}},
193-
{{/allParams}}
194-
}: {{#lambda.titlecase}}{{nickname}}{{/lambda.titlecase}}Props,
195-
{{/queryParams.0}}
196-
{{/bodyParams.0}}
197-
{{/allParams.0}}
138+
{{/x-create-wrapping-object}}
139+
{{#x-is-single-body-param}}
140+
{{#bodyParams}}
141+
{{paramName}}: {{{dataType}}},
142+
{{/bodyParams}}
143+
{{/x-is-single-body-param}}
144+
{{/vendorExtensions}}
198145
requestOptions?: RequestOptions
199146
) : Promise<{{{returnType}}}> {
200147
{{#allParams}}
@@ -252,51 +199,7 @@ export function create{{capitalizedApiName}}(options: CreateClientOptions{{#hasR
252199
export type {{capitalizedApiName}} = ReturnType<typeof create{{capitalizedApiName}}>;
253200

254201
{{#operation}}
255-
{{#allParams.0}}
256-
{{^bodyParams.0}}
257-
export type {{#lambda.titlecase}}{{nickname}}{{/lambda.titlecase}}Props = {
258-
{{#allParams}}
259-
{{#description}}
260-
/**
261-
* {{{description}}}
262-
*/
263-
{{/description}}
264-
{{paramName}}{{^required}}?{{/required}}: {{{dataType}}};
265-
{{/allParams}}
266-
}
267-
{{/bodyParams.0}}
268-
{{#bodyParams.0}}
269-
{{^queryParams.0}}
270-
{{^pathParams.0}}
271-
{{#bodyParams.0.isArray}}
272-
{{^bodyParams.1}}
273-
export type {{#lambda.titlecase}}{{nickname}}{{/lambda.titlecase}}Props = {
274-
{{#allParams}}
275-
{{#description}}
276-
/**
277-
* {{{description}}}
278-
*/
279-
{{/description}}
280-
{{paramName}}{{^required}}?{{/required}}: {{{dataType}}};
281-
{{/allParams}}
282-
}
283-
{{/bodyParams.1}}
284-
{{/bodyParams.0.isArray}}
285-
{{/pathParams.0}}
286-
{{#pathParams.0}}
287-
export type {{#lambda.titlecase}}{{nickname}}{{/lambda.titlecase}}Props = {
288-
{{#allParams}}
289-
{{#description}}
290-
/**
291-
* {{{description}}}
292-
*/
293-
{{/description}}
294-
{{paramName}}{{^required}}?{{/required}}: {{{dataType}}};
295-
{{/allParams}}
296-
}
297-
{{/pathParams.0}}
298-
{{/queryParams.0}}
299-
{{#queryParams.0}}
202+
{{#vendorExtensions.x-create-wrapping-object}}
300203
export type {{#lambda.titlecase}}{{nickname}}{{/lambda.titlecase}}Props = {
301204
{{#allParams}}
302205
{{#description}}
@@ -307,9 +210,7 @@ export type {{#lambda.titlecase}}{{nickname}}{{/lambda.titlecase}}Props = {
307210
{{paramName}}{{^required}}?{{/required}}: {{{dataType}}};
308211
{{/allParams}}
309212
}
310-
{{/queryParams.0}}
311-
{{/bodyParams.0}}
312-
{{/allParams.0}}
213+
{{/vendorExtensions.x-create-wrapping-object}}
313214

314215
{{/operation}}
315216

0 commit comments

Comments
 (0)