Skip to content
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 @@ -801,6 +801,29 @@ private Map<String, Object> buildSupportFileBundle(List<Object> allOperations, L
bundle.put("hasServers", true);
}

if (allOperations != null) {
// Scan all API Group Operations to determine if there is at least one Operation within this API that
// returns a File Response, in which case the API hasFileOperations, and we may use this property in
// the mustache templates if useful...
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 (CodegenOperation apiGroupOperation : apiGroupOperations) {
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;
}
}
}

if (openAPI.getExternalDocs() != null) {
bundle.put("externalDocs", openAPI.getExternalDocs());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,41 @@ export class RequiredError extends Error {
}
}

{{#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}}
{{#models}}
{{#model}}{{#isEnum}}{{>modelEnum}}{{/isEnum}}{{^isEnum}}{{>modelGeneric}}{{/isEnum}}{{/model}}
{{/models}}
Expand Down Expand Up @@ -264,12 +299,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
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.1.2-SNAPSHOT
3.3.3-SNAPSHOT
8 changes: 4 additions & 4 deletions samples/client/petstore-security-test/typescript-fetch/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export interface Return {
export const FakeApiFetchParamCreator = function (configuration?: Configuration) {
return {
/**
*
* To test code injection *_/ ' \" =end -- \\r\\n \\n \\r
* @summary To test code injection *_/ ' \" =end -- \\r\\n \\n \\r
* @param {UNKNOWN_BASE_TYPE} [UNKNOWN_BASE_TYPE]
* @param {*} [options] Override http request option.
Expand Down Expand Up @@ -141,7 +141,7 @@ export const FakeApiFetchParamCreator = function (configuration?: Configuration)
export const FakeApiFp = function(configuration?: Configuration) {
return {
/**
*
* To test code injection *_/ ' \" =end -- \\r\\n \\n \\r
* @summary To test code injection *_/ ' \" =end -- \\r\\n \\n \\r
* @param {UNKNOWN_BASE_TYPE} [UNKNOWN_BASE_TYPE]
* @param {*} [options] Override http request option.
Expand Down Expand Up @@ -169,7 +169,7 @@ export const FakeApiFp = function(configuration?: Configuration) {
export const FakeApiFactory = function (configuration?: Configuration, fetch?: FetchAPI, basePath?: string) {
return {
/**
*
* To test code injection *_/ ' \" =end -- \\r\\n \\n \\r
* @summary To test code injection *_/ ' \" =end -- \\r\\n \\n \\r
* @param {UNKNOWN_BASE_TYPE} [UNKNOWN_BASE_TYPE]
* @param {*} [options] Override http request option.
Expand All @@ -189,7 +189,7 @@ export const FakeApiFactory = function (configuration?: Configuration, fetch?: F
*/
export class FakeApi extends BaseAPI {
/**
*
* To test code injection *_/ ' \" =end -- \\r\\n \\n \\r
* @summary To test code injection *_/ ' \" =end -- \\r\\n \\n \\r
* @param {UNKNOWN_BASE_TYPE} [UNKNOWN_BASE_TYPE]
* @param {*} [options] Override http request option.
Expand Down