Skip to content

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

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 @@ -71,6 +71,7 @@ Options:
--clean-output clean output folder before generate api. WARNING: May cause data loss (default: false)
--api-class-name <string> name of the api class
--patch fix up small errors in the swagger source definition (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
```

Expand Down
7 changes: 7 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ interface GenerateApiParamsBase {
* fix up small errors in the swagger source definition
*/
patch?: boolean;

/**
* use the brackets convention for array in query params: "?a[]=foo&a[]=bar" instead of repeat convention: "?a=foo&a=bar"
*/
queryParamsWithBrackets?: boolean;

/**
* authorization token
*/
Expand Down Expand Up @@ -350,6 +356,7 @@ export interface GenerateApiConfiguration {
};
routeNameDuplicatesMap: Map<string, string>;
apiClassName: string;
queryParamsWithBrackets: boolean;
};
modelTypes: ModelType[];
rawModelTypes: SchemaComponent[];
Expand Down
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ const options = program
.option("--api-class-name <string>", "name of the api class")
.option("--patch", "fix up small errors in the swagger source definition", false)
.option("--debug", "additional information about processes inside this tool", false)
.option(
"--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"',
)
.parse(process.argv)
.opts();

Expand Down Expand Up @@ -105,6 +109,7 @@ generateApi({
typePrefix: options.typePrefix,
typeSuffix: options.typeSuffix,
patch: !!options.patch,
queryParamsWithBrackets: !!options.queryParamsWithBrackets,
apiClassName: options.apiClassName,
debug: options.debug,
}).catch((err) => {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
"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",
"test:partialBaseTemplate": "node tests/spec/partialBaseTemplate/test.js",
"test:partialDefaultTemplate": "node tests/spec/partialDefaultTemplate/test.js",
"test:--patch": "node tests/spec/patch/test.js"
"test:--patch": "node tests/spec/patch/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/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ const config = {
typePrefix: "",
typeSuffix: "",
patch: false,
queryParamsWithBrackets: false,
componentTypeNameResolver: new NameResolver([]),
/** name of the main exported class */
apiClassName: "Api",
Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ module.exports = {
typePrefix = config.typePrefix,
typeSuffix = config.typeSuffix,
patch = config.patch,
queryParamsWithBrackets = config.queryParamsWithBrackets,
authorizationToken,
apiClassName = config.apiClassName,
debug = config.debug,
Expand Down Expand Up @@ -101,6 +102,7 @@ module.exports = {
typePrefix,
typeSuffix,
patch,
queryParamsWithBrackets,
apiClassName,
debug,
});
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 @@ -69,9 +69,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 @@ -80,7 +80,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 @@ -229,9 +229,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 @@ -203,9 +203,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 @@ -96,9 +96,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 @@ -75,9 +75,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 @@ -121,9 +121,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 @@ -140,9 +140,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 @@ -118,9 +118,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 @@ -75,9 +75,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 @@ -148,9 +148,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 @@ -341,9 +341,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 @@ -1971,9 +1971,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 @@ -75,9 +75,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 @@ -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/petstore-minimal.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/v2.0/petstore-simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,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 @@ -136,9 +136,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 @@ -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/petstore.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
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 @@ -75,9 +75,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 @@ -154,9 +154,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 @@ -82,9 +82,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