mirror of
https://github.com/ferdikoomen/openapi-typescript-codegen.git
synced 2025-12-08 20:16:21 +00:00
Merge pull request #740 from sjoerdmulder/feature/formdata-as-body
Allow FormData as body argument
This commit is contained in:
commit
d11d7c1448
@ -33,7 +33,7 @@ async function getHeaders(options: ApiRequestOptions): Promise<Headers> {
|
||||
headers.append('Content-Type', options.body.type || 'application/octet-stream');
|
||||
} else if (isString(options.body)) {
|
||||
headers.append('Content-Type', 'text/plain');
|
||||
} else {
|
||||
} else if (!isFormData(options.body)) {
|
||||
headers.append('Content-Type', 'application/json');
|
||||
}
|
||||
}
|
||||
|
||||
@ -2,7 +2,7 @@ function getRequestBody(options: ApiRequestOptions): BodyInit | undefined {
|
||||
if (options.body) {
|
||||
if (options.mediaType?.includes('/json')) {
|
||||
return JSON.stringify(options.body)
|
||||
} else if (isString(options.body) || isBlob(options.body)) {
|
||||
} else if (isString(options.body) || isBlob(options.body) || isFormData(options.body)) {
|
||||
return options.body;
|
||||
} else {
|
||||
return JSON.stringify(options.body);
|
||||
|
||||
@ -19,6 +19,9 @@ import { OpenAPI } from './OpenAPI';
|
||||
{{>functions/isBlob}}
|
||||
|
||||
|
||||
{{>functions/isFormData}}
|
||||
|
||||
|
||||
{{>functions/base64}}
|
||||
|
||||
|
||||
|
||||
3
src/templates/core/functions/isFormData.hbs
Normal file
3
src/templates/core/functions/isFormData.hbs
Normal file
@ -0,0 +1,3 @@
|
||||
function isFormData(value: any): value is FormData {
|
||||
return value instanceof FormData;
|
||||
}
|
||||
@ -33,7 +33,7 @@ async function getHeaders(options: ApiRequestOptions): Promise<Headers> {
|
||||
headers.append('Content-Type', 'application/octet-stream');
|
||||
} else if (isString(options.body)) {
|
||||
headers.append('Content-Type', 'text/plain');
|
||||
} else {
|
||||
} else if (!isFormData(options.body)) {
|
||||
headers.append('Content-Type', 'application/json');
|
||||
}
|
||||
}
|
||||
|
||||
@ -2,7 +2,7 @@ function getRequestBody(options: ApiRequestOptions): BodyInit | undefined {
|
||||
if (options.body) {
|
||||
if (options.mediaType?.includes('/json')) {
|
||||
return JSON.stringify(options.body)
|
||||
} else if (isString(options.body) || isBlob(options.body)) {
|
||||
} else if (isString(options.body) || isBlob(options.body) || isFormData(options.body)) {
|
||||
return options.body as any;
|
||||
} else {
|
||||
return JSON.stringify(options.body);
|
||||
|
||||
@ -33,7 +33,7 @@ async function getHeaders(options: ApiRequestOptions): Promise<Headers> {
|
||||
headers.append('Content-Type', options.body.type || 'application/octet-stream');
|
||||
} else if (isString(options.body)) {
|
||||
headers.append('Content-Type', 'text/plain');
|
||||
} else {
|
||||
} else if (!isFormData(options.body)) {
|
||||
headers.append('Content-Type', 'application/json');
|
||||
}
|
||||
}
|
||||
|
||||
@ -2,7 +2,7 @@ function getRequestBody(options: ApiRequestOptions): any {
|
||||
if (options.body) {
|
||||
if (options.mediaType?.includes('/json')) {
|
||||
return JSON.stringify(options.body)
|
||||
} else if (isString(options.body) || isBlob(options.body)) {
|
||||
} else if (isString(options.body) || isBlob(options.body) || isFormData(options.body)) {
|
||||
return options.body;
|
||||
} else {
|
||||
return JSON.stringify(options.body);
|
||||
|
||||
@ -24,6 +24,7 @@ import functionGetQueryString from '../templates/core/functions/getQueryString.h
|
||||
import functionGetUrl from '../templates/core/functions/getUrl.hbs';
|
||||
import functionIsBlob from '../templates/core/functions/isBlob.hbs';
|
||||
import functionIsDefined from '../templates/core/functions/isDefined.hbs';
|
||||
import functionIsFormData from '../templates/core/functions/isFormData.hbs';
|
||||
import functionIsString from '../templates/core/functions/isString.hbs';
|
||||
import functionIsStringWithValue from '../templates/core/functions/isStringWithValue.hbs';
|
||||
import functionIsSuccess from '../templates/core/functions/isSuccess.hbs';
|
||||
@ -157,6 +158,7 @@ export function registerHandlebarTemplates(root: {
|
||||
Handlebars.registerPartial('functions/getUrl', Handlebars.template(functionGetUrl));
|
||||
Handlebars.registerPartial('functions/isBlob', Handlebars.template(functionIsBlob));
|
||||
Handlebars.registerPartial('functions/isDefined', Handlebars.template(functionIsDefined));
|
||||
Handlebars.registerPartial('functions/isFormData', Handlebars.template(functionIsFormData));
|
||||
Handlebars.registerPartial('functions/isString', Handlebars.template(functionIsString));
|
||||
Handlebars.registerPartial('functions/isStringWithValue', Handlebars.template(functionIsStringWithValue));
|
||||
Handlebars.registerPartial('functions/isSuccess', Handlebars.template(functionIsSuccess));
|
||||
|
||||
@ -233,6 +233,10 @@ function isBlob(value: any): value is Blob {
|
||||
return value instanceof Blob;
|
||||
}
|
||||
|
||||
function isFormData(value: any): value is FormData {
|
||||
return value instanceof FormData;
|
||||
}
|
||||
|
||||
function base64(str: string): string {
|
||||
try {
|
||||
return btoa(str);
|
||||
@ -347,7 +351,7 @@ async function getHeaders(options: ApiRequestOptions): Promise<Headers> {
|
||||
headers.append('Content-Type', options.body.type || 'application/octet-stream');
|
||||
} else if (isString(options.body)) {
|
||||
headers.append('Content-Type', 'text/plain');
|
||||
} else {
|
||||
} else if (!isFormData(options.body)) {
|
||||
headers.append('Content-Type', 'application/json');
|
||||
}
|
||||
}
|
||||
@ -359,7 +363,7 @@ function getRequestBody(options: ApiRequestOptions): BodyInit | undefined {
|
||||
if (options.body) {
|
||||
if (options.mediaType?.includes('/json')) {
|
||||
return JSON.stringify(options.body)
|
||||
} else if (isString(options.body) || isBlob(options.body)) {
|
||||
} else if (isString(options.body) || isBlob(options.body) || isFormData(options.body)) {
|
||||
return options.body;
|
||||
} else {
|
||||
return JSON.stringify(options.body);
|
||||
@ -2933,6 +2937,10 @@ function isBlob(value: any): value is Blob {
|
||||
return value instanceof Blob;
|
||||
}
|
||||
|
||||
function isFormData(value: any): value is FormData {
|
||||
return value instanceof FormData;
|
||||
}
|
||||
|
||||
function base64(str: string): string {
|
||||
try {
|
||||
return btoa(str);
|
||||
@ -3047,7 +3055,7 @@ async function getHeaders(options: ApiRequestOptions): Promise<Headers> {
|
||||
headers.append('Content-Type', options.body.type || 'application/octet-stream');
|
||||
} else if (isString(options.body)) {
|
||||
headers.append('Content-Type', 'text/plain');
|
||||
} else {
|
||||
} else if (!isFormData(options.body)) {
|
||||
headers.append('Content-Type', 'application/json');
|
||||
}
|
||||
}
|
||||
@ -3059,7 +3067,7 @@ function getRequestBody(options: ApiRequestOptions): BodyInit | undefined {
|
||||
if (options.body) {
|
||||
if (options.mediaType?.includes('/json')) {
|
||||
return JSON.stringify(options.body)
|
||||
} else if (isString(options.body) || isBlob(options.body)) {
|
||||
} else if (isString(options.body) || isBlob(options.body) || isFormData(options.body)) {
|
||||
return options.body;
|
||||
} else {
|
||||
return JSON.stringify(options.body);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user