Skip to content

Commit 3f440a7

Browse files
committed
feat(query-params): add an option to use brackets convention with arrays
1 parent 52bc5bf commit 3f440a7

Some content is hidden

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

74 files changed

+676
-130
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ Options:
7171
--clean-output clean output folder before generate api. WARNING: May cause data loss (default: false)
7272
--api-class-name <string> name of the api class
7373
--patch fix up small errors in the swagger source definition (default: false)
74+
--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`
7475
-h, --help display help for command
7576
```
7677

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
*/
@@ -350,6 +356,7 @@ export interface GenerateApiConfiguration {
350356
};
351357
routeNameDuplicatesMap: Map<string, string>;
352358
apiClassName: string;
359+
queryParamsWithBrackets: boolean;
353360
};
354361
modelTypes: ModelType[];
355362
rawModelTypes: SchemaComponent[];

index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ const options = program
6868
.option("--api-class-name <string>", "name of the api class")
6969
.option("--patch", "fix up small errors in the swagger source definition", false)
7070
.option("--debug", "additional information about processes inside this tool", false)
71+
.option(
72+
"--query-params-with-brackets",
73+
'use the brackets convention for array in query params: "?a[]=foo&a[]=bar" instead of repeat convention: "?a=foo&a=bar"',
74+
)
7175
.parse(process.argv)
7276
.opts();
7377

