Skip to content

Commit cd47bd4

Browse files
committed
feat(query-params): add an option to use brackets convention with arrays
1 parent 00d77f9 commit cd47bd4

File tree

110 files changed

+1004
-199
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

110 files changed

+1004
-199
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ Options:
7676
--another-array-type generate array types as Array<Type> (by default Type[]) (default: false)
7777
--sort-types sort fields and types (default: false)
7878
--extract-enums extract all enums from inline interface\type content to typescript enum construction (default: false)
79+
--query-params-with-brackets use the `brackets` convention for array in query params: `?a[]=foo&a[]=bar` instead of `repeat` convention: `?a=foo&a=bar`
7980
-h, --help display help for command
8081
8182
Commands:

index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,7 @@ export interface GenerateApiConfiguration {
554554
cleanOutput: boolean;
555555
debug: boolean;
556556
anotherArrayType: boolean;
557+
queryParamsWithBrackets: false;
557558
extractRequestBody: boolean;
558559
httpClientType: "axios" | "fetch";
559560
addReadonly: boolean;

index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,12 @@ const program = cli({
230230
description: "sort routes in alphabetical order",
231231
default: codeGenBaseConfig.sortRoutes,
232232
},
233+
{
234+
flags: "--query-params-with-brackets",
235+
description:
236+
'use the brackets convention for array in query params: "?a[]=foo&a[]=bar" instead of repeat convention: "?a=foo&a=bar"',
237+
default: codeGenBaseConfig.queryParamsWithBrackets,
238+
},
233239
],
234240
});
235241

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@
6262
"test:extract-enums": "node tests/spec/extract-enums/test.js",
6363
"test:discriminator": "node tests/spec/discriminator/test.js",
6464
"test:sort-types": "node tests/spec/sortTypes/test.js",
65-
"test:sort-types(false)": "node tests/spec/sortTypes(false)/test.js"
65+
"test:sort-types(false)": "node tests/spec/sortTypes(false)/test.js",
66+
"test:--query-params-with-brackets": "node tests/spec/queryParamsWithBrackets/test.js"
6667
},
6768
"author": "acacode",
6869
"license": "MIT",

