Skip to content

Commit ab9ead7

Browse files
committed
feat(query-params): add an option to use brackets convention with arrays
1 parent 5891b60 commit ab9ead7

File tree

94 files changed

+951
-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.

94 files changed

+951
-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
*/
@@ -445,6 +451,7 @@ export interface GenerateApiConfiguration {
445451
};
446452
routeNameDuplicatesMap: Map<string, string>;
447453
apiClassName: string;
454+
queryParamsWithBrackets: boolean;
448455
};
449456
modelTypes: ModelType[];
450457
rawModelTypes: SchemaComponent[];

index.js

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

@@ -106,6 +110,7 @@ generateApi({
106110
typePrefix: options.typePrefix,
107111
typeSuffix: options.typeSuffix,
108112
patch: !!options.patch,
113+
queryParamsWithBrackets: !!options.queryParamsWithBrackets,
109114
apiClassName: options.apiClassName,
110115
debug: options.debug,
111116
anotherArrayType: options.anotherArrayType,

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@
5050
"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",
5151
"test:partialBaseTemplate": "node tests/spec/partialBaseTemplate/test.js",
5252
"test:partialDefaultTemplate": "node tests/spec/partialDefaultTemplate/test.js",
53-
"test:--patch": "node tests/spec/patch/test.js"
53+
"test:--patch": "node tests/spec/patch/test.js",
54+
"test:--query-params-with-brackets": "node tests/spec/queryParamsWithBrackets/test.js"
5455
},
5556
"author": "acacode",
5657
"license": "MIT",

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
@@ -197,9 +197,11 @@ export class HttpClient<SecurityDataType = unknown> {
197197
this.securityData = data;
198198
};
199199

200-
protected encodeQueryParam(key: string, value: any) {
200+
protected encodeQueryParam(key: string, value: any, withBrackets: boolean = false) {
201201
const encodedKey = encodeURIComponent(key);
202-
return `${encodedKey}=${encodeURIComponent(typeof value === "number" ? value : `${value}`)}`;
202+
return `${encodedKey}${withBrackets ? "[]" : ""}=${encodeURIComponent(
203+
typeof value === "number" ? value : `${value}`,
204+
)}`;
203205
}
204206

205207
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
@@ -115,9 +115,11 @@ export class HttpClient<SecurityDataType = unknown> {
115115
this.securityData = data;
116116
};
117117

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

123125
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
@@ -134,9 +134,11 @@ export class HttpClient<SecurityDataType = unknown> {
134134
this.securityData = data;
135135
};
136136

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

142144
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
@@ -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/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
@@ -329,9 +329,11 @@ export class HttpClient<SecurityDataType = unknown> {
329329
this.securityData = data;
330330
};
331331

332-
protected encodeQueryParam(key: string, value: any) {
332+
protected encodeQueryParam(key: string, value: any, withBrackets: boolean = false) {
333333
const encodedKey = encodeURIComponent(key);
334-
return `${encodedKey}=${encodeURIComponent(typeof value === "number" ? value : `${value}`)}`;
334+
return `${encodedKey}${withBrackets ? "[]" : ""}=${encodeURIComponent(
335+
typeof value === "number" ? value : `${value}`,
336+
)}`;
335337
}
336338

337339
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
@@ -1973,9 +1973,11 @@ export class HttpClient<SecurityDataType = unknown> {
19731973
this.securityData = data;
19741974
};
19751975

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

19811983
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
@@ -111,9 +111,11 @@ export class HttpClient<SecurityDataType = unknown> {
111111
this.securityData = data;
112112
};
113113

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

119121
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
@@ -99,9 +99,11 @@ export class HttpClient<SecurityDataType = unknown> {
9999
this.securityData = data;
100100
};
101101

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

107109
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) {

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

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

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

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

0 commit comments

Comments
 (0)