@@ -105,6 +109,7 @@ generateApi({
105109
typePrefix: options.typePrefix,
106110
typeSuffix: options.typeSuffix,
107111
patch: !!options.patch,
112+
queryParamsWithBrackets: !!options.queryParamsWithBrackets,
108113
apiClassName: options.apiClassName,
109114
debug: options.debug,
110115
}).catch((err) => {

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@
4646
"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",
4747
"test:partialBaseTemplate": "node tests/spec/partialBaseTemplate/test.js",
4848
"test:partialDefaultTemplate": "node tests/spec/partialDefaultTemplate/test.js",
49-
"test:--patch": "node tests/spec/patch/test.js"
49+
"test:--patch": "node tests/spec/patch/test.js",
50+
"test:--query-params-with-brackets": "node tests/spec/queryParamsWithBrackets/test.js"
5051
},
5152
"author": "acacode",
5253
"license": "MIT",

src/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ const config = {
9090
typePrefix: "",
9191
typeSuffix: "",
9292
patch: false,
93+
queryParamsWithBrackets: false,
9394
componentTypeNameResolver: new NameResolver([]),
9495
/** name of the main exported class */
9596
apiClassName: "Api",

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ module.exports = {
6363
typePrefix = config.typePrefix,
6464
typeSuffix = config.typeSuffix,
6565
patch = config.patch,
66+
queryParamsWithBrackets = config.queryParamsWithBrackets,
6667
authorizationToken,
6768
apiClassName = config.apiClassName,
6869
debug = config.debug,
@@ -101,6 +102,7 @@ module.exports = {
101102
typePrefix,
102103
typeSuffix,
103104
patch,
105+
queryParamsWithBrackets,
104106
apiClassName,
105107
debug,
106108
});

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

Lines changed: 7 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,12 @@ export class HttpClient<SecurityDataType = unknown> {
8080

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

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

tests/generated/v2.0/adafruit.ts

Lines changed: 5 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) {
@@ -240,6 +242,7 @@ export class HttpClient<SecurityDataType = unknown> {
240242

241243
protected addArrayQueryParam(query: QueryParamsType, key: string) {
242244
const value = query[key];
245+
243246
return value.map((v: any) => this.encodeQueryParam(key, v)).join("&");
244247
}
245248

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

Lines changed: 5 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) {
@@ -214,6 +216,7 @@ export class HttpClient<SecurityDataType = unknown> {
214216

215217
protected addArrayQueryParam(query: QueryParamsType, key: string) {
216218
const value = query[key];
219+
217220
return value.map((v: any) => this.encodeQueryParam(key, v)).join("&");
218221
}
219222

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

Lines changed: 5 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) {
@@ -107,6 +109,7 @@ export class HttpClient<SecurityDataType = unknown> {
107109

108110
protected addArrayQueryParam(query: QueryParamsType, key: string) {
109111
const value = query[key];
112+
110113
return value.map((v: any) => this.encodeQueryParam(key, v)).join("&");
111114
}
112115

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

Lines changed: 5 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) {
@@ -86,6 +88,7 @@ export class HttpClient<SecurityDataType = unknown> {
8688

8789
protected addArrayQueryParam(query: QueryParamsType, key: string) {
8890
const value = query[key];
91+
8992
return value.map((v: any) => this.encodeQueryParam(key, v)).join("&");
9093
}
9194

tests/generated/v2.0/authentiq.ts

Lines changed: 5 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) {
@@ -132,6 +134,7 @@ export class HttpClient<SecurityDataType = unknown> {
132134

133135
protected addArrayQueryParam(query: QueryParamsType, key: string) {
134136
const value = query[key];
137+
135138
return value.map((v: any) => this.encodeQueryParam(key, v)).join("&");
136139
}
137140

tests/generated/v2.0/enums.ts

Lines changed: 5 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) {
@@ -151,6 +153,7 @@ export class HttpClient<SecurityDataType = unknown> {
151153

152154
protected addArrayQueryParam(query: QueryParamsType, key: string) {
153155
const value = query[key];
156+
154157
return value.map((v: any) => this.encodeQueryParam(key, v)).join("&");
155158
}
156159

tests/generated/v2.0/example1.ts

Lines changed: 5 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) {
@@ -129,6 +131,7 @@ export class HttpClient<SecurityDataType = unknown> {
129131

130132
protected addArrayQueryParam(query: QueryParamsType, key: string) {
131133
const value = query[key];
134+
132135
return value.map((v: any) => this.encodeQueryParam(key, v)).join("&");
133136
}
134137

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

Lines changed: 5 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) {
@@ -86,6 +88,7 @@ export class HttpClient<SecurityDataType = unknown> {
8688

8789
protected addArrayQueryParam(query: QueryParamsType, key: string) {
8890
const value = query[key];
91+
8992
return value.map((v: any) => this.encodeQueryParam(key, v)).join("&");
9093
}
9194

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

Lines changed: 5 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) {
@@ -159,6 +161,7 @@ export class HttpClient<SecurityDataType = unknown> {
159161

160162
protected addArrayQueryParam(query: QueryParamsType, key: string) {
161163
const value = query[key];
164+
162165
return value.map((v: any) => this.encodeQueryParam(key, v)).join("&");
163166
}
164167

tests/generated/v2.0/giphy.ts

Lines changed: 5 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) {
@@ -352,6 +354,7 @@ export class HttpClient<SecurityDataType = unknown> {
352354

353355
protected addArrayQueryParam(query: QueryParamsType, key: string) {
354356
const value = query[key];
357+
355358
return value.map((v: any) => this.encodeQueryParam(key, v)).join("&");
356359
}
357360

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

Lines changed: 5 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) {
@@ -1982,6 +1984,7 @@ export class HttpClient<SecurityDataType = unknown> {
19821984

19831985
protected addArrayQueryParam(query: QueryParamsType, key: string) {
19841986
const value = query[key];
1987+
19851988
return value.map((v: any) => this.encodeQueryParam(key, v)).join("&");
19861989
}
19871990

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

Lines changed: 5 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) {
@@ -86,6 +88,7 @@ export class HttpClient<SecurityDataType = unknown> {
8688

8789
protected addArrayQueryParam(query: QueryParamsType, key: string) {
8890
const value = query[key];
91+
8992
return value.map((v: any) => this.encodeQueryParam(key, v)).join("&");
9093
}
9194

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

Lines changed: 5 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) {
@@ -124,6 +126,7 @@ export class HttpClient<SecurityDataType = unknown> {
124126

125127
protected addArrayQueryParam(query: QueryParamsType, key: string) {
126128
const value = query[key];
129+
127130
return value.map((v: any) => this.encodeQueryParam(key, v)).join("&");
128131
}
129132

0 commit comments

Comments
 (0)