Skip to content

Commit 3a26ff5

Browse files
committed
feat(query-params): add an option to use brackets convention with arrays
1 parent 9c59409 commit 3a26ff5

File tree

96 files changed

+954
-171
lines changed

Some content is hidden

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

96 files changed

+954
-171
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ Options:
7373
--patch fix up small errors in the swagger source definition (default: false)
7474
--debug additional information about processes inside this tool (default: false)
7575
--another-array-type generate array types as Array<Type> (by default Type[]) (default: false)
76+
--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`
7677
-h, --help display help for command
7778
```
7879

index.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,12 @@ interface GenerateApiParamsBase {
119119
* fix up small errors in the swagger source definition
120120
*/
121121
patch?: boolean;
122+
123+
/**
124+
* use the brackets convention for array in query params: "?a[]=foo&a[]=bar" instead of repeat convention: "?a=foo&a=bar"
125+
*/
126+
queryParamsWithBrackets?: boolean;
127+
122128
/**
123129
* authorization token
124130
*/
@@ -354,6 +360,7 @@ export interface GenerateApiConfiguration {
354360
};
355361
routeNameDuplicatesMap: Map<string, string>;
356362
apiClassName: string;
363+
queryParamsWithBrackets: boolean;
357364
};
358365
modelTypes: ModelType[];
359366
rawModelTypes: SchemaComponent[];

index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ const options = program
7070
.option("--patch", "fix up small errors in the swagger source definition", false)
7171
.option("--debug", "additional information about processes inside this tool", false)
7272
.option("--another-array-type", "generate array types as Array<Type> (by default Type[])", false)
73+
.option(
74+
"--query-params-with-brackets",
75+
'use the brackets convention for array in query params: "?a[]=foo&a[]=bar" instead of repeat convention: "?a=foo&a=bar"',
76+
)
7377
.parse(process.argv)
7478
.opts();
7579

