diff --git a/README.md b/README.md index a06899c8..21b70a22 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,7 @@ Options: --clean-output clean output folder before generate api. WARNING: May cause data loss (default: false) --api-class-name 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 ``` diff --git a/index.d.ts b/index.d.ts index f71d5cf1..c5f78241 100644 --- a/index.d.ts +++ b/index.d.ts @@ -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 */ @@ -350,6 +356,7 @@ export interface GenerateApiConfiguration { }; routeNameDuplicatesMap: Map; apiClassName: string; + queryParamsWithBrackets: boolean; }; modelTypes: ModelType[]; rawModelTypes: SchemaComponent[]; diff --git a/index.js b/index.js index 6aa2688f..83863704 100755 --- a/index.js +++ b/index.js @@ -68,6 +68,10 @@ const options = program .option("--api-class-name ", "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(); @@ -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) => { diff --git a/package.json b/package.json index 3ea38bd7..67f8097c 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/config.js b/src/config.js index 9b728116..9b4b6498 100644 --- a/src/config.js +++ b/src/config.js @@ -90,6 +90,7 @@ const config = { typePrefix: "", typeSuffix: "", patch: false, + queryParamsWithBrackets: false, componentTypeNameResolver: new NameResolver([]), /** name of the main exported class */ apiClassName: "Api", diff --git a/src/index.js b/src/index.js index c7a17166..78cc4efd 100644 --- a/src/index.js +++ b/src/index.js @@ -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, @@ -101,6 +102,7 @@ module.exports = { typePrefix, typeSuffix, patch, + queryParamsWithBrackets, apiClassName, debug, }); diff --git a/templates/base/http-clients/fetch-http-client.ejs b/templates/base/http-clients/fetch-http-client.ejs index 224253cc..50ddd22a 100644 --- a/templates/base/http-clients/fetch-http-client.ejs +++ b/templates/base/http-clients/fetch-http-client.ejs @@ -69,9 +69,9 @@ export class HttpClient { 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) { @@ -80,7 +80,11 @@ export class HttpClient { 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 { diff --git a/tests/generated/v2.0/adafruit.ts b/tests/generated/v2.0/adafruit.ts index a0f312f9..31937d48 100644 --- a/tests/generated/v2.0/adafruit.ts +++ b/tests/generated/v2.0/adafruit.ts @@ -229,9 +229,11 @@ export class HttpClient { 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) { diff --git a/tests/generated/v2.0/another-example.ts b/tests/generated/v2.0/another-example.ts index e2de7324..1242b16d 100644 --- a/tests/generated/v2.0/another-example.ts +++ b/tests/generated/v2.0/another-example.ts @@ -203,9 +203,11 @@ export class HttpClient { 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) { diff --git a/tests/generated/v2.0/another-schema.ts b/tests/generated/v2.0/another-schema.ts index b16c5bb3..640c0db0 100644 --- a/tests/generated/v2.0/another-schema.ts +++ b/tests/generated/v2.0/another-schema.ts @@ -96,9 +96,11 @@ export class HttpClient { 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) { diff --git a/tests/generated/v2.0/api-with-examples.ts b/tests/generated/v2.0/api-with-examples.ts index 1e9d793b..2527e909 100644 --- a/tests/generated/v2.0/api-with-examples.ts +++ b/tests/generated/v2.0/api-with-examples.ts @@ -75,9 +75,11 @@ export class HttpClient { 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) { diff --git a/tests/generated/v2.0/authentiq.ts b/tests/generated/v2.0/authentiq.ts index cd0bbf3a..220c9882 100644 --- a/tests/generated/v2.0/authentiq.ts +++ b/tests/generated/v2.0/authentiq.ts @@ -121,9 +121,11 @@ export class HttpClient { 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) { diff --git a/tests/generated/v2.0/enums.ts b/tests/generated/v2.0/enums.ts index cf52f296..50e627a1 100644 --- a/tests/generated/v2.0/enums.ts +++ b/tests/generated/v2.0/enums.ts @@ -140,9 +140,11 @@ export class HttpClient { 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) { diff --git a/tests/generated/v2.0/example1.ts b/tests/generated/v2.0/example1.ts index 70071d08..0aff5005 100644 --- a/tests/generated/v2.0/example1.ts +++ b/tests/generated/v2.0/example1.ts @@ -118,9 +118,11 @@ export class HttpClient { 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) { diff --git a/tests/generated/v2.0/file-formdata-example.ts b/tests/generated/v2.0/file-formdata-example.ts index 663b158d..9fb7b993 100644 --- a/tests/generated/v2.0/file-formdata-example.ts +++ b/tests/generated/v2.0/file-formdata-example.ts @@ -75,9 +75,11 @@ export class HttpClient { 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) { diff --git a/tests/generated/v2.0/furkot-example.ts b/tests/generated/v2.0/furkot-example.ts index 2e480acb..ca2873e1 100644 --- a/tests/generated/v2.0/furkot-example.ts +++ b/tests/generated/v2.0/furkot-example.ts @@ -148,9 +148,11 @@ export class HttpClient { 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) { diff --git a/tests/generated/v2.0/giphy.ts b/tests/generated/v2.0/giphy.ts index aa621c65..5fbbddff 100644 --- a/tests/generated/v2.0/giphy.ts +++ b/tests/generated/v2.0/giphy.ts @@ -341,9 +341,11 @@ export class HttpClient { 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) { diff --git a/tests/generated/v2.0/github-swagger.ts b/tests/generated/v2.0/github-swagger.ts index 4c3f8f48..cc0e482e 100644 --- a/tests/generated/v2.0/github-swagger.ts +++ b/tests/generated/v2.0/github-swagger.ts @@ -1971,9 +1971,11 @@ export class HttpClient { 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) { diff --git a/tests/generated/v2.0/path-args.ts b/tests/generated/v2.0/path-args.ts index fe451be7..31a1bed6 100644 --- a/tests/generated/v2.0/path-args.ts +++ b/tests/generated/v2.0/path-args.ts @@ -75,9 +75,11 @@ export class HttpClient { 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) { diff --git a/tests/generated/v2.0/petstore-expanded.ts b/tests/generated/v2.0/petstore-expanded.ts index c5bbfd5d..82e376ef 100644 --- a/tests/generated/v2.0/petstore-expanded.ts +++ b/tests/generated/v2.0/petstore-expanded.ts @@ -113,9 +113,11 @@ export class HttpClient { 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) { diff --git a/tests/generated/v2.0/petstore-minimal.ts b/tests/generated/v2.0/petstore-minimal.ts index 493f757e..3d36a59d 100644 --- a/tests/generated/v2.0/petstore-minimal.ts +++ b/tests/generated/v2.0/petstore-minimal.ts @@ -83,9 +83,11 @@ export class HttpClient { 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) { diff --git a/tests/generated/v2.0/petstore-simple.ts b/tests/generated/v2.0/petstore-simple.ts index 88626f5f..e45519ff 100644 --- a/tests/generated/v2.0/petstore-simple.ts +++ b/tests/generated/v2.0/petstore-simple.ts @@ -101,9 +101,11 @@ export class HttpClient { 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) { diff --git a/tests/generated/v2.0/petstore-swagger-io.ts b/tests/generated/v2.0/petstore-swagger-io.ts index 916cde57..8cffaefc 100644 --- a/tests/generated/v2.0/petstore-swagger-io.ts +++ b/tests/generated/v2.0/petstore-swagger-io.ts @@ -136,9 +136,11 @@ export class HttpClient { 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) { diff --git a/tests/generated/v2.0/petstore-with-external-docs.ts b/tests/generated/v2.0/petstore-with-external-docs.ts index f018a8b9..b336f01f 100644 --- a/tests/generated/v2.0/petstore-with-external-docs.ts +++ b/tests/generated/v2.0/petstore-with-external-docs.ts @@ -91,9 +91,11 @@ export class HttpClient { 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) { diff --git a/tests/generated/v2.0/petstore.ts b/tests/generated/v2.0/petstore.ts index f8fb903d..0e06e925 100644 --- a/tests/generated/v2.0/petstore.ts +++ b/tests/generated/v2.0/petstore.ts @@ -90,9 +90,11 @@ export class HttpClient { 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) { diff --git a/tests/generated/v2.0/query-path-param.ts b/tests/generated/v2.0/query-path-param.ts index 78d2dc47..00366fb3 100644 --- a/tests/generated/v2.0/query-path-param.ts +++ b/tests/generated/v2.0/query-path-param.ts @@ -75,9 +75,11 @@ export class HttpClient { 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) { diff --git a/tests/generated/v2.0/uber.ts b/tests/generated/v2.0/uber.ts index bf89468d..759c7bed 100644 --- a/tests/generated/v2.0/uber.ts +++ b/tests/generated/v2.0/uber.ts @@ -154,9 +154,11 @@ export class HttpClient { 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) { diff --git a/tests/generated/v3.0/additional-properties.ts b/tests/generated/v3.0/additional-properties.ts index 9663dab9..018f6017 100644 --- a/tests/generated/v3.0/additional-properties.ts +++ b/tests/generated/v3.0/additional-properties.ts @@ -82,9 +82,11 @@ export class HttpClient { 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) { diff --git a/tests/generated/v3.0/additional-properties2.ts b/tests/generated/v3.0/additional-properties2.ts index 9149e492..eb995eef 100644 --- a/tests/generated/v3.0/additional-properties2.ts +++ b/tests/generated/v3.0/additional-properties2.ts @@ -79,9 +79,11 @@ export class HttpClient { 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) { diff --git a/tests/generated/v3.0/allof-example.ts b/tests/generated/v3.0/allof-example.ts index ebb09b04..1d6f855a 100644 --- a/tests/generated/v3.0/allof-example.ts +++ b/tests/generated/v3.0/allof-example.ts @@ -89,9 +89,11 @@ export class HttpClient { 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) { diff --git a/tests/generated/v3.0/anyof-example.ts b/tests/generated/v3.0/anyof-example.ts index 26ee4f7a..6382ed06 100644 --- a/tests/generated/v3.0/anyof-example.ts +++ b/tests/generated/v3.0/anyof-example.ts @@ -85,9 +85,11 @@ export class HttpClient { 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) { diff --git a/tests/generated/v3.0/api-with-examples.ts b/tests/generated/v3.0/api-with-examples.ts index 2f048e56..193a27e8 100644 --- a/tests/generated/v3.0/api-with-examples.ts +++ b/tests/generated/v3.0/api-with-examples.ts @@ -75,9 +75,11 @@ export class HttpClient { 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) { diff --git a/tests/generated/v3.0/callback-example.ts b/tests/generated/v3.0/callback-example.ts index 51a64d3a..861bab03 100644 --- a/tests/generated/v3.0/callback-example.ts +++ b/tests/generated/v3.0/callback-example.ts @@ -75,9 +75,11 @@ export class HttpClient { 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) { diff --git a/tests/generated/v3.0/components-responses.ts b/tests/generated/v3.0/components-responses.ts index 063850e1..172703a4 100644 --- a/tests/generated/v3.0/components-responses.ts +++ b/tests/generated/v3.0/components-responses.ts @@ -75,9 +75,11 @@ export class HttpClient { 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) { diff --git a/tests/generated/v3.0/explode-param-3.0.1.ts b/tests/generated/v3.0/explode-param-3.0.1.ts index b7be6ac0..5d47d617 100644 --- a/tests/generated/v3.0/explode-param-3.0.1.ts +++ b/tests/generated/v3.0/explode-param-3.0.1.ts @@ -94,9 +94,11 @@ export class HttpClient { 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) { diff --git a/tests/generated/v3.0/full-swagger-scheme.ts b/tests/generated/v3.0/full-swagger-scheme.ts index 9d34a118..856f4fb7 100644 --- a/tests/generated/v3.0/full-swagger-scheme.ts +++ b/tests/generated/v3.0/full-swagger-scheme.ts @@ -8661,9 +8661,11 @@ export class HttpClient { 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) { diff --git a/tests/generated/v3.0/link-example.ts b/tests/generated/v3.0/link-example.ts index 5c8bdf38..370f312c 100644 --- a/tests/generated/v3.0/link-example.ts +++ b/tests/generated/v3.0/link-example.ts @@ -92,9 +92,11 @@ export class HttpClient { 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) { diff --git a/tests/generated/v3.0/no-definitions-schema.ts b/tests/generated/v3.0/no-definitions-schema.ts index b72c6ac6..1d4da799 100644 --- a/tests/generated/v3.0/no-definitions-schema.ts +++ b/tests/generated/v3.0/no-definitions-schema.ts @@ -89,9 +89,11 @@ export class HttpClient { 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) { diff --git a/tests/generated/v3.0/nullable-refs.ts b/tests/generated/v3.0/nullable-refs.ts index a29c0f41..c22b02a6 100644 --- a/tests/generated/v3.0/nullable-refs.ts +++ b/tests/generated/v3.0/nullable-refs.ts @@ -89,9 +89,11 @@ export class HttpClient { 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) { diff --git a/tests/generated/v3.0/oneof-example.ts b/tests/generated/v3.0/oneof-example.ts index 99bcef0d..49481a8d 100644 --- a/tests/generated/v3.0/oneof-example.ts +++ b/tests/generated/v3.0/oneof-example.ts @@ -85,9 +85,11 @@ export class HttpClient { 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) { diff --git a/tests/generated/v3.0/personal-api-example.ts b/tests/generated/v3.0/personal-api-example.ts index db3d41fa..6e58089b 100644 --- a/tests/generated/v3.0/personal-api-example.ts +++ b/tests/generated/v3.0/personal-api-example.ts @@ -276,9 +276,11 @@ export class HttpClient { 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) { diff --git a/tests/generated/v3.0/petstore-expanded.ts b/tests/generated/v3.0/petstore-expanded.ts index e4ca6738..b26bc4d2 100644 --- a/tests/generated/v3.0/petstore-expanded.ts +++ b/tests/generated/v3.0/petstore-expanded.ts @@ -91,9 +91,11 @@ export class HttpClient { 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) { diff --git a/tests/generated/v3.0/petstore.ts b/tests/generated/v3.0/petstore.ts index 0df4cc75..c10fe413 100644 --- a/tests/generated/v3.0/petstore.ts +++ b/tests/generated/v3.0/petstore.ts @@ -92,9 +92,11 @@ export class HttpClient { 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) { diff --git a/tests/generated/v3.0/recursive-schema.ts b/tests/generated/v3.0/recursive-schema.ts index 68d5b17e..5fd100da 100644 --- a/tests/generated/v3.0/recursive-schema.ts +++ b/tests/generated/v3.0/recursive-schema.ts @@ -88,9 +88,11 @@ export class HttpClient { 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) { diff --git a/tests/generated/v3.0/responses.ts b/tests/generated/v3.0/responses.ts index fe1185d9..13f8a1a7 100644 --- a/tests/generated/v3.0/responses.ts +++ b/tests/generated/v3.0/responses.ts @@ -75,9 +75,11 @@ export class HttpClient { 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) { diff --git a/tests/generated/v3.0/swaggerhub-template.ts b/tests/generated/v3.0/swaggerhub-template.ts index e4b1f3db..14716cd1 100644 --- a/tests/generated/v3.0/swaggerhub-template.ts +++ b/tests/generated/v3.0/swaggerhub-template.ts @@ -75,9 +75,11 @@ export class HttpClient { 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) { diff --git a/tests/generated/v3.0/tsoa-odd-types-3.0.2.ts b/tests/generated/v3.0/tsoa-odd-types-3.0.2.ts index 71f008d0..27c07fd4 100644 --- a/tests/generated/v3.0/tsoa-odd-types-3.0.2.ts +++ b/tests/generated/v3.0/tsoa-odd-types-3.0.2.ts @@ -192,9 +192,11 @@ export class HttpClient { 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) { diff --git a/tests/generated/v3.0/up-banking.ts b/tests/generated/v3.0/up-banking.ts index d2afc22a..6936e700 100644 --- a/tests/generated/v3.0/up-banking.ts +++ b/tests/generated/v3.0/up-banking.ts @@ -1075,9 +1075,11 @@ export class HttpClient { 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) { diff --git a/tests/generated/v3.0/uspto.ts b/tests/generated/v3.0/uspto.ts index 64f85ab6..c9c37b35 100644 --- a/tests/generated/v3.0/uspto.ts +++ b/tests/generated/v3.0/uspto.ts @@ -107,9 +107,11 @@ export class HttpClient { 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) { diff --git a/tests/generated/v3.0/wrong-enum-subtypes.ts b/tests/generated/v3.0/wrong-enum-subtypes.ts index f5d5b610..be400aa8 100644 --- a/tests/generated/v3.0/wrong-enum-subtypes.ts +++ b/tests/generated/v3.0/wrong-enum-subtypes.ts @@ -81,9 +81,11 @@ export class HttpClient { 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) { diff --git a/tests/generated/v3.0/wrong-schema-names.ts b/tests/generated/v3.0/wrong-schema-names.ts index 9167d97d..7c8ab281 100644 --- a/tests/generated/v3.0/wrong-schema-names.ts +++ b/tests/generated/v3.0/wrong-schema-names.ts @@ -99,9 +99,11 @@ export class HttpClient { 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) { diff --git a/tests/spec/defaultAsSuccess/expected.ts b/tests/spec/defaultAsSuccess/expected.ts index 442069ed..26b10bd6 100644 --- a/tests/spec/defaultAsSuccess/expected.ts +++ b/tests/spec/defaultAsSuccess/expected.ts @@ -121,9 +121,11 @@ export class HttpClient { 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) { diff --git a/tests/spec/defaultAsSuccess/schema.ts b/tests/spec/defaultAsSuccess/schema.ts index 442069ed..26b10bd6 100644 --- a/tests/spec/defaultAsSuccess/schema.ts +++ b/tests/spec/defaultAsSuccess/schema.ts @@ -121,9 +121,11 @@ export class HttpClient { 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) { diff --git a/tests/spec/defaultResponse/expected.ts b/tests/spec/defaultResponse/expected.ts index 6becb046..4e9d3e75 100644 --- a/tests/spec/defaultResponse/expected.ts +++ b/tests/spec/defaultResponse/expected.ts @@ -75,9 +75,11 @@ export class HttpClient { 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) { diff --git a/tests/spec/defaultResponse/schema.ts b/tests/spec/defaultResponse/schema.ts index 6becb046..4e9d3e75 100644 --- a/tests/spec/defaultResponse/schema.ts +++ b/tests/spec/defaultResponse/schema.ts @@ -75,9 +75,11 @@ export class HttpClient { 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) { diff --git a/tests/spec/enumNamesAsValues/expected.ts b/tests/spec/enumNamesAsValues/expected.ts index ff29449f..b0892a67 100644 --- a/tests/spec/enumNamesAsValues/expected.ts +++ b/tests/spec/enumNamesAsValues/expected.ts @@ -273,9 +273,11 @@ export class HttpClient { 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) { diff --git a/tests/spec/enumNamesAsValues/schema.ts b/tests/spec/enumNamesAsValues/schema.ts index ff29449f..b0892a67 100644 --- a/tests/spec/enumNamesAsValues/schema.ts +++ b/tests/spec/enumNamesAsValues/schema.ts @@ -273,9 +273,11 @@ export class HttpClient { 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) { diff --git a/tests/spec/extractRequestBody/expected.ts b/tests/spec/extractRequestBody/expected.ts index ea84fb9b..00d0713c 100644 --- a/tests/spec/extractRequestBody/expected.ts +++ b/tests/spec/extractRequestBody/expected.ts @@ -223,9 +223,11 @@ export class HttpClient { 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) { diff --git a/tests/spec/extractRequestBody/schema.ts b/tests/spec/extractRequestBody/schema.ts index ea84fb9b..00d0713c 100644 --- a/tests/spec/extractRequestBody/schema.ts +++ b/tests/spec/extractRequestBody/schema.ts @@ -223,9 +223,11 @@ export class HttpClient { 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) { diff --git a/tests/spec/extractRequestParams/expected.ts b/tests/spec/extractRequestParams/expected.ts index 73910194..3ccfb18f 100644 --- a/tests/spec/extractRequestParams/expected.ts +++ b/tests/spec/extractRequestParams/expected.ts @@ -156,9 +156,11 @@ export class HttpClient { 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) { diff --git a/tests/spec/extractRequestParams/schema.ts b/tests/spec/extractRequestParams/schema.ts index 73910194..3ccfb18f 100644 --- a/tests/spec/extractRequestParams/schema.ts +++ b/tests/spec/extractRequestParams/schema.ts @@ -156,9 +156,11 @@ export class HttpClient { 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) { diff --git a/tests/spec/extractResponseBody/expected.ts b/tests/spec/extractResponseBody/expected.ts index 42d962c5..c1104c72 100644 --- a/tests/spec/extractResponseBody/expected.ts +++ b/tests/spec/extractResponseBody/expected.ts @@ -225,9 +225,11 @@ export class HttpClient { 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) { diff --git a/tests/spec/extractResponseBody/schema.ts b/tests/spec/extractResponseBody/schema.ts index 42d962c5..c1104c72 100644 --- a/tests/spec/extractResponseBody/schema.ts +++ b/tests/spec/extractResponseBody/schema.ts @@ -225,9 +225,11 @@ export class HttpClient { 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) { diff --git a/tests/spec/extractResponseError/expected.ts b/tests/spec/extractResponseError/expected.ts index 8e4e02be..fad5aa88 100644 --- a/tests/spec/extractResponseError/expected.ts +++ b/tests/spec/extractResponseError/expected.ts @@ -220,9 +220,11 @@ export class HttpClient { 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) { diff --git a/tests/spec/extractResponseError/schema.ts b/tests/spec/extractResponseError/schema.ts index 8e4e02be..fad5aa88 100644 --- a/tests/spec/extractResponseError/schema.ts +++ b/tests/spec/extractResponseError/schema.ts @@ -220,9 +220,11 @@ export class HttpClient { 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) { diff --git a/tests/spec/js/schema.d.ts b/tests/spec/js/schema.d.ts index 80ec2342..b005dd04 100644 --- a/tests/spec/js/schema.d.ts +++ b/tests/spec/js/schema.d.ts @@ -1825,7 +1825,7 @@ export declare class HttpClient { private baseApiParams; constructor(apiConfig?: ApiConfig); setSecurityData: (data: SecurityDataType | null) => void; - protected encodeQueryParam(key: string, value: any): string; + protected encodeQueryParam(key: string, value: any, withBrackets?: boolean): string; protected addQueryParam(query: QueryParamsType, key: string): string; protected addArrayQueryParam(query: QueryParamsType, key: string): any; protected toQueryString(rawQuery?: QueryParamsType): string; diff --git a/tests/spec/js/schema.js b/tests/spec/js/schema.js index d86f3017..ff7a1f73 100644 --- a/tests/spec/js/schema.js +++ b/tests/spec/js/schema.js @@ -33,9 +33,11 @@ export class HttpClient { setSecurityData = (data) => { this.securityData = data; }; - encodeQueryParam(key, value) { + encodeQueryParam(key, value, withBrackets = false) { const encodedKey = encodeURIComponent(key); - return `${encodedKey}=${encodeURIComponent(typeof value === "number" ? value : `${value}`)}`; + return `${encodedKey}${withBrackets ? "[]" : ""}=${encodeURIComponent( + typeof value === "number" ? value : `${value}`, + )}`; } addQueryParam(query, key) { return this.encodeQueryParam(key, query[key]); diff --git a/tests/spec/modular/expected/http-client.ts b/tests/spec/modular/expected/http-client.ts index f0f5ed83..3428c1c0 100644 --- a/tests/spec/modular/expected/http-client.ts +++ b/tests/spec/modular/expected/http-client.ts @@ -75,9 +75,11 @@ export class HttpClient { 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) { diff --git a/tests/spec/modular/generated/http-client.ts b/tests/spec/modular/generated/http-client.ts index f0f5ed83..3428c1c0 100644 --- a/tests/spec/modular/generated/http-client.ts +++ b/tests/spec/modular/generated/http-client.ts @@ -75,9 +75,11 @@ export class HttpClient { 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) { diff --git a/tests/spec/moduleNameFirstTag/expected.ts b/tests/spec/moduleNameFirstTag/expected.ts index 08000b74..3fc36a7a 100644 --- a/tests/spec/moduleNameFirstTag/expected.ts +++ b/tests/spec/moduleNameFirstTag/expected.ts @@ -200,9 +200,11 @@ export class HttpClient { 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) { diff --git a/tests/spec/moduleNameFirstTag/schema.ts b/tests/spec/moduleNameFirstTag/schema.ts index 08000b74..3fc36a7a 100644 --- a/tests/spec/moduleNameFirstTag/schema.ts +++ b/tests/spec/moduleNameFirstTag/schema.ts @@ -200,9 +200,11 @@ export class HttpClient { 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) { diff --git a/tests/spec/moduleNameIndex/expected.ts b/tests/spec/moduleNameIndex/expected.ts index cdee6ae9..1f506789 100644 --- a/tests/spec/moduleNameIndex/expected.ts +++ b/tests/spec/moduleNameIndex/expected.ts @@ -200,9 +200,11 @@ export class HttpClient { 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) { diff --git a/tests/spec/moduleNameIndex/schema.ts b/tests/spec/moduleNameIndex/schema.ts index cdee6ae9..1f506789 100644 --- a/tests/spec/moduleNameIndex/schema.ts +++ b/tests/spec/moduleNameIndex/schema.ts @@ -200,9 +200,11 @@ export class HttpClient { 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) { diff --git a/tests/spec/partialBaseTemplate/expected.ts b/tests/spec/partialBaseTemplate/expected.ts index 9338e036..74f9d9ca 100644 --- a/tests/spec/partialBaseTemplate/expected.ts +++ b/tests/spec/partialBaseTemplate/expected.ts @@ -87,9 +87,11 @@ export class HttpClient { 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) { diff --git a/tests/spec/partialBaseTemplate/schema.ts b/tests/spec/partialBaseTemplate/schema.ts index 9338e036..74f9d9ca 100644 --- a/tests/spec/partialBaseTemplate/schema.ts +++ b/tests/spec/partialBaseTemplate/schema.ts @@ -87,9 +87,11 @@ export class HttpClient { 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) { diff --git a/tests/spec/partialDefaultTemplate/expected.ts b/tests/spec/partialDefaultTemplate/expected.ts index c9c76784..74aa8ca4 100644 --- a/tests/spec/partialDefaultTemplate/expected.ts +++ b/tests/spec/partialDefaultTemplate/expected.ts @@ -83,9 +83,11 @@ export class HttpClient { 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) { diff --git a/tests/spec/partialDefaultTemplate/schema.ts b/tests/spec/partialDefaultTemplate/schema.ts index c9c76784..74aa8ca4 100644 --- a/tests/spec/partialDefaultTemplate/schema.ts +++ b/tests/spec/partialDefaultTemplate/schema.ts @@ -83,9 +83,11 @@ export class HttpClient { 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) { diff --git a/tests/spec/patch/expected.ts b/tests/spec/patch/expected.ts index 4d303ba1..ce90e97e 100644 --- a/tests/spec/patch/expected.ts +++ b/tests/spec/patch/expected.ts @@ -1971,9 +1971,11 @@ export class HttpClient { 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) { diff --git a/tests/spec/patch/schema.ts b/tests/spec/patch/schema.ts index 4d303ba1..ce90e97e 100644 --- a/tests/spec/patch/schema.ts +++ b/tests/spec/patch/schema.ts @@ -1971,9 +1971,11 @@ export class HttpClient { 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) { diff --git a/tests/spec/queryParamsWithBrackets/expected.ts b/tests/spec/queryParamsWithBrackets/expected.ts new file mode 100644 index 00000000..2a6b4f11 --- /dev/null +++ b/tests/spec/queryParamsWithBrackets/expected.ts @@ -0,0 +1,259 @@ +/* eslint-disable */ +/* tslint:disable */ +/* + * --------------------------------------------------------------- + * ## THIS FILE WAS GENERATED VIA SWAGGER-TYPESCRIPT-API ## + * ## ## + * ## AUTHOR: acacode ## + * ## SOURCE: https://github.com/acacode/swagger-typescript-api ## + * --------------------------------------------------------------- + */ + +export type QueryParamsType = Record; +export type ResponseFormat = keyof Omit; + +export interface FullRequestParams extends Omit { + /** set parameter to `true` for call `securityWorker` for this request */ + secure?: boolean; + /** request path */ + path: string; + /** content type of request body */ + type?: ContentType; + /** query params */ + query?: QueryParamsType; + /** format of response (i.e. response.json() -> format: "json") */ + format?: ResponseFormat; + /** request body */ + body?: unknown; + /** base url */ + baseUrl?: string; + /** request cancellation token */ + cancelToken?: CancelToken; +} + +export type RequestParams = Omit; + +export interface ApiConfig { + baseUrl?: string; + baseApiParams?: Omit; + securityWorker?: (securityData: SecurityDataType | null) => Promise | RequestParams | void; + customFetch?: typeof fetch; +} + +export interface HttpResponse extends Response { + data: D; + error: E; +} + +type CancelToken = Symbol | string | number; + +export enum ContentType { + Json = "application/json", + FormData = "multipart/form-data", + UrlEncoded = "application/x-www-form-urlencoded", +} + +export class HttpClient { + public baseUrl: string = "http://petstore.swagger.io/api"; + private securityData: SecurityDataType | null = null; + private securityWorker?: ApiConfig["securityWorker"]; + private abortControllers = new Map(); + private customFetch = (...fetchParams: Parameters) => fetch(...fetchParams); + + private baseApiParams: RequestParams = { + credentials: "same-origin", + headers: {}, + redirect: "follow", + referrerPolicy: "no-referrer", + }; + + constructor(apiConfig: ApiConfig = {}) { + Object.assign(this, apiConfig); + } + + public setSecurityData = (data: SecurityDataType | null) => { + this.securityData = data; + }; + + protected encodeQueryParam(key: string, value: any, withBrackets: boolean = false) { + const encodedKey = encodeURIComponent(key); + return `${encodedKey}${withBrackets ? "[]" : ""}=${encodeURIComponent( + typeof value === "number" ? value : `${value}`, + )}`; + } + + protected addQueryParam(query: QueryParamsType, key: string) { + return this.encodeQueryParam(key, query[key]); + } + + protected addArrayQueryParam(query: QueryParamsType, key: string) { + const value = query[key]; + return value.map((v: any) => this.encodeQueryParam(key, v, true)).join("&"); + } + + protected toQueryString(rawQuery?: QueryParamsType): string { + const query = rawQuery || {}; + const keys = Object.keys(query).filter((key) => "undefined" !== typeof query[key]); + return keys + .map((key) => (Array.isArray(query[key]) ? this.addArrayQueryParam(query, key) : this.addQueryParam(query, key))) + .join("&"); + } + + protected addQueryParams(rawQuery?: QueryParamsType): string { + const queryString = this.toQueryString(rawQuery); + return queryString ? `?${queryString}` : ""; + } + + private contentFormatters: Record any> = { + [ContentType.Json]: (input: any) => + input !== null && (typeof input === "object" || typeof input === "string") ? JSON.stringify(input) : input, + [ContentType.FormData]: (input: any) => + Object.keys(input || {}).reduce((formData, key) => { + const property = input[key]; + formData.append( + key, + property instanceof Blob + ? property + : typeof property === "object" && property !== null + ? JSON.stringify(property) + : `${property}`, + ); + return formData; + }, new FormData()), + [ContentType.UrlEncoded]: (input: any) => this.toQueryString(input), + }; + + protected mergeRequestParams(params1: RequestParams, params2?: RequestParams): RequestParams { + return { + ...this.baseApiParams, + ...params1, + ...(params2 || {}), + headers: { + ...(this.baseApiParams.headers || {}), + ...(params1.headers || {}), + ...((params2 && params2.headers) || {}), + }, + }; + } + + protected createAbortSignal = (cancelToken: CancelToken): AbortSignal | undefined => { + if (this.abortControllers.has(cancelToken)) { + const abortController = this.abortControllers.get(cancelToken); + if (abortController) { + return abortController.signal; + } + return void 0; + } + + const abortController = new AbortController(); + this.abortControllers.set(cancelToken, abortController); + return abortController.signal; + }; + + public abortRequest = (cancelToken: CancelToken) => { + const abortController = this.abortControllers.get(cancelToken); + + if (abortController) { + abortController.abort(); + this.abortControllers.delete(cancelToken); + } + }; + + public request = async ({ + body, + secure, + path, + type, + query, + format, + baseUrl, + cancelToken, + ...params + }: FullRequestParams): Promise> => { + const secureParams = + ((typeof secure === "boolean" ? secure : this.baseApiParams.secure) && + this.securityWorker && + (await this.securityWorker(this.securityData))) || + {}; + const requestParams = this.mergeRequestParams(params, secureParams); + const queryString = query && this.toQueryString(query); + const payloadFormatter = this.contentFormatters[type || ContentType.Json]; + const responseFormat = format || requestParams.format; + + return this.customFetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { + ...requestParams, + headers: { + ...(requestParams.headers || {}), + ...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}), + }, + signal: cancelToken ? this.createAbortSignal(cancelToken) : requestParams.signal, + body: typeof body === "undefined" || body === null ? null : payloadFormatter(body), + }).then(async (response) => { + const r = response as HttpResponse; + r.data = null as unknown as T; + r.error = null as unknown as E; + + const data = !responseFormat + ? r + : await response[responseFormat]() + .then((data) => { + if (r.ok) { + r.data = data; + } else { + r.error = data; + } + return r; + }) + .catch((e) => { + r.error = e; + return r; + }); + + if (cancelToken) { + this.abortControllers.delete(cancelToken); + } + + if (!response.ok) throw data; + return data; + }); + }; +} + +/** + * @title Swagger Petstore + * @version 1.0.0 + * @license Apache 2.0 (https://www.apache.org/licenses/LICENSE-2.0.html) + * @termsOfService http://swagger.io/terms/ + * @baseUrl http://petstore.swagger.io/api + * @contact Swagger API Team (http://swagger.io) + * + * A sample API that uses a petstore as an example to demonstrate features in the swagger-2.0 specification + */ +export class Api extends HttpClient { + pets = { + /** + * @description Returns all pets from the system that the user has access to Nam sed condimentum est. Maecenas tempor sagittis sapien, nec rhoncus sem sagittis sit amet. Aenean at gravida augue, ac iaculis sem. Curabitur odio lorem, ornare eget elementum nec, cursus id lectus. Duis mi turpis, pulvinar ac eros ac, tincidunt varius justo. In hac habitasse platea dictumst. Integer at adipiscing ante, a sagittis ligula. Aenean pharetra tempor ante molestie imperdiet. Vivamus id aliquam diam. Cras quis velit non tortor eleifend sagittis. Praesent at enim pharetra urna volutpat venenatis eget eget mauris. In eleifend fermentum facilisis. Praesent enim enim, gravida ac sodales sed, placerat id erat. Suspendisse lacus dolor, consectetur non augue vel, vehicula interdum libero. Morbi euismod sagittis libero sed lacinia. Sed tempus felis lobortis leo pulvinar rutrum. Nam mattis velit nisl, eu condimentum ligula luctus nec. Phasellus semper velit eget aliquet faucibus. In a mattis elit. Phasellus vel urna viverra, condimentum lorem id, rhoncus nibh. Ut pellentesque posuere elementum. Sed a varius odio. Morbi rhoncus ligula libero, vel eleifend nunc tristique vitae. Fusce et sem dui. Aenean nec scelerisque tortor. Fusce malesuada accumsan magna vel tempus. Quisque mollis felis eu dolor tristique, sit amet auctor felis gravida. Sed libero lorem, molestie sed nisl in, accumsan tempor nisi. Fusce sollicitudin massa ut lacinia mattis. Sed vel eleifend lorem. Pellentesque vitae felis pretium, pulvinar elit eu, euismod sapien. + * + * @name FindPets + * @request GET:/pets + */ + findPets: ( + query?: { + /** tags to filter by */ + tags?: string[]; + /** + * maximum number of results to return + * @format int32 + */ + limit?: number; + }, + params: RequestParams = {}, + ) => + this.request({ + path: `/pets`, + method: "GET", + query: query, + ...params, + }), + }; +} diff --git a/tests/spec/queryParamsWithBrackets/schema.json b/tests/spec/queryParamsWithBrackets/schema.json new file mode 100644 index 00000000..7629acb1 --- /dev/null +++ b/tests/spec/queryParamsWithBrackets/schema.json @@ -0,0 +1,57 @@ +{ + "swagger": "2.0", + "info": { + "version": "1.0.0", + "title": "Swagger Petstore", + "description": "A sample API that uses a petstore as an example to demonstrate features in the swagger-2.0 specification", + "termsOfService": "http://swagger.io/terms/", + "contact": { + "name": "Swagger API Team", + "email": "apiteam@swagger.io", + "url": "http://swagger.io" + }, + "license": { + "name": "Apache 2.0", + "url": "https://www.apache.org/licenses/LICENSE-2.0.html" + } + }, + "host": "petstore.swagger.io", + "basePath": "/api", + "schemes": ["http"], + "consumes": ["application/json"], + "produces": ["application/json"], + "paths": { + "/pets": { + "get": { + "description": "Returns all pets from the system that the user has access to\nNam sed condimentum est. Maecenas tempor sagittis sapien, nec rhoncus sem sagittis sit amet. Aenean at gravida augue, ac iaculis sem. Curabitur odio lorem, ornare eget elementum nec, cursus id lectus. Duis mi turpis, pulvinar ac eros ac, tincidunt varius justo. In hac habitasse platea dictumst. Integer at adipiscing ante, a sagittis ligula. Aenean pharetra tempor ante molestie imperdiet. Vivamus id aliquam diam. Cras quis velit non tortor eleifend sagittis. Praesent at enim pharetra urna volutpat venenatis eget eget mauris. In eleifend fermentum facilisis. Praesent enim enim, gravida ac sodales sed, placerat id erat. Suspendisse lacus dolor, consectetur non augue vel, vehicula interdum libero. Morbi euismod sagittis libero sed lacinia.\n\nSed tempus felis lobortis leo pulvinar rutrum. Nam mattis velit nisl, eu condimentum ligula luctus nec. Phasellus semper velit eget aliquet faucibus. In a mattis elit. Phasellus vel urna viverra, condimentum lorem id, rhoncus nibh. Ut pellentesque posuere elementum. Sed a varius odio. Morbi rhoncus ligula libero, vel eleifend nunc tristique vitae. Fusce et sem dui. Aenean nec scelerisque tortor. Fusce malesuada accumsan magna vel tempus. Quisque mollis felis eu dolor tristique, sit amet auctor felis gravida. Sed libero lorem, molestie sed nisl in, accumsan tempor nisi. Fusce sollicitudin massa ut lacinia mattis. Sed vel eleifend lorem. Pellentesque vitae felis pretium, pulvinar elit eu, euismod sapien.\n", + "operationId": "findPets", + "parameters": [ + { + "name": "tags", + "in": "query", + "description": "tags to filter by", + "required": false, + "type": "array", + "collectionFormat": "csv", + "items": { + "type": "string" + } + }, + { + "name": "limit", + "in": "query", + "description": "maximum number of results to return", + "required": false, + "type": "integer", + "format": "int32" + } + ], + "responses": { + "200": {}, + "default": {} + } + } + } + }, + "definitions": {} +} diff --git a/tests/spec/queryParamsWithBrackets/schema.ts b/tests/spec/queryParamsWithBrackets/schema.ts new file mode 100644 index 00000000..2a6b4f11 --- /dev/null +++ b/tests/spec/queryParamsWithBrackets/schema.ts @@ -0,0 +1,259 @@ +/* eslint-disable */ +/* tslint:disable */ +/* + * --------------------------------------------------------------- + * ## THIS FILE WAS GENERATED VIA SWAGGER-TYPESCRIPT-API ## + * ## ## + * ## AUTHOR: acacode ## + * ## SOURCE: https://github.com/acacode/swagger-typescript-api ## + * --------------------------------------------------------------- + */ + +export type QueryParamsType = Record; +export type ResponseFormat = keyof Omit; + +export interface FullRequestParams extends Omit { + /** set parameter to `true` for call `securityWorker` for this request */ + secure?: boolean; + /** request path */ + path: string; + /** content type of request body */ + type?: ContentType; + /** query params */ + query?: QueryParamsType; + /** format of response (i.e. response.json() -> format: "json") */ + format?: ResponseFormat; + /** request body */ + body?: unknown; + /** base url */ + baseUrl?: string; + /** request cancellation token */ + cancelToken?: CancelToken; +} + +export type RequestParams = Omit; + +export interface ApiConfig { + baseUrl?: string; + baseApiParams?: Omit; + securityWorker?: (securityData: SecurityDataType | null) => Promise | RequestParams | void; + customFetch?: typeof fetch; +} + +export interface HttpResponse extends Response { + data: D; + error: E; +} + +type CancelToken = Symbol | string | number; + +export enum ContentType { + Json = "application/json", + FormData = "multipart/form-data", + UrlEncoded = "application/x-www-form-urlencoded", +} + +export class HttpClient { + public baseUrl: string = "http://petstore.swagger.io/api"; + private securityData: SecurityDataType | null = null; + private securityWorker?: ApiConfig["securityWorker"]; + private abortControllers = new Map(); + private customFetch = (...fetchParams: Parameters) => fetch(...fetchParams); + + private baseApiParams: RequestParams = { + credentials: "same-origin", + headers: {}, + redirect: "follow", + referrerPolicy: "no-referrer", + }; + + constructor(apiConfig: ApiConfig = {}) { + Object.assign(this, apiConfig); + } + + public setSecurityData = (data: SecurityDataType | null) => { + this.securityData = data; + }; + + protected encodeQueryParam(key: string, value: any, withBrackets: boolean = false) { + const encodedKey = encodeURIComponent(key); + return `${encodedKey}${withBrackets ? "[]" : ""}=${encodeURIComponent( + typeof value === "number" ? value : `${value}`, + )}`; + } + + protected addQueryParam(query: QueryParamsType, key: string) { + return this.encodeQueryParam(key, query[key]); + } + + protected addArrayQueryParam(query: QueryParamsType, key: string) { + const value = query[key]; + return value.map((v: any) => this.encodeQueryParam(key, v, true)).join("&"); + } + + protected toQueryString(rawQuery?: QueryParamsType): string { + const query = rawQuery || {}; + const keys = Object.keys(query).filter((key) => "undefined" !== typeof query[key]); + return keys + .map((key) => (Array.isArray(query[key]) ? this.addArrayQueryParam(query, key) : this.addQueryParam(query, key))) + .join("&"); + } + + protected addQueryParams(rawQuery?: QueryParamsType): string { + const queryString = this.toQueryString(rawQuery); + return queryString ? `?${queryString}` : ""; + } + + private contentFormatters: Record any> = { + [ContentType.Json]: (input: any) => + input !== null && (typeof input === "object" || typeof input === "string") ? JSON.stringify(input) : input, + [ContentType.FormData]: (input: any) => + Object.keys(input || {}).reduce((formData, key) => { + const property = input[key]; + formData.append( + key, + property instanceof Blob + ? property + : typeof property === "object" && property !== null + ? JSON.stringify(property) + : `${property}`, + ); + return formData; + }, new FormData()), + [ContentType.UrlEncoded]: (input: any) => this.toQueryString(input), + }; + + protected mergeRequestParams(params1: RequestParams, params2?: RequestParams): RequestParams { + return { + ...this.baseApiParams, + ...params1, + ...(params2 || {}), + headers: { + ...(this.baseApiParams.headers || {}), + ...(params1.headers || {}), + ...((params2 && params2.headers) || {}), + }, + }; + } + + protected createAbortSignal = (cancelToken: CancelToken): AbortSignal | undefined => { + if (this.abortControllers.has(cancelToken)) { + const abortController = this.abortControllers.get(cancelToken); + if (abortController) { + return abortController.signal; + } + return void 0; + } + + const abortController = new AbortController(); + this.abortControllers.set(cancelToken, abortController); + return abortController.signal; + }; + + public abortRequest = (cancelToken: CancelToken) => { + const abortController = this.abortControllers.get(cancelToken); + + if (abortController) { + abortController.abort(); + this.abortControllers.delete(cancelToken); + } + }; + + public request = async ({ + body, + secure, + path, + type, + query, + format, + baseUrl, + cancelToken, + ...params + }: FullRequestParams): Promise> => { + const secureParams = + ((typeof secure === "boolean" ? secure : this.baseApiParams.secure) && + this.securityWorker && + (await this.securityWorker(this.securityData))) || + {}; + const requestParams = this.mergeRequestParams(params, secureParams); + const queryString = query && this.toQueryString(query); + const payloadFormatter = this.contentFormatters[type || ContentType.Json]; + const responseFormat = format || requestParams.format; + + return this.customFetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { + ...requestParams, + headers: { + ...(requestParams.headers || {}), + ...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}), + }, + signal: cancelToken ? this.createAbortSignal(cancelToken) : requestParams.signal, + body: typeof body === "undefined" || body === null ? null : payloadFormatter(body), + }).then(async (response) => { + const r = response as HttpResponse; + r.data = null as unknown as T; + r.error = null as unknown as E; + + const data = !responseFormat + ? r + : await response[responseFormat]() + .then((data) => { + if (r.ok) { + r.data = data; + } else { + r.error = data; + } + return r; + }) + .catch((e) => { + r.error = e; + return r; + }); + + if (cancelToken) { + this.abortControllers.delete(cancelToken); + } + + if (!response.ok) throw data; + return data; + }); + }; +} + +/** + * @title Swagger Petstore + * @version 1.0.0 + * @license Apache 2.0 (https://www.apache.org/licenses/LICENSE-2.0.html) + * @termsOfService http://swagger.io/terms/ + * @baseUrl http://petstore.swagger.io/api + * @contact Swagger API Team (http://swagger.io) + * + * A sample API that uses a petstore as an example to demonstrate features in the swagger-2.0 specification + */ +export class Api extends HttpClient { + pets = { + /** + * @description Returns all pets from the system that the user has access to Nam sed condimentum est. Maecenas tempor sagittis sapien, nec rhoncus sem sagittis sit amet. Aenean at gravida augue, ac iaculis sem. Curabitur odio lorem, ornare eget elementum nec, cursus id lectus. Duis mi turpis, pulvinar ac eros ac, tincidunt varius justo. In hac habitasse platea dictumst. Integer at adipiscing ante, a sagittis ligula. Aenean pharetra tempor ante molestie imperdiet. Vivamus id aliquam diam. Cras quis velit non tortor eleifend sagittis. Praesent at enim pharetra urna volutpat venenatis eget eget mauris. In eleifend fermentum facilisis. Praesent enim enim, gravida ac sodales sed, placerat id erat. Suspendisse lacus dolor, consectetur non augue vel, vehicula interdum libero. Morbi euismod sagittis libero sed lacinia. Sed tempus felis lobortis leo pulvinar rutrum. Nam mattis velit nisl, eu condimentum ligula luctus nec. Phasellus semper velit eget aliquet faucibus. In a mattis elit. Phasellus vel urna viverra, condimentum lorem id, rhoncus nibh. Ut pellentesque posuere elementum. Sed a varius odio. Morbi rhoncus ligula libero, vel eleifend nunc tristique vitae. Fusce et sem dui. Aenean nec scelerisque tortor. Fusce malesuada accumsan magna vel tempus. Quisque mollis felis eu dolor tristique, sit amet auctor felis gravida. Sed libero lorem, molestie sed nisl in, accumsan tempor nisi. Fusce sollicitudin massa ut lacinia mattis. Sed vel eleifend lorem. Pellentesque vitae felis pretium, pulvinar elit eu, euismod sapien. + * + * @name FindPets + * @request GET:/pets + */ + findPets: ( + query?: { + /** tags to filter by */ + tags?: string[]; + /** + * maximum number of results to return + * @format int32 + */ + limit?: number; + }, + params: RequestParams = {}, + ) => + this.request({ + path: `/pets`, + method: "GET", + query: query, + ...params, + }), + }; +} diff --git a/tests/spec/queryParamsWithBrackets/test.js b/tests/spec/queryParamsWithBrackets/test.js new file mode 100644 index 00000000..b3734c02 --- /dev/null +++ b/tests/spec/queryParamsWithBrackets/test.js @@ -0,0 +1,21 @@ +const { generateApiForTest } = require("../../helpers/generateApiForTest"); +const { resolve } = require("path"); +const validateGeneratedModule = require("../../helpers/validateGeneratedModule"); +const createSchemaInfos = require("../../helpers/createSchemaInfos"); +const assertGeneratedModule = require("../../helpers/assertGeneratedModule"); + +const schemas = createSchemaInfos({ absolutePathToSchemas: resolve(__dirname, "./") }); + +schemas.forEach(({ absolutePath, apiFileName }) => { + generateApiForTest({ + testName: "--query-params-with-brackets option test", + silent: true, + name: apiFileName, + spec: require(absolutePath), + output: resolve(__dirname, "./"), + queryParamsWithBrackets: true, + }).then(() => { + validateGeneratedModule(resolve(__dirname, `./${apiFileName}`)); + assertGeneratedModule(resolve(__dirname, `./${apiFileName}`), resolve(__dirname, `./expected.ts`)); + }); +}); diff --git a/tests/spec/responses/expected.ts b/tests/spec/responses/expected.ts index 46026c9b..ed63f046 100644 --- a/tests/spec/responses/expected.ts +++ b/tests/spec/responses/expected.ts @@ -121,9 +121,11 @@ export class HttpClient { 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) { diff --git a/tests/spec/responses/schema.json b/tests/spec/responses/schema.json index b6b9ce0c..c188b78d 100644 --- a/tests/spec/responses/schema.json +++ b/tests/spec/responses/schema.json @@ -874,4 +874,4 @@ ] } } -} \ No newline at end of file +} diff --git a/tests/spec/responses/schema.ts b/tests/spec/responses/schema.ts index 46026c9b..ed63f046 100644 --- a/tests/spec/responses/schema.ts +++ b/tests/spec/responses/schema.ts @@ -121,9 +121,11 @@ export class HttpClient { 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) { diff --git a/tests/spec/singleHttpClient/expected.ts b/tests/spec/singleHttpClient/expected.ts index 5ed79f80..8b079a26 100644 --- a/tests/spec/singleHttpClient/expected.ts +++ b/tests/spec/singleHttpClient/expected.ts @@ -75,9 +75,11 @@ export class HttpClient { 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) { diff --git a/tests/spec/singleHttpClient/schema.ts b/tests/spec/singleHttpClient/schema.ts index 5ed79f80..8b079a26 100644 --- a/tests/spec/singleHttpClient/schema.ts +++ b/tests/spec/singleHttpClient/schema.ts @@ -75,9 +75,11 @@ export class HttpClient { 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) { diff --git a/tests/spec/templates/expected.ts b/tests/spec/templates/expected.ts index ac7617fd..f160f49c 100644 --- a/tests/spec/templates/expected.ts +++ b/tests/spec/templates/expected.ts @@ -1973,9 +1973,11 @@ export class HttpClient { 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) { diff --git a/tests/spec/templates/schema.ts b/tests/spec/templates/schema.ts index ac7617fd..f160f49c 100644 --- a/tests/spec/templates/schema.ts +++ b/tests/spec/templates/schema.ts @@ -1973,9 +1973,11 @@ export class HttpClient { 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) { diff --git a/tests/spec/typeSuffixPrefix/expected.ts b/tests/spec/typeSuffixPrefix/expected.ts index c1ef6ca7..a848beae 100644 --- a/tests/spec/typeSuffixPrefix/expected.ts +++ b/tests/spec/typeSuffixPrefix/expected.ts @@ -1971,9 +1971,11 @@ export class HttpClient { 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) { diff --git a/tests/spec/typeSuffixPrefix/schema.ts b/tests/spec/typeSuffixPrefix/schema.ts index c1ef6ca7..a848beae 100644 --- a/tests/spec/typeSuffixPrefix/schema.ts +++ b/tests/spec/typeSuffixPrefix/schema.ts @@ -1971,9 +1971,11 @@ export class HttpClient { 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) { diff --git a/tests/spec/unionEnums/expected.ts b/tests/spec/unionEnums/expected.ts index 334e0277..0559df7b 100644 --- a/tests/spec/unionEnums/expected.ts +++ b/tests/spec/unionEnums/expected.ts @@ -87,9 +87,11 @@ export class HttpClient { 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) { diff --git a/tests/spec/unionEnums/schema.ts b/tests/spec/unionEnums/schema.ts index 334e0277..0559df7b 100644 --- a/tests/spec/unionEnums/schema.ts +++ b/tests/spec/unionEnums/schema.ts @@ -87,9 +87,11 @@ export class HttpClient { 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) {