Skip to content

Commit

Permalink
[typescript-fetch] Fix uploading files (#2900)
Browse files Browse the repository at this point in the history
* [typescript-fetch] Fix uploading files

* Check for Blob instead of File

* Update samples

* Update samples

* Update samples

* Update samples

* Regenerate samples

* Bug

* Manually fix samples

* Implement support for Buffer and Blob in a backwards-compatible way

* Rework how blob and buffer instance checking works

* Check for Blob/Buffer existence properly

* Avoid using Buffer and Blob in type declarations

* Remove Buffer support

* Update samples/client/petstore/typescript-fetch/tests/default/test/PetApi.ts

Co-Authored-By: Esteban Marin <estebanmarin@gmx.ch>

* Update samples/client/petstore/typescript-fetch/tests/default/test/PetApi.ts

Co-Authored-By: Esteban Marin <estebanmarin@gmx.ch>
  • Loading branch information
2 people authored and wing328 committed May 31, 2019
1 parent ddf21f0 commit c509d98
Show file tree
Hide file tree
Showing 13 changed files with 441 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

export const BASE_PATH = "{{{basePath}}}".replace(/\/+$/, "");

const isBlob = (value: any) => typeof Blob !== 'undefined' && value instanceof Blob;

/**
* This is the base class for all generated API classes.
*/
Expand Down Expand Up @@ -47,7 +49,9 @@ export class BaseAPI {
// do not handle correctly sometimes.
url += '?' + querystring(context.query);
}
const body = context.body instanceof FormData ? context.body : JSON.stringify(context.body);
const body = (context.body instanceof FormData || isBlob(context.body))
? context.body
: JSON.stringify(context.body);
const init = {
method: context.method,
headers: context.headers,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// tslint:disable
/**
* OpenAPI Petstore
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
*
* The version of the OpenAPI document: 1.0.0
*
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/

import { exists, mapValues } from '../runtime';
/**
*
* @export
* @interface InlineObject
*/
export interface InlineObject {
/**
* Updated name of the pet
* @type {string}
* @memberof InlineObject
*/
name?: string;
/**
* Updated status of the pet
* @type {string}
* @memberof InlineObject
*/
status?: string;
}

export function InlineObjectFromJSON(json: any): InlineObject {
return {
'name': !exists(json, 'name') ? undefined : json['name'],
'status': !exists(json, 'status') ? undefined : json['status'],
};
}

export function InlineObjectToJSON(value?: InlineObject): any {
if (value === undefined) {
return undefined;
}
return {
'name': value.name,
'status': value.status,
};
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// tslint:disable
/**
* OpenAPI Petstore
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
*
* The version of the OpenAPI document: 1.0.0
*
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/

import { exists, mapValues } from '../runtime';
/**
*
* @export
* @interface InlineObject1
*/
export interface InlineObject1 {
/**
* Additional data to pass to server
* @type {string}
* @memberof InlineObject1
*/
additionalMetadata?: string;
/**
* file to upload
* @type {Blob}
* @memberof InlineObject1
*/
file?: Blob;
}

export function InlineObject1FromJSON(json: any): InlineObject1 {
return {
'additionalMetadata': !exists(json, 'additionalMetadata') ? undefined : json['additionalMetadata'],
'file': !exists(json, 'file') ? undefined : json['file'],
};
}

export function InlineObject1ToJSON(value?: InlineObject1): any {
if (value === undefined) {
return undefined;
}
return {
'additionalMetadata': value.additionalMetadata,
'file': value.file,
};
}


Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

export const BASE_PATH = "http://petstore.swagger.io/v2".replace(/\/+$/, "");

const isBlob = (value: any) => typeof Blob !== 'undefined' && value instanceof Blob;

/**
* This is the base class for all generated API classes.
*/
Expand Down Expand Up @@ -58,7 +60,9 @@ export class BaseAPI {
// do not handle correctly sometimes.
url += '?' + querystring(context.query);
}
const body = context.body instanceof FormData ? context.body : JSON.stringify(context.body);
const body = (context.body instanceof FormData || isBlob(context.body))
? context.body
: JSON.stringify(context.body);
const init = {
method: context.method,
headers: context.headers,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// tslint:disable
/**
* OpenAPI Petstore
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
*
* The version of the OpenAPI document: 1.0.0
*
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/

import { exists, mapValues } from '../runtime';
/**
*
* @export
* @interface InlineObject
*/
export interface InlineObject {
/**
* Updated name of the pet
* @type {string}
* @memberof InlineObject
*/
name?: string;
/**
* Updated status of the pet
* @type {string}
* @memberof InlineObject
*/
status?: string;
}

export function InlineObjectFromJSON(json: any): InlineObject {
return {
'name': !exists(json, 'name') ? undefined : json['name'],
'status': !exists(json, 'status') ? undefined : json['status'],
};
}

export function InlineObjectToJSON(value?: InlineObject): any {
if (value === undefined) {
return undefined;
}
return {
'name': value.name,
'status': value.status,
};
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// tslint:disable
/**
* OpenAPI Petstore
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
*
* The version of the OpenAPI document: 1.0.0
*
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/

import { exists, mapValues } from '../runtime';
/**
*
* @export
* @interface InlineObject1
*/
export interface InlineObject1 {
/**
* Additional data to pass to server
* @type {string}
* @memberof InlineObject1
*/
additionalMetadata?: string;
/**
* file to upload
* @type {Blob}
* @memberof InlineObject1
*/
file?: Blob;
}

export function InlineObject1FromJSON(json: any): InlineObject1 {
return {
'additionalMetadata': !exists(json, 'additionalMetadata') ? undefined : json['additionalMetadata'],
'file': !exists(json, 'file') ? undefined : json['file'],
};
}

export function InlineObject1ToJSON(value?: InlineObject1): any {
if (value === undefined) {
return undefined;
}
return {
'additionalMetadata': value.additionalMetadata,
'file': value.file,
};
}


Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

export const BASE_PATH = "http://petstore.swagger.io/v2".replace(/\/+$/, "");

const isBlob = (value: any) => typeof Blob !== 'undefined' && value instanceof Blob;

/**
* This is the base class for all generated API classes.
*/
Expand Down Expand Up @@ -58,7 +60,9 @@ export class BaseAPI {
// do not handle correctly sometimes.
url += '?' + querystring(context.query);
}
const body = context.body instanceof FormData ? context.body : JSON.stringify(context.body);
const body = (context.body instanceof FormData || isBlob(context.body))
? context.body
: JSON.stringify(context.body);
const init = {
method: context.method,
headers: context.headers,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// tslint:disable
/**
* OpenAPI Petstore
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
*
* The version of the OpenAPI document: 1.0.0
*
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/

import { exists, mapValues } from '../runtime';
/**
*
* @export
* @interface InlineObject
*/
export interface InlineObject {
/**
* Updated name of the pet
* @type {string}
* @memberof InlineObject
*/
name?: string;
/**
* Updated status of the pet
* @type {string}
* @memberof InlineObject
*/
status?: string;
}

export function InlineObjectFromJSON(json: any): InlineObject {
return {
'name': !exists(json, 'name') ? undefined : json['name'],
'status': !exists(json, 'status') ? undefined : json['status'],
};
}

export function InlineObjectToJSON(value?: InlineObject): any {
if (value === undefined) {
return undefined;
}
return {
'name': value.name,
'status': value.status,
};
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// tslint:disable
/**
* OpenAPI Petstore
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
*
* The version of the OpenAPI document: 1.0.0
*
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/

import { exists, mapValues } from '../runtime';
/**
*
* @export
* @interface InlineObject1
*/
export interface InlineObject1 {
/**
* Additional data to pass to server
* @type {string}
* @memberof InlineObject1
*/
additionalMetadata?: string;
/**
* file to upload
* @type {Blob}
* @memberof InlineObject1
*/
file?: Blob;
}

export function InlineObject1FromJSON(json: any): InlineObject1 {
return {
'additionalMetadata': !exists(json, 'additionalMetadata') ? undefined : json['additionalMetadata'],
'file': !exists(json, 'file') ? undefined : json['file'],
};
}

export function InlineObject1ToJSON(value?: InlineObject1): any {
if (value === undefined) {
return undefined;
}
return {
'additionalMetadata': value.additionalMetadata,
'file': value.file,
};
}


Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

export const BASE_PATH = "http://petstore.swagger.io/v2".replace(/\/+$/, "");

const isBlob = (value: any) => typeof Blob !== 'undefined' && value instanceof Blob;

/**
* This is the base class for all generated API classes.
*/
Expand Down Expand Up @@ -58,7 +60,9 @@ export class BaseAPI {
// do not handle correctly sometimes.
url += '?' + querystring(context.query);
}
const body = context.body instanceof FormData ? context.body : JSON.stringify(context.body);
const body = (context.body instanceof FormData || isBlob(context.body))
? context.body
: JSON.stringify(context.body);
const init = {
method: context.method,
headers: context.headers,
Expand Down
Loading

0 comments on commit c509d98

Please sign in to comment.