@@ -107,6 +111,7 @@ generateApi({
107111
typePrefix: options.typePrefix,
108112
typeSuffix: options.typeSuffix,
109113
patch: !!options.patch,
114+
queryParamsWithBrackets: !!options.queryParamsWithBrackets,
110115
apiClassName: options.apiClassName,
111116
debug: options.debug,
112117
anotherArrayType: options.anotherArrayType,

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@
4949
"test:--cli": "node index.js -p tests/spec/cli/schema.json -o tests/spec/cli -n schema.ts --extract-response-body --extract-response-error --type-prefix Prefix --api-class-name MySuperApi --no-client",
5050
"test:partialBaseTemplate": "node tests/spec/partialBaseTemplate/test.js",
5151
"test:partialDefaultTemplate": "node tests/spec/partialDefaultTemplate/test.js",
52-
"test:--patch": "node tests/spec/patch/test.js"
52+
"test:--patch": "node tests/spec/patch/test.js",
53+
"test:--query-params-with-brackets": "node tests/spec/queryParamsWithBrackets/test.js"
5354
},
5455
"author": "acacode",
5556
"license": "MIT",

src/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ const config = {
9393
typePrefix: "",
9494
typeSuffix: "",
9595
patch: false,
96+
queryParamsWithBrackets: false,
9697
componentTypeNameResolver: new NameResolver([]),
9798
/** name of the main exported class */
9899
apiClassName: "Api",

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ module.exports = {
6464
typePrefix = config.typePrefix,
6565
typeSuffix = config.typeSuffix,
6666
patch = config.patch,
67+
queryParamsWithBrackets = config.queryParamsWithBrackets,
6768
authorizationToken,
6869
apiClassName = config.apiClassName,
6970
debug = config.debug,
@@ -112,6 +113,7 @@ module.exports = {
112113
typePrefix,
113114
typeSuffix,
114115
patch,
116+
queryParamsWithBrackets,
115117
apiClassName,
116118
debug,
117119
anotherArrayType,

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

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

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

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

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

8690
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
@@ -229,9 +229,11 @@ export class HttpClient<SecurityDataType = unknown> {
229229
this.securityData = data;
230230
};
231231

232-
protected encodeQueryParam(key: string, value: any) {
232+
protected encodeQueryParam(key: string, value: any, withBrackets: boolean = false) {
233233
const encodedKey = encodeURIComponent(key);
234-
return `${encodedKey}=${encodeURIComponent(typeof value === "number" ? value : `${value}`)}`;
234+
return `${encodedKey}${withBrackets ? "[]" : ""}=${encodeURIComponent(
235+
typeof value === "number" ? value : `${value}`,
236+
)}`;
235237
}
236238

237239
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
@@ -203,9 +203,11 @@ export class HttpClient<SecurityDataType = unknown> {
203203
this.securityData = data;
204204
};
205205

206-
protected encodeQueryParam(key: string, value: any) {
206+
protected encodeQueryParam(key: string, value: any, withBrackets: boolean = false) {
207207
const encodedKey = encodeURIComponent(key);
208-
return `${encodedKey}=${encodeURIComponent(typeof value === "number" ? value : `${value}`)}`;
208+
return `${encodedKey}${withBrackets ? "[]" : ""}=${encodeURIComponent(
209+
typeof value === "number" ? value : `${value}`,
210+
)}`;
209211
}
210212

211213
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
@@ -96,9 +96,11 @@ export class HttpClient<SecurityDataType = unknown> {
9696
this.securityData = data;
9797
};
9898

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

104106
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
@@ -75,9 +75,11 @@ export class HttpClient<SecurityDataType = unknown> {
7575
this.securityData = data;
7676
};
7777

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

8385
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
@@ -121,9 +121,11 @@ export class HttpClient<SecurityDataType = unknown> {
121121
this.securityData = data;
122122
};
123123

124-
protected encodeQueryParam(key: string, value: any) {
124+
protected encodeQueryParam(key: string, value: any, withBrackets: boolean = false) {
125125
const encodedKey = encodeURIComponent(key);
126-
return `${encodedKey}=${encodeURIComponent(typeof value === "number" ? value : `${value}`)}`;
126+
return `${encodedKey}${withBrackets ? "[]" : ""}=${encodeURIComponent(
127+
typeof value === "number" ? value : `${value}`,
128+
)}`;
127129
}
128130

129131
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
@@ -140,9 +140,11 @@ export class HttpClient<SecurityDataType = unknown> {
140140
this.securityData = data;
141141
};
142142

143-
protected encodeQueryParam(key: string, value: any) {
143+
protected encodeQueryParam(key: string, value: any, withBrackets: boolean = false) {
144144
const encodedKey = encodeURIComponent(key);
145-
return `${encodedKey}=${encodeURIComponent(typeof value === "number" ? value : `${value}`)}`;
145+
return `${encodedKey}${withBrackets ? "[]" : ""}=${encodeURIComponent(
146+
typeof value === "number" ? value : `${value}`,
147+
)}`;
146148
}
147149

148150
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
@@ -118,9 +118,11 @@ export class HttpClient<SecurityDataType = unknown> {
118118
this.securityData = data;
119119
};
120120

121-
protected encodeQueryParam(key: string, value: any) {
121+
protected encodeQueryParam(key: string, value: any, withBrackets: boolean = false) {
122122
const encodedKey = encodeURIComponent(key);
123-
return `${encodedKey}=${encodeURIComponent(typeof value === "number" ? value : `${value}`)}`;
123+
return `${encodedKey}${withBrackets ? "[]" : ""}=${encodeURIComponent(
124+
typeof value === "number" ? value : `${value}`,
125+
)}`;
124126
}
125127

126128
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
@@ -75,9 +75,11 @@ export class HttpClient<SecurityDataType = unknown> {
7575
this.securityData = data;
7676
};
7777

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

8385
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
@@ -148,9 +148,11 @@ export class HttpClient<SecurityDataType = unknown> {
148148
this.securityData = data;
149149
};
150150

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

156158
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
@@ -341,9 +341,11 @@ export class HttpClient<SecurityDataType = unknown> {
341341
this.securityData = data;
342342
};
343343

344-
protected encodeQueryParam(key: string, value: any) {
344+
protected encodeQueryParam(key: string, value: any, withBrackets: boolean = false) {
345345
const encodedKey = encodeURIComponent(key);
346-
return `${encodedKey}=${encodeURIComponent(typeof value === "number" ? value : `${value}`)}`;
346+
return `${encodedKey}${withBrackets ? "[]" : ""}=${encodeURIComponent(
347+
typeof value === "number" ? value : `${value}`,
348+
)}`;
347349
}
348350

349351
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
@@ -1971,9 +1971,11 @@ export class HttpClient<SecurityDataType = unknown> {
19711971
this.securityData = data;
19721972
};
19731973

1974-
protected encodeQueryParam(key: string, value: any) {
1974+
protected encodeQueryParam(key: string, value: any, withBrackets: boolean = false) {
19751975
const encodedKey = encodeURIComponent(key);
1976-
return `${encodedKey}=${encodeURIComponent(typeof value === "number" ? value : `${value}`)}`;
1976+
return `${encodedKey}${withBrackets ? "[]" : ""}=${encodeURIComponent(
1977+
typeof value === "number" ? value : `${value}`,
1978+
)}`;
19771979
}
19781980

19791981
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
@@ -75,9 +75,11 @@ export class HttpClient<SecurityDataType = unknown> {
7575
this.securityData = data;
7676
};
7777

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

8385
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
@@ -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/petstore-minimal.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/v2.0/petstore-simple.ts

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

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

109111
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
@@ -136,9 +136,11 @@ export class HttpClient<SecurityDataType = unknown> {
136136
this.securityData = data;
137137
};
138138

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

144146
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
@@ -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/petstore.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) {

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

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

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

8385
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
@@ -154,9 +154,11 @@ export class HttpClient<SecurityDataType = unknown> {
154154
this.securityData = data;
155155
};
156156

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

162164
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
@@ -82,9 +82,11 @@ export class HttpClient<SecurityDataType = unknown> {
8282
this.securityData = data;
8383
};
8484

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

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

0 commit comments

Comments
 (0)