Skip to content

feat(query-params): add an option to use brackets convention with arrays #428

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ Options:
--another-array-type generate array types as Array<Type> (by default Type[]) (default: false)
--sort-types sort fields and types (default: false)
--extract-enums extract all enums from inline interface\type content to typescript enum construction (default: false)
--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`
-h, --help display help for command

Commands:
Expand Down
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,7 @@ export interface GenerateApiConfiguration {
cleanOutput: boolean;
debug: boolean;
anotherArrayType: boolean;
queryParamsWithBrackets: false;
extractRequestBody: boolean;
httpClientType: "axios" | "fetch";
addReadonly: boolean;
Expand Down
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,12 @@ const program = cli({
description: "sort routes in alphabetical order",
default: codeGenBaseConfig.sortRoutes,
},
{
flags: "--query-params-with-brackets",
description:
'use the brackets convention for array in query params: "?a[]=foo&a[]=bar" instead of repeat convention: "?a=foo&a=bar"',
default: codeGenBaseConfig.queryParamsWithBrackets,
},
],
});

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@
"test:extract-enums": "node tests/spec/extract-enums/test.js",
"test:discriminator": "node tests/spec/discriminator/test.js",
"test:sort-types": "node tests/spec/sortTypes/test.js",
"test:sort-types(false)": "node tests/spec/sortTypes(false)/test.js"
"test:sort-types(false)": "node tests/spec/sortTypes(false)/test.js",
"test:--query-params-with-brackets": "node tests/spec/queryParamsWithBrackets/test.js"
},
"author": "acacode",
"license": "MIT",
Expand Down
1 change: 1 addition & 0 deletions src/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ class CodeGenConfig {
apiClassName = "Api";
debug = false;
anotherArrayType = false;
queryParamsWithBrackets = false;
internalTemplateOptions = {
addUtilRequiredKeysType: false,
};
Expand Down
8 changes: 6 additions & 2 deletions templates/base/http-clients/fetch-http-client.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ export class HttpClient<SecurityDataType = unknown> {
this.securityData = data;
}

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

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

protected addArrayQueryParam(query: QueryParamsType, key: string) {
const value = query[key];
<% if (config.queryParamsWithBrackets) { %>
return value.map((v: any) => this.encodeQueryParam(key, v, true)).join("&");
<% } else { %>
return value.map((v: any) => this.encodeQueryParam(key, v)).join("&");
<% } %>
}

protected toQueryString(rawQuery?: QueryParamsType): string {
Expand Down
6 changes: 4 additions & 2 deletions tests/generated/v2.0/adafruit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,11 @@ export class HttpClient<SecurityDataType = unknown> {
this.securityData = data;
};

protected encodeQueryParam(key: string, value: any) {
protected encodeQueryParam(key: string, value: any, withBrackets: boolean = false) {
const encodedKey = encodeURIComponent(key);
return `${encodedKey}=${encodeURIComponent(typeof value === "number" ? value : `${value}`)}`;
return `${encodedKey}${withBrackets ? "[]" : ""}=${encodeURIComponent(
typeof value === "number" ? value : `${value}`,
)}`;
}

protected addQueryParam(query: QueryParamsType, key: string) {
Expand Down
6 changes: 4 additions & 2 deletions tests/generated/v2.0/another-example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,11 @@ export class HttpClient<SecurityDataType = unknown> {
this.securityData = data;
};

protected encodeQueryParam(key: string, value: any) {
protected encodeQueryParam(key: string, value: any, withBrackets: boolean = false) {
const encodedKey = encodeURIComponent(key);
return `${encodedKey}=${encodeURIComponent(typeof value === "number" ? value : `${value}`)}`;
return `${encodedKey}${withBrackets ? "[]" : ""}=${encodeURIComponent(
typeof value === "number" ? value : `${value}`,
)}`;
}

protected addQueryParam(query: QueryParamsType, key: string) {
Expand Down
6 changes: 4 additions & 2 deletions tests/generated/v2.0/another-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,11 @@ export class HttpClient<SecurityDataType = unknown> {
this.securityData = data;
};

protected encodeQueryParam(key: string, value: any) {
protected encodeQueryParam(key: string, value: any, withBrackets: boolean = false) {
const encodedKey = encodeURIComponent(key);
return `${encodedKey}=${encodeURIComponent(typeof value === "number" ? value : `${value}`)}`;
return `${encodedKey}${withBrackets ? "[]" : ""}=${encodeURIComponent(
typeof value === "number" ? value : `${value}`,
)}`;
}

protected addQueryParam(query: QueryParamsType, key: string) {
Expand Down
6 changes: 4 additions & 2 deletions tests/generated/v2.0/api-with-examples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,11 @@ export class HttpClient<SecurityDataType = unknown> {
this.securityData = data;
};

protected encodeQueryParam(key: string, value: any) {
protected encodeQueryParam(key: string, value: any, withBrackets: boolean = false) {
const encodedKey = encodeURIComponent(key);
return `${encodedKey}=${encodeURIComponent(typeof value === "number" ? value : `${value}`)}`;
return `${encodedKey}${withBrackets ? "[]" : ""}=${encodeURIComponent(
typeof value === "number" ? value : `${value}`,
)}`;
}

protected addQueryParam(query: QueryParamsType, key: string) {
Expand Down
6 changes: 4 additions & 2 deletions tests/generated/v2.0/authentiq.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,11 @@ export class HttpClient<SecurityDataType = unknown> {
this.securityData = data;
};

protected encodeQueryParam(key: string, value: any) {
protected encodeQueryParam(key: string, value: any, withBrackets: boolean = false) {
const encodedKey = encodeURIComponent(key);
return `${encodedKey}=${encodeURIComponent(typeof value === "number" ? value : `${value}`)}`;
return `${encodedKey}${withBrackets ? "[]" : ""}=${encodeURIComponent(
typeof value === "number" ? value : `${value}`,
)}`;
}

protected addQueryParam(query: QueryParamsType, key: string) {
Expand Down
6 changes: 4 additions & 2 deletions tests/generated/v2.0/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,11 @@ export class HttpClient<SecurityDataType = unknown> {
this.securityData = data;
};

protected encodeQueryParam(key: string, value: any) {
protected encodeQueryParam(key: string, value: any, withBrackets: boolean = false) {
const encodedKey = encodeURIComponent(key);
return `${encodedKey}=${encodeURIComponent(typeof value === "number" ? value : `${value}`)}`;
return `${encodedKey}${withBrackets ? "[]" : ""}=${encodeURIComponent(
typeof value === "number" ? value : `${value}`,
)}`;
}

protected addQueryParam(query: QueryParamsType, key: string) {
Expand Down
6 changes: 4 additions & 2 deletions tests/generated/v2.0/example1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,11 @@ export class HttpClient<SecurityDataType = unknown> {
this.securityData = data;
};

protected encodeQueryParam(key: string, value: any) {
protected encodeQueryParam(key: string, value: any, withBrackets: boolean = false) {
const encodedKey = encodeURIComponent(key);
return `${encodedKey}=${encodeURIComponent(typeof value === "number" ? value : `${value}`)}`;
return `${encodedKey}${withBrackets ? "[]" : ""}=${encodeURIComponent(
typeof value === "number" ? value : `${value}`,
)}`;
}

protected addQueryParam(query: QueryParamsType, key: string) {
Expand Down
6 changes: 4 additions & 2 deletions tests/generated/v2.0/file-formdata-example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,11 @@ export class HttpClient<SecurityDataType = unknown> {
this.securityData = data;
};

protected encodeQueryParam(key: string, value: any) {
protected encodeQueryParam(key: string, value: any, withBrackets: boolean = false) {
const encodedKey = encodeURIComponent(key);
return `${encodedKey}=${encodeURIComponent(typeof value === "number" ? value : `${value}`)}`;
return `${encodedKey}${withBrackets ? "[]" : ""}=${encodeURIComponent(
typeof value === "number" ? value : `${value}`,
)}`;
}

protected addQueryParam(query: QueryParamsType, key: string) {
Expand Down
6 changes: 4 additions & 2 deletions tests/generated/v2.0/furkot-example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,11 @@ export class HttpClient<SecurityDataType = unknown> {
this.securityData = data;
};

protected encodeQueryParam(key: string, value: any) {
protected encodeQueryParam(key: string, value: any, withBrackets: boolean = false) {
const encodedKey = encodeURIComponent(key);
return `${encodedKey}=${encodeURIComponent(typeof value === "number" ? value : `${value}`)}`;
return `${encodedKey}${withBrackets ? "[]" : ""}=${encodeURIComponent(
typeof value === "number" ? value : `${value}`,
)}`;
}

protected addQueryParam(query: QueryParamsType, key: string) {
Expand Down
6 changes: 4 additions & 2 deletions tests/generated/v2.0/giphy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,9 +333,11 @@ export class HttpClient<SecurityDataType = unknown> {
this.securityData = data;
};

protected encodeQueryParam(key: string, value: any) {
protected encodeQueryParam(key: string, value: any, withBrackets: boolean = false) {
const encodedKey = encodeURIComponent(key);
return `${encodedKey}=${encodeURIComponent(typeof value === "number" ? value : `${value}`)}`;
return `${encodedKey}${withBrackets ? "[]" : ""}=${encodeURIComponent(
typeof value === "number" ? value : `${value}`,
)}`;
}

protected addQueryParam(query: QueryParamsType, key: string) {
Expand Down
6 changes: 4 additions & 2 deletions tests/generated/v2.0/github-swagger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1974,9 +1974,11 @@ export class HttpClient<SecurityDataType = unknown> {
this.securityData = data;
};

protected encodeQueryParam(key: string, value: any) {
protected encodeQueryParam(key: string, value: any, withBrackets: boolean = false) {
const encodedKey = encodeURIComponent(key);
return `${encodedKey}=${encodeURIComponent(typeof value === "number" ? value : `${value}`)}`;
return `${encodedKey}${withBrackets ? "[]" : ""}=${encodeURIComponent(
typeof value === "number" ? value : `${value}`,
)}`;
}

protected addQueryParam(query: QueryParamsType, key: string) {
Expand Down
6 changes: 4 additions & 2 deletions tests/generated/v2.0/path-args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,11 @@ export class HttpClient<SecurityDataType = unknown> {
this.securityData = data;
};

protected encodeQueryParam(key: string, value: any) {
protected encodeQueryParam(key: string, value: any, withBrackets: boolean = false) {
const encodedKey = encodeURIComponent(key);
return `${encodedKey}=${encodeURIComponent(typeof value === "number" ? value : `${value}`)}`;
return `${encodedKey}${withBrackets ? "[]" : ""}=${encodeURIComponent(
typeof value === "number" ? value : `${value}`,
)}`;
}

protected addQueryParam(query: QueryParamsType, key: string) {
Expand Down
6 changes: 4 additions & 2 deletions tests/generated/v2.0/petstore-expanded.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,11 @@ export class HttpClient<SecurityDataType = unknown> {
this.securityData = data;
};

protected encodeQueryParam(key: string, value: any) {
protected encodeQueryParam(key: string, value: any, withBrackets: boolean = false) {
const encodedKey = encodeURIComponent(key);
return `${encodedKey}=${encodeURIComponent(typeof value === "number" ? value : `${value}`)}`;
return `${encodedKey}${withBrackets ? "[]" : ""}=${encodeURIComponent(
typeof value === "number" ? value : `${value}`,
)}`;
}

protected addQueryParam(query: QueryParamsType, key: string) {
Expand Down
6 changes: 4 additions & 2 deletions tests/generated/v2.0/petstore-minimal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,11 @@ export class HttpClient<SecurityDataType = unknown> {
this.securityData = data;
};

protected encodeQueryParam(key: string, value: any) {
protected encodeQueryParam(key: string, value: any, withBrackets: boolean = false) {
const encodedKey = encodeURIComponent(key);
return `${encodedKey}=${encodeURIComponent(typeof value === "number" ? value : `${value}`)}`;
return `${encodedKey}${withBrackets ? "[]" : ""}=${encodeURIComponent(
typeof value === "number" ? value : `${value}`,
)}`;
}

protected addQueryParam(query: QueryParamsType, key: string) {
Expand Down
6 changes: 4 additions & 2 deletions tests/generated/v2.0/petstore-simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,11 @@ export class HttpClient<SecurityDataType = unknown> {
this.securityData = data;
};

protected encodeQueryParam(key: string, value: any) {
protected encodeQueryParam(key: string, value: any, withBrackets: boolean = false) {
const encodedKey = encodeURIComponent(key);
return `${encodedKey}=${encodeURIComponent(typeof value === "number" ? value : `${value}`)}`;
return `${encodedKey}${withBrackets ? "[]" : ""}=${encodeURIComponent(
typeof value === "number" ? value : `${value}`,
)}`;
}

protected addQueryParam(query: QueryParamsType, key: string) {
Expand Down
6 changes: 4 additions & 2 deletions tests/generated/v2.0/petstore-swagger-io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,11 @@ export class HttpClient<SecurityDataType = unknown> {
this.securityData = data;
};

protected encodeQueryParam(key: string, value: any) {
protected encodeQueryParam(key: string, value: any, withBrackets: boolean = false) {
const encodedKey = encodeURIComponent(key);
return `${encodedKey}=${encodeURIComponent(typeof value === "number" ? value : `${value}`)}`;
return `${encodedKey}${withBrackets ? "[]" : ""}=${encodeURIComponent(
typeof value === "number" ? value : `${value}`,
)}`;
}

protected addQueryParam(query: QueryParamsType, key: string) {
Expand Down
6 changes: 4 additions & 2 deletions tests/generated/v2.0/petstore-with-external-docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,11 @@ export class HttpClient<SecurityDataType = unknown> {
this.securityData = data;
};

protected encodeQueryParam(key: string, value: any) {
protected encodeQueryParam(key: string, value: any, withBrackets: boolean = false) {
const encodedKey = encodeURIComponent(key);
return `${encodedKey}=${encodeURIComponent(typeof value === "number" ? value : `${value}`)}`;
return `${encodedKey}${withBrackets ? "[]" : ""}=${encodeURIComponent(
typeof value === "number" ? value : `${value}`,
)}`;
}

protected addQueryParam(query: QueryParamsType, key: string) {
Expand Down
6 changes: 4 additions & 2 deletions tests/generated/v2.0/petstore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,11 @@ export class HttpClient<SecurityDataType = unknown> {
this.securityData = data;
};

protected encodeQueryParam(key: string, value: any) {
protected encodeQueryParam(key: string, value: any, withBrackets: boolean = false) {
const encodedKey = encodeURIComponent(key);
return `${encodedKey}=${encodeURIComponent(typeof value === "number" ? value : `${value}`)}`;
return `${encodedKey}${withBrackets ? "[]" : ""}=${encodeURIComponent(
typeof value === "number" ? value : `${value}`,
)}`;
}

protected addQueryParam(query: QueryParamsType, key: string) {
Expand Down
6 changes: 4 additions & 2 deletions tests/generated/v2.0/query-path-param.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,11 @@ export class HttpClient<SecurityDataType = unknown> {
this.securityData = data;
};

protected encodeQueryParam(key: string, value: any) {
protected encodeQueryParam(key: string, value: any, withBrackets: boolean = false) {
const encodedKey = encodeURIComponent(key);
return `${encodedKey}=${encodeURIComponent(typeof value === "number" ? value : `${value}`)}`;
return `${encodedKey}${withBrackets ? "[]" : ""}=${encodeURIComponent(
typeof value === "number" ? value : `${value}`,
)}`;
}

protected addQueryParam(query: QueryParamsType, key: string) {
Expand Down
6 changes: 4 additions & 2 deletions tests/generated/v2.0/uber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,11 @@ export class HttpClient<SecurityDataType = unknown> {
this.securityData = data;
};

protected encodeQueryParam(key: string, value: any) {
protected encodeQueryParam(key: string, value: any, withBrackets: boolean = false) {
const encodedKey = encodeURIComponent(key);
return `${encodedKey}=${encodeURIComponent(typeof value === "number" ? value : `${value}`)}`;
return `${encodedKey}${withBrackets ? "[]" : ""}=${encodeURIComponent(
typeof value === "number" ? value : `${value}`,
)}`;
}

protected addQueryParam(query: QueryParamsType, key: string) {
Expand Down
6 changes: 4 additions & 2 deletions tests/generated/v3.0/additional-properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,11 @@ export class HttpClient<SecurityDataType = unknown> {
this.securityData = data;
};

protected encodeQueryParam(key: string, value: any) {
protected encodeQueryParam(key: string, value: any, withBrackets: boolean = false) {
const encodedKey = encodeURIComponent(key);
return `${encodedKey}=${encodeURIComponent(typeof value === "number" ? value : `${value}`)}`;
return `${encodedKey}${withBrackets ? "[]" : ""}=${encodeURIComponent(
typeof value === "number" ? value : `${value}`,
)}`;
}

protected addQueryParam(query: QueryParamsType, key: string) {
Expand Down
6 changes: 4 additions & 2 deletions tests/generated/v3.0/additional-properties2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,11 @@ export class HttpClient<SecurityDataType = unknown> {
this.securityData = data;
};

protected encodeQueryParam(key: string, value: any) {
protected encodeQueryParam(key: string, value: any, withBrackets: boolean = false) {
const encodedKey = encodeURIComponent(key);
return `${encodedKey}=${encodeURIComponent(typeof value === "number" ? value : `${value}`)}`;
return `${encodedKey}${withBrackets ? "[]" : ""}=${encodeURIComponent(
typeof value === "number" ? value : `${value}`,
)}`;
}

protected addQueryParam(query: QueryParamsType, key: string) {
Expand Down
6 changes: 4 additions & 2 deletions tests/generated/v3.0/allof-example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,11 @@ export class HttpClient<SecurityDataType = unknown> {
this.securityData = data;
};

protected encodeQueryParam(key: string, value: any) {
protected encodeQueryParam(key: string, value: any, withBrackets: boolean = false) {
const encodedKey = encodeURIComponent(key);
return `${encodedKey}=${encodeURIComponent(typeof value === "number" ? value : `${value}`)}`;
return `${encodedKey}${withBrackets ? "[]" : ""}=${encodeURIComponent(
typeof value === "number" ? value : `${value}`,
)}`;
}

protected addQueryParam(query: QueryParamsType, key: string) {
Expand Down
Loading