diff --git a/CHANGELOG.md b/CHANGELOG.md index d1d678fc..0894d7b5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,9 +1,18 @@ # next release -# 4.3.0 +# 4.4.0 Fixes: - Client generation for `Content-Type: application/x-www-form-urlencoded` (issue #146, thanks @Larox) + +Internal: +- Changed templates: + - `http-client.eta` + - `procedure-call.eta` + +# 4.3.0 + +Fixes: - enum + nullable: true doesn't compute the good type (issue #145, thanks @RoXuS) - Underscores are omitted from enum keys (issue #108, thanks @eolant) - CLI silently fails if the directory to put new files in doesn't exist yet (issue #141, thanks @Styn) diff --git a/package-lock.json b/package-lock.json index cb38a7dd..08af59dc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,11 +1,11 @@ { "name": "swagger-typescript-api", - "version": "4.3.0", + "version": "4.4.0", "lockfileVersion": 2, "requires": true, "packages": { "": { - "version": "4.3.0", + "version": "4.4.0", "license": "MIT", "dependencies": { "@types/swagger-schema-official": "2.0.21", diff --git a/package.json b/package.json index 8181aa09..ba743e6a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "swagger-typescript-api", - "version": "4.3.0", + "version": "4.4.0", "description": "Create typescript api module from swagger schema", "scripts": { "cli:json": "node index.js -r -d -p ./swagger-test-cli.json -n swagger-test-cli.ts --extract-request-params --enum-names-as-values", diff --git a/src/routes.js b/src/routes.js index bb4e334b..d52b510f 100644 --- a/src/routes.js +++ b/src/routes.js @@ -277,10 +277,10 @@ const getContentTypes = (requestInfo, extraContentTypes) => ); const CONTENT_KIND = { - JSON: "json", - QUERY: "query", - FORM_DATA: "formData", - UNKNOWN: "unknown", + JSON: "JSON", + URL_ENCODED: "URL_ENCODED", + FORM_DATA: "FORM_DATA", + OTHER: "OTHER", }; const getContentKind = (contentTypes) => { @@ -289,14 +289,14 @@ const getContentKind = (contentTypes) => { } if (contentTypes.includes("application/x-www-form-urlencoded")) { - return CONTENT_KIND.QUERY; + return CONTENT_KIND.URL_ENCODED; } if (contentTypes.includes("multipart/form-data")) { return CONTENT_KIND.FORM_DATA; } - return CONTENT_KIND.UNKNOWN; + return CONTENT_KIND.OTHER; }; const getRequestBodyInfo = (routeInfo, routeParams, parsedSchemas) => { @@ -442,31 +442,22 @@ const parseRoutes = ({ usageSchema, parsedSchemas, moduleNameIndex, extractReque : null; const specificArgs = { - query: - queryType || requestBodyInfo.contentKind === CONTENT_KIND.QUERY - ? { - name: pathArgs.some((pathArg) => pathArg.name === "query") - ? "queryParams" - : "query", - optional: - requestBodyInfo.contentKind === CONTENT_KIND.QUERY - ? !requestBodyInfo.required && - (!queryType || parseSchema(queryObjectSchema, null).allFieldsAreOptional) - : parseSchema(queryObjectSchema, null).allFieldsAreOptional, - type: - requestBodyInfo.contentKind === CONTENT_KIND.QUERY - ? _.compact([queryType, requestBodyInfo.type]).join(" & ") - : queryType, - } - : void 0, - body: - requestBodyInfo.contentKind !== CONTENT_KIND.QUERY && requestBodyInfo.type - ? { - name: requestBodyInfo.paramName, - optional: !requestBodyInfo.required, - type: requestBodyInfo.type, - } - : void 0, + query: queryType + ? { + name: pathArgs.some((pathArg) => pathArg.name === "query") + ? "queryParams" + : "query", + optional: parseSchema(queryObjectSchema, null).allFieldsAreOptional, + type: queryType, + } + : void 0, + body: requestBodyInfo.type + ? { + name: requestBodyInfo.paramName, + optional: !requestBodyInfo.required, + type: requestBodyInfo.type, + } + : void 0, requestParams: { name: pathArgs.some((pathArg) => pathArg.name === "params") ? "requestParams" @@ -533,7 +524,7 @@ const parseRoutes = ({ usageSchema, parsedSchemas, moduleNameIndex, extractReque query: specificArgs.query, path: route.replace(/{/g, "${"), formData: requestBodyInfo.contentKind === CONTENT_KIND.FORM_DATA, - isQueryBody: requestBodyInfo.contentKind === CONTENT_KIND.QUERY, + isQueryBody: requestBodyInfo.contentKind === CONTENT_KIND.URL_ENCODED, security: hasSecurity, method: method, payload: specificArgs.body, diff --git a/src/swagger.js b/src/swagger.js index e7ec0150..54adc8db 100644 --- a/src/swagger.js +++ b/src/swagger.js @@ -83,6 +83,13 @@ const fixSwaggerScheme = (usage, original) => { const usageRouteParams = _.get(usageRouteInfo, "parameters", []); const originalRouteParams = _.get(originalRouteInfo, "parameters", []); + usageRouteInfo.consumes = _.uniq( + _.compact([...(usageRouteInfo.consumes || []), ...(originalRouteInfo.consumes || [])]), + ); + usageRouteInfo.produces = _.uniq( + _.compact([...(usageRouteInfo.produces || []), ...(originalRouteInfo.produces || [])]), + ); + _.each(originalRouteParams, (originalRouteParam) => { const existUsageParam = _.find( usageRouteParams, @@ -90,8 +97,6 @@ const fixSwaggerScheme = (usage, original) => { ); if (!existUsageParam) { usageRouteParams.push(originalRouteParam); - } else if (originalRouteParam.in === "formData") { - // console.log("HERE"); } }); }); diff --git a/templates/default/http-client.eta b/templates/default/http-client.eta index 616f8f38..39e2285c 100644 --- a/templates/default/http-client.eta +++ b/templates/default/http-client.eta @@ -30,7 +30,8 @@ interface HttpResponse extends R enum BodyType { Json, - FormData + FormData, + UrlEncoded, } export class HttpClient<<%~ apiConfig.generic.map(g => `${g.name} = unknown`).join(', ') %>> { @@ -59,14 +60,21 @@ export class HttpClient<<%~ apiConfig.generic.map(g => `${g.name} = unknown`).jo return encodeURIComponent(key) + "=" + encodeURIComponent(Array.isArray(query[key]) ? query[key].join(",") : query[key]) } - protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + protected toQueryString(rawQuery?: RequestQueryParamsType): string { const query = rawQuery || {}; const keys = Object.keys(query).filter((key) => "undefined" !== typeof query[key]); - return keys.length ? `?${keys.map(key => - typeof query[key] === "object" && !Array.isArray(query[key]) ? - this.addQueryParams(query[key] as object).substring(1) : - this.addQueryParam(query, key)).join("&") - }` : ""; + return keys + .map((key) => + typeof query[key] === "object" && !Array.isArray(query[key]) + ? this.toQueryString(query[key] as object) + : this.addQueryParam(query, key), + ) + .join("&"); + } + + protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + const queryString = this.toQueryString(rawQuery); + return queryString ? `?${queryString}` : ""; } private bodyFormatters: Record any> = { @@ -76,6 +84,7 @@ export class HttpClient<<%~ apiConfig.generic.map(g => `${g.name} = unknown`).jo data.append(key, input[key]); return data; }, new FormData()), + [BodyType.UrlEncoded]: (input: any) => this.toQueryString(input), } private mergeRequestOptions(params: RequestParams, securityParams?: RequestParams): RequestParams { diff --git a/templates/default/procedure-call.eta b/templates/default/procedure-call.eta index 06e8c004..49758d1b 100644 --- a/templates/default/procedure-call.eta +++ b/templates/default/procedure-call.eta @@ -1,5 +1,6 @@ <% const { utils, route, config } = it; +const { requestBodyInfo } = route; const { _, getInlineParseContent, getParseContent, parseSchema, getComponentByRef, require } = utils; const { parameters, path, method, payload, params, query, formData, security, requestParams } = route.request; const { type, errorType, contentTypes } = route.response; @@ -33,11 +34,13 @@ const wrapperArgs = _ .map(argToTmpl) .join(', ') -const bodyModeTmpl = formData - ? 'BodyType.FormData' - : security - ? 'BodyType.Json' - : null +const requestContentKind = { + "JSON": "BodyType.Json", + "URL_ENCODED": "BodyType.UrlEncoded", + "FORM_DATA": "BodyType.FormData" +} + +const bodyModeTmpl = requestContentKind[requestBodyInfo.contentKind] || (security && requestContentKind.JSON) || null const securityTmpl = security ? 'true' : null const pathTmpl = query != null ? `\`${path}\${this.addQueryParams(${queryName})}\`` diff --git a/templates/modular/http-client.eta b/templates/modular/http-client.eta index ec9844ab..e5156a60 100644 --- a/templates/modular/http-client.eta +++ b/templates/modular/http-client.eta @@ -27,7 +27,8 @@ export interface HttpResponse ex export enum BodyType { Json, - FormData + FormData, + UrlEncoded, } export class HttpClient<<%~ it.apiConfig.generic.map(g => `${g.name} = unknown`).join(', ') %>> { @@ -56,14 +57,21 @@ export class HttpClient<<%~ it.apiConfig.generic.map(g => `${g.name} = unknown`) return encodeURIComponent(key) + "=" + encodeURIComponent(Array.isArray(query[key]) ? query[key].join(",") : query[key]) } - protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + protected toQueryString(rawQuery?: RequestQueryParamsType): string { const query = rawQuery || {}; const keys = Object.keys(query).filter((key) => "undefined" !== typeof query[key]); - return keys.length ? `?${keys.map(key => - typeof query[key] === "object" && !Array.isArray(query[key]) ? - this.addQueryParams(query[key] as object).substring(1) : - this.addQueryParam(query, key)).join("&") - }` : ""; + return keys + .map((key) => + typeof query[key] === "object" && !Array.isArray(query[key]) + ? this.toQueryString(query[key] as object) + : this.addQueryParam(query, key), + ) + .join("&"); + } + + protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + const queryString = this.toQueryString(rawQuery); + return queryString ? `?${queryString}` : ""; } private bodyFormatters: Record any> = { @@ -73,6 +81,7 @@ export class HttpClient<<%~ it.apiConfig.generic.map(g => `${g.name} = unknown`) data.append(key, input[key]); return data; }, new FormData()), + [BodyType.UrlEncoded]: (input: any) => this.toQueryString(input), } private mergeRequestOptions(params: RequestParams, securityParams?: RequestParams): RequestParams { diff --git a/templates/modular/procedure-call.eta b/templates/modular/procedure-call.eta index f9dbd356..4b3b0e58 100644 --- a/templates/modular/procedure-call.eta +++ b/templates/modular/procedure-call.eta @@ -1,5 +1,6 @@ <% const { utils, route, config } = it; +const { requestBodyInfo } = route; const { _, getInlineParseContent, getParseContent, parseSchema, getComponentByRef, require } = utils; const { parameters, path, method, payload, params, query, formData, security, requestParams } = route.request; const { type, errorType, contentTypes } = route.response; @@ -33,11 +34,13 @@ const wrapperArgs = _ .map(argToTmpl) .join(', ') -const bodyModeTmpl = formData - ? 'BodyType.FormData' - : security - ? 'BodyType.Json' - : null +const requestContentKind = { + "JSON": "BodyType.Json", + "URL_ENCODED": "BodyType.UrlEncoded", + "FORM_DATA": "BodyType.FormData" +} + +const bodyModeTmpl = requestContentKind[requestBodyInfo.contentKind] || (security && requestContentKind.JSON) || null const securityTmpl = security ? 'true' : null const pathTmpl = query != null ? '`' + path + '${this.addQueryParams(' + query.name + ')}' + '`' diff --git a/tests/generated/v2.0/adafruit.ts b/tests/generated/v2.0/adafruit.ts index 5ef6ae56..dd6e31dd 100644 --- a/tests/generated/v2.0/adafruit.ts +++ b/tests/generated/v2.0/adafruit.ts @@ -184,6 +184,7 @@ interface HttpResponse extends R enum BodyType { Json, FormData, + UrlEncoded, } export class HttpClient { @@ -214,18 +215,21 @@ export class HttpClient { ); } - protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + protected toQueryString(rawQuery?: RequestQueryParamsType): string { const query = rawQuery || {}; const keys = Object.keys(query).filter((key) => "undefined" !== typeof query[key]); - return keys.length - ? `?${keys - .map((key) => - typeof query[key] === "object" && !Array.isArray(query[key]) - ? this.addQueryParams(query[key] as object).substring(1) - : this.addQueryParam(query, key), - ) - .join("&")}` - : ""; + return keys + .map((key) => + typeof query[key] === "object" && !Array.isArray(query[key]) + ? this.toQueryString(query[key] as object) + : this.addQueryParam(query, key), + ) + .join("&"); + } + + protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + const queryString = this.toQueryString(rawQuery); + return queryString ? `?${queryString}` : ""; } private bodyFormatters: Record any> = { @@ -235,6 +239,7 @@ export class HttpClient { data.append(key, input[key]); return data; }, new FormData()), + [BodyType.UrlEncoded]: (input: any) => this.toQueryString(input), }; private mergeRequestOptions(params: RequestParams, securityParams?: RequestParams): RequestParams { diff --git a/tests/generated/v2.0/another-example.ts b/tests/generated/v2.0/another-example.ts index a81962c1..de30c953 100644 --- a/tests/generated/v2.0/another-example.ts +++ b/tests/generated/v2.0/another-example.ts @@ -160,6 +160,7 @@ interface HttpResponse extends R enum BodyType { Json, FormData, + UrlEncoded, } export class HttpClient { @@ -190,18 +191,21 @@ export class HttpClient { ); } - protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + protected toQueryString(rawQuery?: RequestQueryParamsType): string { const query = rawQuery || {}; const keys = Object.keys(query).filter((key) => "undefined" !== typeof query[key]); - return keys.length - ? `?${keys - .map((key) => - typeof query[key] === "object" && !Array.isArray(query[key]) - ? this.addQueryParams(query[key] as object).substring(1) - : this.addQueryParam(query, key), - ) - .join("&")}` - : ""; + return keys + .map((key) => + typeof query[key] === "object" && !Array.isArray(query[key]) + ? this.toQueryString(query[key] as object) + : this.addQueryParam(query, key), + ) + .join("&"); + } + + protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + const queryString = this.toQueryString(rawQuery); + return queryString ? `?${queryString}` : ""; } private bodyFormatters: Record any> = { @@ -211,6 +215,7 @@ export class HttpClient { data.append(key, input[key]); return data; }, new FormData()), + [BodyType.UrlEncoded]: (input: any) => this.toQueryString(input), }; private mergeRequestOptions(params: RequestParams, securityParams?: RequestParams): RequestParams { @@ -331,8 +336,8 @@ export class Api extends HttpClient { * @summary summary * @request POST:/pet/single-form-url-encoded */ - singleFormUrlEncodedRequest: (query: { param1: string; param2: string }, params?: RequestParams) => - this.request(`/pet/single-form-url-encoded${this.addQueryParams(query)}`, "POST", params), + singleFormUrlEncodedRequest: (data: { param1: string; param2: string }, params?: RequestParams) => + this.request(`/pet/single-form-url-encoded`, "POST", params, data, BodyType.UrlEncoded), /** * No description @@ -343,7 +348,20 @@ export class Api extends HttpClient { * @request POST:/pet/form-url-encoded */ formUrlEncodedRequest: (data: { param1: string; param2: string }, params?: RequestParams) => - this.request(`/pet/form-url-encoded`, "POST", params, data, BodyType.FormData), + this.request(`/pet/form-url-encoded`, "POST", params, data, BodyType.UrlEncoded), + + /** + * No description + * + * @tags pet + * @name FormUrlEncodedRequest2 + * @summary summary + * @request POST:/pet/end-form-url-encoded + * @originalName formUrlEncodedRequest + * @duplicate + */ + formUrlEncodedRequest2: (data: { param1: string; param2: string }, params?: RequestParams) => + this.request(`/pet/end-form-url-encoded`, "POST", params, data, BodyType.UrlEncoded), /** * @description Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing. @@ -433,7 +451,8 @@ export class Api extends HttpClient { * @summary Place an order for a pet * @request POST:/store/order */ - placeOrder: (body: Order, params?: RequestParams) => this.request(`/store/order`, "POST", params, body), + placeOrder: (body: Order, params?: RequestParams) => + this.request(`/store/order`, "POST", params, body, BodyType.Json), /** * @description For valid response try integer IDs with value <= 5 or > 10. Other values will generated exceptions @@ -466,7 +485,8 @@ export class Api extends HttpClient { * @summary Create user * @request POST:/user */ - createUser: (body: User, params?: RequestParams) => this.request(`/user`, "POST", params, body), + createUser: (body: User, params?: RequestParams) => + this.request(`/user`, "POST", params, body, BodyType.Json), /** * No description @@ -477,7 +497,7 @@ export class Api extends HttpClient { * @request POST:/user/createWithArray */ createUsersWithArrayInput: (body: User[], params?: RequestParams) => - this.request(`/user/createWithArray`, "POST", params, body), + this.request(`/user/createWithArray`, "POST", params, body, BodyType.Json), /** * No description @@ -488,7 +508,7 @@ export class Api extends HttpClient { * @request POST:/user/createWithList */ createUsersWithListInput: (body: User[], params?: RequestParams) => - this.request(`/user/createWithList`, "POST", params, body), + this.request(`/user/createWithList`, "POST", params, body, BodyType.Json), /** * No description @@ -531,7 +551,7 @@ export class Api extends HttpClient { * @request PUT:/user/{username} */ updateUser: (username: string, body: User, params?: RequestParams) => - this.request(`/user/${username}`, "PUT", params, body), + this.request(`/user/${username}`, "PUT", params, body, BodyType.Json), /** * @description This can only be done by the logged in user. diff --git a/tests/generated/v2.0/api-with-examples.ts b/tests/generated/v2.0/api-with-examples.ts index 4c0e0ffc..6d2cf11b 100644 --- a/tests/generated/v2.0/api-with-examples.ts +++ b/tests/generated/v2.0/api-with-examples.ts @@ -29,6 +29,7 @@ interface HttpResponse extends R enum BodyType { Json, FormData, + UrlEncoded, } export class HttpClient { @@ -59,18 +60,21 @@ export class HttpClient { ); } - protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + protected toQueryString(rawQuery?: RequestQueryParamsType): string { const query = rawQuery || {}; const keys = Object.keys(query).filter((key) => "undefined" !== typeof query[key]); - return keys.length - ? `?${keys - .map((key) => - typeof query[key] === "object" && !Array.isArray(query[key]) - ? this.addQueryParams(query[key] as object).substring(1) - : this.addQueryParam(query, key), - ) - .join("&")}` - : ""; + return keys + .map((key) => + typeof query[key] === "object" && !Array.isArray(query[key]) + ? this.toQueryString(query[key] as object) + : this.addQueryParam(query, key), + ) + .join("&"); + } + + protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + const queryString = this.toQueryString(rawQuery); + return queryString ? `?${queryString}` : ""; } private bodyFormatters: Record any> = { @@ -80,6 +84,7 @@ export class HttpClient { data.append(key, input[key]); return data; }, new FormData()), + [BodyType.UrlEncoded]: (input: any) => this.toQueryString(input), }; private mergeRequestOptions(params: RequestParams, securityParams?: RequestParams): RequestParams { diff --git a/tests/generated/v2.0/authentiq.ts b/tests/generated/v2.0/authentiq.ts index 67d79309..37635c70 100644 --- a/tests/generated/v2.0/authentiq.ts +++ b/tests/generated/v2.0/authentiq.ts @@ -81,6 +81,7 @@ interface HttpResponse extends R enum BodyType { Json, FormData, + UrlEncoded, } export class HttpClient { @@ -111,18 +112,21 @@ export class HttpClient { ); } - protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + protected toQueryString(rawQuery?: RequestQueryParamsType): string { const query = rawQuery || {}; const keys = Object.keys(query).filter((key) => "undefined" !== typeof query[key]); - return keys.length - ? `?${keys - .map((key) => - typeof query[key] === "object" && !Array.isArray(query[key]) - ? this.addQueryParams(query[key] as object).substring(1) - : this.addQueryParam(query, key), - ) - .join("&")}` - : ""; + return keys + .map((key) => + typeof query[key] === "object" && !Array.isArray(query[key]) + ? this.toQueryString(query[key] as object) + : this.addQueryParam(query, key), + ) + .join("&"); + } + + protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + const queryString = this.toQueryString(rawQuery); + return queryString ? `?${queryString}` : ""; } private bodyFormatters: Record any> = { @@ -132,6 +136,7 @@ export class HttpClient { data.append(key, input[key]); return data; }, new FormData()), + [BodyType.UrlEncoded]: (input: any) => this.toQueryString(input), }; private mergeRequestOptions(params: RequestParams, securityParams?: RequestParams): RequestParams { @@ -335,7 +340,7 @@ export class Api extends HttpClient { * @request POST:/scope/{job} */ signConfirm: (job: string, params?: RequestParams) => - this.request<{ status?: string }, Error>(`/scope/${job}`, "POST", params), + this.request<{ status?: string }, Error>(`/scope/${job}`, "POST", params, null, BodyType.Json), /** * @description authority updates a JWT with its signature See: https://github.com/skion/authentiq/wiki/JWT-Examples diff --git a/tests/generated/v2.0/example1.ts b/tests/generated/v2.0/example1.ts index 3d4cc77a..7119a02b 100644 --- a/tests/generated/v2.0/example1.ts +++ b/tests/generated/v2.0/example1.ts @@ -56,6 +56,7 @@ interface HttpResponse extends R enum BodyType { Json, FormData, + UrlEncoded, } export class HttpClient { @@ -86,18 +87,21 @@ export class HttpClient { ); } - protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + protected toQueryString(rawQuery?: RequestQueryParamsType): string { const query = rawQuery || {}; const keys = Object.keys(query).filter((key) => "undefined" !== typeof query[key]); - return keys.length - ? `?${keys - .map((key) => - typeof query[key] === "object" && !Array.isArray(query[key]) - ? this.addQueryParams(query[key] as object).substring(1) - : this.addQueryParam(query, key), - ) - .join("&")}` - : ""; + return keys + .map((key) => + typeof query[key] === "object" && !Array.isArray(query[key]) + ? this.toQueryString(query[key] as object) + : this.addQueryParam(query, key), + ) + .join("&"); + } + + protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + const queryString = this.toQueryString(rawQuery); + return queryString ? `?${queryString}` : ""; } private bodyFormatters: Record any> = { @@ -107,6 +111,7 @@ export class HttpClient { data.append(key, input[key]); return data; }, new FormData()), + [BodyType.UrlEncoded]: (input: any) => this.toQueryString(input), }; private mergeRequestOptions(params: RequestParams, securityParams?: RequestParams): RequestParams { @@ -198,6 +203,7 @@ export class Api extends HttpClient { "POST", params, parameters, + BodyType.Json, ), }; } diff --git a/tests/generated/v2.0/file-formdata-example.ts b/tests/generated/v2.0/file-formdata-example.ts index 297be08a..4e9e331e 100644 --- a/tests/generated/v2.0/file-formdata-example.ts +++ b/tests/generated/v2.0/file-formdata-example.ts @@ -29,6 +29,7 @@ interface HttpResponse extends R enum BodyType { Json, FormData, + UrlEncoded, } export class HttpClient { @@ -59,18 +60,21 @@ export class HttpClient { ); } - protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + protected toQueryString(rawQuery?: RequestQueryParamsType): string { const query = rawQuery || {}; const keys = Object.keys(query).filter((key) => "undefined" !== typeof query[key]); - return keys.length - ? `?${keys - .map((key) => - typeof query[key] === "object" && !Array.isArray(query[key]) - ? this.addQueryParams(query[key] as object).substring(1) - : this.addQueryParam(query, key), - ) - .join("&")}` - : ""; + return keys + .map((key) => + typeof query[key] === "object" && !Array.isArray(query[key]) + ? this.toQueryString(query[key] as object) + : this.addQueryParam(query, key), + ) + .join("&"); + } + + protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + const queryString = this.toQueryString(rawQuery); + return queryString ? `?${queryString}` : ""; } private bodyFormatters: Record any> = { @@ -80,6 +84,7 @@ export class HttpClient { data.append(key, input[key]); return data; }, new FormData()), + [BodyType.UrlEncoded]: (input: any) => this.toQueryString(input), }; private mergeRequestOptions(params: RequestParams, securityParams?: RequestParams): RequestParams { diff --git a/tests/generated/v2.0/furkot-example.ts b/tests/generated/v2.0/furkot-example.ts index 3ddb4544..24b48df1 100644 --- a/tests/generated/v2.0/furkot-example.ts +++ b/tests/generated/v2.0/furkot-example.ts @@ -92,6 +92,7 @@ interface HttpResponse extends R enum BodyType { Json, FormData, + UrlEncoded, } export class HttpClient { @@ -122,18 +123,21 @@ export class HttpClient { ); } - protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + protected toQueryString(rawQuery?: RequestQueryParamsType): string { const query = rawQuery || {}; const keys = Object.keys(query).filter((key) => "undefined" !== typeof query[key]); - return keys.length - ? `?${keys - .map((key) => - typeof query[key] === "object" && !Array.isArray(query[key]) - ? this.addQueryParams(query[key] as object).substring(1) - : this.addQueryParam(query, key), - ) - .join("&")}` - : ""; + return keys + .map((key) => + typeof query[key] === "object" && !Array.isArray(query[key]) + ? this.toQueryString(query[key] as object) + : this.addQueryParam(query, key), + ) + .join("&"); + } + + protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + const queryString = this.toQueryString(rawQuery); + return queryString ? `?${queryString}` : ""; } private bodyFormatters: Record any> = { @@ -143,6 +147,7 @@ export class HttpClient { data.append(key, input[key]); return data; }, new FormData()), + [BodyType.UrlEncoded]: (input: any) => this.toQueryString(input), }; private mergeRequestOptions(params: RequestParams, securityParams?: RequestParams): RequestParams { diff --git a/tests/generated/v2.0/giphy.ts b/tests/generated/v2.0/giphy.ts index 52934211..12d3a683 100644 --- a/tests/generated/v2.0/giphy.ts +++ b/tests/generated/v2.0/giphy.ts @@ -311,6 +311,7 @@ interface HttpResponse extends R enum BodyType { Json, FormData, + UrlEncoded, } export class HttpClient { @@ -341,18 +342,21 @@ export class HttpClient { ); } - protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + protected toQueryString(rawQuery?: RequestQueryParamsType): string { const query = rawQuery || {}; const keys = Object.keys(query).filter((key) => "undefined" !== typeof query[key]); - return keys.length - ? `?${keys - .map((key) => - typeof query[key] === "object" && !Array.isArray(query[key]) - ? this.addQueryParams(query[key] as object).substring(1) - : this.addQueryParam(query, key), - ) - .join("&")}` - : ""; + return keys + .map((key) => + typeof query[key] === "object" && !Array.isArray(query[key]) + ? this.toQueryString(query[key] as object) + : this.addQueryParam(query, key), + ) + .join("&"); + } + + protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + const queryString = this.toQueryString(rawQuery); + return queryString ? `?${queryString}` : ""; } private bodyFormatters: Record any> = { @@ -362,6 +366,7 @@ export class HttpClient { data.append(key, input[key]); return data; }, new FormData()), + [BodyType.UrlEncoded]: (input: any) => this.toQueryString(input), }; private mergeRequestOptions(params: RequestParams, securityParams?: RequestParams): RequestParams { diff --git a/tests/generated/v2.0/github-swagger.ts b/tests/generated/v2.0/github-swagger.ts index 1e2c62ba..b406fbb8 100644 --- a/tests/generated/v2.0/github-swagger.ts +++ b/tests/generated/v2.0/github-swagger.ts @@ -1454,6 +1454,7 @@ interface HttpResponse extends R enum BodyType { Json, FormData, + UrlEncoded, } export class HttpClient { @@ -1484,18 +1485,21 @@ export class HttpClient { ); } - protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + protected toQueryString(rawQuery?: RequestQueryParamsType): string { const query = rawQuery || {}; const keys = Object.keys(query).filter((key) => "undefined" !== typeof query[key]); - return keys.length - ? `?${keys - .map((key) => - typeof query[key] === "object" && !Array.isArray(query[key]) - ? this.addQueryParams(query[key] as object).substring(1) - : this.addQueryParam(query, key), - ) - .join("&")}` - : ""; + return keys + .map((key) => + typeof query[key] === "object" && !Array.isArray(query[key]) + ? this.toQueryString(query[key] as object) + : this.addQueryParam(query, key), + ) + .join("&"); + } + + protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + const queryString = this.toQueryString(rawQuery); + return queryString ? `?${queryString}` : ""; } private bodyFormatters: Record any> = { @@ -1505,6 +1509,7 @@ export class HttpClient { data.append(key, input[key]); return data; }, new FormData()), + [BodyType.UrlEncoded]: (input: any) => this.toQueryString(input), }; private mergeRequestOptions(params: RequestParams, securityParams?: RequestParams): RequestParams { @@ -1616,7 +1621,8 @@ export class Api extends HttpClient { * @name GistsCreate * @request POST:/gists */ - gistsCreate: (body: PostGist, params?: RequestParams) => this.request(`/gists`, "POST", params, body), + gistsCreate: (body: PostGist, params?: RequestParams) => + this.request(`/gists`, "POST", params, body, BodyType.Json), /** * @description List all public gists. @@ -1659,7 +1665,7 @@ export class Api extends HttpClient { * @request PATCH:/gists/{id} */ gistsPartialUpdate: (id: number, body: PatchGist, params?: RequestParams) => - this.request(`/gists/${id}`, "PATCH", params, body), + this.request(`/gists/${id}`, "PATCH", params, body, BodyType.Json), /** * @description List comments on a gist. @@ -1706,7 +1712,7 @@ export class Api extends HttpClient { * @request PATCH:/gists/{id}/comments/{commentId} */ commentsPartialUpdate: (id: number, commentId: number, body: Comment, params?: RequestParams) => - this.request(`/gists/${id}/comments/${commentId}`, "PATCH", params, body), + this.request(`/gists/${id}/comments/${commentId}`, "PATCH", params, body, BodyType.Json), /** * @description Fork a gist. @@ -1848,7 +1854,7 @@ export class Api extends HttpClient { * @request POST:/markdown */ markdownCreate: (body: Markdown, params?: RequestParams) => - this.request(`/markdown`, "POST", params, body), + this.request(`/markdown`, "POST", params, body, BodyType.Json), /** * @description Render a Markdown document in raw mode @@ -1939,7 +1945,7 @@ export class Api extends HttpClient { * @request PUT:/notifications/threads/{id}/subscription */ threadsSubscriptionUpdate: (id: number, body: PutSubscription, params?: RequestParams) => - this.request(`/notifications/threads/${id}/subscription`, "PUT", params, body), + this.request(`/notifications/threads/${id}/subscription`, "PUT", params, body, BodyType.Json), }; orgs = { /** @@ -1957,7 +1963,7 @@ export class Api extends HttpClient { * @request PATCH:/orgs/{org} */ orgsPartialUpdate: (org: string, body: PatchOrg, params?: RequestParams) => - this.request(`/orgs/${org}`, "PATCH", params, body), + this.request(`/orgs/${org}`, "PATCH", params, body, BodyType.Json), /** * @description List public events for an organization. @@ -2090,7 +2096,7 @@ export class Api extends HttpClient { * @request POST:/orgs/{org}/teams */ teamsCreate: (org: string, body: OrgTeamsPost, params?: RequestParams) => - this.request(`/orgs/${org}/teams`, "POST", params, body), + this.request(`/orgs/${org}/teams`, "POST", params, body, BodyType.Json), }; rateLimit = { /** @@ -2127,7 +2133,7 @@ export class Api extends HttpClient { * @request PATCH:/repos/{owner}/{repo} */ reposPartialUpdate: (owner: string, repo: string, body: RepoEdit, params?: RequestParams) => - this.request(`/repos/${owner}/${repo}`, "PATCH", params, body), + this.request(`/repos/${owner}/${repo}`, "PATCH", params, body, BodyType.Json), /** * @description List assignees. This call lists all the available assignees (owner + collaborators) to which issues may be assigned. @@ -2304,7 +2310,14 @@ export class Api extends HttpClient { shaCode: string, body: CommitCommentBody, params?: RequestParams, - ) => this.request(`/repos/${owner}/${repo}/commits/${shaCode}/comments`, "POST", params, body), + ) => + this.request( + `/repos/${owner}/${repo}/commits/${shaCode}/comments`, + "POST", + params, + body, + BodyType.Json, + ), /** * @description Compare two commits @@ -2322,7 +2335,7 @@ export class Api extends HttpClient { * @request DELETE:/repos/{owner}/{repo}/contents/{path} */ contentsDelete: (owner: string, repo: string, path: string, body: DeleteFileBody, params?: RequestParams) => - this.request(`/repos/${owner}/${repo}/contents/${path}`, "DELETE", params, body), + this.request(`/repos/${owner}/${repo}/contents/${path}`, "DELETE", params, body, BodyType.Json), /** * @description Get contents. This method returns the contents of a file or directory in a repository. Files and symlinks support a custom media type for getting the raw content. Directories and submodules do not support custom media types. Note: This API supports files up to 1 megabyte in size. Here can be many outcomes. For details see "http://developer.github.com/v3/repos/contents/" @@ -2350,7 +2363,7 @@ export class Api extends HttpClient { * @request PUT:/repos/{owner}/{repo}/contents/{path} */ contentsUpdate: (owner: string, repo: string, path: string, body: CreateFileBody, params?: RequestParams) => - this.request(`/repos/${owner}/${repo}/contents/${path}`, "PUT", params, body), + this.request(`/repos/${owner}/${repo}/contents/${path}`, "PUT", params, body, BodyType.Json), /** * @description Get list of contributors. @@ -2377,7 +2390,7 @@ export class Api extends HttpClient { * @request POST:/repos/{owner}/{repo}/deployments */ deploymentsCreate: (owner: string, repo: string, body: Deployment, params?: RequestParams) => - this.request(`/repos/${owner}/${repo}/deployments`, "POST", params, body), + this.request(`/repos/${owner}/${repo}/deployments`, "POST", params, body, BodyType.Json), /** * @description Users with pull access can view deployment statuses for a deployment @@ -2400,7 +2413,8 @@ export class Api extends HttpClient { id: number, body: DeploymentStatusesCreate, params?: RequestParams, - ) => this.request(`/repos/${owner}/${repo}/deployments/${id}/statuses`, "POST", params, body), + ) => + this.request(`/repos/${owner}/${repo}/deployments/${id}/statuses`, "POST", params, body, BodyType.Json), /** * @description Deprecated. List downloads for a repository. @@ -2460,7 +2474,7 @@ export class Api extends HttpClient { * @request POST:/repos/{owner}/{repo}/forks */ forksCreate: (owner: string, repo: string, body: ForkBody, params?: RequestParams) => - this.request(`/repos/${owner}/${repo}/forks`, "POST", params, body), + this.request(`/repos/${owner}/${repo}/forks`, "POST", params, body, BodyType.Json), /** * @description Create a Blob. @@ -2469,7 +2483,7 @@ export class Api extends HttpClient { * @request POST:/repos/{owner}/{repo}/git/blobs */ gitBlobsCreate: (owner: string, repo: string, body: Blob, params?: RequestParams) => - this.request(`/repos/${owner}/${repo}/git/blobs`, "POST", params, body), + this.request(`/repos/${owner}/${repo}/git/blobs`, "POST", params, body, BodyType.Json), /** * @description Get a Blob. Since blobs can be any arbitrary binary data, the input and responses for the blob API takes an encoding parameter that can be either utf-8 or base64. If your data cannot be losslessly sent as a UTF-8 string, you can base64 encode it. @@ -2487,7 +2501,7 @@ export class Api extends HttpClient { * @request POST:/repos/{owner}/{repo}/git/commits */ gitCommitsCreate: (owner: string, repo: string, body: RepoCommitBody, params?: RequestParams) => - this.request(`/repos/${owner}/${repo}/git/commits`, "POST", params, body), + this.request(`/repos/${owner}/${repo}/git/commits`, "POST", params, body, BodyType.Json), /** * @description Get a Commit. @@ -2514,7 +2528,7 @@ export class Api extends HttpClient { * @request POST:/repos/{owner}/{repo}/git/refs */ gitRefsCreate: (owner: string, repo: string, body: RefsBody, params?: RequestParams) => - this.request(`/repos/${owner}/${repo}/git/refs`, "POST", params, body), + this.request(`/repos/${owner}/${repo}/git/refs`, "POST", params, body, BodyType.Json), /** * @description Delete a Reference Example: Deleting a branch: DELETE /repos/octocat/Hello-World/git/refs/heads/feature-a Example: Deleting a tag: DELETE /repos/octocat/Hello-World/git/refs/tags/v1.0 @@ -2543,7 +2557,7 @@ export class Api extends HttpClient { * @request PATCH:/repos/{owner}/{repo}/git/refs/{ref} */ gitRefsPartialUpdate: (owner: string, repo: string, ref: string, body: GitRefPatch, params?: RequestParams) => - this.request(`/repos/${owner}/${repo}/git/refs/${ref}`, "PATCH", params, body), + this.request(`/repos/${owner}/${repo}/git/refs/${ref}`, "PATCH", params, body, BodyType.Json), /** * @description Create a Tag Object. Note that creating a tag object does not create the reference that makes a tag in Git. If you want to create an annotated tag in Git, you have to do this call to create the tag object, and then create the refs/tags/[tag] reference. If you want to create a lightweight tag, you only have to create the tag reference - this call would be unnecessary. @@ -2552,7 +2566,7 @@ export class Api extends HttpClient { * @request POST:/repos/{owner}/{repo}/git/tags */ gitTagsCreate: (owner: string, repo: string, body: TagBody, params?: RequestParams) => - this.request(`/repos/${owner}/${repo}/git/tags`, "POST", params, body), + this.request(`/repos/${owner}/${repo}/git/tags`, "POST", params, body, BodyType.Json), /** * @description Get a Tag. @@ -2570,7 +2584,7 @@ export class Api extends HttpClient { * @request POST:/repos/{owner}/{repo}/git/trees */ gitTreesCreate: (owner: string, repo: string, body: Tree, params?: RequestParams) => - this.request(`/repos/${owner}/${repo}/git/trees`, "POST", params, body), + this.request(`/repos/${owner}/${repo}/git/trees`, "POST", params, body, BodyType.Json), /** * @description Get a Tree. @@ -2948,7 +2962,13 @@ export class Api extends HttpClient { * @request POST:/repos/{owner}/{repo}/merges */ mergesCreate: (owner: string, repo: string, body: MergesBody, params?: RequestParams) => - this.request(`/repos/${owner}/${repo}/merges`, "POST", params, body), + this.request( + `/repos/${owner}/${repo}/merges`, + "POST", + params, + body, + BodyType.Json, + ), /** * @description List milestones for a repository. @@ -3062,7 +3082,7 @@ export class Api extends HttpClient { * @request POST:/repos/{owner}/{repo}/pulls */ pullsCreate: (owner: string, repo: string, body: PullsPost, params?: RequestParams) => - this.request(`/repos/${owner}/${repo}/pulls`, "POST", params, body), + this.request(`/repos/${owner}/${repo}/pulls`, "POST", params, body, BodyType.Json), /** * @description List comments in a repository. By default, Review Comments are ordered by ascending ID. @@ -3134,7 +3154,7 @@ export class Api extends HttpClient { * @request PATCH:/repos/{owner}/{repo}/pulls/{number} */ pullsPartialUpdate: (owner: string, repo: string, number: number, body: PullUpdate, params?: RequestParams) => - this.request(`/repos/${owner}/${repo}/pulls/${number}`, "PATCH", params, body), + this.request(`/repos/${owner}/${repo}/pulls/${number}`, "PATCH", params, body, BodyType.Json), /** * @description List comments on a pull request. @@ -3159,7 +3179,14 @@ export class Api extends HttpClient { number: number, body: PullsCommentPost, params?: RequestParams, - ) => this.request(`/repos/${owner}/${repo}/pulls/${number}/comments`, "POST", params, body), + ) => + this.request( + `/repos/${owner}/${repo}/pulls/${number}/comments`, + "POST", + params, + body, + BodyType.Json, + ), /** * @description List commits on a pull request. @@ -3195,7 +3222,7 @@ export class Api extends HttpClient { * @request PUT:/repos/{owner}/{repo}/pulls/{number}/merge */ pullsMergeUpdate: (owner: string, repo: string, number: number, body: MergePullBody, params?: RequestParams) => - this.request(`/repos/${owner}/${repo}/pulls/${number}/merge`, "PUT", params, body), + this.request(`/repos/${owner}/${repo}/pulls/${number}/merge`, "PUT", params, body, BodyType.Json), /** * @description Get the README. This method returns the preferred README for a repository. @@ -3249,7 +3276,7 @@ export class Api extends HttpClient { * @request PATCH:/repos/{owner}/{repo}/releases/assets/{id} */ releasesAssetsPartialUpdate: (owner: string, repo: string, id: string, body: AssetPatch, params?: RequestParams) => - this.request(`/repos/${owner}/${repo}/releases/assets/${id}`, "PATCH", params, body), + this.request(`/repos/${owner}/${repo}/releases/assets/${id}`, "PATCH", params, body, BodyType.Json), /** * @description Users with push access to the repository can delete a release. @@ -3361,7 +3388,7 @@ export class Api extends HttpClient { * @request POST:/repos/{owner}/{repo}/statuses/{ref} */ statusesCreate: (owner: string, repo: string, ref: string, body: HeadBranch, params?: RequestParams) => - this.request(`/repos/${owner}/${repo}/statuses/${ref}`, "POST", params, body), + this.request(`/repos/${owner}/${repo}/statuses/${ref}`, "POST", params, body, BodyType.Json), /** * @description List watchers. @@ -3397,7 +3424,7 @@ export class Api extends HttpClient { * @request PUT:/repos/{owner}/{repo}/subscription */ subscriptionUpdate: (owner: string, repo: string, body: SubscriptionBody, params?: RequestParams) => - this.request(`/repos/${owner}/${repo}/subscription`, "PUT", params, body), + this.request(`/repos/${owner}/${repo}/subscription`, "PUT", params, body, BodyType.Json), /** * @description Get list of tags. @@ -3520,7 +3547,7 @@ export class Api extends HttpClient { * @request PATCH:/teams/{teamId} */ teamsPartialUpdate: (teamId: number, body: EditTeam, params?: RequestParams) => - this.request(`/teams/${teamId}`, "PATCH", params, body), + this.request(`/teams/${teamId}`, "PATCH", params, body, BodyType.Json), /** * @description List team members. In order to list members in a team, the authenticated user must be a member of the team. @@ -3641,7 +3668,7 @@ export class Api extends HttpClient { * @request PATCH:/user */ userPartialUpdate: (body: UserUpdate, params?: RequestParams) => - this.request(`/user`, "PATCH", params, body), + this.request(`/user`, "PATCH", params, body, BodyType.Json), /** * @description Delete email address(es). You can include a single email address or an array of addresses. @@ -3650,7 +3677,7 @@ export class Api extends HttpClient { * @request DELETE:/user/emails */ emailsDelete: (body: UserEmails, params?: RequestParams) => - this.request(`/user/emails`, "DELETE", params, body), + this.request(`/user/emails`, "DELETE", params, body, BodyType.Json), /** * @description List email addresses for a user. In the final version of the API, this method will return an array of hashes with extended information for each email address indicating if the address has been verified and if it's primary email address for GitHub. Until API v3 is finalized, use the application/vnd.github.v3 media type to get other response format. diff --git a/tests/generated/v2.0/path-args.ts b/tests/generated/v2.0/path-args.ts index ae95e950..1f5f0f36 100644 --- a/tests/generated/v2.0/path-args.ts +++ b/tests/generated/v2.0/path-args.ts @@ -29,6 +29,7 @@ interface HttpResponse extends R enum BodyType { Json, FormData, + UrlEncoded, } export class HttpClient { @@ -59,18 +60,21 @@ export class HttpClient { ); } - protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + protected toQueryString(rawQuery?: RequestQueryParamsType): string { const query = rawQuery || {}; const keys = Object.keys(query).filter((key) => "undefined" !== typeof query[key]); - return keys.length - ? `?${keys - .map((key) => - typeof query[key] === "object" && !Array.isArray(query[key]) - ? this.addQueryParams(query[key] as object).substring(1) - : this.addQueryParam(query, key), - ) - .join("&")}` - : ""; + return keys + .map((key) => + typeof query[key] === "object" && !Array.isArray(query[key]) + ? this.toQueryString(query[key] as object) + : this.addQueryParam(query, key), + ) + .join("&"); + } + + protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + const queryString = this.toQueryString(rawQuery); + return queryString ? `?${queryString}` : ""; } private bodyFormatters: Record any> = { @@ -80,6 +84,7 @@ export class HttpClient { data.append(key, input[key]); return data; }, new FormData()), + [BodyType.UrlEncoded]: (input: any) => this.toQueryString(input), }; private mergeRequestOptions(params: RequestParams, securityParams?: RequestParams): RequestParams { diff --git a/tests/generated/v2.0/petstore-expanded.ts b/tests/generated/v2.0/petstore-expanded.ts index dc4b1dda..2fe1ad21 100644 --- a/tests/generated/v2.0/petstore-expanded.ts +++ b/tests/generated/v2.0/petstore-expanded.ts @@ -66,6 +66,7 @@ interface HttpResponse extends R enum BodyType { Json, FormData, + UrlEncoded, } export class HttpClient { @@ -96,18 +97,21 @@ export class HttpClient { ); } - protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + protected toQueryString(rawQuery?: RequestQueryParamsType): string { const query = rawQuery || {}; const keys = Object.keys(query).filter((key) => "undefined" !== typeof query[key]); - return keys.length - ? `?${keys - .map((key) => - typeof query[key] === "object" && !Array.isArray(query[key]) - ? this.addQueryParams(query[key] as object).substring(1) - : this.addQueryParam(query, key), - ) - .join("&")}` - : ""; + return keys + .map((key) => + typeof query[key] === "object" && !Array.isArray(query[key]) + ? this.toQueryString(query[key] as object) + : this.addQueryParam(query, key), + ) + .join("&"); + } + + protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + const queryString = this.toQueryString(rawQuery); + return queryString ? `?${queryString}` : ""; } private bodyFormatters: Record any> = { @@ -117,6 +121,7 @@ export class HttpClient { data.append(key, input[key]); return data; }, new FormData()), + [BodyType.UrlEncoded]: (input: any) => this.toQueryString(input), }; private mergeRequestOptions(params: RequestParams, securityParams?: RequestParams): RequestParams { @@ -201,7 +206,8 @@ export class Api extends HttpClient { * @name AddPet * @request POST:/pets */ - addPet: (pet: NewPet, params?: RequestParams) => this.request(`/pets`, "POST", params, pet), + addPet: (pet: NewPet, params?: RequestParams) => + this.request(`/pets`, "POST", params, pet, BodyType.Json), /** * @description Returns a user based on a single ID, if the user does not have access to the pet diff --git a/tests/generated/v2.0/petstore-minimal.ts b/tests/generated/v2.0/petstore-minimal.ts index 5cc4fcde..5fe192f6 100644 --- a/tests/generated/v2.0/petstore-minimal.ts +++ b/tests/generated/v2.0/petstore-minimal.ts @@ -37,6 +37,7 @@ interface HttpResponse extends R enum BodyType { Json, FormData, + UrlEncoded, } export class HttpClient { @@ -67,18 +68,21 @@ export class HttpClient { ); } - protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + protected toQueryString(rawQuery?: RequestQueryParamsType): string { const query = rawQuery || {}; const keys = Object.keys(query).filter((key) => "undefined" !== typeof query[key]); - return keys.length - ? `?${keys - .map((key) => - typeof query[key] === "object" && !Array.isArray(query[key]) - ? this.addQueryParams(query[key] as object).substring(1) - : this.addQueryParam(query, key), - ) - .join("&")}` - : ""; + return keys + .map((key) => + typeof query[key] === "object" && !Array.isArray(query[key]) + ? this.toQueryString(query[key] as object) + : this.addQueryParam(query, key), + ) + .join("&"); + } + + protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + const queryString = this.toQueryString(rawQuery); + return queryString ? `?${queryString}` : ""; } private bodyFormatters: Record any> = { @@ -88,6 +92,7 @@ export class HttpClient { data.append(key, input[key]); return data; }, new FormData()), + [BodyType.UrlEncoded]: (input: any) => this.toQueryString(input), }; private mergeRequestOptions(params: RequestParams, securityParams?: RequestParams): RequestParams { diff --git a/tests/generated/v2.0/petstore-simple.ts b/tests/generated/v2.0/petstore-simple.ts index 39fe6c54..d8a6e54c 100644 --- a/tests/generated/v2.0/petstore-simple.ts +++ b/tests/generated/v2.0/petstore-simple.ts @@ -52,6 +52,7 @@ interface HttpResponse extends R enum BodyType { Json, FormData, + UrlEncoded, } export class HttpClient { @@ -82,18 +83,21 @@ export class HttpClient { ); } - protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + protected toQueryString(rawQuery?: RequestQueryParamsType): string { const query = rawQuery || {}; const keys = Object.keys(query).filter((key) => "undefined" !== typeof query[key]); - return keys.length - ? `?${keys - .map((key) => - typeof query[key] === "object" && !Array.isArray(query[key]) - ? this.addQueryParams(query[key] as object).substring(1) - : this.addQueryParam(query, key), - ) - .join("&")}` - : ""; + return keys + .map((key) => + typeof query[key] === "object" && !Array.isArray(query[key]) + ? this.toQueryString(query[key] as object) + : this.addQueryParam(query, key), + ) + .join("&"); + } + + protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + const queryString = this.toQueryString(rawQuery); + return queryString ? `?${queryString}` : ""; } private bodyFormatters: Record any> = { @@ -103,6 +107,7 @@ export class HttpClient { data.append(key, input[key]); return data; }, new FormData()), + [BodyType.UrlEncoded]: (input: any) => this.toQueryString(input), }; private mergeRequestOptions(params: RequestParams, securityParams?: RequestParams): RequestParams { @@ -187,7 +192,8 @@ export class Api extends HttpClient { * @name AddPet * @request POST:/pets */ - addPet: (pet: NewPet, params?: RequestParams) => this.request(`/pets`, "POST", params, pet), + addPet: (pet: NewPet, params?: RequestParams) => + this.request(`/pets`, "POST", params, pet, BodyType.Json), /** * @description Returns a user based on a single ID, if the user does not have access to the pet diff --git a/tests/generated/v2.0/petstore-swagger-io.ts b/tests/generated/v2.0/petstore-swagger-io.ts index 8afd7225..82567a20 100644 --- a/tests/generated/v2.0/petstore-swagger-io.ts +++ b/tests/generated/v2.0/petstore-swagger-io.ts @@ -97,6 +97,7 @@ interface HttpResponse extends R enum BodyType { Json, FormData, + UrlEncoded, } export class HttpClient { @@ -127,18 +128,21 @@ export class HttpClient { ); } - protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + protected toQueryString(rawQuery?: RequestQueryParamsType): string { const query = rawQuery || {}; const keys = Object.keys(query).filter((key) => "undefined" !== typeof query[key]); - return keys.length - ? `?${keys - .map((key) => - typeof query[key] === "object" && !Array.isArray(query[key]) - ? this.addQueryParams(query[key] as object).substring(1) - : this.addQueryParam(query, key), - ) - .join("&")}` - : ""; + return keys + .map((key) => + typeof query[key] === "object" && !Array.isArray(query[key]) + ? this.toQueryString(query[key] as object) + : this.addQueryParam(query, key), + ) + .join("&"); + } + + protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + const queryString = this.toQueryString(rawQuery); + return queryString ? `?${queryString}` : ""; } private bodyFormatters: Record any> = { @@ -148,6 +152,7 @@ export class HttpClient { data.append(key, input[key]); return data; }, new FormData()), + [BodyType.UrlEncoded]: (input: any) => this.toQueryString(input), }; private mergeRequestOptions(params: RequestParams, securityParams?: RequestParams): RequestParams { @@ -370,7 +375,8 @@ export class Api extends HttpClient { * @summary Place an order for a pet * @request POST:/store/order */ - placeOrder: (body: Order, params?: RequestParams) => this.request(`/store/order`, "POST", params, body), + placeOrder: (body: Order, params?: RequestParams) => + this.request(`/store/order`, "POST", params, body, BodyType.Json), }; user = { /** @@ -393,7 +399,7 @@ export class Api extends HttpClient { * @request PUT:/user/{username} */ updateUser: (username: string, body: User, params?: RequestParams) => - this.request(`/user/${username}`, "PUT", params, body), + this.request(`/user/${username}`, "PUT", params, body, BodyType.Json), /** * @description This can only be done by the logged in user. @@ -435,7 +441,8 @@ export class Api extends HttpClient { * @summary Create user * @request POST:/user */ - createUser: (body: User, params?: RequestParams) => this.request(`/user`, "POST", params, body), + createUser: (body: User, params?: RequestParams) => + this.request(`/user`, "POST", params, body, BodyType.Json), /** * No description @@ -446,7 +453,7 @@ export class Api extends HttpClient { * @request POST:/user/createWithArray */ createUsersWithArrayInput: (body: User[], params?: RequestParams) => - this.request(`/user/createWithArray`, "POST", params, body), + this.request(`/user/createWithArray`, "POST", params, body, BodyType.Json), /** * No description @@ -457,6 +464,6 @@ export class Api extends HttpClient { * @request POST:/user/createWithList */ createUsersWithListInput: (body: User[], params?: RequestParams) => - this.request(`/user/createWithList`, "POST", params, body), + this.request(`/user/createWithList`, "POST", params, body, BodyType.Json), }; } diff --git a/tests/generated/v2.0/petstore-with-external-docs.ts b/tests/generated/v2.0/petstore-with-external-docs.ts index c03a4397..e7b81457 100644 --- a/tests/generated/v2.0/petstore-with-external-docs.ts +++ b/tests/generated/v2.0/petstore-with-external-docs.ts @@ -42,6 +42,7 @@ interface HttpResponse extends R enum BodyType { Json, FormData, + UrlEncoded, } export class HttpClient { @@ -72,18 +73,21 @@ export class HttpClient { ); } - protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + protected toQueryString(rawQuery?: RequestQueryParamsType): string { const query = rawQuery || {}; const keys = Object.keys(query).filter((key) => "undefined" !== typeof query[key]); - return keys.length - ? `?${keys - .map((key) => - typeof query[key] === "object" && !Array.isArray(query[key]) - ? this.addQueryParams(query[key] as object).substring(1) - : this.addQueryParam(query, key), - ) - .join("&")}` - : ""; + return keys + .map((key) => + typeof query[key] === "object" && !Array.isArray(query[key]) + ? this.toQueryString(query[key] as object) + : this.addQueryParam(query, key), + ) + .join("&"); + } + + protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + const queryString = this.toQueryString(rawQuery); + return queryString ? `?${queryString}` : ""; } private bodyFormatters: Record any> = { @@ -93,6 +97,7 @@ export class HttpClient { data.append(key, input[key]); return data; }, new FormData()), + [BodyType.UrlEncoded]: (input: any) => this.toQueryString(input), }; private mergeRequestOptions(params: RequestParams, securityParams?: RequestParams): RequestParams { @@ -177,7 +182,8 @@ export class Api extends HttpClient { * @name AddPet * @request POST:/pets */ - addPet: (pet: NewPet, params?: RequestParams) => this.request(`/pets`, "POST", params, pet), + addPet: (pet: NewPet, params?: RequestParams) => + this.request(`/pets`, "POST", params, pet, BodyType.Json), /** * @description Returns a user based on a single ID, if the user does not have access to the pet diff --git a/tests/generated/v2.0/petstore.ts b/tests/generated/v2.0/petstore.ts index 1d0caa9f..cb707482 100644 --- a/tests/generated/v2.0/petstore.ts +++ b/tests/generated/v2.0/petstore.ts @@ -44,6 +44,7 @@ interface HttpResponse extends R enum BodyType { Json, FormData, + UrlEncoded, } export class HttpClient { @@ -74,18 +75,21 @@ export class HttpClient { ); } - protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + protected toQueryString(rawQuery?: RequestQueryParamsType): string { const query = rawQuery || {}; const keys = Object.keys(query).filter((key) => "undefined" !== typeof query[key]); - return keys.length - ? `?${keys - .map((key) => - typeof query[key] === "object" && !Array.isArray(query[key]) - ? this.addQueryParams(query[key] as object).substring(1) - : this.addQueryParam(query, key), - ) - .join("&")}` - : ""; + return keys + .map((key) => + typeof query[key] === "object" && !Array.isArray(query[key]) + ? this.toQueryString(query[key] as object) + : this.addQueryParam(query, key), + ) + .join("&"); + } + + protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + const queryString = this.toQueryString(rawQuery); + return queryString ? `?${queryString}` : ""; } private bodyFormatters: Record any> = { @@ -95,6 +99,7 @@ export class HttpClient { data.append(key, input[key]); return data; }, new FormData()), + [BodyType.UrlEncoded]: (input: any) => this.toQueryString(input), }; private mergeRequestOptions(params: RequestParams, securityParams?: RequestParams): RequestParams { diff --git a/tests/generated/v2.0/query-path-param.ts b/tests/generated/v2.0/query-path-param.ts index 271f0063..65e5e75a 100644 --- a/tests/generated/v2.0/query-path-param.ts +++ b/tests/generated/v2.0/query-path-param.ts @@ -29,6 +29,7 @@ interface HttpResponse extends R enum BodyType { Json, FormData, + UrlEncoded, } export class HttpClient { @@ -59,18 +60,21 @@ export class HttpClient { ); } - protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + protected toQueryString(rawQuery?: RequestQueryParamsType): string { const query = rawQuery || {}; const keys = Object.keys(query).filter((key) => "undefined" !== typeof query[key]); - return keys.length - ? `?${keys - .map((key) => - typeof query[key] === "object" && !Array.isArray(query[key]) - ? this.addQueryParams(query[key] as object).substring(1) - : this.addQueryParam(query, key), - ) - .join("&")}` - : ""; + return keys + .map((key) => + typeof query[key] === "object" && !Array.isArray(query[key]) + ? this.toQueryString(query[key] as object) + : this.addQueryParam(query, key), + ) + .join("&"); + } + + protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + const queryString = this.toQueryString(rawQuery); + return queryString ? `?${queryString}` : ""; } private bodyFormatters: Record any> = { @@ -80,6 +84,7 @@ export class HttpClient { data.append(key, input[key]); return data; }, new FormData()), + [BodyType.UrlEncoded]: (input: any) => this.toQueryString(input), }; private mergeRequestOptions(params: RequestParams, securityParams?: RequestParams): RequestParams { diff --git a/tests/generated/v2.0/uber.ts b/tests/generated/v2.0/uber.ts index 8eeea3b5..0f5195c2 100644 --- a/tests/generated/v2.0/uber.ts +++ b/tests/generated/v2.0/uber.ts @@ -124,6 +124,7 @@ interface HttpResponse extends R enum BodyType { Json, FormData, + UrlEncoded, } export class HttpClient { @@ -154,18 +155,21 @@ export class HttpClient { ); } - protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + protected toQueryString(rawQuery?: RequestQueryParamsType): string { const query = rawQuery || {}; const keys = Object.keys(query).filter((key) => "undefined" !== typeof query[key]); - return keys.length - ? `?${keys - .map((key) => - typeof query[key] === "object" && !Array.isArray(query[key]) - ? this.addQueryParams(query[key] as object).substring(1) - : this.addQueryParam(query, key), - ) - .join("&")}` - : ""; + return keys + .map((key) => + typeof query[key] === "object" && !Array.isArray(query[key]) + ? this.toQueryString(query[key] as object) + : this.addQueryParam(query, key), + ) + .join("&"); + } + + protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + const queryString = this.toQueryString(rawQuery); + return queryString ? `?${queryString}` : ""; } private bodyFormatters: Record any> = { @@ -175,6 +179,7 @@ export class HttpClient { data.append(key, input[key]); return data; }, new FormData()), + [BodyType.UrlEncoded]: (input: any) => this.toQueryString(input), }; private mergeRequestOptions(params: RequestParams, securityParams?: RequestParams): RequestParams { diff --git a/tests/generated/v3.0/additional-properties.ts b/tests/generated/v3.0/additional-properties.ts index 5bcabef4..eba55c68 100644 --- a/tests/generated/v3.0/additional-properties.ts +++ b/tests/generated/v3.0/additional-properties.ts @@ -36,6 +36,7 @@ interface HttpResponse extends R enum BodyType { Json, FormData, + UrlEncoded, } export class HttpClient { @@ -66,18 +67,21 @@ export class HttpClient { ); } - protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + protected toQueryString(rawQuery?: RequestQueryParamsType): string { const query = rawQuery || {}; const keys = Object.keys(query).filter((key) => "undefined" !== typeof query[key]); - return keys.length - ? `?${keys - .map((key) => - typeof query[key] === "object" && !Array.isArray(query[key]) - ? this.addQueryParams(query[key] as object).substring(1) - : this.addQueryParam(query, key), - ) - .join("&")}` - : ""; + return keys + .map((key) => + typeof query[key] === "object" && !Array.isArray(query[key]) + ? this.toQueryString(query[key] as object) + : this.addQueryParam(query, key), + ) + .join("&"); + } + + protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + const queryString = this.toQueryString(rawQuery); + return queryString ? `?${queryString}` : ""; } private bodyFormatters: Record any> = { @@ -87,6 +91,7 @@ export class HttpClient { data.append(key, input[key]); return data; }, new FormData()), + [BodyType.UrlEncoded]: (input: any) => this.toQueryString(input), }; private mergeRequestOptions(params: RequestParams, securityParams?: RequestParams): RequestParams { diff --git a/tests/generated/v3.0/additional-properties2.ts b/tests/generated/v3.0/additional-properties2.ts index c2c7ec84..8022f3e0 100644 --- a/tests/generated/v3.0/additional-properties2.ts +++ b/tests/generated/v3.0/additional-properties2.ts @@ -33,6 +33,7 @@ interface HttpResponse extends R enum BodyType { Json, FormData, + UrlEncoded, } export class HttpClient { @@ -63,18 +64,21 @@ export class HttpClient { ); } - protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + protected toQueryString(rawQuery?: RequestQueryParamsType): string { const query = rawQuery || {}; const keys = Object.keys(query).filter((key) => "undefined" !== typeof query[key]); - return keys.length - ? `?${keys - .map((key) => - typeof query[key] === "object" && !Array.isArray(query[key]) - ? this.addQueryParams(query[key] as object).substring(1) - : this.addQueryParam(query, key), - ) - .join("&")}` - : ""; + return keys + .map((key) => + typeof query[key] === "object" && !Array.isArray(query[key]) + ? this.toQueryString(query[key] as object) + : this.addQueryParam(query, key), + ) + .join("&"); + } + + protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + const queryString = this.toQueryString(rawQuery); + return queryString ? `?${queryString}` : ""; } private bodyFormatters: Record any> = { @@ -84,6 +88,7 @@ export class HttpClient { data.append(key, input[key]); return data; }, new FormData()), + [BodyType.UrlEncoded]: (input: any) => this.toQueryString(input), }; private mergeRequestOptions(params: RequestParams, securityParams?: RequestParams): RequestParams { diff --git a/tests/generated/v3.0/allof-example.ts b/tests/generated/v3.0/allof-example.ts index a08eb6cd..8a6aa15a 100644 --- a/tests/generated/v3.0/allof-example.ts +++ b/tests/generated/v3.0/allof-example.ts @@ -37,6 +37,7 @@ interface HttpResponse extends R enum BodyType { Json, FormData, + UrlEncoded, } export class HttpClient { @@ -67,18 +68,21 @@ export class HttpClient { ); } - protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + protected toQueryString(rawQuery?: RequestQueryParamsType): string { const query = rawQuery || {}; const keys = Object.keys(query).filter((key) => "undefined" !== typeof query[key]); - return keys.length - ? `?${keys - .map((key) => - typeof query[key] === "object" && !Array.isArray(query[key]) - ? this.addQueryParams(query[key] as object).substring(1) - : this.addQueryParam(query, key), - ) - .join("&")}` - : ""; + return keys + .map((key) => + typeof query[key] === "object" && !Array.isArray(query[key]) + ? this.toQueryString(query[key] as object) + : this.addQueryParam(query, key), + ) + .join("&"); + } + + protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + const queryString = this.toQueryString(rawQuery); + return queryString ? `?${queryString}` : ""; } private bodyFormatters: Record any> = { @@ -88,6 +92,7 @@ export class HttpClient { data.append(key, input[key]); return data; }, new FormData()), + [BodyType.UrlEncoded]: (input: any) => this.toQueryString(input), }; private mergeRequestOptions(params: RequestParams, securityParams?: RequestParams): RequestParams { @@ -162,6 +167,6 @@ export class Api extends HttpClient { * @request PATCH:/pets */ petsPartialUpdate: (data: Cat | Dog, params?: RequestParams) => - this.request(`/pets`, "PATCH", params, data), + this.request(`/pets`, "PATCH", params, data, BodyType.Json), }; } diff --git a/tests/generated/v3.0/anyof-example.ts b/tests/generated/v3.0/anyof-example.ts index 6f1d1dcf..ae02d08b 100644 --- a/tests/generated/v3.0/anyof-example.ts +++ b/tests/generated/v3.0/anyof-example.ts @@ -39,6 +39,7 @@ interface HttpResponse extends R enum BodyType { Json, FormData, + UrlEncoded, } export class HttpClient { @@ -69,18 +70,21 @@ export class HttpClient { ); } - protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + protected toQueryString(rawQuery?: RequestQueryParamsType): string { const query = rawQuery || {}; const keys = Object.keys(query).filter((key) => "undefined" !== typeof query[key]); - return keys.length - ? `?${keys - .map((key) => - typeof query[key] === "object" && !Array.isArray(query[key]) - ? this.addQueryParams(query[key] as object).substring(1) - : this.addQueryParam(query, key), - ) - .join("&")}` - : ""; + return keys + .map((key) => + typeof query[key] === "object" && !Array.isArray(query[key]) + ? this.toQueryString(query[key] as object) + : this.addQueryParam(query, key), + ) + .join("&"); + } + + protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + const queryString = this.toQueryString(rawQuery); + return queryString ? `?${queryString}` : ""; } private bodyFormatters: Record any> = { @@ -90,6 +94,7 @@ export class HttpClient { data.append(key, input[key]); return data; }, new FormData()), + [BodyType.UrlEncoded]: (input: any) => this.toQueryString(input), }; private mergeRequestOptions(params: RequestParams, securityParams?: RequestParams): RequestParams { @@ -164,6 +169,6 @@ export class Api extends HttpClient { * @request PATCH:/pets */ petsPartialUpdate: (data: PetByAge | PetByType | (PetByAge & PetByType), params?: RequestParams) => - this.request(`/pets`, "PATCH", params, data), + this.request(`/pets`, "PATCH", params, data, BodyType.Json), }; } diff --git a/tests/generated/v3.0/api-with-examples.ts b/tests/generated/v3.0/api-with-examples.ts index 0507e92f..1528034f 100644 --- a/tests/generated/v3.0/api-with-examples.ts +++ b/tests/generated/v3.0/api-with-examples.ts @@ -29,6 +29,7 @@ interface HttpResponse extends R enum BodyType { Json, FormData, + UrlEncoded, } export class HttpClient { @@ -59,18 +60,21 @@ export class HttpClient { ); } - protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + protected toQueryString(rawQuery?: RequestQueryParamsType): string { const query = rawQuery || {}; const keys = Object.keys(query).filter((key) => "undefined" !== typeof query[key]); - return keys.length - ? `?${keys - .map((key) => - typeof query[key] === "object" && !Array.isArray(query[key]) - ? this.addQueryParams(query[key] as object).substring(1) - : this.addQueryParam(query, key), - ) - .join("&")}` - : ""; + return keys + .map((key) => + typeof query[key] === "object" && !Array.isArray(query[key]) + ? this.toQueryString(query[key] as object) + : this.addQueryParam(query, key), + ) + .join("&"); + } + + protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + const queryString = this.toQueryString(rawQuery); + return queryString ? `?${queryString}` : ""; } private bodyFormatters: Record any> = { @@ -80,6 +84,7 @@ export class HttpClient { data.append(key, input[key]); return data; }, new FormData()), + [BodyType.UrlEncoded]: (input: any) => this.toQueryString(input), }; private mergeRequestOptions(params: RequestParams, securityParams?: RequestParams): RequestParams { diff --git a/tests/generated/v3.0/callback-example.ts b/tests/generated/v3.0/callback-example.ts index 777c6ab9..563a66c4 100644 --- a/tests/generated/v3.0/callback-example.ts +++ b/tests/generated/v3.0/callback-example.ts @@ -29,6 +29,7 @@ interface HttpResponse extends R enum BodyType { Json, FormData, + UrlEncoded, } export class HttpClient { @@ -59,18 +60,21 @@ export class HttpClient { ); } - protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + protected toQueryString(rawQuery?: RequestQueryParamsType): string { const query = rawQuery || {}; const keys = Object.keys(query).filter((key) => "undefined" !== typeof query[key]); - return keys.length - ? `?${keys - .map((key) => - typeof query[key] === "object" && !Array.isArray(query[key]) - ? this.addQueryParams(query[key] as object).substring(1) - : this.addQueryParam(query, key), - ) - .join("&")}` - : ""; + return keys + .map((key) => + typeof query[key] === "object" && !Array.isArray(query[key]) + ? this.toQueryString(query[key] as object) + : this.addQueryParam(query, key), + ) + .join("&"); + } + + protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + const queryString = this.toQueryString(rawQuery); + return queryString ? `?${queryString}` : ""; } private bodyFormatters: Record any> = { @@ -80,6 +84,7 @@ export class HttpClient { data.append(key, input[key]); return data; }, new FormData()), + [BodyType.UrlEncoded]: (input: any) => this.toQueryString(input), }; private mergeRequestOptions(params: RequestParams, securityParams?: RequestParams): RequestParams { diff --git a/tests/generated/v3.0/components-responses.ts b/tests/generated/v3.0/components-responses.ts index 72b1a19e..8f86b14d 100644 --- a/tests/generated/v3.0/components-responses.ts +++ b/tests/generated/v3.0/components-responses.ts @@ -29,6 +29,7 @@ interface HttpResponse extends R enum BodyType { Json, FormData, + UrlEncoded, } export class HttpClient { @@ -59,18 +60,21 @@ export class HttpClient { ); } - protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + protected toQueryString(rawQuery?: RequestQueryParamsType): string { const query = rawQuery || {}; const keys = Object.keys(query).filter((key) => "undefined" !== typeof query[key]); - return keys.length - ? `?${keys - .map((key) => - typeof query[key] === "object" && !Array.isArray(query[key]) - ? this.addQueryParams(query[key] as object).substring(1) - : this.addQueryParam(query, key), - ) - .join("&")}` - : ""; + return keys + .map((key) => + typeof query[key] === "object" && !Array.isArray(query[key]) + ? this.toQueryString(query[key] as object) + : this.addQueryParam(query, key), + ) + .join("&"); + } + + protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + const queryString = this.toQueryString(rawQuery); + return queryString ? `?${queryString}` : ""; } private bodyFormatters: Record any> = { @@ -80,6 +84,7 @@ export class HttpClient { data.append(key, input[key]); return data; }, new FormData()), + [BodyType.UrlEncoded]: (input: any) => this.toQueryString(input), }; private mergeRequestOptions(params: RequestParams, securityParams?: RequestParams): RequestParams { 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 6d4798e7..cfd020cd 100644 --- a/tests/generated/v3.0/explode-param-3.0.1.ts +++ b/tests/generated/v3.0/explode-param-3.0.1.ts @@ -45,6 +45,7 @@ interface HttpResponse extends R enum BodyType { Json, FormData, + UrlEncoded, } export class HttpClient { @@ -75,18 +76,21 @@ export class HttpClient { ); } - protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + protected toQueryString(rawQuery?: RequestQueryParamsType): string { const query = rawQuery || {}; const keys = Object.keys(query).filter((key) => "undefined" !== typeof query[key]); - return keys.length - ? `?${keys - .map((key) => - typeof query[key] === "object" && !Array.isArray(query[key]) - ? this.addQueryParams(query[key] as object).substring(1) - : this.addQueryParam(query, key), - ) - .join("&")}` - : ""; + return keys + .map((key) => + typeof query[key] === "object" && !Array.isArray(query[key]) + ? this.toQueryString(query[key] as object) + : this.addQueryParam(query, key), + ) + .join("&"); + } + + protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + const queryString = this.toQueryString(rawQuery); + return queryString ? `?${queryString}` : ""; } private bodyFormatters: Record any> = { @@ -96,6 +100,7 @@ export class HttpClient { data.append(key, input[key]); return data; }, new FormData()), + [BodyType.UrlEncoded]: (input: any) => this.toQueryString(input), }; private mergeRequestOptions(params: RequestParams, securityParams?: RequestParams): RequestParams { diff --git a/tests/generated/v3.0/link-example.ts b/tests/generated/v3.0/link-example.ts index 81772b50..afa01173 100644 --- a/tests/generated/v3.0/link-example.ts +++ b/tests/generated/v3.0/link-example.ts @@ -46,6 +46,7 @@ interface HttpResponse extends R enum BodyType { Json, FormData, + UrlEncoded, } export class HttpClient { @@ -76,18 +77,21 @@ export class HttpClient { ); } - protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + protected toQueryString(rawQuery?: RequestQueryParamsType): string { const query = rawQuery || {}; const keys = Object.keys(query).filter((key) => "undefined" !== typeof query[key]); - return keys.length - ? `?${keys - .map((key) => - typeof query[key] === "object" && !Array.isArray(query[key]) - ? this.addQueryParams(query[key] as object).substring(1) - : this.addQueryParam(query, key), - ) - .join("&")}` - : ""; + return keys + .map((key) => + typeof query[key] === "object" && !Array.isArray(query[key]) + ? this.toQueryString(query[key] as object) + : this.addQueryParam(query, key), + ) + .join("&"); + } + + protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + const queryString = this.toQueryString(rawQuery); + return queryString ? `?${queryString}` : ""; } private bodyFormatters: Record any> = { @@ -97,6 +101,7 @@ export class HttpClient { data.append(key, input[key]); return data; }, new FormData()), + [BodyType.UrlEncoded]: (input: any) => this.toQueryString(input), }; private mergeRequestOptions(params: RequestParams, securityParams?: RequestParams): RequestParams { diff --git a/tests/generated/v3.0/no-definitions-schema.ts b/tests/generated/v3.0/no-definitions-schema.ts index d5281fdd..aedac87a 100644 --- a/tests/generated/v3.0/no-definitions-schema.ts +++ b/tests/generated/v3.0/no-definitions-schema.ts @@ -41,6 +41,7 @@ interface HttpResponse extends R enum BodyType { Json, FormData, + UrlEncoded, } export class HttpClient { @@ -71,18 +72,21 @@ export class HttpClient { ); } - protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + protected toQueryString(rawQuery?: RequestQueryParamsType): string { const query = rawQuery || {}; const keys = Object.keys(query).filter((key) => "undefined" !== typeof query[key]); - return keys.length - ? `?${keys - .map((key) => - typeof query[key] === "object" && !Array.isArray(query[key]) - ? this.addQueryParams(query[key] as object).substring(1) - : this.addQueryParam(query, key), - ) - .join("&")}` - : ""; + return keys + .map((key) => + typeof query[key] === "object" && !Array.isArray(query[key]) + ? this.toQueryString(query[key] as object) + : this.addQueryParam(query, key), + ) + .join("&"); + } + + protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + const queryString = this.toQueryString(rawQuery); + return queryString ? `?${queryString}` : ""; } private bodyFormatters: Record any> = { @@ -92,6 +96,7 @@ export class HttpClient { data.append(key, input[key]); return data; }, new FormData()), + [BodyType.UrlEncoded]: (input: any) => this.toQueryString(input), }; private mergeRequestOptions(params: RequestParams, securityParams?: RequestParams): RequestParams { diff --git a/tests/generated/v3.0/nullable-refs.ts b/tests/generated/v3.0/nullable-refs.ts index 365ed5ce..9fc8eacb 100644 --- a/tests/generated/v3.0/nullable-refs.ts +++ b/tests/generated/v3.0/nullable-refs.ts @@ -43,6 +43,7 @@ interface HttpResponse extends R enum BodyType { Json, FormData, + UrlEncoded, } export class HttpClient { @@ -73,18 +74,21 @@ export class HttpClient { ); } - protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + protected toQueryString(rawQuery?: RequestQueryParamsType): string { const query = rawQuery || {}; const keys = Object.keys(query).filter((key) => "undefined" !== typeof query[key]); - return keys.length - ? `?${keys - .map((key) => - typeof query[key] === "object" && !Array.isArray(query[key]) - ? this.addQueryParams(query[key] as object).substring(1) - : this.addQueryParam(query, key), - ) - .join("&")}` - : ""; + return keys + .map((key) => + typeof query[key] === "object" && !Array.isArray(query[key]) + ? this.toQueryString(query[key] as object) + : this.addQueryParam(query, key), + ) + .join("&"); + } + + protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + const queryString = this.toQueryString(rawQuery); + return queryString ? `?${queryString}` : ""; } private bodyFormatters: Record any> = { @@ -94,6 +98,7 @@ export class HttpClient { data.append(key, input[key]); return data; }, new FormData()), + [BodyType.UrlEncoded]: (input: any) => this.toQueryString(input), }; private mergeRequestOptions(params: RequestParams, securityParams?: RequestParams): RequestParams { diff --git a/tests/generated/v3.0/oneof-example.ts b/tests/generated/v3.0/oneof-example.ts index ceed1f3d..6876f403 100644 --- a/tests/generated/v3.0/oneof-example.ts +++ b/tests/generated/v3.0/oneof-example.ts @@ -39,6 +39,7 @@ interface HttpResponse extends R enum BodyType { Json, FormData, + UrlEncoded, } export class HttpClient { @@ -69,18 +70,21 @@ export class HttpClient { ); } - protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + protected toQueryString(rawQuery?: RequestQueryParamsType): string { const query = rawQuery || {}; const keys = Object.keys(query).filter((key) => "undefined" !== typeof query[key]); - return keys.length - ? `?${keys - .map((key) => - typeof query[key] === "object" && !Array.isArray(query[key]) - ? this.addQueryParams(query[key] as object).substring(1) - : this.addQueryParam(query, key), - ) - .join("&")}` - : ""; + return keys + .map((key) => + typeof query[key] === "object" && !Array.isArray(query[key]) + ? this.toQueryString(query[key] as object) + : this.addQueryParam(query, key), + ) + .join("&"); + } + + protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + const queryString = this.toQueryString(rawQuery); + return queryString ? `?${queryString}` : ""; } private bodyFormatters: Record any> = { @@ -90,6 +94,7 @@ export class HttpClient { data.append(key, input[key]); return data; }, new FormData()), + [BodyType.UrlEncoded]: (input: any) => this.toQueryString(input), }; private mergeRequestOptions(params: RequestParams, securityParams?: RequestParams): RequestParams { @@ -164,6 +169,6 @@ export class Api extends HttpClient { * @request PATCH:/pets */ petsPartialUpdate: (data: Cat | Dog, params?: RequestParams) => - this.request(`/pets`, "PATCH", params, data), + this.request(`/pets`, "PATCH", params, data, BodyType.Json), }; } diff --git a/tests/generated/v3.0/personal-api-example.ts b/tests/generated/v3.0/personal-api-example.ts index dd913c86..8ed3c554 100644 --- a/tests/generated/v3.0/personal-api-example.ts +++ b/tests/generated/v3.0/personal-api-example.ts @@ -230,6 +230,7 @@ interface HttpResponse extends R enum BodyType { Json, FormData, + UrlEncoded, } export class HttpClient { @@ -260,18 +261,21 @@ export class HttpClient { ); } - protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + protected toQueryString(rawQuery?: RequestQueryParamsType): string { const query = rawQuery || {}; const keys = Object.keys(query).filter((key) => "undefined" !== typeof query[key]); - return keys.length - ? `?${keys - .map((key) => - typeof query[key] === "object" && !Array.isArray(query[key]) - ? this.addQueryParams(query[key] as object).substring(1) - : this.addQueryParam(query, key), - ) - .join("&")}` - : ""; + return keys + .map((key) => + typeof query[key] === "object" && !Array.isArray(query[key]) + ? this.toQueryString(query[key] as object) + : this.addQueryParam(query, key), + ) + .join("&"); + } + + protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + const queryString = this.toQueryString(rawQuery); + return queryString ? `?${queryString}` : ""; } private bodyFormatters: Record any> = { @@ -281,6 +285,7 @@ export class HttpClient { data.append(key, input[key]); return data; }, new FormData()), + [BodyType.UrlEncoded]: (input: any) => this.toQueryString(input), }; private mergeRequestOptions(params: RequestParams, securityParams?: RequestParams): RequestParams { @@ -355,7 +360,8 @@ export class Api extends HttpClient { * @name Login * @request POST:/auth */ - login: (data: AuthUserType, params?: RequestParams) => this.request(`/auth`, "POST", params, data), + login: (data: AuthUserType, params?: RequestParams) => + this.request(`/auth`, "POST", params, data, BodyType.Json), /** * No description diff --git a/tests/generated/v3.0/petstore-expanded.ts b/tests/generated/v3.0/petstore-expanded.ts index cd917e0b..aa9a7b35 100644 --- a/tests/generated/v3.0/petstore-expanded.ts +++ b/tests/generated/v3.0/petstore-expanded.ts @@ -42,6 +42,7 @@ interface HttpResponse extends R enum BodyType { Json, FormData, + UrlEncoded, } export class HttpClient { @@ -72,18 +73,21 @@ export class HttpClient { ); } - protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + protected toQueryString(rawQuery?: RequestQueryParamsType): string { const query = rawQuery || {}; const keys = Object.keys(query).filter((key) => "undefined" !== typeof query[key]); - return keys.length - ? `?${keys - .map((key) => - typeof query[key] === "object" && !Array.isArray(query[key]) - ? this.addQueryParams(query[key] as object).substring(1) - : this.addQueryParam(query, key), - ) - .join("&")}` - : ""; + return keys + .map((key) => + typeof query[key] === "object" && !Array.isArray(query[key]) + ? this.toQueryString(query[key] as object) + : this.addQueryParam(query, key), + ) + .join("&"); + } + + protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + const queryString = this.toQueryString(rawQuery); + return queryString ? `?${queryString}` : ""; } private bodyFormatters: Record any> = { @@ -93,6 +97,7 @@ export class HttpClient { data.append(key, input[key]); return data; }, new FormData()), + [BodyType.UrlEncoded]: (input: any) => this.toQueryString(input), }; private mergeRequestOptions(params: RequestParams, securityParams?: RequestParams): RequestParams { @@ -177,7 +182,8 @@ export class Api extends HttpClient { * @name AddPet * @request POST:/pets */ - addPet: (data: NewPet, params?: RequestParams) => this.request(`/pets`, "POST", params, data), + addPet: (data: NewPet, params?: RequestParams) => + this.request(`/pets`, "POST", params, data, BodyType.Json), /** * @description Returns a user based on a single ID, if the user does not have access to the pet diff --git a/tests/generated/v3.0/petstore.ts b/tests/generated/v3.0/petstore.ts index 26fe5248..32ad65b2 100644 --- a/tests/generated/v3.0/petstore.ts +++ b/tests/generated/v3.0/petstore.ts @@ -46,6 +46,7 @@ interface HttpResponse extends R enum BodyType { Json, FormData, + UrlEncoded, } export class HttpClient { @@ -76,18 +77,21 @@ export class HttpClient { ); } - protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + protected toQueryString(rawQuery?: RequestQueryParamsType): string { const query = rawQuery || {}; const keys = Object.keys(query).filter((key) => "undefined" !== typeof query[key]); - return keys.length - ? `?${keys - .map((key) => - typeof query[key] === "object" && !Array.isArray(query[key]) - ? this.addQueryParams(query[key] as object).substring(1) - : this.addQueryParam(query, key), - ) - .join("&")}` - : ""; + return keys + .map((key) => + typeof query[key] === "object" && !Array.isArray(query[key]) + ? this.toQueryString(query[key] as object) + : this.addQueryParam(query, key), + ) + .join("&"); + } + + protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + const queryString = this.toQueryString(rawQuery); + return queryString ? `?${queryString}` : ""; } private bodyFormatters: Record any> = { @@ -97,6 +101,7 @@ export class HttpClient { data.append(key, input[key]); return data; }, new FormData()), + [BodyType.UrlEncoded]: (input: any) => this.toQueryString(input), }; private mergeRequestOptions(params: RequestParams, securityParams?: RequestParams): RequestParams { diff --git a/tests/generated/v3.0/responses.ts b/tests/generated/v3.0/responses.ts index 72b1a19e..8f86b14d 100644 --- a/tests/generated/v3.0/responses.ts +++ b/tests/generated/v3.0/responses.ts @@ -29,6 +29,7 @@ interface HttpResponse extends R enum BodyType { Json, FormData, + UrlEncoded, } export class HttpClient { @@ -59,18 +60,21 @@ export class HttpClient { ); } - protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + protected toQueryString(rawQuery?: RequestQueryParamsType): string { const query = rawQuery || {}; const keys = Object.keys(query).filter((key) => "undefined" !== typeof query[key]); - return keys.length - ? `?${keys - .map((key) => - typeof query[key] === "object" && !Array.isArray(query[key]) - ? this.addQueryParams(query[key] as object).substring(1) - : this.addQueryParam(query, key), - ) - .join("&")}` - : ""; + return keys + .map((key) => + typeof query[key] === "object" && !Array.isArray(query[key]) + ? this.toQueryString(query[key] as object) + : this.addQueryParam(query, key), + ) + .join("&"); + } + + protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + const queryString = this.toQueryString(rawQuery); + return queryString ? `?${queryString}` : ""; } private bodyFormatters: Record any> = { @@ -80,6 +84,7 @@ export class HttpClient { data.append(key, input[key]); return data; }, new FormData()), + [BodyType.UrlEncoded]: (input: any) => this.toQueryString(input), }; private mergeRequestOptions(params: RequestParams, securityParams?: RequestParams): RequestParams { diff --git a/tests/generated/v3.0/swaggerhub-template.ts b/tests/generated/v3.0/swaggerhub-template.ts index 7accc4f5..dbd89739 100644 --- a/tests/generated/v3.0/swaggerhub-template.ts +++ b/tests/generated/v3.0/swaggerhub-template.ts @@ -29,6 +29,7 @@ interface HttpResponse extends R enum BodyType { Json, FormData, + UrlEncoded, } export class HttpClient { @@ -59,18 +60,21 @@ export class HttpClient { ); } - protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + protected toQueryString(rawQuery?: RequestQueryParamsType): string { const query = rawQuery || {}; const keys = Object.keys(query).filter((key) => "undefined" !== typeof query[key]); - return keys.length - ? `?${keys - .map((key) => - typeof query[key] === "object" && !Array.isArray(query[key]) - ? this.addQueryParams(query[key] as object).substring(1) - : this.addQueryParam(query, key), - ) - .join("&")}` - : ""; + return keys + .map((key) => + typeof query[key] === "object" && !Array.isArray(query[key]) + ? this.toQueryString(query[key] as object) + : this.addQueryParam(query, key), + ) + .join("&"); + } + + protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + const queryString = this.toQueryString(rawQuery); + return queryString ? `?${queryString}` : ""; } private bodyFormatters: Record any> = { @@ -80,6 +84,7 @@ export class HttpClient { data.append(key, input[key]); return data; }, new FormData()), + [BodyType.UrlEncoded]: (input: any) => this.toQueryString(input), }; private mergeRequestOptions(params: RequestParams, securityParams?: RequestParams): RequestParams { 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 c11de3b2..3072904e 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 @@ -150,6 +150,7 @@ interface HttpResponse extends R enum BodyType { Json, FormData, + UrlEncoded, } export class HttpClient { @@ -180,18 +181,21 @@ export class HttpClient { ); } - protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + protected toQueryString(rawQuery?: RequestQueryParamsType): string { const query = rawQuery || {}; const keys = Object.keys(query).filter((key) => "undefined" !== typeof query[key]); - return keys.length - ? `?${keys - .map((key) => - typeof query[key] === "object" && !Array.isArray(query[key]) - ? this.addQueryParams(query[key] as object).substring(1) - : this.addQueryParam(query, key), - ) - .join("&")}` - : ""; + return keys + .map((key) => + typeof query[key] === "object" && !Array.isArray(query[key]) + ? this.toQueryString(query[key] as object) + : this.addQueryParam(query, key), + ) + .join("&"); + } + + protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + const queryString = this.toQueryString(rawQuery); + return queryString ? `?${queryString}` : ""; } private bodyFormatters: Record any> = { @@ -201,6 +205,7 @@ export class HttpClient { data.append(key, input[key]); return data; }, new FormData()), + [BodyType.UrlEncoded]: (input: any) => this.toQueryString(input), }; private mergeRequestOptions(params: RequestParams, securityParams?: RequestParams): RequestParams { @@ -275,7 +280,8 @@ export class Api extends HttpClient { * @name Login * @request POST:/auth */ - login: (data?: AuthUser, params?: RequestParams) => this.request(`/auth`, "POST", params, data), + login: (data?: AuthUser, params?: RequestParams) => + this.request(`/auth`, "POST", params, data, BodyType.Json), /** * No description diff --git a/tests/generated/v3.0/uspto.ts b/tests/generated/v3.0/uspto.ts index 5cecb87a..132f25e6 100644 --- a/tests/generated/v3.0/uspto.ts +++ b/tests/generated/v3.0/uspto.ts @@ -46,6 +46,7 @@ interface HttpResponse extends R enum BodyType { Json, FormData, + UrlEncoded, } export class HttpClient { @@ -76,18 +77,21 @@ export class HttpClient { ); } - protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + protected toQueryString(rawQuery?: RequestQueryParamsType): string { const query = rawQuery || {}; const keys = Object.keys(query).filter((key) => "undefined" !== typeof query[key]); - return keys.length - ? `?${keys - .map((key) => - typeof query[key] === "object" && !Array.isArray(query[key]) - ? this.addQueryParams(query[key] as object).substring(1) - : this.addQueryParam(query, key), - ) - .join("&")}` - : ""; + return keys + .map((key) => + typeof query[key] === "object" && !Array.isArray(query[key]) + ? this.toQueryString(query[key] as object) + : this.addQueryParam(query, key), + ) + .join("&"); + } + + protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + const queryString = this.toQueryString(rawQuery); + return queryString ? `?${queryString}` : ""; } private bodyFormatters: Record any> = { @@ -97,6 +101,7 @@ export class HttpClient { data.append(key, input[key]); return data; }, new FormData()), + [BodyType.UrlEncoded]: (input: any) => this.toQueryString(input), }; private mergeRequestOptions(params: RequestParams, securityParams?: RequestParams): RequestParams { @@ -198,13 +203,15 @@ export class Api extends HttpClient { performSearch: ( version: string, dataset: string, - query: { criteria: string; start?: number; rows?: number }, + data: { criteria: string; start?: number; rows?: number }, params?: RequestParams, ) => this.request[], any>( - `/${dataset}/${version}/records${this.addQueryParams(query)}`, + `/${dataset}/${version}/records`, "POST", params, + data, + BodyType.UrlEncoded, ), }; } diff --git a/tests/generated/v3.0/wrong-enum-subtypes.ts b/tests/generated/v3.0/wrong-enum-subtypes.ts index 45874107..11fc352a 100644 --- a/tests/generated/v3.0/wrong-enum-subtypes.ts +++ b/tests/generated/v3.0/wrong-enum-subtypes.ts @@ -31,6 +31,7 @@ interface HttpResponse extends R enum BodyType { Json, FormData, + UrlEncoded, } export class HttpClient { @@ -61,18 +62,21 @@ export class HttpClient { ); } - protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + protected toQueryString(rawQuery?: RequestQueryParamsType): string { const query = rawQuery || {}; const keys = Object.keys(query).filter((key) => "undefined" !== typeof query[key]); - return keys.length - ? `?${keys - .map((key) => - typeof query[key] === "object" && !Array.isArray(query[key]) - ? this.addQueryParams(query[key] as object).substring(1) - : this.addQueryParam(query, key), - ) - .join("&")}` - : ""; + return keys + .map((key) => + typeof query[key] === "object" && !Array.isArray(query[key]) + ? this.toQueryString(query[key] as object) + : this.addQueryParam(query, key), + ) + .join("&"); + } + + protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + const queryString = this.toQueryString(rawQuery); + return queryString ? `?${queryString}` : ""; } private bodyFormatters: Record any> = { @@ -82,6 +86,7 @@ export class HttpClient { data.append(key, input[key]); return data; }, new FormData()), + [BodyType.UrlEncoded]: (input: any) => this.toQueryString(input), }; private mergeRequestOptions(params: RequestParams, securityParams?: RequestParams): RequestParams { diff --git a/tests/schemas/v2.0/another-example.json b/tests/schemas/v2.0/another-example.json index 1c94d0a8..913efd44 100644 --- a/tests/schemas/v2.0/another-example.json +++ b/tests/schemas/v2.0/another-example.json @@ -229,6 +229,44 @@ "tags": ["pet"] } }, + "/pet/end-form-url-encoded": { + "post": { + "responses": { + "500": { + "description": "500" + }, + "400": { + "description": "400" + }, + "401": { + "description": "401" + }, + "200": { + "description": "200" + } + }, + "summary": "summary", + "operationId": "formUrlEncodedRequest", + "parameters": [ + { + "name": "param1", + "in": "formData", + "type": "string", + "format": "string", + "required": true + }, + { + "name": "param2", + "in": "formData", + "type": "string", + "required": true + } + ], + "security": [], + "consumes": ["multipart/form-data", "application/x-www-form-urlencoded"], + "tags": ["pet"] + } + }, "/pet/findByTags": { "get": { "tags": ["pet"], diff --git a/tests/spec/defaultAsSuccess/schema.ts b/tests/spec/defaultAsSuccess/schema.ts index 8a5320ca..0c4d035b 100644 --- a/tests/spec/defaultAsSuccess/schema.ts +++ b/tests/spec/defaultAsSuccess/schema.ts @@ -81,6 +81,7 @@ interface HttpResponse extends R enum BodyType { Json, FormData, + UrlEncoded, } export class HttpClient { @@ -111,18 +112,21 @@ export class HttpClient { ); } - protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + protected toQueryString(rawQuery?: RequestQueryParamsType): string { const query = rawQuery || {}; const keys = Object.keys(query).filter((key) => "undefined" !== typeof query[key]); - return keys.length - ? `?${keys - .map((key) => - typeof query[key] === "object" && !Array.isArray(query[key]) - ? this.addQueryParams(query[key] as object).substring(1) - : this.addQueryParam(query, key), - ) - .join("&")}` - : ""; + return keys + .map((key) => + typeof query[key] === "object" && !Array.isArray(query[key]) + ? this.toQueryString(query[key] as object) + : this.addQueryParam(query, key), + ) + .join("&"); + } + + protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + const queryString = this.toQueryString(rawQuery); + return queryString ? `?${queryString}` : ""; } private bodyFormatters: Record any> = { @@ -132,6 +136,7 @@ export class HttpClient { data.append(key, input[key]); return data; }, new FormData()), + [BodyType.UrlEncoded]: (input: any) => this.toQueryString(input), }; private mergeRequestOptions(params: RequestParams, securityParams?: RequestParams): RequestParams { @@ -335,7 +340,7 @@ export class Api extends HttpClient { * @request POST:/scope/{job} */ signConfirm: (job: string, params?: RequestParams) => - this.request<{ status?: string }, Error>(`/scope/${job}`, "POST", params), + this.request<{ status?: string }, Error>(`/scope/${job}`, "POST", params, null, BodyType.Json), /** * @description authority updates a JWT with its signature See: https://github.com/skion/authentiq/wiki/JWT-Examples diff --git a/tests/spec/enumNamesAsValues/schema.ts b/tests/spec/enumNamesAsValues/schema.ts index 1ca0b8d7..a660c0be 100644 --- a/tests/spec/enumNamesAsValues/schema.ts +++ b/tests/spec/enumNamesAsValues/schema.ts @@ -227,6 +227,7 @@ interface HttpResponse extends R enum BodyType { Json, FormData, + UrlEncoded, } export class HttpClient { @@ -257,18 +258,21 @@ export class HttpClient { ); } - protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + protected toQueryString(rawQuery?: RequestQueryParamsType): string { const query = rawQuery || {}; const keys = Object.keys(query).filter((key) => "undefined" !== typeof query[key]); - return keys.length - ? `?${keys - .map((key) => - typeof query[key] === "object" && !Array.isArray(query[key]) - ? this.addQueryParams(query[key] as object).substring(1) - : this.addQueryParam(query, key), - ) - .join("&")}` - : ""; + return keys + .map((key) => + typeof query[key] === "object" && !Array.isArray(query[key]) + ? this.toQueryString(query[key] as object) + : this.addQueryParam(query, key), + ) + .join("&"); + } + + protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + const queryString = this.toQueryString(rawQuery); + return queryString ? `?${queryString}` : ""; } private bodyFormatters: Record any> = { @@ -278,6 +282,7 @@ export class HttpClient { data.append(key, input[key]); return data; }, new FormData()), + [BodyType.UrlEncoded]: (input: any) => this.toQueryString(input), }; private mergeRequestOptions(params: RequestParams, securityParams?: RequestParams): RequestParams { @@ -352,7 +357,8 @@ export class Api extends HttpClient { * @name Login * @request POST:/auth */ - login: (data: AuthUserType, params?: RequestParams) => this.request(`/auth`, "POST", params, data), + login: (data: AuthUserType, params?: RequestParams) => + this.request(`/auth`, "POST", params, data, BodyType.Json), /** * No description diff --git a/tests/spec/extractRequestParams/schema.ts b/tests/spec/extractRequestParams/schema.ts index d43eb544..2c23f570 100644 --- a/tests/spec/extractRequestParams/schema.ts +++ b/tests/spec/extractRequestParams/schema.ts @@ -110,6 +110,7 @@ interface HttpResponse extends R enum BodyType { Json, FormData, + UrlEncoded, } export class HttpClient { @@ -140,18 +141,21 @@ export class HttpClient { ); } - protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + protected toQueryString(rawQuery?: RequestQueryParamsType): string { const query = rawQuery || {}; const keys = Object.keys(query).filter((key) => "undefined" !== typeof query[key]); - return keys.length - ? `?${keys - .map((key) => - typeof query[key] === "object" && !Array.isArray(query[key]) - ? this.addQueryParams(query[key] as object).substring(1) - : this.addQueryParam(query, key), - ) - .join("&")}` - : ""; + return keys + .map((key) => + typeof query[key] === "object" && !Array.isArray(query[key]) + ? this.toQueryString(query[key] as object) + : this.addQueryParam(query, key), + ) + .join("&"); + } + + protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + const queryString = this.toQueryString(rawQuery); + return queryString ? `?${queryString}` : ""; } private bodyFormatters: Record any> = { @@ -161,6 +165,7 @@ export class HttpClient { data.append(key, input[key]); return data; }, new FormData()), + [BodyType.UrlEncoded]: (input: any) => this.toQueryString(input), }; private mergeRequestOptions(params: RequestParams, securityParams?: RequestParams): RequestParams { @@ -364,7 +369,7 @@ export class Api extends HttpClient { * @request POST:/scope/{job} */ signConfirm: (job: string, params?: RequestParams) => - this.request<{ status?: string }, Error>(`/scope/${job}`, "POST", params), + this.request<{ status?: string }, Error>(`/scope/${job}`, "POST", params, null, BodyType.Json), /** * @description authority updates a JWT with its signature See: https://github.com/skion/authentiq/wiki/JWT-Examples diff --git a/tests/spec/js/schema.d.ts b/tests/spec/js/schema.d.ts index 2aba1d4e..5f3e0767 100644 --- a/tests/spec/js/schema.d.ts +++ b/tests/spec/js/schema.d.ts @@ -1710,6 +1710,7 @@ interface HttpResponse extends R declare enum BodyType { Json = 0, FormData = 1, + UrlEncoded = 2, } export declare class HttpClient { baseUrl: string; @@ -1719,6 +1720,7 @@ export declare class HttpClient { constructor(apiConfig?: ApiConfig); setSecurityData: (data: SecurityDataType) => void; private addQueryParam; + protected toQueryString(rawQuery?: RequestQueryParamsType): string; protected addQueryParams(rawQuery?: RequestQueryParamsType): string; private bodyFormatters; private mergeRequestOptions; diff --git a/tests/spec/js/schema.js b/tests/spec/js/schema.js index 53c41456..664976ee 100644 --- a/tests/spec/js/schema.js +++ b/tests/spec/js/schema.js @@ -13,6 +13,7 @@ var BodyType; (function (BodyType) { BodyType[(BodyType["Json"] = 0)] = "Json"; BodyType[(BodyType["FormData"] = 1)] = "FormData"; + BodyType[(BodyType["UrlEncoded"] = 2)] = "UrlEncoded"; })(BodyType || (BodyType = {})); export class HttpClient { constructor(apiConfig = {}) { @@ -37,6 +38,7 @@ export class HttpClient { data.append(key, input[key]); return data; }, new FormData()), + [BodyType.UrlEncoded]: (input) => this.toQueryString(input), }; this.safeParseResponse = (response) => { const r = response; @@ -79,18 +81,20 @@ export class HttpClient { encodeURIComponent(key) + "=" + encodeURIComponent(Array.isArray(query[key]) ? query[key].join(",") : query[key]) ); } - addQueryParams(rawQuery) { + toQueryString(rawQuery) { const query = rawQuery || {}; const keys = Object.keys(query).filter((key) => "undefined" !== typeof query[key]); - return keys.length - ? `?${keys - .map((key) => - typeof query[key] === "object" && !Array.isArray(query[key]) - ? this.addQueryParams(query[key]).substring(1) - : this.addQueryParam(query, key), - ) - .join("&")}` - : ""; + return keys + .map((key) => + typeof query[key] === "object" && !Array.isArray(query[key]) + ? this.toQueryString(query[key]) + : this.addQueryParam(query, key), + ) + .join("&"); + } + addQueryParams(rawQuery) { + const queryString = this.toQueryString(rawQuery); + return queryString ? `?${queryString}` : ""; } mergeRequestOptions(params, securityParams) { return { @@ -155,7 +159,7 @@ export class Api extends HttpClient { * @name GistsCreate * @request POST:/gists */ - gistsCreate: (body, params) => this.request(`/gists`, "POST", params, body), + gistsCreate: (body, params) => this.request(`/gists`, "POST", params, body, BodyType.Json), /** * @description List all public gists. * @@ -190,7 +194,7 @@ export class Api extends HttpClient { * @name GistsPartialUpdate * @request PATCH:/gists/{id} */ - gistsPartialUpdate: (id, body, params) => this.request(`/gists/${id}`, "PATCH", params, body), + gistsPartialUpdate: (id, body, params) => this.request(`/gists/${id}`, "PATCH", params, body, BodyType.Json), /** * @description List comments on a gist. * @@ -228,7 +232,7 @@ export class Api extends HttpClient { * @request PATCH:/gists/{id}/comments/{commentId} */ commentsPartialUpdate: (id, commentId, body, params) => - this.request(`/gists/${id}/comments/${commentId}`, "PATCH", params, body), + this.request(`/gists/${id}/comments/${commentId}`, "PATCH", params, body, BodyType.Json), /** * @description Fork a gist. * @@ -323,7 +327,7 @@ export class Api extends HttpClient { * @name MarkdownCreate * @request POST:/markdown */ - markdownCreate: (body, params) => this.request(`/markdown`, "POST", params, body), + markdownCreate: (body, params) => this.request(`/markdown`, "POST", params, body, BodyType.Json), /** * @description Render a Markdown document in raw mode * @@ -402,7 +406,7 @@ export class Api extends HttpClient { * @request PUT:/notifications/threads/{id}/subscription */ threadsSubscriptionUpdate: (id, body, params) => - this.request(`/notifications/threads/${id}/subscription`, "PUT", params, body), + this.request(`/notifications/threads/${id}/subscription`, "PUT", params, body, BodyType.Json), }; this.orgs = { /** @@ -418,7 +422,7 @@ export class Api extends HttpClient { * @name OrgsPartialUpdate * @request PATCH:/orgs/{org} */ - orgsPartialUpdate: (org, body, params) => this.request(`/orgs/${org}`, "PATCH", params, body), + orgsPartialUpdate: (org, body, params) => this.request(`/orgs/${org}`, "PATCH", params, body, BodyType.Json), /** * @description List public events for an organization. * @@ -518,7 +522,7 @@ export class Api extends HttpClient { * @name TeamsCreate * @request POST:/orgs/{org}/teams */ - teamsCreate: (org, body, params) => this.request(`/orgs/${org}/teams`, "POST", params, body), + teamsCreate: (org, body, params) => this.request(`/orgs/${org}/teams`, "POST", params, body, BodyType.Json), }; this.rateLimit = { /** @@ -550,7 +554,8 @@ export class Api extends HttpClient { * @name ReposPartialUpdate * @request PATCH:/repos/{owner}/{repo} */ - reposPartialUpdate: (owner, repo, body, params) => this.request(`/repos/${owner}/${repo}`, "PATCH", params, body), + reposPartialUpdate: (owner, repo, body, params) => + this.request(`/repos/${owner}/${repo}`, "PATCH", params, body, BodyType.Json), /** * @description List assignees. This call lists all the available assignees (owner + collaborators) to which issues may be assigned. * @@ -693,7 +698,7 @@ export class Api extends HttpClient { * @request POST:/repos/{owner}/{repo}/commits/{shaCode}/comments */ commitsCommentsCreate: (owner, repo, shaCode, body, params) => - this.request(`/repos/${owner}/${repo}/commits/${shaCode}/comments`, "POST", params, body), + this.request(`/repos/${owner}/${repo}/commits/${shaCode}/comments`, "POST", params, body, BodyType.Json), /** * @description Compare two commits * @@ -709,7 +714,7 @@ export class Api extends HttpClient { * @request DELETE:/repos/{owner}/{repo}/contents/{path} */ contentsDelete: (owner, repo, path, body, params) => - this.request(`/repos/${owner}/${repo}/contents/${path}`, "DELETE", params, body), + this.request(`/repos/${owner}/${repo}/contents/${path}`, "DELETE", params, body, BodyType.Json), /** * @description Get contents. This method returns the contents of a file or directory in a repository. Files and symlinks support a custom media type for getting the raw content. Directories and submodules do not support custom media types. Note: This API supports files up to 1 megabyte in size. Here can be many outcomes. For details see "http://developer.github.com/v3/repos/contents/" * @@ -725,7 +730,7 @@ export class Api extends HttpClient { * @request PUT:/repos/{owner}/{repo}/contents/{path} */ contentsUpdate: (owner, repo, path, body, params) => - this.request(`/repos/${owner}/${repo}/contents/${path}`, "PUT", params, body), + this.request(`/repos/${owner}/${repo}/contents/${path}`, "PUT", params, body, BodyType.Json), /** * @description Get list of contributors. * @@ -748,7 +753,7 @@ export class Api extends HttpClient { * @request POST:/repos/{owner}/{repo}/deployments */ deploymentsCreate: (owner, repo, body, params) => - this.request(`/repos/${owner}/${repo}/deployments`, "POST", params, body), + this.request(`/repos/${owner}/${repo}/deployments`, "POST", params, body, BodyType.Json), /** * @description Users with pull access can view deployment statuses for a deployment * @@ -764,7 +769,7 @@ export class Api extends HttpClient { * @request POST:/repos/{owner}/{repo}/deployments/{id}/statuses */ deploymentsStatusesCreate: (owner, repo, id, body, params) => - this.request(`/repos/${owner}/${repo}/deployments/${id}/statuses`, "POST", params, body), + this.request(`/repos/${owner}/${repo}/deployments/${id}/statuses`, "POST", params, body, BodyType.Json), /** * @description Deprecated. List downloads for a repository. * @@ -811,7 +816,8 @@ export class Api extends HttpClient { * @name ForksCreate * @request POST:/repos/{owner}/{repo}/forks */ - forksCreate: (owner, repo, body, params) => this.request(`/repos/${owner}/${repo}/forks`, "POST", params, body), + forksCreate: (owner, repo, body, params) => + this.request(`/repos/${owner}/${repo}/forks`, "POST", params, body, BodyType.Json), /** * @description Create a Blob. * @@ -819,7 +825,7 @@ export class Api extends HttpClient { * @request POST:/repos/{owner}/{repo}/git/blobs */ gitBlobsCreate: (owner, repo, body, params) => - this.request(`/repos/${owner}/${repo}/git/blobs`, "POST", params, body), + this.request(`/repos/${owner}/${repo}/git/blobs`, "POST", params, body, BodyType.Json), /** * @description Get a Blob. Since blobs can be any arbitrary binary data, the input and responses for the blob API takes an encoding parameter that can be either utf-8 or base64. If your data cannot be losslessly sent as a UTF-8 string, you can base64 encode it. * @@ -835,7 +841,7 @@ export class Api extends HttpClient { * @request POST:/repos/{owner}/{repo}/git/commits */ gitCommitsCreate: (owner, repo, body, params) => - this.request(`/repos/${owner}/${repo}/git/commits`, "POST", params, body), + this.request(`/repos/${owner}/${repo}/git/commits`, "POST", params, body, BodyType.Json), /** * @description Get a Commit. * @@ -858,7 +864,7 @@ export class Api extends HttpClient { * @request POST:/repos/{owner}/{repo}/git/refs */ gitRefsCreate: (owner, repo, body, params) => - this.request(`/repos/${owner}/${repo}/git/refs`, "POST", params, body), + this.request(`/repos/${owner}/${repo}/git/refs`, "POST", params, body, BodyType.Json), /** * @description Delete a Reference Example: Deleting a branch: DELETE /repos/octocat/Hello-World/git/refs/heads/feature-a Example: Deleting a tag: DELETE /repos/octocat/Hello-World/git/refs/tags/v1.0 * @@ -884,7 +890,7 @@ export class Api extends HttpClient { * @request PATCH:/repos/{owner}/{repo}/git/refs/{ref} */ gitRefsPartialUpdate: (owner, repo, ref, body, params) => - this.request(`/repos/${owner}/${repo}/git/refs/${ref}`, "PATCH", params, body), + this.request(`/repos/${owner}/${repo}/git/refs/${ref}`, "PATCH", params, body, BodyType.Json), /** * @description Create a Tag Object. Note that creating a tag object does not create the reference that makes a tag in Git. If you want to create an annotated tag in Git, you have to do this call to create the tag object, and then create the refs/tags/[tag] reference. If you want to create a lightweight tag, you only have to create the tag reference - this call would be unnecessary. * @@ -892,7 +898,7 @@ export class Api extends HttpClient { * @request POST:/repos/{owner}/{repo}/git/tags */ gitTagsCreate: (owner, repo, body, params) => - this.request(`/repos/${owner}/${repo}/git/tags`, "POST", params, body), + this.request(`/repos/${owner}/${repo}/git/tags`, "POST", params, body, BodyType.Json), /** * @description Get a Tag. * @@ -908,7 +914,7 @@ export class Api extends HttpClient { * @request POST:/repos/{owner}/{repo}/git/trees */ gitTreesCreate: (owner, repo, body, params) => - this.request(`/repos/${owner}/${repo}/git/trees`, "POST", params, body), + this.request(`/repos/${owner}/${repo}/git/trees`, "POST", params, body, BodyType.Json), /** * @description Get a Tree. * @@ -1203,7 +1209,8 @@ export class Api extends HttpClient { * @name MergesCreate * @request POST:/repos/{owner}/{repo}/merges */ - mergesCreate: (owner, repo, body, params) => this.request(`/repos/${owner}/${repo}/merges`, "POST", params, body), + mergesCreate: (owner, repo, body, params) => + this.request(`/repos/${owner}/${repo}/merges`, "POST", params, body, BodyType.Json), /** * @description List milestones for a repository. * @@ -1284,7 +1291,8 @@ export class Api extends HttpClient { * @name PullsCreate * @request POST:/repos/{owner}/{repo}/pulls */ - pullsCreate: (owner, repo, body, params) => this.request(`/repos/${owner}/${repo}/pulls`, "POST", params, body), + pullsCreate: (owner, repo, body, params) => + this.request(`/repos/${owner}/${repo}/pulls`, "POST", params, body, BodyType.Json), /** * @description List comments in a repository. By default, Review Comments are ordered by ascending ID. * @@ -1336,7 +1344,7 @@ export class Api extends HttpClient { * @request PATCH:/repos/{owner}/{repo}/pulls/{number} */ pullsPartialUpdate: (owner, repo, number, body, params) => - this.request(`/repos/${owner}/${repo}/pulls/${number}`, "PATCH", params, body), + this.request(`/repos/${owner}/${repo}/pulls/${number}`, "PATCH", params, body, BodyType.Json), /** * @description List comments on a pull request. * @@ -1354,7 +1362,7 @@ export class Api extends HttpClient { * @request POST:/repos/{owner}/{repo}/pulls/{number}/comments */ pullsCommentsCreate: (owner, repo, number, body, params) => - this.request(`/repos/${owner}/${repo}/pulls/${number}/comments`, "POST", params, body), + this.request(`/repos/${owner}/${repo}/pulls/${number}/comments`, "POST", params, body, BodyType.Json), /** * @description List commits on a pull request. * @@ -1386,7 +1394,7 @@ export class Api extends HttpClient { * @request PUT:/repos/{owner}/{repo}/pulls/{number}/merge */ pullsMergeUpdate: (owner, repo, number, body, params) => - this.request(`/repos/${owner}/${repo}/pulls/${number}/merge`, "PUT", params, body), + this.request(`/repos/${owner}/${repo}/pulls/${number}/merge`, "PUT", params, body, BodyType.Json), /** * @description Get the README. This method returns the preferred README for a repository. * @@ -1433,7 +1441,7 @@ export class Api extends HttpClient { * @request PATCH:/repos/{owner}/{repo}/releases/assets/{id} */ releasesAssetsPartialUpdate: (owner, repo, id, body, params) => - this.request(`/repos/${owner}/${repo}/releases/assets/${id}`, "PATCH", params, body), + this.request(`/repos/${owner}/${repo}/releases/assets/${id}`, "PATCH", params, body, BodyType.Json), /** * @description Users with push access to the repository can delete a release. * @@ -1532,7 +1540,7 @@ export class Api extends HttpClient { * @request POST:/repos/{owner}/{repo}/statuses/{ref} */ statusesCreate: (owner, repo, ref, body, params) => - this.request(`/repos/${owner}/${repo}/statuses/${ref}`, "POST", params, body), + this.request(`/repos/${owner}/${repo}/statuses/${ref}`, "POST", params, body, BodyType.Json), /** * @description List watchers. * @@ -1562,7 +1570,7 @@ export class Api extends HttpClient { * @request PUT:/repos/{owner}/{repo}/subscription */ subscriptionUpdate: (owner, repo, body, params) => - this.request(`/repos/${owner}/${repo}/subscription`, "PUT", params, body), + this.request(`/repos/${owner}/${repo}/subscription`, "PUT", params, body, BodyType.Json), /** * @description Get list of tags. * @@ -1656,7 +1664,8 @@ export class Api extends HttpClient { * @name TeamsPartialUpdate * @request PATCH:/teams/{teamId} */ - teamsPartialUpdate: (teamId, body, params) => this.request(`/teams/${teamId}`, "PATCH", params, body), + teamsPartialUpdate: (teamId, body, params) => + this.request(`/teams/${teamId}`, "PATCH", params, body, BodyType.Json), /** * @description List team members. In order to list members in a team, the authenticated user must be a member of the team. * @@ -1760,14 +1769,14 @@ export class Api extends HttpClient { * @name UserPartialUpdate * @request PATCH:/user */ - userPartialUpdate: (body, params) => this.request(`/user`, "PATCH", params, body), + userPartialUpdate: (body, params) => this.request(`/user`, "PATCH", params, body, BodyType.Json), /** * @description Delete email address(es). You can include a single email address or an array of addresses. * * @name EmailsDelete * @request DELETE:/user/emails */ - emailsDelete: (body, params) => this.request(`/user/emails`, "DELETE", params, body), + emailsDelete: (body, params) => this.request(`/user/emails`, "DELETE", params, body, BodyType.Json), /** * @description List email addresses for a user. In the final version of the API, this method will return an array of hashes with extended information for each email address indicating if the address has been verified and if it's primary email address for GitHub. Until API v3 is finalized, use the application/vnd.github.v3 media type to get other response format. * diff --git a/tests/spec/modular/Scope.ts b/tests/spec/modular/Scope.ts index e52f2047..2b2f43f9 100644 --- a/tests/spec/modular/Scope.ts +++ b/tests/spec/modular/Scope.ts @@ -10,7 +10,7 @@ */ import { Claims, Error } from "./data-contracts"; -import { HttpClient, RequestParams } from "./http-client"; +import { BodyType, HttpClient, RequestParams } from "./http-client"; export class Scope extends HttpClient { /** @@ -62,7 +62,7 @@ export class Scope extends HttpClient { * @request POST:/scope/{job} */ signConfirm = (job: string, params?: RequestParams) => - this.request<{ status?: string }, Error>(`/scope/${job}`, "POST", params); + this.request<{ status?: string }, Error>(`/scope/${job}`, "POST", params, null, BodyType.Json); /** * @description authority updates a JWT with its signature See: https://github.com/skion/authentiq/wiki/JWT-Examples * diff --git a/tests/spec/modular/http-client.ts b/tests/spec/modular/http-client.ts index 9808d614..33f5bf06 100644 --- a/tests/spec/modular/http-client.ts +++ b/tests/spec/modular/http-client.ts @@ -29,6 +29,7 @@ export interface HttpResponse ex export enum BodyType { Json, FormData, + UrlEncoded, } export class HttpClient { @@ -59,18 +60,21 @@ export class HttpClient { ); } - protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + protected toQueryString(rawQuery?: RequestQueryParamsType): string { const query = rawQuery || {}; const keys = Object.keys(query).filter((key) => "undefined" !== typeof query[key]); - return keys.length - ? `?${keys - .map((key) => - typeof query[key] === "object" && !Array.isArray(query[key]) - ? this.addQueryParams(query[key] as object).substring(1) - : this.addQueryParam(query, key), - ) - .join("&")}` - : ""; + return keys + .map((key) => + typeof query[key] === "object" && !Array.isArray(query[key]) + ? this.toQueryString(query[key] as object) + : this.addQueryParam(query, key), + ) + .join("&"); + } + + protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + const queryString = this.toQueryString(rawQuery); + return queryString ? `?${queryString}` : ""; } private bodyFormatters: Record any> = { @@ -80,6 +84,7 @@ export class HttpClient { data.append(key, input[key]); return data; }, new FormData()), + [BodyType.UrlEncoded]: (input: any) => this.toQueryString(input), }; private mergeRequestOptions(params: RequestParams, securityParams?: RequestParams): RequestParams { diff --git a/tests/spec/moduleNameIndex/schema.ts b/tests/spec/moduleNameIndex/schema.ts index bca4d0a9..900ed907 100644 --- a/tests/spec/moduleNameIndex/schema.ts +++ b/tests/spec/moduleNameIndex/schema.ts @@ -157,6 +157,7 @@ interface HttpResponse extends R enum BodyType { Json, FormData, + UrlEncoded, } export class HttpClient { @@ -187,18 +188,21 @@ export class HttpClient { ); } - protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + protected toQueryString(rawQuery?: RequestQueryParamsType): string { const query = rawQuery || {}; const keys = Object.keys(query).filter((key) => "undefined" !== typeof query[key]); - return keys.length - ? `?${keys - .map((key) => - typeof query[key] === "object" && !Array.isArray(query[key]) - ? this.addQueryParams(query[key] as object).substring(1) - : this.addQueryParam(query, key), - ) - .join("&")}` - : ""; + return keys + .map((key) => + typeof query[key] === "object" && !Array.isArray(query[key]) + ? this.toQueryString(query[key] as object) + : this.addQueryParam(query, key), + ) + .join("&"); + } + + protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + const queryString = this.toQueryString(rawQuery); + return queryString ? `?${queryString}` : ""; } private bodyFormatters: Record any> = { @@ -208,6 +212,7 @@ export class HttpClient { data.append(key, input[key]); return data; }, new FormData()), + [BodyType.UrlEncoded]: (input: any) => this.toQueryString(input), }; private mergeRequestOptions(params: RequestParams, securityParams?: RequestParams): RequestParams { @@ -409,7 +414,7 @@ export class Api extends HttpClient { * @request POST:api/v1/store/order */ placeOrder: (body: Order, params?: RequestParams) => - this.request(`api/v1/store/order`, "POST", params, body), + this.request(`api/v1/store/order`, "POST", params, body, BodyType.Json), /** * @description For valid response try integer IDs with value <= 5 or > 10. Other values will generated exceptions @@ -442,7 +447,8 @@ export class Api extends HttpClient { * @summary Create user * @request POST:api/v1/user */ - createUser: (body: User, params?: RequestParams) => this.request(`api/v1/user`, "POST", params, body), + createUser: (body: User, params?: RequestParams) => + this.request(`api/v1/user`, "POST", params, body, BodyType.Json), /** * No description @@ -453,7 +459,7 @@ export class Api extends HttpClient { * @request POST:api/v1/user/createWithArray */ createUsersWithArrayInput: (body: User[], params?: RequestParams) => - this.request(`api/v1/user/createWithArray`, "POST", params, body), + this.request(`api/v1/user/createWithArray`, "POST", params, body, BodyType.Json), /** * No description @@ -464,7 +470,7 @@ export class Api extends HttpClient { * @request POST:api/v1/user/createWithList */ createUsersWithListInput: (body: User[], params?: RequestParams) => - this.request(`api/v1/user/createWithList`, "POST", params, body), + this.request(`api/v1/user/createWithList`, "POST", params, body, BodyType.Json), /** * No description @@ -507,7 +513,7 @@ export class Api extends HttpClient { * @request PUT:api/v1/user/{username} */ updateUser: (username: string, body: User, params?: RequestParams) => - this.request(`api/v1/user/${username}`, "PUT", params, body), + this.request(`api/v1/user/${username}`, "PUT", params, body, BodyType.Json), /** * @description This can only be done by the logged in user. @@ -541,7 +547,7 @@ export class Api extends HttpClient { * @request PUT:api/v1/{username} */ updateUser: (username: string, body: User, params?: RequestParams) => - this.request(`api/v1/${username}`, "PUT", params, body), + this.request(`api/v1/${username}`, "PUT", params, body, BodyType.Json), /** * @description This can only be done by the logged in user. diff --git a/tests/spec/responses/schema.ts b/tests/spec/responses/schema.ts index 2bc0edee..69653382 100644 --- a/tests/spec/responses/schema.ts +++ b/tests/spec/responses/schema.ts @@ -92,6 +92,7 @@ interface HttpResponse extends R enum BodyType { Json, FormData, + UrlEncoded, } export class HttpClient { @@ -122,18 +123,21 @@ export class HttpClient { ); } - protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + protected toQueryString(rawQuery?: RequestQueryParamsType): string { const query = rawQuery || {}; const keys = Object.keys(query).filter((key) => "undefined" !== typeof query[key]); - return keys.length - ? `?${keys - .map((key) => - typeof query[key] === "object" && !Array.isArray(query[key]) - ? this.addQueryParams(query[key] as object).substring(1) - : this.addQueryParam(query, key), - ) - .join("&")}` - : ""; + return keys + .map((key) => + typeof query[key] === "object" && !Array.isArray(query[key]) + ? this.toQueryString(query[key] as object) + : this.addQueryParam(query, key), + ) + .join("&"); + } + + protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + const queryString = this.toQueryString(rawQuery); + return queryString ? `?${queryString}` : ""; } private bodyFormatters: Record any> = { @@ -143,6 +147,7 @@ export class HttpClient { data.append(key, input[key]); return data; }, new FormData()), + [BodyType.UrlEncoded]: (input: any) => this.toQueryString(input), }; private mergeRequestOptions(params: RequestParams, securityParams?: RequestParams): RequestParams { @@ -395,7 +400,7 @@ export class Api extends HttpClient { * @response `default` `Error` */ signConfirm: (job: string, params?: RequestParams) => - this.request<{ status?: string }, Error>(`/scope/${job}`, "POST", params), + this.request<{ status?: string }, Error>(`/scope/${job}`, "POST", params, null, BodyType.Json), /** * @description authority updates a JWT with its signature See: https://github.com/skion/authentiq/wiki/JWT-Examples diff --git a/tests/spec/routeTypes/schema.ts b/tests/spec/routeTypes/schema.ts index f3811b0c..9b897b0d 100644 --- a/tests/spec/routeTypes/schema.ts +++ b/tests/spec/routeTypes/schema.ts @@ -4284,6 +4284,7 @@ interface HttpResponse extends R enum BodyType { Json, FormData, + UrlEncoded, } export class HttpClient { @@ -4314,18 +4315,21 @@ export class HttpClient { ); } - protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + protected toQueryString(rawQuery?: RequestQueryParamsType): string { const query = rawQuery || {}; const keys = Object.keys(query).filter((key) => "undefined" !== typeof query[key]); - return keys.length - ? `?${keys - .map((key) => - typeof query[key] === "object" && !Array.isArray(query[key]) - ? this.addQueryParams(query[key] as object).substring(1) - : this.addQueryParam(query, key), - ) - .join("&")}` - : ""; + return keys + .map((key) => + typeof query[key] === "object" && !Array.isArray(query[key]) + ? this.toQueryString(query[key] as object) + : this.addQueryParam(query, key), + ) + .join("&"); + } + + protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + const queryString = this.toQueryString(rawQuery); + return queryString ? `?${queryString}` : ""; } private bodyFormatters: Record any> = { @@ -4335,6 +4339,7 @@ export class HttpClient { data.append(key, input[key]); return data; }, new FormData()), + [BodyType.UrlEncoded]: (input: any) => this.toQueryString(input), }; private mergeRequestOptions(params: RequestParams, securityParams?: RequestParams): RequestParams { @@ -4446,7 +4451,8 @@ export class Api extends HttpClient { * @name GistsCreate * @request POST:/gists */ - gistsCreate: (body: PostGist, params?: RequestParams) => this.request(`/gists`, "POST", params, body), + gistsCreate: (body: PostGist, params?: RequestParams) => + this.request(`/gists`, "POST", params, body, BodyType.Json), /** * @description List all public gists. @@ -4489,7 +4495,7 @@ export class Api extends HttpClient { * @request PATCH:/gists/{id} */ gistsPartialUpdate: (id: number, body: PatchGist, params?: RequestParams) => - this.request(`/gists/${id}`, "PATCH", params, body), + this.request(`/gists/${id}`, "PATCH", params, body, BodyType.Json), /** * @description List comments on a gist. @@ -4536,7 +4542,7 @@ export class Api extends HttpClient { * @request PATCH:/gists/{id}/comments/{commentId} */ commentsPartialUpdate: (id: number, commentId: number, body: Comment, params?: RequestParams) => - this.request(`/gists/${id}/comments/${commentId}`, "PATCH", params, body), + this.request(`/gists/${id}/comments/${commentId}`, "PATCH", params, body, BodyType.Json), /** * @description Fork a gist. @@ -4678,7 +4684,7 @@ export class Api extends HttpClient { * @request POST:/markdown */ markdownCreate: (body: Markdown, params?: RequestParams) => - this.request(`/markdown`, "POST", params, body), + this.request(`/markdown`, "POST", params, body, BodyType.Json), /** * @description Render a Markdown document in raw mode @@ -4769,7 +4775,7 @@ export class Api extends HttpClient { * @request PUT:/notifications/threads/{id}/subscription */ threadsSubscriptionUpdate: (id: number, body: PutSubscription, params?: RequestParams) => - this.request(`/notifications/threads/${id}/subscription`, "PUT", params, body), + this.request(`/notifications/threads/${id}/subscription`, "PUT", params, body, BodyType.Json), }; orgs = { /** @@ -4787,7 +4793,7 @@ export class Api extends HttpClient { * @request PATCH:/orgs/{org} */ orgsPartialUpdate: (org: string, body: PatchOrg, params?: RequestParams) => - this.request(`/orgs/${org}`, "PATCH", params, body), + this.request(`/orgs/${org}`, "PATCH", params, body, BodyType.Json), /** * @description List public events for an organization. @@ -4920,7 +4926,7 @@ export class Api extends HttpClient { * @request POST:/orgs/{org}/teams */ teamsCreate: (org: string, body: OrgTeamsPost, params?: RequestParams) => - this.request(`/orgs/${org}/teams`, "POST", params, body), + this.request(`/orgs/${org}/teams`, "POST", params, body, BodyType.Json), }; rateLimit = { /** @@ -4957,7 +4963,7 @@ export class Api extends HttpClient { * @request PATCH:/repos/{owner}/{repo} */ reposPartialUpdate: (owner: string, repo: string, body: RepoEdit, params?: RequestParams) => - this.request(`/repos/${owner}/${repo}`, "PATCH", params, body), + this.request(`/repos/${owner}/${repo}`, "PATCH", params, body, BodyType.Json), /** * @description List assignees. This call lists all the available assignees (owner + collaborators) to which issues may be assigned. @@ -5134,7 +5140,14 @@ export class Api extends HttpClient { shaCode: string, body: CommitCommentBody, params?: RequestParams, - ) => this.request(`/repos/${owner}/${repo}/commits/${shaCode}/comments`, "POST", params, body), + ) => + this.request( + `/repos/${owner}/${repo}/commits/${shaCode}/comments`, + "POST", + params, + body, + BodyType.Json, + ), /** * @description Compare two commits @@ -5152,7 +5165,7 @@ export class Api extends HttpClient { * @request DELETE:/repos/{owner}/{repo}/contents/{path} */ contentsDelete: (owner: string, repo: string, path: string, body: DeleteFileBody, params?: RequestParams) => - this.request(`/repos/${owner}/${repo}/contents/${path}`, "DELETE", params, body), + this.request(`/repos/${owner}/${repo}/contents/${path}`, "DELETE", params, body, BodyType.Json), /** * @description Get contents. This method returns the contents of a file or directory in a repository. Files and symlinks support a custom media type for getting the raw content. Directories and submodules do not support custom media types. Note: This API supports files up to 1 megabyte in size. Here can be many outcomes. For details see "http://developer.github.com/v3/repos/contents/" @@ -5180,7 +5193,7 @@ export class Api extends HttpClient { * @request PUT:/repos/{owner}/{repo}/contents/{path} */ contentsUpdate: (owner: string, repo: string, path: string, body: CreateFileBody, params?: RequestParams) => - this.request(`/repos/${owner}/${repo}/contents/${path}`, "PUT", params, body), + this.request(`/repos/${owner}/${repo}/contents/${path}`, "PUT", params, body, BodyType.Json), /** * @description Get list of contributors. @@ -5207,7 +5220,7 @@ export class Api extends HttpClient { * @request POST:/repos/{owner}/{repo}/deployments */ deploymentsCreate: (owner: string, repo: string, body: Deployment, params?: RequestParams) => - this.request(`/repos/${owner}/${repo}/deployments`, "POST", params, body), + this.request(`/repos/${owner}/${repo}/deployments`, "POST", params, body, BodyType.Json), /** * @description Users with pull access can view deployment statuses for a deployment @@ -5230,7 +5243,8 @@ export class Api extends HttpClient { id: number, body: DeploymentStatusesCreate, params?: RequestParams, - ) => this.request(`/repos/${owner}/${repo}/deployments/${id}/statuses`, "POST", params, body), + ) => + this.request(`/repos/${owner}/${repo}/deployments/${id}/statuses`, "POST", params, body, BodyType.Json), /** * @description Deprecated. List downloads for a repository. @@ -5290,7 +5304,7 @@ export class Api extends HttpClient { * @request POST:/repos/{owner}/{repo}/forks */ forksCreate: (owner: string, repo: string, body: ForkBody, params?: RequestParams) => - this.request(`/repos/${owner}/${repo}/forks`, "POST", params, body), + this.request(`/repos/${owner}/${repo}/forks`, "POST", params, body, BodyType.Json), /** * @description Create a Blob. @@ -5299,7 +5313,7 @@ export class Api extends HttpClient { * @request POST:/repos/{owner}/{repo}/git/blobs */ gitBlobsCreate: (owner: string, repo: string, body: Blob, params?: RequestParams) => - this.request(`/repos/${owner}/${repo}/git/blobs`, "POST", params, body), + this.request(`/repos/${owner}/${repo}/git/blobs`, "POST", params, body, BodyType.Json), /** * @description Get a Blob. Since blobs can be any arbitrary binary data, the input and responses for the blob API takes an encoding parameter that can be either utf-8 or base64. If your data cannot be losslessly sent as a UTF-8 string, you can base64 encode it. @@ -5317,7 +5331,7 @@ export class Api extends HttpClient { * @request POST:/repos/{owner}/{repo}/git/commits */ gitCommitsCreate: (owner: string, repo: string, body: RepoCommitBody, params?: RequestParams) => - this.request(`/repos/${owner}/${repo}/git/commits`, "POST", params, body), + this.request(`/repos/${owner}/${repo}/git/commits`, "POST", params, body, BodyType.Json), /** * @description Get a Commit. @@ -5344,7 +5358,7 @@ export class Api extends HttpClient { * @request POST:/repos/{owner}/{repo}/git/refs */ gitRefsCreate: (owner: string, repo: string, body: RefsBody, params?: RequestParams) => - this.request(`/repos/${owner}/${repo}/git/refs`, "POST", params, body), + this.request(`/repos/${owner}/${repo}/git/refs`, "POST", params, body, BodyType.Json), /** * @description Delete a Reference Example: Deleting a branch: DELETE /repos/octocat/Hello-World/git/refs/heads/feature-a Example: Deleting a tag: DELETE /repos/octocat/Hello-World/git/refs/tags/v1.0 @@ -5373,7 +5387,7 @@ export class Api extends HttpClient { * @request PATCH:/repos/{owner}/{repo}/git/refs/{ref} */ gitRefsPartialUpdate: (owner: string, repo: string, ref: string, body: GitRefPatch, params?: RequestParams) => - this.request(`/repos/${owner}/${repo}/git/refs/${ref}`, "PATCH", params, body), + this.request(`/repos/${owner}/${repo}/git/refs/${ref}`, "PATCH", params, body, BodyType.Json), /** * @description Create a Tag Object. Note that creating a tag object does not create the reference that makes a tag in Git. If you want to create an annotated tag in Git, you have to do this call to create the tag object, and then create the refs/tags/[tag] reference. If you want to create a lightweight tag, you only have to create the tag reference - this call would be unnecessary. @@ -5382,7 +5396,7 @@ export class Api extends HttpClient { * @request POST:/repos/{owner}/{repo}/git/tags */ gitTagsCreate: (owner: string, repo: string, body: TagBody, params?: RequestParams) => - this.request(`/repos/${owner}/${repo}/git/tags`, "POST", params, body), + this.request(`/repos/${owner}/${repo}/git/tags`, "POST", params, body, BodyType.Json), /** * @description Get a Tag. @@ -5400,7 +5414,7 @@ export class Api extends HttpClient { * @request POST:/repos/{owner}/{repo}/git/trees */ gitTreesCreate: (owner: string, repo: string, body: Tree, params?: RequestParams) => - this.request(`/repos/${owner}/${repo}/git/trees`, "POST", params, body), + this.request(`/repos/${owner}/${repo}/git/trees`, "POST", params, body, BodyType.Json), /** * @description Get a Tree. @@ -5778,7 +5792,13 @@ export class Api extends HttpClient { * @request POST:/repos/{owner}/{repo}/merges */ mergesCreate: (owner: string, repo: string, body: MergesBody, params?: RequestParams) => - this.request(`/repos/${owner}/${repo}/merges`, "POST", params, body), + this.request( + `/repos/${owner}/${repo}/merges`, + "POST", + params, + body, + BodyType.Json, + ), /** * @description List milestones for a repository. @@ -5892,7 +5912,7 @@ export class Api extends HttpClient { * @request POST:/repos/{owner}/{repo}/pulls */ pullsCreate: (owner: string, repo: string, body: PullsPost, params?: RequestParams) => - this.request(`/repos/${owner}/${repo}/pulls`, "POST", params, body), + this.request(`/repos/${owner}/${repo}/pulls`, "POST", params, body, BodyType.Json), /** * @description List comments in a repository. By default, Review Comments are ordered by ascending ID. @@ -5964,7 +5984,7 @@ export class Api extends HttpClient { * @request PATCH:/repos/{owner}/{repo}/pulls/{number} */ pullsPartialUpdate: (owner: string, repo: string, number: number, body: PullUpdate, params?: RequestParams) => - this.request(`/repos/${owner}/${repo}/pulls/${number}`, "PATCH", params, body), + this.request(`/repos/${owner}/${repo}/pulls/${number}`, "PATCH", params, body, BodyType.Json), /** * @description List comments on a pull request. @@ -5989,7 +6009,14 @@ export class Api extends HttpClient { number: number, body: PullsCommentPost, params?: RequestParams, - ) => this.request(`/repos/${owner}/${repo}/pulls/${number}/comments`, "POST", params, body), + ) => + this.request( + `/repos/${owner}/${repo}/pulls/${number}/comments`, + "POST", + params, + body, + BodyType.Json, + ), /** * @description List commits on a pull request. @@ -6025,7 +6052,7 @@ export class Api extends HttpClient { * @request PUT:/repos/{owner}/{repo}/pulls/{number}/merge */ pullsMergeUpdate: (owner: string, repo: string, number: number, body: MergePullBody, params?: RequestParams) => - this.request(`/repos/${owner}/${repo}/pulls/${number}/merge`, "PUT", params, body), + this.request(`/repos/${owner}/${repo}/pulls/${number}/merge`, "PUT", params, body, BodyType.Json), /** * @description Get the README. This method returns the preferred README for a repository. @@ -6079,7 +6106,7 @@ export class Api extends HttpClient { * @request PATCH:/repos/{owner}/{repo}/releases/assets/{id} */ releasesAssetsPartialUpdate: (owner: string, repo: string, id: string, body: AssetPatch, params?: RequestParams) => - this.request(`/repos/${owner}/${repo}/releases/assets/${id}`, "PATCH", params, body), + this.request(`/repos/${owner}/${repo}/releases/assets/${id}`, "PATCH", params, body, BodyType.Json), /** * @description Users with push access to the repository can delete a release. @@ -6191,7 +6218,7 @@ export class Api extends HttpClient { * @request POST:/repos/{owner}/{repo}/statuses/{ref} */ statusesCreate: (owner: string, repo: string, ref: string, body: HeadBranch, params?: RequestParams) => - this.request(`/repos/${owner}/${repo}/statuses/${ref}`, "POST", params, body), + this.request(`/repos/${owner}/${repo}/statuses/${ref}`, "POST", params, body, BodyType.Json), /** * @description List watchers. @@ -6227,7 +6254,7 @@ export class Api extends HttpClient { * @request PUT:/repos/{owner}/{repo}/subscription */ subscriptionUpdate: (owner: string, repo: string, body: SubscriptionBody, params?: RequestParams) => - this.request(`/repos/${owner}/${repo}/subscription`, "PUT", params, body), + this.request(`/repos/${owner}/${repo}/subscription`, "PUT", params, body, BodyType.Json), /** * @description Get list of tags. @@ -6350,7 +6377,7 @@ export class Api extends HttpClient { * @request PATCH:/teams/{teamId} */ teamsPartialUpdate: (teamId: number, body: EditTeam, params?: RequestParams) => - this.request(`/teams/${teamId}`, "PATCH", params, body), + this.request(`/teams/${teamId}`, "PATCH", params, body, BodyType.Json), /** * @description List team members. In order to list members in a team, the authenticated user must be a member of the team. @@ -6471,7 +6498,7 @@ export class Api extends HttpClient { * @request PATCH:/user */ userPartialUpdate: (body: UserUpdate, params?: RequestParams) => - this.request(`/user`, "PATCH", params, body), + this.request(`/user`, "PATCH", params, body, BodyType.Json), /** * @description Delete email address(es). You can include a single email address or an array of addresses. @@ -6480,7 +6507,7 @@ export class Api extends HttpClient { * @request DELETE:/user/emails */ emailsDelete: (body: UserEmails, params?: RequestParams) => - this.request(`/user/emails`, "DELETE", params, body), + this.request(`/user/emails`, "DELETE", params, body, BodyType.Json), /** * @description List email addresses for a user. In the final version of the API, this method will return an array of hashes with extended information for each email address indicating if the address has been verified and if it's primary email address for GitHub. Until API v3 is finalized, use the application/vnd.github.v3 media type to get other response format. diff --git a/tests/spec/specProperty/schema.ts b/tests/spec/specProperty/schema.ts index 99f86ab6..d271f073 100644 --- a/tests/spec/specProperty/schema.ts +++ b/tests/spec/specProperty/schema.ts @@ -113,6 +113,7 @@ interface HttpResponse extends R enum BodyType { Json, FormData, + UrlEncoded, } export class HttpClient { @@ -143,18 +144,21 @@ export class HttpClient { ); } - protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + protected toQueryString(rawQuery?: RequestQueryParamsType): string { const query = rawQuery || {}; const keys = Object.keys(query).filter((key) => "undefined" !== typeof query[key]); - return keys.length - ? `?${keys - .map((key) => - typeof query[key] === "object" && !Array.isArray(query[key]) - ? this.addQueryParams(query[key] as object).substring(1) - : this.addQueryParam(query, key), - ) - .join("&")}` - : ""; + return keys + .map((key) => + typeof query[key] === "object" && !Array.isArray(query[key]) + ? this.toQueryString(query[key] as object) + : this.addQueryParam(query, key), + ) + .join("&"); + } + + protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + const queryString = this.toQueryString(rawQuery); + return queryString ? `?${queryString}` : ""; } private bodyFormatters: Record any> = { @@ -164,6 +168,7 @@ export class HttpClient { data.append(key, input[key]); return data; }, new FormData()), + [BodyType.UrlEncoded]: (input: any) => this.toQueryString(input), }; private mergeRequestOptions(params: RequestParams, securityParams?: RequestParams): RequestParams { @@ -248,7 +253,8 @@ export class Api extends HttpClient { * @name AddPet * @request POST:/pets */ - addPet: (pet: NewPet, params?: RequestParams) => this.request(`/pets`, "POST", params, pet), + addPet: (pet: NewPet, params?: RequestParams) => + this.request(`/pets`, "POST", params, pet, BodyType.Json), /** * @description Returns a user based on a single ID, if the user does not have access to the pet diff --git a/tests/spec/unionEnums/schema.ts b/tests/spec/unionEnums/schema.ts index 40938b68..8c3544a7 100644 --- a/tests/spec/unionEnums/schema.ts +++ b/tests/spec/unionEnums/schema.ts @@ -39,6 +39,7 @@ interface HttpResponse extends R enum BodyType { Json, FormData, + UrlEncoded, } export class HttpClient { @@ -69,18 +70,21 @@ export class HttpClient { ); } - protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + protected toQueryString(rawQuery?: RequestQueryParamsType): string { const query = rawQuery || {}; const keys = Object.keys(query).filter((key) => "undefined" !== typeof query[key]); - return keys.length - ? `?${keys - .map((key) => - typeof query[key] === "object" && !Array.isArray(query[key]) - ? this.addQueryParams(query[key] as object).substring(1) - : this.addQueryParam(query, key), - ) - .join("&")}` - : ""; + return keys + .map((key) => + typeof query[key] === "object" && !Array.isArray(query[key]) + ? this.toQueryString(query[key] as object) + : this.addQueryParam(query, key), + ) + .join("&"); + } + + protected addQueryParams(rawQuery?: RequestQueryParamsType): string { + const queryString = this.toQueryString(rawQuery); + return queryString ? `?${queryString}` : ""; } private bodyFormatters: Record any> = { @@ -90,6 +94,7 @@ export class HttpClient { data.append(key, input[key]); return data; }, new FormData()), + [BodyType.UrlEncoded]: (input: any) => this.toQueryString(input), }; private mergeRequestOptions(params: RequestParams, securityParams?: RequestParams): RequestParams {