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

File tree

74 files changed

+676
-130
lines changed

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

0 commit comments

Comments
 (0)