Skip to content
Permalink
Browse files

typescript-fetch: Add application/x-www-form-urlencoded content suppo…

…rt (#3934)

* typescript-fetch: to be able to handle application/x-www-form-urlencoded content

* typescript-fetch: run typescript-fetch-petstore-all.sh

* typescript-fetch: add test's dist dir

* typescript-fetch: delete Specified Content-Type header
  • Loading branch information...
tkrtmy authored and macjohnny committed Sep 23, 2019
1 parent ee2e439 commit 1a550201940637f4fa2d7f6210c922d1b563ead3
Showing with 392 additions and 70 deletions.
  1. +1 −0 .gitignore
  2. +27 −6 modules/openapi-generator/src/main/resources/typescript-fetch/apis.mustache
  3. +14 −1 modules/openapi-generator/src/main/resources/typescript-fetch/runtime.mustache
  4. +36 −8 samples/client/petstore/typescript-fetch/builds/default/src/apis/PetApi.ts
  5. +14 −1 samples/client/petstore/typescript-fetch/builds/default/src/runtime.ts
  6. +36 −8 samples/client/petstore/typescript-fetch/builds/es6-target/src/apis/PetApi.ts
  7. +14 −1 samples/client/petstore/typescript-fetch/builds/es6-target/src/runtime.ts
  8. +36 −8 samples/client/petstore/typescript-fetch/builds/multiple-parameters/src/apis/PetApi.ts
  9. +14 −1 samples/client/petstore/typescript-fetch/builds/multiple-parameters/src/runtime.ts
  10. +36 −8 samples/client/petstore/typescript-fetch/builds/prefix-parameter-interfaces/src/apis/PetApi.ts
  11. +14 −1 samples/client/petstore/typescript-fetch/builds/prefix-parameter-interfaces/src/runtime.ts
  12. +36 −8 samples/client/petstore/typescript-fetch/builds/typescript-three-plus/src/apis/PetApi.ts
  13. +14 −1 samples/client/petstore/typescript-fetch/builds/typescript-three-plus/src/runtime.ts
  14. +36 −8 samples/client/petstore/typescript-fetch/builds/with-interfaces/src/apis/PetApi.ts
  15. +14 −1 samples/client/petstore/typescript-fetch/builds/with-interfaces/src/runtime.ts
  16. +36 −8 samples/client/petstore/typescript-fetch/builds/with-npm-version/src/apis/PetApi.ts
  17. +14 −1 samples/client/petstore/typescript-fetch/builds/with-npm-version/src/runtime.ts
@@ -174,6 +174,7 @@ samples/client/petstore/python-tornado/.venv/
samples/client/petstore/typescript-angular2/npm/npm-debug.log
samples/client/petstore/typescript-node/npm/npm-debug.log
samples/client/petstore/typescript-angular/tsd-debug.log
samples/client/petstore/typescript-fetch/tests/**/dist/

# aspnetcore
samples/server/petstore/aspnetcore/.vs/
@@ -150,29 +150,50 @@ export class {{classname}} extends runtime.BaseAPI {
{{/isOAuth}}
{{/authMethods}}
{{#hasFormParams}}
const formData = new FormData();
{{/hasFormParams}}
const consumes: runtime.Consume[] = [
{{#consumes}}
{ contentType: '{{{mediaType}}}' },
{{/consumes}}
];
// @ts-ignore: canConsumeForm may be unused
const canConsumeForm = runtime.canConsumeForm(consumes);

let formParams: { append(param: string, value: any): any };
let useForm = false;
{{#formParams}}
{{#isFile}}
// use FormData to transmit files using content-type "multipart/form-data"
useForm = canConsumeForm;
{{/isFile}}
{{/formParams}}
if (useForm) {
formParams = new FormData();
} else {
formParams = new URLSearchParams();
}

{{#formParams}}
{{#isListContainer}}
if (requestParameters.{{paramName}}) {
{{#isCollectionFormatMulti}}
requestParameters.{{paramName}}.forEach((element) => {
formData.append('{{baseName}}', element as any);
formParams.append('{{baseName}}', element as any);
})
{{/isCollectionFormatMulti}}
{{^isCollectionFormatMulti}}
formData.append('{{baseName}}', requestParameters.{{paramName}}.join(runtime.COLLECTION_FORMATS["{{collectionFormat}}"]));
formParams.append('{{baseName}}', requestParameters.{{paramName}}.join(runtime.COLLECTION_FORMATS["{{collectionFormat}}"]));
{{/isCollectionFormatMulti}}
}

{{/isListContainer}}
{{^isListContainer}}
if (requestParameters.{{paramName}} !== undefined) {
formData.append('{{baseName}}', requestParameters.{{paramName}} as any);
formParams.append('{{baseName}}', requestParameters.{{paramName}} as any);
}

{{/isListContainer}}
{{/formParams}}
{{/hasFormParams}}
const response = await this.request({
path: `{{{path}}}`{{#pathParams}}.replace(`{${"{{baseName}}"}}`, encodeURIComponent(String(requestParameters.{{paramName}}))){{/pathParams}},
method: '{{httpMethod}}',
@@ -194,7 +215,7 @@ export class {{classname}} extends runtime.BaseAPI {
{{/bodyParam}}
{{/hasBodyParam}}
{{#hasFormParams}}
body: formData,
body: formParams,
{{/hasFormParams}}
});

@@ -184,7 +184,7 @@ export type Json = any;
export type HTTPMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS';
export type HTTPHeaders = { [key: string]: string };
export type HTTPQuery = { [key: string]: string | number | null | boolean | Array<string | number | null | boolean> | HTTPQuery };
export type HTTPBody = Json | FormData;
export type HTTPBody = Json | FormData | URLSearchParams;
export type ModelPropertyNaming = 'camelCase' | 'snake_case' | 'PascalCase' | 'original';

export interface FetchParams {
@@ -231,6 +231,19 @@ export function mapValues(data: any, fn: (item: any) => any) {
);
}

export function canConsumeForm(consumes: Consume[]): boolean {
for (const consume of consumes) {
if ('multipart/form-data' === consume.contentType) {
return true;
}
}
return false;
}

export interface Consume {
contentType: string
}

export interface RequestContext {
fetch: FetchAPI;
url: string;
@@ -335,21 +335,34 @@ export class PetApi extends runtime.BaseAPI {
}
}

const formData = new FormData();
const consumes: runtime.Consume[] = [
{ contentType: 'application/x-www-form-urlencoded' },
];
// @ts-ignore: canConsumeForm may be unused
const canConsumeForm = runtime.canConsumeForm(consumes);

let formParams: { append(param: string, value: any): any };
let useForm = false;
if (useForm) {
formParams = new FormData();
} else {
formParams = new URLSearchParams();
}

if (requestParameters.name !== undefined) {
formData.append('name', requestParameters.name as any);
formParams.append('name', requestParameters.name as any);
}

if (requestParameters.status !== undefined) {
formData.append('status', requestParameters.status as any);
formParams.append('status', requestParameters.status as any);
}

const response = await this.request({
path: `/pet/{petId}`.replace(`{${"petId"}}`, encodeURIComponent(String(requestParameters.petId))),
method: 'POST',
headers: headerParameters,
query: queryParameters,
body: formData,
body: formParams,
});

return new runtime.VoidApiResponse(response);
@@ -383,21 +396,36 @@ export class PetApi extends runtime.BaseAPI {
}
}

const formData = new FormData();
const consumes: runtime.Consume[] = [
{ contentType: 'multipart/form-data' },
];
// @ts-ignore: canConsumeForm may be unused
const canConsumeForm = runtime.canConsumeForm(consumes);

let formParams: { append(param: string, value: any): any };
let useForm = false;
// use FormData to transmit files using content-type "multipart/form-data"
useForm = canConsumeForm;
if (useForm) {
formParams = new FormData();
} else {
formParams = new URLSearchParams();
}

if (requestParameters.additionalMetadata !== undefined) {
formData.append('additionalMetadata', requestParameters.additionalMetadata as any);
formParams.append('additionalMetadata', requestParameters.additionalMetadata as any);
}

if (requestParameters.file !== undefined) {
formData.append('file', requestParameters.file as any);
formParams.append('file', requestParameters.file as any);
}

const response = await this.request({
path: `/pet/{petId}/uploadImage`.replace(`{${"petId"}}`, encodeURIComponent(String(requestParameters.petId))),
method: 'POST',
headers: headerParameters,
query: queryParameters,
body: formData,
body: formParams,
});

return new runtime.JSONApiResponse(response, (jsonValue) => ModelApiResponseFromJSON(jsonValue));
@@ -195,7 +195,7 @@ export type Json = any;
export type HTTPMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS';
export type HTTPHeaders = { [key: string]: string };
export type HTTPQuery = { [key: string]: string | number | null | boolean | Array<string | number | null | boolean> | HTTPQuery };
export type HTTPBody = Json | FormData;
export type HTTPBody = Json | FormData | URLSearchParams;
export type ModelPropertyNaming = 'camelCase' | 'snake_case' | 'PascalCase' | 'original';

export interface FetchParams {
@@ -242,6 +242,19 @@ export function mapValues(data: any, fn: (item: any) => any) {
);
}

export function canConsumeForm(consumes: Consume[]): boolean {
for (const consume of consumes) {
if ('multipart/form-data' === consume.contentType) {
return true;
}
}
return false;
}

export interface Consume {
contentType: string
}

export interface RequestContext {
fetch: FetchAPI;
url: string;
@@ -335,21 +335,34 @@ export class PetApi extends runtime.BaseAPI {
}
}

const formData = new FormData();
const consumes: runtime.Consume[] = [
{ contentType: 'application/x-www-form-urlencoded' },
];
// @ts-ignore: canConsumeForm may be unused
const canConsumeForm = runtime.canConsumeForm(consumes);

let formParams: { append(param: string, value: any): any };
let useForm = false;
if (useForm) {
formParams = new FormData();
} else {
formParams = new URLSearchParams();
}

if (requestParameters.name !== undefined) {
formData.append('name', requestParameters.name as any);
formParams.append('name', requestParameters.name as any);
}

if (requestParameters.status !== undefined) {
formData.append('status', requestParameters.status as any);
formParams.append('status', requestParameters.status as any);
}

const response = await this.request({
path: `/pet/{petId}`.replace(`{${"petId"}}`, encodeURIComponent(String(requestParameters.petId))),
method: 'POST',
headers: headerParameters,
query: queryParameters,
body: formData,
body: formParams,
});

return new runtime.VoidApiResponse(response);
@@ -383,21 +396,36 @@ export class PetApi extends runtime.BaseAPI {
}
}

const formData = new FormData();
const consumes: runtime.Consume[] = [
{ contentType: 'multipart/form-data' },
];
// @ts-ignore: canConsumeForm may be unused
const canConsumeForm = runtime.canConsumeForm(consumes);

let formParams: { append(param: string, value: any): any };
let useForm = false;
// use FormData to transmit files using content-type "multipart/form-data"
useForm = canConsumeForm;
if (useForm) {
formParams = new FormData();
} else {
formParams = new URLSearchParams();
}

if (requestParameters.additionalMetadata !== undefined) {
formData.append('additionalMetadata', requestParameters.additionalMetadata as any);
formParams.append('additionalMetadata', requestParameters.additionalMetadata as any);
}

if (requestParameters.file !== undefined) {
formData.append('file', requestParameters.file as any);
formParams.append('file', requestParameters.file as any);
}

const response = await this.request({
path: `/pet/{petId}/uploadImage`.replace(`{${"petId"}}`, encodeURIComponent(String(requestParameters.petId))),
method: 'POST',
headers: headerParameters,
query: queryParameters,
body: formData,
body: formParams,
});

return new runtime.JSONApiResponse(response, (jsonValue) => ModelApiResponseFromJSON(jsonValue));
@@ -195,7 +195,7 @@ export type Json = any;
export type HTTPMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS';
export type HTTPHeaders = { [key: string]: string };
export type HTTPQuery = { [key: string]: string | number | null | boolean | Array<string | number | null | boolean> | HTTPQuery };
export type HTTPBody = Json | FormData;
export type HTTPBody = Json | FormData | URLSearchParams;
export type ModelPropertyNaming = 'camelCase' | 'snake_case' | 'PascalCase' | 'original';

export interface FetchParams {
@@ -242,6 +242,19 @@ export function mapValues(data: any, fn: (item: any) => any) {
);
}

export function canConsumeForm(consumes: Consume[]): boolean {
for (const consume of consumes) {
if ('multipart/form-data' === consume.contentType) {
return true;
}
}
return false;
}

export interface Consume {
contentType: string
}

export interface RequestContext {
fetch: FetchAPI;
url: string;
@@ -335,21 +335,34 @@ export class PetApi extends runtime.BaseAPI {
}
}

const formData = new FormData();
const consumes: runtime.Consume[] = [
{ contentType: 'application/x-www-form-urlencoded' },
];
// @ts-ignore: canConsumeForm may be unused
const canConsumeForm = runtime.canConsumeForm(consumes);

let formParams: { append(param: string, value: any): any };
let useForm = false;
if (useForm) {
formParams = new FormData();
} else {
formParams = new URLSearchParams();
}

if (requestParameters.name !== undefined) {
formData.append('name', requestParameters.name as any);
formParams.append('name', requestParameters.name as any);
}

if (requestParameters.status !== undefined) {
formData.append('status', requestParameters.status as any);
formParams.append('status', requestParameters.status as any);
}

const response = await this.request({
path: `/pet/{petId}`.replace(`{${"petId"}}`, encodeURIComponent(String(requestParameters.petId))),
method: 'POST',
headers: headerParameters,
query: queryParameters,
body: formData,
body: formParams,
});

return new runtime.VoidApiResponse(response);
@@ -383,21 +396,36 @@ export class PetApi extends runtime.BaseAPI {
}
}

const formData = new FormData();
const consumes: runtime.Consume[] = [
{ contentType: 'multipart/form-data' },
];
// @ts-ignore: canConsumeForm may be unused
const canConsumeForm = runtime.canConsumeForm(consumes);

let formParams: { append(param: string, value: any): any };
let useForm = false;
// use FormData to transmit files using content-type "multipart/form-data"
useForm = canConsumeForm;
if (useForm) {
formParams = new FormData();
} else {
formParams = new URLSearchParams();
}

if (requestParameters.additionalMetadata !== undefined) {
formData.append('additionalMetadata', requestParameters.additionalMetadata as any);
formParams.append('additionalMetadata', requestParameters.additionalMetadata as any);
}

if (requestParameters.file !== undefined) {
formData.append('file', requestParameters.file as any);
formParams.append('file', requestParameters.file as any);
}

const response = await this.request({
path: `/pet/{petId}/uploadImage`.replace(`{${"petId"}}`, encodeURIComponent(String(requestParameters.petId))),
method: 'POST',
headers: headerParameters,
query: queryParameters,
body: formData,
body: formParams,
});

return new runtime.JSONApiResponse(response, (jsonValue) => ModelApiResponseFromJSON(jsonValue));

0 comments on commit 1a55020

Please sign in to comment.
You can’t perform that action at this time.