src/configuration.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ class CodeGenConfig {
142142
apiClassName = "Api";
143143
debug = false;
144144
anotherArrayType = false;
145+
queryParamsWithBrackets = false;
145146
internalTemplateOptions = {
146147
addUtilRequiredKeysType: false,
147148
};

templates/base/http-clients/fetch-http-client.ejs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ export class HttpClient<SecurityDataType = unknown> {
7070
this.securityData = data;
7171
}
7272

73-
protected encodeQueryParam(key: string, value: any) {
73+
protected encodeQueryParam(key: string, value: any, withBrackets: boolean = false) {
7474
const encodedKey = encodeURIComponent(key);
75-
return `${encodedKey}=${encodeURIComponent(typeof value === "number" ? value : `${value}`)}`;
75+
return `${encodedKey}${withBrackets ? '[]' : ''}=${encodeURIComponent(typeof value === "number" ? value : `${value}`)}`;
7676
}
7777

7878
protected addQueryParam(query: QueryParamsType, key: string) {
@@ -81,7 +81,11 @@ export class HttpClient<SecurityDataType = unknown> {
8181

8282
protected addArrayQueryParam(query: QueryParamsType, key: string) {
8383
const value = query[key];
84+
<% if (config.queryParamsWithBrackets) { %>
85+
return value.map((v: any) => this.encodeQueryParam(key, v, true)).join("&");
86+
<% } else { %>
8487
return value.map((v: any) => this.encodeQueryParam(key, v)).join("&");
88+
<% } %>
8589
}
8690

8791
protected toQueryString(rawQuery?: QueryParamsType): string {

tests/generated/v2.0/adafruit.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,9 +233,11 @@ export class HttpClient<SecurityDataType = unknown> {
233233
this.securityData = data;
234234
};
235235

236-
protected encodeQueryParam(key: string, value: any) {
236+
protected encodeQueryParam(key: string, value: any, withBrackets: boolean = false) {
237237
const encodedKey = encodeURIComponent(key);
238-
return `${encodedKey}=${encodeURIComponent(typeof value === "number" ? value : `${value}`)}`;
238+
return `${encodedKey}${withBrackets ? "[]" : ""}=${encodeURIComponent(
239+
typeof value === "number" ? value : `${value}`,
240+
)}`;
239241
}
240242

241243
protected addQueryParam(query: QueryParamsType, key: string) {

tests/generated/v2.0/another-example.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,11 @@ export class HttpClient<SecurityDataType = unknown> {
209209
this.securityData = data;
210210
};
211211

212-
protected encodeQueryParam(key: string, value: any) {
212+
protected encodeQueryParam(key: string, value: any, withBrackets: boolean = false) {
213213
const encodedKey = encodeURIComponent(key);
214-
return `${encodedKey}=${encodeURIComponent(typeof value === "number" ? value : `${value}`)}`;
214+
return `${encodedKey}${withBrackets ? "[]" : ""}=${encodeURIComponent(
215+
typeof value === "number" ? value : `${value}`,
216+
)}`;
215217
}
216218

217219
protected addQueryParam(query: QueryParamsType, key: string) {

tests/generated/v2.0/another-schema.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,11 @@ export class HttpClient<SecurityDataType = unknown> {
9797
this.securityData = data;
9898
};
9999

100-
protected encodeQueryParam(key: string, value: any) {
100+
protected encodeQueryParam(key: string, value: any, withBrackets: boolean = false) {
101101
const encodedKey = encodeURIComponent(key);
102-
return `${encodedKey}=${encodeURIComponent(typeof value === "number" ? value : `${value}`)}`;
102+
return `${encodedKey}${withBrackets ? "[]" : ""}=${encodeURIComponent(
103+
typeof value === "number" ? value : `${value}`,
104+
)}`;
103105
}
104106

105107
protected addQueryParam(query: QueryParamsType, key: string) {

tests/generated/v2.0/api-with-examples.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,11 @@ export class HttpClient<SecurityDataType = unknown> {
7676
this.securityData = data;
7777
};
7878

79-
protected encodeQueryParam(key: string, value: any) {
79+
protected encodeQueryParam(key: string, value: any, withBrackets: boolean = false) {
8080
const encodedKey = encodeURIComponent(key);
81-
return `${encodedKey}=${encodeURIComponent(typeof value === "number" ? value : `${value}`)}`;
81+
return `${encodedKey}${withBrackets ? "[]" : ""}=${encodeURIComponent(
82+
typeof value === "number" ? value : `${value}`,
83+
)}`;
8284
}
8385

8486
protected addQueryParam(query: QueryParamsType, key: string) {

tests/generated/v2.0/authentiq.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,11 @@ export class HttpClient<SecurityDataType = unknown> {
116116
this.securityData = data;
117117
};
118118

119-
protected encodeQueryParam(key: string, value: any) {
119+
protected encodeQueryParam(key: string, value: any, withBrackets: boolean = false) {
120120
const encodedKey = encodeURIComponent(key);
121-
return `${encodedKey}=${encodeURIComponent(typeof value === "number" ? value : `${value}`)}`;
121+
return `${encodedKey}${withBrackets ? "[]" : ""}=${encodeURIComponent(
122+
typeof value === "number" ? value : `${value}`,
123+
)}`;
122124
}
123125

124126
protected addQueryParam(query: QueryParamsType, key: string) {

tests/generated/v2.0/enums.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,11 @@ export class HttpClient<SecurityDataType = unknown> {
135135
this.securityData = data;
136136
};
137137

138-
protected encodeQueryParam(key: string, value: any) {
138+
protected encodeQueryParam(key: string, value: any, withBrackets: boolean = false) {
139139
const encodedKey = encodeURIComponent(key);
140-
return `${encodedKey}=${encodeURIComponent(typeof value === "number" ? value : `${value}`)}`;
140+
return `${encodedKey}${withBrackets ? "[]" : ""}=${encodeURIComponent(
141+
typeof value === "number" ? value : `${value}`,
142+
)}`;
141143
}
142144

143145
protected addQueryParam(query: QueryParamsType, key: string) {

tests/generated/v2.0/example1.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,11 @@ export class HttpClient<SecurityDataType = unknown> {
113113
this.securityData = data;
114114
};
115115

116-
protected encodeQueryParam(key: string, value: any) {
116+
protected encodeQueryParam(key: string, value: any, withBrackets: boolean = false) {
117117
const encodedKey = encodeURIComponent(key);
118-
return `${encodedKey}=${encodeURIComponent(typeof value === "number" ? value : `${value}`)}`;
118+
return `${encodedKey}${withBrackets ? "[]" : ""}=${encodeURIComponent(
119+
typeof value === "number" ? value : `${value}`,
120+
)}`;
119121
}
120122

121123
protected addQueryParam(query: QueryParamsType, key: string) {

tests/generated/v2.0/file-formdata-example.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,11 @@ export class HttpClient<SecurityDataType = unknown> {
7676
this.securityData = data;
7777
};
7878

79-
protected encodeQueryParam(key: string, value: any) {
79+
protected encodeQueryParam(key: string, value: any, withBrackets: boolean = false) {
8080
const encodedKey = encodeURIComponent(key);
81-
return `${encodedKey}=${encodeURIComponent(typeof value === "number" ? value : `${value}`)}`;
81+
return `${encodedKey}${withBrackets ? "[]" : ""}=${encodeURIComponent(
82+
typeof value === "number" ? value : `${value}`,
83+
)}`;
8284
}
8385

8486
protected addQueryParam(query: QueryParamsType, key: string) {

tests/generated/v2.0/furkot-example.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,11 @@ export class HttpClient<SecurityDataType = unknown> {
149149
this.securityData = data;
150150
};
151151

152-
protected encodeQueryParam(key: string, value: any) {
152+
protected encodeQueryParam(key: string, value: any, withBrackets: boolean = false) {
153153
const encodedKey = encodeURIComponent(key);
154-
return `${encodedKey}=${encodeURIComponent(typeof value === "number" ? value : `${value}`)}`;
154+
return `${encodedKey}${withBrackets ? "[]" : ""}=${encodeURIComponent(
155+
typeof value === "number" ? value : `${value}`,
156+
)}`;
155157
}
156158

157159
protected addQueryParam(query: QueryParamsType, key: string) {

tests/generated/v2.0/giphy.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,9 +333,11 @@ export class HttpClient<SecurityDataType = unknown> {
333333
this.securityData = data;
334334
};
335335

336-
protected encodeQueryParam(key: string, value: any) {
336+
protected encodeQueryParam(key: string, value: any, withBrackets: boolean = false) {
337337
const encodedKey = encodeURIComponent(key);
338-
return `${encodedKey}=${encodeURIComponent(typeof value === "number" ? value : `${value}`)}`;
338+
return `${encodedKey}${withBrackets ? "[]" : ""}=${encodeURIComponent(
339+
typeof value === "number" ? value : `${value}`,
340+
)}`;
339341
}
340342

341343
protected addQueryParam(query: QueryParamsType, key: string) {

tests/generated/v2.0/github-swagger.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1974,9 +1974,11 @@ export class HttpClient<SecurityDataType = unknown> {
19741974
this.securityData = data;
19751975
};
19761976

1977-
protected encodeQueryParam(key: string, value: any) {
1977+
protected encodeQueryParam(key: string, value: any, withBrackets: boolean = false) {
19781978
const encodedKey = encodeURIComponent(key);
1979-
return `${encodedKey}=${encodeURIComponent(typeof value === "number" ? value : `${value}`)}`;
1979+
return `${encodedKey}${withBrackets ? "[]" : ""}=${encodeURIComponent(
1980+
typeof value === "number" ? value : `${value}`,
1981+
)}`;
19801982
}
19811983

19821984
protected addQueryParam(query: QueryParamsType, key: string) {

tests/generated/v2.0/path-args.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,11 @@ export class HttpClient<SecurityDataType = unknown> {
7676
this.securityData = data;
7777
};
7878

79-
protected encodeQueryParam(key: string, value: any) {
79+
protected encodeQueryParam(key: string, value: any, withBrackets: boolean = false) {
8080
const encodedKey = encodeURIComponent(key);
81-
return `${encodedKey}=${encodeURIComponent(typeof value === "number" ? value : `${value}`)}`;
81+
return `${encodedKey}${withBrackets ? "[]" : ""}=${encodeURIComponent(
82+
typeof value === "number" ? value : `${value}`,
83+
)}`;
8284
}
8385

8486
protected addQueryParam(query: QueryParamsType, key: string) {

tests/generated/v2.0/petstore-expanded.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,11 @@ export class HttpClient<SecurityDataType = unknown> {
112112
this.securityData = data;
113113
};
114114

115-
protected encodeQueryParam(key: string, value: any) {
115+
protected encodeQueryParam(key: string, value: any, withBrackets: boolean = false) {
116116
const encodedKey = encodeURIComponent(key);
117-
return `${encodedKey}=${encodeURIComponent(typeof value === "number" ? value : `${value}`)}`;
117+
return `${encodedKey}${withBrackets ? "[]" : ""}=${encodeURIComponent(
118+
typeof value === "number" ? value : `${value}`,
119+
)}`;
118120
}
119121

120122
protected addQueryParam(query: QueryParamsType, key: string) {

tests/generated/v2.0/petstore-minimal.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,11 @@ export class HttpClient<SecurityDataType = unknown> {
8484
this.securityData = data;
8585
};
8686

87-
protected encodeQueryParam(key: string, value: any) {
87+
protected encodeQueryParam(key: string, value: any, withBrackets: boolean = false) {
8888
const encodedKey = encodeURIComponent(key);
89-
return `${encodedKey}=${encodeURIComponent(typeof value === "number" ? value : `${value}`)}`;
89+
return `${encodedKey}${withBrackets ? "[]" : ""}=${encodeURIComponent(
90+
typeof value === "number" ? value : `${value}`,
91+
)}`;
9092
}
9193

9294
protected addQueryParam(query: QueryParamsType, key: string) {

tests/generated/v2.0/petstore-simple.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,11 @@ export class HttpClient<SecurityDataType = unknown> {
100100
this.securityData = data;
101101
};
102102

103-
protected encodeQueryParam(key: string, value: any) {
103+
protected encodeQueryParam(key: string, value: any, withBrackets: boolean = false) {
104104
const encodedKey = encodeURIComponent(key);
105-
return `${encodedKey}=${encodeURIComponent(typeof value === "number" ? value : `${value}`)}`;
105+
return `${encodedKey}${withBrackets ? "[]" : ""}=${encodeURIComponent(
106+
typeof value === "number" ? value : `${value}`,
107+
)}`;
106108
}
107109

108110
protected addQueryParam(query: QueryParamsType, key: string) {

tests/generated/v2.0/petstore-swagger-io.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,11 @@ export class HttpClient<SecurityDataType = unknown> {
137137
this.securityData = data;
138138
};
139139

140-
protected encodeQueryParam(key: string, value: any) {
140+
protected encodeQueryParam(key: string, value: any, withBrackets: boolean = false) {
141141
const encodedKey = encodeURIComponent(key);
142-
return `${encodedKey}=${encodeURIComponent(typeof value === "number" ? value : `${value}`)}`;
142+
return `${encodedKey}${withBrackets ? "[]" : ""}=${encodeURIComponent(
143+
typeof value === "number" ? value : `${value}`,
144+
)}`;
143145
}
144146

145147
protected addQueryParam(query: QueryParamsType, key: string) {

tests/generated/v2.0/petstore-with-external-docs.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,11 @@ export class HttpClient<SecurityDataType = unknown> {
9292
this.securityData = data;
9393
};
9494

95-
protected encodeQueryParam(key: string, value: any) {
95+
protected encodeQueryParam(key: string, value: any, withBrackets: boolean = false) {
9696
const encodedKey = encodeURIComponent(key);
97-
return `${encodedKey}=${encodeURIComponent(typeof value === "number" ? value : `${value}`)}`;
97+
return `${encodedKey}${withBrackets ? "[]" : ""}=${encodeURIComponent(
98+
typeof value === "number" ? value : `${value}`,
99+
)}`;
98100
}
99101

100102
protected addQueryParam(query: QueryParamsType, key: string) {

tests/generated/v2.0/petstore.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,11 @@ export class HttpClient<SecurityDataType = unknown> {
9191
this.securityData = data;
9292
};
9393

94-
protected encodeQueryParam(key: string, value: any) {
94+
protected encodeQueryParam(key: string, value: any, withBrackets: boolean = false) {
9595
const encodedKey = encodeURIComponent(key);
96-
return `${encodedKey}=${encodeURIComponent(typeof value === "number" ? value : `${value}`)}`;
96+
return `${encodedKey}${withBrackets ? "[]" : ""}=${encodeURIComponent(
97+
typeof value === "number" ? value : `${value}`,
98+
)}`;
9799
}
98100

99101
protected addQueryParam(query: QueryParamsType, key: string) {

tests/generated/v2.0/query-path-param.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,11 @@ export class HttpClient<SecurityDataType = unknown> {
7676
this.securityData = data;
7777
};
7878

79-
protected encodeQueryParam(key: string, value: any) {
79+
protected encodeQueryParam(key: string, value: any, withBrackets: boolean = false) {
8080
const encodedKey = encodeURIComponent(key);
81-
return `${encodedKey}=${encodeURIComponent(typeof value === "number" ? value : `${value}`)}`;
81+
return `${encodedKey}${withBrackets ? "[]" : ""}=${encodeURIComponent(
82+
typeof value === "number" ? value : `${value}`,
83+
)}`;
8284
}
8385

8486
protected addQueryParam(query: QueryParamsType, key: string) {

tests/generated/v2.0/uber.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,11 @@ export class HttpClient<SecurityDataType = unknown> {
155155
this.securityData = data;
156156
};
157157

158-
protected encodeQueryParam(key: string, value: any) {
158+
protected encodeQueryParam(key: string, value: any, withBrackets: boolean = false) {
159159
const encodedKey = encodeURIComponent(key);
160-
return `${encodedKey}=${encodeURIComponent(typeof value === "number" ? value : `${value}`)}`;
160+
return `${encodedKey}${withBrackets ? "[]" : ""}=${encodeURIComponent(
161+
typeof value === "number" ? value : `${value}`,
162+
)}`;
161163
}
162164

163165
protected addQueryParam(query: QueryParamsType, key: string) {

tests/generated/v3.0/additional-properties.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,11 @@ export class HttpClient<SecurityDataType = unknown> {
8383
this.securityData = data;
8484
};
8585

86-
protected encodeQueryParam(key: string, value: any) {
86+
protected encodeQueryParam(key: string, value: any, withBrackets: boolean = false) {
8787
const encodedKey = encodeURIComponent(key);
88-
return `${encodedKey}=${encodeURIComponent(typeof value === "number" ? value : `${value}`)}`;
88+
return `${encodedKey}${withBrackets ? "[]" : ""}=${encodeURIComponent(
89+
typeof value === "number" ? value : `${value}`,
90+
)}`;
8991
}
9092

9193
protected addQueryParam(query: QueryParamsType, key: string) {

tests/generated/v3.0/additional-properties2.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,11 @@ export class HttpClient<SecurityDataType = unknown> {
8080
this.securityData = data;
8181
};
8282

83-
protected encodeQueryParam(key: string, value: any) {
83+
protected encodeQueryParam(key: string, value: any, withBrackets: boolean = false) {
8484
const encodedKey = encodeURIComponent(key);
85-
return `${encodedKey}=${encodeURIComponent(typeof value === "number" ? value : `${value}`)}`;
85+
return `${encodedKey}${withBrackets ? "[]" : ""}=${encodeURIComponent(
86+
typeof value === "number" ? value : `${value}`,
87+
)}`;
8688
}
8789

8890
protected addQueryParam(query: QueryParamsType, key: string) {

tests/generated/v3.0/allof-example.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,11 @@ export class HttpClient<SecurityDataType = unknown> {
9090
this.securityData = data;
9191
};
9292

93-
protected encodeQueryParam(key: string, value: any) {
93+
protected encodeQueryParam(key: string, value: any, withBrackets: boolean = false) {
9494
const encodedKey = encodeURIComponent(key);
95-
return `${encodedKey}=${encodeURIComponent(typeof value === "number" ? value : `${value}`)}`;
95+
return `${encodedKey}${withBrackets ? "[]" : ""}=${encodeURIComponent(
96+
typeof value === "number" ? value : `${value}`,
97+
)}`;
9698
}
9799

98100
protected addQueryParam(query: QueryParamsType, key: string) {

0 commit comments

Comments
 (0)