Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[typescript-fetch] Return file content (application/octet-stream) as Blob #8410

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,28 @@ private Map<String, Object> buildSupportFileBundle(List<Object> allOperations, L
bundle.put("models", allModels);
bundle.put("apiFolder", config.apiPackage().replace('.', File.separatorChar));
bundle.put("modelPackage", config.modelPackage());
if (allOperations != null) {
// For each API group...
for (HashMap<String, Object> apiGroup : (List<HashMap<String, Object>>) (Object) allOperations) {
HashMap<String, Object> apiGroupOperationsContainer =
(HashMap<String, Object>) apiGroup.get("operations");
List<CodegenOperation> apiGroupOperations =
(List<CodegenOperation>) (Object) apiGroupOperationsContainer.get("operation");
// For each operation within the API group...
for (CodegenOperation apiGroupOperation : apiGroupOperations) {
// If this operation has a file response, indicate that the API hasFileOperations...
if (apiGroupOperation.isResponseFile) {
bundle.put("hasFileOperations", true);
break;
}
}
// If we've already found at least one API Group containing an operation that must return
// file responses, we're done...
if (bundle.containsKey("hasFileOperations")) {
break;
}
}
}
List<CodegenSecurity> authMethods = config.fromSecurity(swagger.getSecurityDefinitions());
if (authMethods != null && !authMethods.isEmpty()) {
bundle.put("authMethods", authMethods);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,41 @@ import { Configuration } from "./configuration";

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

{{#hasFileOperations}}
const responseToFile = (fileResponse: Response): Promise<File> => {
let filename = "download"; // Default filename of 'download'

const contentTypeHeader = fileResponse.headers.get("Content-Type");
const lastModifiedHeader = fileResponse.headers.get("Last-Modified");
const contentDispositionHeader = fileResponse.headers.get("Content-Disposition");

const type = contentTypeHeader || "";
const lastModified = lastModifiedHeader ? Date.parse(lastModifiedHeader) : Date.now();
if (contentDispositionHeader) {
const filenameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/;
const filenameMatches = filenameRegex.exec(contentDispositionHeader);
if (filenameMatches && filenameMatches[1]) {
filename = filenameMatches[1].replace(/['"]/g, "");
}
}

return fileResponse.blob().then(blob => {
// If the File constructor is available and this is not Edge (which has an unimplemented constructor), use it...
// (https://developer.mozilla.org/en-US/docs/Web/API/File/File)
if (typeof File === "function" && !/Edge/.test(navigator.userAgent)) {
return new File([blob], filename, { type, lastModified });
}
// Otherwise, extend the Blob with the additional properties and return it as a File...
else {
const file = blob as any;
file.name = filename;
file.lastModified = lastModified;
return file as File;
}
});
};

{{/hasFileOperations}}
/**
*
* @export
Expand Down Expand Up @@ -260,12 +295,17 @@ export const {{classname}}Fp = function(configuration?: Configuration) {
* @param {*} [options] Override http request option.
* @throws {RequiredError}
*/
{{nickname}}({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}options?: any): (fetch?: FetchAPI, basePath?: string) => Promise<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Response{{/returnType}}> {
{{nickname}}({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}options?: any): (fetch?: FetchAPI, basePath?: string) => Promise<{{#returnType}}{{^isResponseFile}}{{{returnType}}}{{/isResponseFile}}{{#isResponseFile}}File{{/isResponseFile}}{{/returnType}}{{^returnType}}Response{{/returnType}}> {
const localVarFetchArgs = {{classname}}FetchParamCreator(configuration).{{nickname}}({{#allParams}}{{paramName}}, {{/allParams}}options);
return (fetch: FetchAPI = portableFetch, basePath: string = BASE_PATH) => {
return fetch(basePath + localVarFetchArgs.url, localVarFetchArgs.options).then((response) => {
if (response.status >= 200 && response.status < 300) {
{{^isResponseFile}}
return response{{#returnType}}.json(){{/returnType}};
{{/isResponseFile}}
{{#isResponseFile}}
return responseToFile(response);
{{/isResponseFile}}
} else {
throw response;
}
Expand Down
28 changes: 28 additions & 0 deletions samples/client/petstore/typescript-fetch/builds/default/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,26 @@ export class RequiredError extends Error {
}
}

/**
* some description
* @export
* @interface Amount
*/
export interface Amount {
/**
* some description
* @type {number}
* @memberof Amount
*/
value: number;
/**
*
* @type {Currency}
* @memberof Amount
*/
currency: Currency;
}

/**
* Describes the result of uploading an image resource
* @export
Expand Down Expand Up @@ -124,6 +144,14 @@ export interface Category {
name?: string;
}

/**
* some description
* @export
* @interface Currency
*/
export interface Currency {
}

/**
* An order for a pets from the pet store
* @export
Expand Down